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.
1import https from '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: any) {
10 console.log("unverified hostname");
11 },
12 };
13 var request = https.request(options, (response: any) => {
14 let data = "hello";
15 console.log(data);
16 });
17}
1import https from '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: string) {
10 if (host != "github.com") {
11 console.log("verified hostname");
12 }
13 },
14 };
15 var request = https.request(options, (response: any) => {
16 let data = "hello";
17 console.log(data);
18 });
19}