Node.js HTTPS call fails with UNABLE_TO_VERIFY_LEAF_SIGNATURE
Error: unable to verify the first certificateShort answer
Node.js could not build a trusted chain from the server's certificate to a root it knows. Usual causes: the server does not send its intermediate certificate, a company proxy re-signs traffic with its own CA, or your code replaces the default CA list. Fix the chain or add the right CA. Never turn verification off.
Fix it fast
1Find out which cause you have
Print the chain the server sends. If only one certificate is listed, the intermediate is missing.
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null | grep -E "^ *[0-9]+ s:|Verify return"2Match what you see, then jump to its fix
- 1
s_client lists a single certificate; browsers may still work because they fetch intermediates, Node.js does notThe server sends only its own certificate, not the intermediateFix - 2
Works from home, fails on the office network; the issuer in s_client is your company, not a public CAA company proxy or antivirus re-signs HTTPS trafficFix - 3
Only requests from one agent or client fail; the code sets ca: or an https.Agent with certificatesCode passes its own ca option and replaces the defaultsFix
- 1
What does the error look like?
Error: unable to verify the first certificatecode: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'
Cause 1 of 3
The server sends only its own certificate, not the intermediate
1Check
OpenSSL first looks in the trust store and then in the untrusted intermediates it was given, to build the path to a root. If the server never sends the intermediate, no path can be built.
openssl s_client -connect example.com:443 -servername example.com -showcerts </dev/null | grep -c "BEGIN CERTIFICATE"2Fix
On the server, install the full chain file from your certificate authority (leaf plus intermediates), not the leaf alone. For nginx, point ssl_certificate at the chain file and reload.
ssl_certificate /etc/ssl/example/fullchain.pem;
ssl_certificate_key /etc/ssl/example/privkey.pem;3Verify
The chain now lists two or more certificates and Node.js connects.
sudo nginx -t && sudo systemctl reload nginx
node -e "fetch('https://example.com').then(r=>console.log(r.status))"Confirmed by docs.openssl.org
Cause 2 of 3
A company proxy or antivirus re-signs HTTPS traffic
1Check
The issuer name in the chain shows who signs. If it is an internal CA, Node.js does not trust it, because it uses its own bundled list.
openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -issuer2Fix
Get the CA certificate in PEM format from your IT team and add it with NODE_EXTRA_CA_CERTS, which extends the well-known roots. It is read only when the process starts. On Node.js 23.8 or newer, --use-system-ca can use the operating system store instead.
export NODE_EXTRA_CA_CERTS=/etc/ssl/corp-root-ca.pem
node app.js
# or, Node.js 23.8+
node --use-system-ca app.js3Verify
The request returns a status code.
node -e "fetch('https://example.com').then(r=>console.log(r.status))"Confirmed by nodejs.org
Cause 3 of 3
Code passes its own ca option and replaces the defaults
1Check
When the ca option is set on a TLS or HTTPS client, neither the well-known nor the extra certificates are used. Search for it.
grep -rn "ca:" src/ | head2Fix
Put every CA you need in that option, public ones included, or remove the option and use NODE_EXTRA_CA_CERTS instead.
// prefer: no ca option, plus NODE_EXTRA_CA_CERTS in the environment
const agent = new https.Agent({ keepAlive: true });3Verify
Run the failing request again.
node request-test.jsConfirmed by nodejs.org
How do I stop it coming back?
- Test every new certificate with openssl s_client before go-live and check that more than one certificate is sent.
- Keep corporate CA files in version control or config management and set NODE_EXTRA_CA_CERTS in the service environment.
- Never ship NODE_TLS_REJECT_UNAUTHORIZED=0; the Node.js docs strongly discourage it.
- Alert on certificate expiry and chain changes.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Back end
npm rejects the certificate it gets from a company proxy
npm ERR! request to https://registry.npmjs.org/express failed, reason: self signed certificate in certificate chain
A proxy that inspects HTTPS traffic re-signs it with the company's own root certificate, which Node does not know, so npm refuses the connection.
3 causes, each with a fixSecurity
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
The browser blocks a script because of your Content Security Policy
Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.
The page's Content-Security-Policy does not allow what it tried to run or load.
5 causes, each with a fixSecurity
A valid JWT fails to decode because its segments are base64url, not base64
Convert.FromBase64String throws FormatException on a JWT segment
A JWT is three base64url segments joined by dots, and a standard base64 decoder refuses all three differences: base64url uses - and _ instead of + and /, leaves off the = padding, and the dots are not part of any base64 alphabet.
3 causes, each with a fixSecurity
jsonwebtoken rejects a token: invalid signature, jwt expired or jwt malformed
JsonWebTokenError: invalid signature
Each message points at a different step.
5 causes, each with a fixSecurity
An HTTPS page is blocked from loading an insecure http:// resource
Mixed Content: The page at 'https://example.com/' was loaded over HTTPS, but attempted to connect to the insecure endpoint 'http://api.example.com/items'. This request has been blocked.
Your page came over HTTPS but something on it points at an http:// address, and the browser refuses the risky kinds (scripts, styles, frames, fetch calls).
4 causes, each with a fix