Skip to content
Crypto Disclosed report Intermediate CVE-2021-22939

Built-in TLS module unexpectedly treats "rejectUnauthorized: undefined" as "rejectUnauthorized: false", disabling all certificate validation

The Node.js TLS module treats an explicit 'undefined' value for the 'rejectUnauthorized' option as 'false', disabling certificate validation. An attacker who can intercept network traffic could then read or modify HTTPS connections made by applications that pass this option as undefined.

Tim Perry Follow

Originally disclosed on HackerOne

Bounty$150 Low View original
  • Triaged in 7 days
  • Fixed in 16 days
  • Paid in 57 days
  • Disclosed after 46 days

NOTE! Thanks for submitting a report! Please replace all the [square] sections below with the pertinent details. Remember, the more detail you provide, the easier it is for us to triage and respond quickly, so be sure to take your time filling out the report!

Summary: "rejectUnauthorized: false" disables all TLS validation, and should not be set in almost all circumstances. The documentation says only the specific 'false' value will disable this validation, but in fact a 'undefined' value does also disables it, unexpectedly disabling TLS entirely.

Description:

The documentation for tls.connect (https://nodejs.org/api/tls.html#tls_tls_connect_options_callback) says:

rejectUnauthorized <boolean> If not false, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails; err.code contains the OpenSSL error code. Default: true.

This is not true (see repro below) - in addition to false, an explicit undefined value does also disable server certificate verification.

This is very problematic, because it's reasonable to assume that undefined will be equivalent to setting the default, and it's also easy to accidentally produce undefined fields when dynamically building configuration. In any system that has done so, they are unknowingly silently not validating any TLS connections.

I've discovered this because I've found that https://www.npmjs.com/package/global-agent does exactly this (uses explicit undefineds when building options objects) and it is vulnerable because of this issue (i.e. all users of that package are by default unintentionally not validating TLS certificates for all connections).

This appears to affect all active node versions. Unless I'm missing something, this seems very bad.

Steps To Reproduce:

Repro code:

const https = require('https');
const request = https.get('https://expired.badssl.com', { rejectUnauthorized: undefined });
request.on('error', (e) => console.log('Request failed:', e.message));
request.on('response', (e) => console.log('Request succeeded'));
  1. Run the above
  2. The request succeeds! It should not, because expired.badssl.com by design has an expired TLS certificate
  3. Remove the { rejectUnauthorized: undefined } option, or change it to 'true'
  4. It fails, as expected, due to an expired certificate.

Impact:

This breaks all TLS and HTTPS security for anybody who accidentally provides an undefined value, assuming it will be equivalent to providing no value at all.

Impact

Breaks all HTTPS protections, so complete disclosure or trivial manipulation of all HTTPS requests and responses by anybody capable of MITMing the TCP connection.

The original report as published on HackerOne, by pimterry. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 5 min read.

Context

Node.js ships a built-in tls module, and the https module is built on top of it. When a client connects to a TLS server, the tls.connect options include rejectUnauthorized, which controls whether the server certificate is verified against the list of supplied CAs. The documentation for tls.connect describes the option as follows:

rejectUnauthorized <boolean> If not false, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails; err.code contains the OpenSSL error code. Default: true.

Read literally, only the specific value false should turn verification off. Any other value, including an explicit undefined, should leave the default behaviour in place, which is verification enabled. The report shows that this is not what happens.

Setup

The report does not describe any special accounts, roles or tooling. The reproduction uses only the Node.js standard library, specifically the https module, and a public test host, expired.badssl.com, which by design serves an expired TLS certificate. The researcher states that the behaviour appears to affect all active Node versions. No specific version number is given.

Finding it

The researcher found the discrepancy after discovering that the global-agent package builds its options objects with explicit undefined values. The documentation promises that verification is skipped only when rejectUnauthorized is false, but in practice an explicit undefined value also disables server certificate verification.

Two properties of this make it dangerous. First, it is reasonable to assume that undefined is equivalent to not setting the option at all, and therefore equivalent to the documented default of true. Second, configuration objects assembled at runtime easily end up with fields whose value is undefined, for example when a value is copied from a variable that was never assigned. In any system that builds its TLS options this way, connections are silently made without any certificate validation.

The researcher traced the issue to a concrete package: global-agent (https://www.npmjs.com/package/global-agent) builds its options objects with explicit undefined values, and is therefore vulnerable through this behaviour. As a result, all users of that package are, by default, unintentionally not validating TLS certificates for all connections.

Exploitation

Step 1: request an expired certificate with an explicit undefined option

The reproduction is a short script that issues an HTTPS GET to a host with an expired certificate, passing rejectUnauthorized: undefined explicitly:

JavaScript
const https = require('https');
const request = https.get('https://expired.badssl.com', { rejectUnauthorized: undefined });
request.on('error', (e) => console.log('Request failed:', e.message));
request.on('response', (e) => console.log('Request succeeded'));

The https.get call passes the URL and an options object whose only field is rejectUnauthorized set to undefined. The script registers an error handler that prints the failure message and a response handler that prints a success message, so the outcome of the connection is visible either way.

Step 2: observe that the request succeeds

Running the script produces a successful request. This is the bug: the request should fail, because expired.badssl.com by design presents an expired TLS certificate, and the documented default is to verify the certificate and emit an error event when verification fails.

Step 3: remove the option or set it to true

The researcher then removes the { rejectUnauthorized: undefined } option entirely, or changes its value to true. In both cases the request fails as expected, due to the expired certificate. This confirms that the failure in step 2 is caused specifically by the explicit undefined value, not by the test host or the environment.

Root cause

The report does not include a patch or a code-level analysis of the option handling, so the precise implementation mistake is not shown. What the report establishes is the observable behaviour: the check that decides whether to disable verification treats an explicit undefined the same way as false, instead of falling back to the documented default of true. The documentation and the implementation therefore disagree, and the implementation is the more permissive of the two.

That reading is likely, given the observed behaviour, but the report does not state the internal mechanism.

Impact

Any system that accidentally supplies an undefined value for rejectUnauthorized, while assuming this is equivalent to supplying no value at all, loses all TLS and HTTPS security. The report describes the consequence as breaking all HTTPS protections, allowing complete disclosure or trivial manipulation of all HTTPS requests and responses by anybody capable of carrying out a man-in-the-middle attack on the TCP connection.

The condition is accidental misconfiguration rather than a deliberate attacker action: the vulnerable party has to build its options with an undefined field. The global-agent package is given as a real example where this happens by default, so all its users are unintentionally not validating TLS certificates for all connections.

Fix

Based on the finding, the behaviour should match the documentation: only an explicit false should disable certificate verification, and an undefined value should fall back to the default of true. Callers that build options dynamically should also avoid emitting fields with undefined values, but that is a workaround, not a fix for the module itself.

Variants to test

  1. Pass other falsy values for rejectUnauthorized, such as 0, '' or null, against a host with an invalid certificate. A positive result is a successful connection where an error event was expected.
  2. Build the options object dynamically so the field is present but unassigned, then connect to a host with an expired or self-signed certificate. A positive result is a successful response, showing that runtime-built configuration silently disables verification.
  3. Test the same pattern in libraries that wrap TLS options, such as HTTP agents and proxy packages, by checking whether they forward undefined fields into the connection options. A positive result is a connection to an invalid-certificate host that succeeds through the wrapper.
  4. Repeat the reproduction on each Node.js release line in use. A positive result is the same successful request on versions that should verify by default.

Hunting notes

This class of bug hides where configuration objects are assembled at runtime rather than written literally: option builders, agent and proxy wrappers, and anything that copies values from environment variables or optional settings into a TLS options object. The signal during review is a field that can be present with an undefined value, combined with documentation that promises a secure default. The cheapest confirmation is a request to a host with a deliberately invalid certificate, such as expired.badssl.com, with the suspect option set the same way the target sets it; a successful response where a certificate error is expected is the proof.

The researcher demonstrated impact by tying the behaviour to a widely used package whose default configuration triggers it, which turns an edge case in option handling into a real exposure for that package's users. The report was rated low severity with a 150.00 USD bounty and disclosed on 2021-09-10.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 11 min read.

What happened, in plain words

Node.js is a program that lets people run JavaScript code outside a web browser, for example on a server. It comes with built-in tools for talking to other computers over the internet. One of those tools is called tls, and it handles the encryption that keeps a connection private. Another tool, https, is built on top of tls and is what most code uses to fetch web pages securely.

When one computer connects to another over HTTPS, the server presents a certificate. A certificate is a small digital document that proves the server is who it claims to be, a bit like a passport. The client checks that passport against a list of trusted authorities (called CAs, short for Certificate Authorities). If the passport is expired, self-signed or issued by someone untrusted, the check should fail and the connection should be refused.

Node.js has a setting called rejectUnauthorized that controls this check. The documentation says the check is only switched off when the value is exactly false. Any other value, including the special JavaScript value undefined, should leave the check switched on. The researcher found that this is not what happens: an explicit undefined also switches the check off, silently. That means a program can believe it is verifying certificates while in fact it is accepting anything.

This matters because undefined is what JavaScript gives you when a variable has been declared but never given a value. It is very easy to end up with undefined in a configuration object without meaning to. The researcher also found a popular package, global-agent, that builds its settings this way, so everyone using that package was affected by default.

Background you need first

A few ideas come up again and again in this report. Here they are from zero.

API. An API is a set of functions a program offers so that other programs can use its features. Think of a restaurant menu: you do not go into the kitchen, you order from the menu and the kitchen does the work. Node.js offers an API for making network connections, and the tls and https modules are parts of that menu.

Module. A module is a bundle of related code that you load into your program. require('https') loads the HTTPS module, the same way you might pick up a toolbox labelled "web requests".

TLS and HTTPS. TLS is the encryption layer that protects data travelling between two computers. HTTPS is simply the web (HTTP) running inside TLS. When you see the padlock in your browser, TLS is what put it there.

Certificate. A certificate is a digital identity document for a server. It contains the server's name and a signature from a Certificate Authority, an organisation trusted to vouch for identities. Your computer ships with a list of these authorities it trusts.

Certificate validation. Validation is the act of checking that certificate: is it signed by a trusted authority, is it for the right name, and has it expired? If any answer is wrong, the connection should be rejected.

Man-in-the-middle (MITM). Imagine two people passing notes through a third person who secretly reads and rewrites them. In networking, a MITM is someone sitting between your computer and the server, able to read and change traffic. TLS exists precisely to stop this, but only if certificates are actually checked.

undefined. In JavaScript, undefined is a special value meaning "nothing here". A variable that was never assigned has the value undefined. It is not the same as false, and it is not the same as null (which is an intentional "empty" value).

Boolean. A boolean is a value that is either true or false. The rejectUnauthorized setting is documented as a boolean.

Falsy values. JavaScript treats several values as if they were false when used in a condition: false, 0, an empty string '', null, undefined and NaN. This is a common source of confusion, and it is central to this bug.

Default value. A default is what a setting falls back to when you do not supply one. The documentation says the default for rejectUnauthorized is true, meaning verification is on unless you say otherwise.

The product and the feature

Node.js is a runtime for JavaScript. It includes a tls module for encrypted connections and an https module built on top of it. When a client connects to a TLS server, it passes an options object to tls.connect. One field of that object is rejectUnauthorized, which decides whether the server certificate is checked against the trusted authorities.

The documentation for tls.connect describes the option like this:

rejectUnauthorized <boolean> If not false, the server certificate is verified against the list of supplied CAs. An 'error' event is emitted if verification fails; err.code contains the OpenSSL error code. Default: true.

Read literally, only the exact value false should switch verification off. Every other value, including an explicit undefined, should leave the default in place, which is verification switched on. The report shows that reality does not match this promise.

Setting up the test

The report does not describe any special accounts, roles or tools. The reproduction uses only the Node.js standard library, specifically the https module, and a public test host called expired.badssl.com. That host is built on purpose to serve an expired certificate, so it is a safe and reliable way to see whether a client checks certificates or not.

The researcher states that the behaviour appears to affect all active Node versions. No specific version number is given.

How the researcher found it

The researcher found this after discovering that the global-agent package builds its settings objects with explicit undefined values. The documentation promises that verification is skipped only when rejectUnauthorized is false, but in practice an explicit undefined also switches verification off.

Two things make this dangerous. First, it is reasonable to assume that undefined behaves the same as not setting the option at all, and therefore the same as the documented default of true. Second, settings objects assembled while the program runs easily end up with fields whose value is undefined, for example when a value is copied from a variable that was never assigned.

In any system that builds its TLS settings this way, connections are made without any certificate checking, and nothing warns the program or the user.

The researcher traced the issue to a concrete package: global-agent (https://www.npmjs.com/package/global-agent). That package builds its settings objects with explicit undefined values, so it is affected by this behaviour. As a result, all users of that package are, by default, unintentionally not validating TLS certificates for all connections.

Reproducing the bug

Step 1: request an expired certificate with an explicit undefined option

The reproduction is a short script. It makes an HTTPS request to a host with an expired certificate and passes rejectUnauthorized: undefined explicitly:

JavaScript
const https = require('https');
const request = https.get('https://expired.badssl.com', { rejectUnauthorized: undefined });
request.on('error', (e) => console.log('Request failed:', e.message));
request.on('response', (e) => console.log('Request succeeded'));

Line by line:

  1. const https = require('https'); loads the built-in HTTPS module and stores it in a variable called https. This is the toolbox the script will use.
  2. const request = https.get('https://expired.badssl.com', { rejectUnauthorized: undefined }); starts a request to the test host. The second argument is the options object, and its only field is rejectUnauthorized set to undefined. The result of the call is stored in request so the script can listen for what happens next.
  3. request.on('error', (e) => console.log('Request failed:', e.message)); registers a handler for the error event. If the connection fails, for example because the certificate is rejected, this prints the failure message.
  4. request.on('response', (e) => console.log('Request succeeded')); registers a handler for the response event. If the connection succeeds, this prints a success message.

Having both handlers means the outcome is visible either way. The script does not need to guess: whichever line prints tells you whether the certificate was checked.

Step 2: observe that the request succeeds

Running the script produces a successful request. That is the bug. The request should fail, because expired.badssl.com by design presents an expired certificate, and the documented default is to verify the certificate and emit an error event when verification fails.

Step 3: remove the option or set it to true

The researcher then removes the { rejectUnauthorized: undefined } option entirely, or changes its value to true. In both cases the request fails as expected, because of the expired certificate.

This step matters because it rules out other explanations. If the request failed in every case, you might suspect the test host or the network. By showing that the same host fails when the option is absent or true, and succeeds only when the option is explicitly undefined, the researcher pins the cause on that one value.

Why it happens

The report does not include a patch or a line-by-line analysis of the option handling, so the precise implementation mistake is not shown. What the report establishes is the observable behaviour: the check that decides whether to switch verification off treats an explicit undefined the same way as false, instead of falling back to the documented default of true. The documentation and the implementation therefore disagree, and the implementation is the more permissive of the two.

The option was present in the object, just with the value undefined. That reading fits the observed behaviour, but the report does not state the internal mechanism.

What an attacker could do

Any system that accidentally supplies an undefined value for rejectUnauthorized, while assuming this is the same as supplying no value at all, loses all TLS and HTTPS protection. The report describes the consequence as breaking all HTTPS protections, allowing complete disclosure or trivial manipulation of all HTTPS requests and responses by anybody capable of carrying out a man-in-the-middle attack on the TCP connection.

In everyday terms: anybody capable of intercepting the TCP connection could read and change all HTTPS requests and responses without the program noticing.

The condition is accidental misconfiguration rather than a deliberate attacker action. The vulnerable party has to build its settings with an undefined field. The global-agent package is given as a real example where this happens by default, so all its users are unintentionally not validating TLS certificates for all connections.

The fix

Based on the finding, the behaviour should match the documentation: only an explicit false should switch certificate verification off, and an undefined value should fall back to the default of true. Programs that build settings dynamically should also avoid emitting fields with undefined values, but that is a workaround, not a fix for the module itself.

Other cases worth testing

  1. Pass other falsy values for rejectUnauthorized, such as 0, '' or null, against a host with an invalid certificate. A positive result is a successful connection where an error event was expected.
  2. Build the settings object dynamically so the field is present but unassigned, then connect to a host with an expired or self-signed certificate. A positive result is a successful response, showing that runtime-built configuration silently switches verification off.
  3. Test the same pattern in libraries that wrap TLS settings, such as HTTP agents and proxy packages, by checking whether they pass undefined fields into the connection settings. A positive result is a connection to an invalid-certificate host that succeeds through the wrapper.
  4. Repeat the reproduction on each Node.js release line in use. A positive result is the same successful request on versions that should verify by default.

Where to look for this kind of bug

This class of bug hides where settings objects are assembled while the program runs rather than written out literally: option builders, agent and proxy wrappers, and anything that copies values from environment variables or optional settings into a TLS settings object. The signal during review is a field that can be present with an undefined value, combined with documentation that promises a secure default. The cheapest confirmation is a request to a host with a deliberately invalid certificate, such as expired.badssl.com, with the suspect option set the same way the target sets it. A successful response where a certificate error is expected is the proof.

The researcher demonstrated impact by tying the behaviour to a widely used package whose default configuration triggers it, which turns an edge case in option handling into a real exposure for that package's users. The report was rated low severity with a 150.00 USD bounty and disclosed on 2021-09-10.

Glossary

  • API: a set of functions a program offers so other programs can use its features, like a restaurant menu.
  • Boolean: a value that is either true or false.
  • CA (Certificate Authority): an organisation trusted to vouch for the identity of servers by signing their certificates.
  • Certificate: a digital identity document for a server, containing its name and a signature from a trusted authority.
  • Certificate validation: checking that a certificate is signed by a trusted authority, is for the right name and has not expired.
  • Default value: what a setting falls back to when you do not supply one.
  • Falsy value: a value JavaScript treats as false in a condition, such as false, 0, '', null, undefined or NaN.
  • HTTPS: the web (HTTP) running inside TLS, shown by the padlock in a browser.
  • Man-in-the-middle (MITM): someone sitting between your computer and a server, able to read and change traffic.
  • Module: a bundle of related code loaded into a program, such as Node.js's https module.
  • Node.js: a runtime that lets JavaScript run outside a browser, for example on a server.
  • Options object: a JavaScript object passed to a function to configure how it behaves.
  • TLS: the encryption layer that protects data travelling between two computers.
  • undefined: a special JavaScript value meaning "nothing here", given to variables that were never assigned.

More by Tim Perry

View all

3 more disclosed reports, best first.

More from Node.js

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cryptographic weaknesses

The lesson behind this report.

Encryption, signatures, randomness or certificate checks that do not protect what they claim to.