Node.js Reference ยท CLI Flags
Node.js NODE_TLS_REJECT_UNAUTHORIZED environment variable
Why setting NODE_TLS_REJECT_UNAUTHORIZED=0 in Node.js disables certificate checks for every HTTPS connection, and safer ways to trust a self-signed server.
When Node.js opens a TLS connection (HTTPS, secure WebSockets, database drivers with TLS), it checks that the server's certificate chains up to a trusted authority and that the name matches the host you asked for. If the check fails, the connection is refused with an error such as DEPTH_ZERO_SELF_SIGNED_CERT or CERT_HAS_EXPIRED.
Setting NODE_TLS_REJECT_UNAUTHORIZED=0 turns this check off for every TLS connection in the process. Without verification, anyone on the network path can impersonate the server, read the traffic and change it, and neither side notices. The variable shows up in countless "quick fix" answers; treat it as a bug report, not a solution.
// shell (do NOT ship this): NODE_TLS_REJECT_UNAUTHORIZED=0 node app.js
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
// NODE_TLS_REJECT_UNAUTHORIZED=0 (disable verification, process-wide) // tls.connect({ rejectUnauthorized: false }) (one connection) // new https.Agent({ ca: pemText }) (trust one more authority)
| Setting | Effect |
|---|---|
unset or 1 |
Certificates and host names are verified (default). |
0 |
Verification is skipped for all TLS clients in the process; Node prints a warning. |
ca option |
Replaces the trusted list for that connection or agent. |
NODE_EXTRA_CA_CERTS |
Adds authorities for the whole process while keeping verification. |
Usage notes
- What exactly is lost. The certificate chain check and the host name check. The traffic is still encrypted, but you may be talking to an attacker, so the encryption gives no protection.
- Process-wide. The variable affects database clients, API calls, webhooks and every dependency in your tree, including ones you did not write.
- Node warns when a process with the variable set to
0first creates a TLS connection (also when your code assignsprocess.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"), as the first example shows. A warning in production logs is a finding. - Better fixes, in order of preference: use a certificate from a public CA (Let's Encrypt is free); give Node your private CA with
NODE_EXTRA_CA_CERTS; passcaon the single agent that talks to the private server; for local development generate a certificate with a local CA tool instead of switching checks off. - Self-signed server for tests is a legitimate case: put the test certificate in
cafor the test client. Do not switch the whole process. - Leaks into children. Setting it in a parent shell or
.envfile silently affects every Node process started below it, including CI jobs and deployment scripts. - Error codes to recognize:
UNABLE_TO_VERIFY_LEAF_SIGNATURE(missing intermediate or unknown CA),SELF_SIGNED_CERT_IN_CHAIN,CERT_HAS_EXPIRED, andERR_TLS_CERT_ALTNAME_INVALID(name mismatch). Each has a specific fix; none needs verification turned off. - Code review rule: search a repository for
rejectUnauthorized: falseandNODE_TLS_REJECT_UNAUTHORIZEDbefore release.