Skip to content

src: honor NODE_EXTRA_CA_CERTS set in an env file - #66638

Open
marcopiraccini wants to merge 3 commits into
nodejs:mainfrom
marcopiraccini:env-file-extra-ca-certs
Open

marcopiraccini wants to merge 3 commits into
nodejs:mainfrom
marcopiraccini:env-file-extra-ca-certs

Conversation

@marcopiraccini

@marcopiraccini marcopiraccini commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

NODE_EXTRA_CA_CERTS set through --env-file appeared in process.env, but its certificates were not loaded because the startup lookup ran before the env files were applied.

Read the value from the parsed env files when it is absent from the process environment. This supports both --env-file and --env-file-if-exists, preserves environment precedence, and uses case-insensitive lookup on Windows.

Both lookups share the existing SafeGetenv() privilege check, so the env-file fallback cannot bypass it. Runtime changes, including process.loadEnvFile(), still do not reload certificates; the documentation now states this explicitly.

This revisits #51497, where review raised the concern that reading from env files could bypass SafeGetenv()'s privilege checks. Both lookups now share that check, addressing the concern.

Regression tests cover both flags, environment precedence, lowercase variable names on Windows, and a TLS connection that requires the extra CA.

Fixes: #51426
Refs: #51497

NODE_EXTRA_CA_CERTS is read once at startup, before the files passed
with --env-file are applied to the environment. A value set in an env
file showed up in process.env but the certificates were never loaded.

Look the variable up in the parsed env files when it is not in the
environment. The environment keeps precedence, and the env files are
ignored under the same conditions as the environment: when node runs
setuid, setgid or with Linux file capabilities.

Fixes: nodejs#51426
Refs: nodejs#51497
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/config
  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Oct 10, 2026
@marcopiraccini
marcopiraccini marked this pull request as ready for review October 10, 2026 08:15
Comment thread doc/api/cli.md Outdated
added: v7.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/00000

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: replace this. But not blocking.

Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
@codecov

codecov Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.64516% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.66%. Comparing base (ae5a0f4) to head (8c120dd).
⚠️ Report is 324 commits behind head on main.

Files with missing lines Patch % Lines
src/node_dotenv.h 50.00% 3 Missing and 1 partial ⚠️
src/node_credentials.cc 84.61% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66638      +/-   ##
==========================================
- Coverage   92.78%   90.66%   -2.12%     
==========================================
  Files         422      797     +375     
  Lines      193692   278815   +85123     
  Branches    29881    53620   +23739     
==========================================
+ Hits       179718   252801   +73083     
- Misses      13645    16360    +2715     
- Partials      329     9654    +9325     
Files with missing lines Coverage Δ
src/node.cc 79.86% <100.00%> (ø)
src/node_dotenv.cc 85.25% <100.00%> (ø)
src/node_internals.h 80.35% <ø> (ø)
src/node_credentials.cc 68.60% <84.61%> (ø)
src/node_dotenv.h 60.00% <50.00%> (ø)

... and 547 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@marcopiraccini

Copy link
Copy Markdown
Contributor Author

Note on coverage: The two partials are the elevated-privilege branch of HasElevatedPrivileges(), which cannot run in CI (it needs a setuid or capability-flagged binary). The same check was already a partial in SafeGetenv() on main; the PR only moves it into a shared helper.

@ShogunPanda ShogunPanda added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Oct 26, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Oct 26, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Comment thread src/node_dotenv.cc Outdated
auto match = store_.find(key);
if (match == store_.end()) return std::nullopt;
return match->second;
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of an #ifdef here, we should probably adjust the comparison functions for store_ to handle case-insensitivity on Windows

@marcopiraccini marcopiraccini Oct 31, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, good point. Changed store_ to use a case-insensitive comparator on Windows for the --env-file .

@addaleax note that This is broader than the original NODE_EXTRA_CA_CERTS fix: it also makes NODE_OPTIONS lookup case-insensitive (n win) and makes the last value win for keys that differ only in case.

Give the Dotenv store a comparator and let the instance that holds the
--env-file variables ignore case on Windows, where environment variable
names are case-insensitive, instead of scanning the store in
Dotenv::Get(). NODE_OPTIONS read from an env file follows the same rule.

util.parseEnv() and process.loadEnvFile() keep comparing keys
case-sensitively on every platform: their keys also end up in
case-sensitive stores, such as the environment of a Worker.

Refs: nodejs#66638 (comment)
Signed-off-by: marcopiraccini <marco.piraccini@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NODE_EXTRA_CA_CERTS does not work when set in env file

4 participants