Lack of validation or insufficient validation of a security certificate can lead to host impersonation and sensitive data leaks.
1import tls from 'tls'
2
3function improperCertificateValidationNoncompliant() {
4 var options = {
5 host: 'encrypted.example.com',
6 // Noncompliant: rejectUnauthorized is set to 'false'.
7 rejectUnauthorized: false
8 }
9
10 tls.createServer(options, (req: any, res: { writeHead: (arg0: number) => void; end: () => void }) => {
11 res.writeHead(200)
12 res.end()
13 }).listen(8000)
14}
1import tls from 'tls'
2import fs from 'fs'
3
4function improperCertificateValidationCompliant() {
5 var options = {
6 host: 'encrypted.example.com',
7 // Compliant: certificate is provided.
8 key: fs.readFileSync('keys/client-key.pem'),
9 cert: fs.readFileSync('keys/client-cert.pem')
10 }
11
12 tls.createServer(options, (req: any, res: { writeHead: (arg0: number) => void; end: () => void }) => {
13 res.writeHead(200)
14 res.end()
15 }).listen(8000)
16}