Description
With rejectUnauthorized: false, a TLS connection can succeed despite an untrusted certificate or a hostname mismatch. Setting NODE_TLS_REJECT_UNAUTHORIZED=0 changes the verification default for the entire process.
Potential impact
The connection can still be encrypted while an impersonating server receives credentials or session tokens and alters responses.
Remediation
- Keep
rejectUnauthorizedat its default oftrueand remove the environment setting that disables verification. Keep test CAs separate from production configuration. - Setting
caonhttps.Agentortls.connectreplaces the default CA list. Include all required CAs and keep hostname checks enabled. If the server requires SNI, setservernameas well ashostontls.connect. - If you pin a certificate or public key, obtain the expected value from a trusted source and check how renewal affects it.
Examples
Before
javascript
// Before, example 1: disable certificate validation globally
const https = require('https');
const tls = require('tls');
function fetchInsecure() {
// Disabling TLS validation globally is dangerous
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
https.get({ hostname: 'api.example.org', path: '/v1/data' }, (res) => {
res.on('data', () => {});
});
}
// Before, example 2: disable certificate validation in an agent
function postInsecure() {
const insecureAgent = new https.Agent({ keepAlive: true, rejectUnauthorized: false });
const req = https.request({
hostname: 'secure.example.org',
method: 'POST',
path: '/login',
agent: insecureAgent,
headers: { 'Content-Type': 'application/json' }
}, (res) => res.resume());
req.write(JSON.stringify({ user: 'alice', pass: 'secret' }));
req.end();
}
After
javascript
// After: retain default validation and use a trusted CA bundle
const https = require('https');
const fs = require('fs');
// Example path to a private or public CA bundle
const caBundle = fs.readFileSync('/etc/ssl/certs/company-ca-bundle.pem');
function fetchSecure() {
// rejectUnauthorized defaults to true
const secureAgent = new https.Agent({
keepAlive: true,
ca: caBundle, // Supply trusted CAs
// https derives servername from the request host; do not disable it
});
https.get({
hostname: 'api.example.org',
path: '/v1/data',
agent: secureAgent
}, (res) => {
let body = '';
res.on('data', (c) => body += c);
res.on('end', () => console.log('OK:', body.length));
});
}
// Optional: certificate pinning using the certificate SHA-256 fingerprint
const tls = require('tls');
function connectWithPinning(expectedFingerprint256) {
const socket = tls.connect({ host: 'secure.example.org', port: 443, ca: caBundle }, () => {
const cert = socket.getPeerCertificate(true);
if (!cert || !cert.fingerprint256 || cert.fingerprint256 !== expectedFingerprint256) {
socket.destroy(new Error('Certificate pinning mismatch'));
return;
}
socket.write('GET / HTTP/1.1\r\nHost: secure.example.org\r\n\r\n');
});
}
The example uses a CA bundle. Its optional fingerprint256 check compares the SHA-256 fingerprint of the entire certificate and closes the connection before sending a request if the fingerprint differs.