Unverified hostname High

Unverified hostnames can allow man-in-the-middle attacks. When establishing an SSL/TLS connection, use the default checkServerIdentity or confirm that the certificate's hostname-specific data matches the server hostname to avoid these attacks.

Detector ID
javascript/unverified-hostname@v1.0
Category
Common Weakness Enumeration (CWE) external icon

Noncompliant example

1var https = require("https")
2function unverifiedHostnameNoncompliant() {
3    var options = {
4        hostname: 'encrypted.google.com',
5        port: 3000,
6        path: '/pathname/',
7        method: 'POST',
8        // Noncompliant: hostname is not verified.
9        checkServerIdentity: function (host) {
10            console.log('unverified hostname')
11        }
12    }
13    var request = https.request(options, (response) => {
14        let data = 'hello'
15        console.log(data)
16    })
17}

Compliant example

1var https = require("https")
2function unverifiedHostnameCompliant() {
3    var options = {
4        hostname: 'encrypted.google.com',
5        port: 3000,
6        path: '/pathname/',
7        method: 'POST',
8        // Compliant: hostname is verified before using it.
9        checkServerIdentity: function (host) {
10            if (host != "github.com") {
11                console.log('verified hostname')
12            }
13        }
14    }
15    var request = https.request(options, (response) => {
16        let data = 'hello'
17        console.log(data)
18    })
19}