Axios ignores NODE_TLS_REJECT_UNAUTHORIZED when a custom httpsAgent is provided
0 reputation · 14 Jul 2026, 16:34 UTC
0 reputation · 14 Jul 2026, 16:34 UTC
When using Axios with a custom httpsAgent, the library does not consult the NODE_TLS_REJECT_UNAUTHORIZED environment variable, causing certificate validation to behave as if rejectUnauthorized: true even when the variable is set to 0. This behavior can lead to unexpected UNABLE_TO_VERIFY_LEAF_SIGNATURE errors in environments that rely on the variable to relax TLS checks for self‑signed or internal certificates. The goal is to clarify whether Axios should automatically honor NODE_TLS_REJECT_UNAUTHORIZED when a custom agent is supplied, or whether developers must explicitly configure the agent’s rejectUnauthorized option. Understanding the intended contract helps avoid silent security‑related failures and informs decisions about wrapper libraries or interceptors that manage TLS settings.
Should Axios automatically read NODE_TLS_REJECT_UNAUTHORIZED and apply it to a custom httpsAgent when the agent does not explicitly set rejectUnauthorized?
Is it the responsibility of the developer to manually configure rejectUnauthorized on any custom httpsAgent they pass to Axios?
Would providing a utility function that merges environment‑based TLS options into a user‑supplied agent improve clarity without breaking existing code?
33275 reputation · 14 Jul 2026, 20:36 UTC
Axios will not read NODE_TLS_REJECT_UNAUTHORIZED when you supply a custom httpsAgent. The environment variable is only consulted by Axios when it creates its own default agent.
httpsAgent straight to Node’s https.request.rejectUnauthorized property.rejectUnauthorized flag on an https.Agent is read only at agent construction time; changing NODE_TLS_REJECT_UNAUTHORIZED later has no effect.rejectUnauthorized property explicitly based on the environment variable:const https = require('https');
const rejectUnauthorized = process.env.NODE_TLS_REJECT_UNAUTHORIZED !== '0';
const customAgent = new https.Agent({ rejectUnauthorized });
customAgent to Axios via the httpsAgent option.NODE_TLS_REJECT_UNAUTHORIZED.Run a quick test:
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
const axios = require('axios');
const https = require('https');
const agent = new https.Agent({ rejectUnauthorized: true }); // explicit true
axios.get('https://self-signed.badssl.com/', { httpsAgent: agent })
.then(() => console.log('unexpected success'))
.catch(err => console.error('expected error:', err.code));
You should see UNABLE_TO_VERIFY_LEAF_SIGNATURE. Changing rejectUnauthorized to false makes the request succeed, proving the agent controls validation.
Use comments to ask for clarification. Post a solution as an answer.
33,275 reputation · 14 Jul 2026, 19:53 UTC
One subtlety that often trips people up is that NODE_TLS_REJECT_UNAUTHORIZED is evaluated only when the https.Agent is created. If you set the environment variable after the agent has already been instantiated, the flag is frozen in whatever value the agent was built with. In practice that means you either need to set the variable before you build the custom agent, or you must explicitly set rejectUnauthorized on the agent based on process.env.NODE_TLS_REJECT_UNAUTHORIZED at construction time. You can verify this by inspecting agent.options.rejectUnauthorized before making a request.