2020-06-30 14:41:58 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
|
|
|
https://nodejs.org/api/https.html[https] built-in module:
|
|
|
|
|
|
|
|
----
|
|
|
|
let options = {
|
2020-12-21 15:38:52 +01:00
|
|
|
hostname: 'www.example.com',
|
2020-06-30 14:41:58 +02:00
|
|
|
port: 443,
|
|
|
|
path: '/',
|
|
|
|
method: 'GET',
|
|
|
|
secureProtocol: 'TLSv1_2_method',
|
2020-12-21 15:38:52 +01:00
|
|
|
checkServerIdentity: function() {} // Noncompliant: hostname is not verified
|
2020-06-30 14:41:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let req = https.request(options, (res) => {
|
|
|
|
res.on('data', (d) => {
|
|
|
|
process.stdout.write(d);
|
|
|
|
});
|
|
|
|
}); // Noncompliant
|
|
|
|
----
|
|
|
|
|
|
|
|
https://nodejs.org/api/tls.html[tls] built-in module:
|
|
|
|
|
|
|
|
----
|
|
|
|
let options = {
|
|
|
|
secureProtocol: 'TLSv1_2_method',
|
2020-12-21 15:38:52 +01:00
|
|
|
checkServerIdentity: function() {} // Noncompliant: hostname is not verified
|
2020-06-30 14:41:58 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let socket = tls.connect(443, "www.example.com", options, () => {
|
|
|
|
process.stdin.pipe(socket);
|
|
|
|
process.stdin.resume();
|
|
|
|
}); // Noncompliant
|
|
|
|
----
|
|
|
|
|
|
|
|
https://www.npmjs.com/package/request[request] module:
|
|
|
|
|
|
|
|
----
|
|
|
|
let socket = request.get({
|
2020-12-21 15:38:52 +01:00
|
|
|
url: 'https://www.example.com',
|
2020-06-30 14:41:58 +02:00
|
|
|
secureProtocol: 'TLSv1_2_method',
|
2020-12-21 15:38:52 +01:00
|
|
|
checkServerIdentity: function() {} // Noncompliant: hostname is not verified
|
2020-06-30 14:41:58 +02:00
|
|
|
});
|
|
|
|
----
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
https://nodejs.org/api/https.html[https] built-in module:
|
|
|
|
|
|
|
|
----
|
|
|
|
let options = {
|
2020-12-21 15:38:52 +01:00
|
|
|
hostname: 'www.example.com',
|
2020-06-30 14:41:58 +02:00
|
|
|
port: 443,
|
|
|
|
path: '/',
|
|
|
|
method: 'GET',
|
|
|
|
secureProtocol: 'TLSv1_2_method'
|
|
|
|
};
|
|
|
|
|
|
|
|
let req = https.request(options, (res) => {
|
|
|
|
res.on('data', (d) => {
|
|
|
|
process.stdout.write(d);
|
|
|
|
});
|
|
|
|
}); // Compliant: default checkServerIdentity function is secure
|
|
|
|
----
|
|
|
|
|
|
|
|
|
|
|
|
https://nodejs.org/api/tls.html[tls] built-in module:
|
|
|
|
|
|
|
|
----
|
|
|
|
let options = {
|
|
|
|
secureProtocol: 'TLSv1_2_method',
|
|
|
|
checkServerIdentity: (servername, peer) => {
|
|
|
|
if (servername !== "www.example.com") {
|
|
|
|
return new Error ('Error'); // Compliant: there is at least one check
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let socket = tls.connect(443, "www.example.com", options, () => {
|
|
|
|
process.stdin.pipe(socket);
|
|
|
|
process.stdin.resume();
|
|
|
|
}); // Compliant
|
|
|
|
----
|
|
|
|
|
|
|
|
https://www.npmjs.com/package/request[request] module:
|
|
|
|
|
|
|
|
----
|
|
|
|
let socket = request.get({
|
|
|
|
url: 'https://www.example.com/',
|
|
|
|
secureProtocol: 'TLSv1_2_method' // Compliant
|
|
|
|
}); // Compliant: default checkServerIdentity function is secure
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../see.adoc[]
|