Skip to content

src,lib: add --allow-env permission - #66132

Open
jasnell wants to merge 2 commits into
nodejs:mainfrom
jasnell:jasnell/allow-env
Open

jasnell wants to merge 2 commits into
nodejs:mainfrom
jasnell:jasnell/allow-env

Conversation

@jasnell

@jasnell jasnell commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

Necessarily semver-major.

Alternative for a previous attempt in #62827 that stalled out.

When --permission is on, every env var not matched by --allow-env is removed at startup. It takes names, patterns (PREFI_*), or *, repeatable or comma-sep'd.

There are a range of env vars that Node.js itself uses, and a default range that are generally known to be safe in common usage. These are never scrubbed. These include things like NODE_OPTIONS, NODE_EXTRA_CA_CERTS, PATH, HOME, etc.

Env vars can be dropped at runtime after reading using permission.drop(). This is a stronger protection than using process.env.FOO = undefined because it will scrub the env var also from the environment block.

On Linux, the removed entries are overwritten in the initial environment block and fs reads of /proc/*/environ are denied.

On Windows, removal also clears the C runtime's copy of the environ using _wputenv_s

Reading a removed name returns undefined, warns once per name, and publishes to a diagnostics channel.

Env file keys are allowed. If the user had reason to pass in an env file the assumption is they meant to allow them.

File-source config (node.config.json and NODE_OPTIONS from a .env file can only narrow the allow list.

Embedders must call ScrubProcessEnvironment() themselves on startup. This is left up to the embedder to determine the exact timing but needs to be called before startup actually happens.

Child processes are started with --allow-env=*. Those either receive the explicit env they were started with or only the env they inherit from the parent. Since the parent process is scrubbed, it should never be more than what the parent can see.

The key motivation here is that environment variables are the primary mechanism for injecting secrets into applications. These can be trivially exfiltrated using simple one-liners like fetch('...', { headers: { secret: process.env.SECRET } }). Simply unsetting those via process.env.SECRET = undefined is typically not enough since those can still be read from the underlying environment block. This PR provides stronger protection but it obviously cannot be 100% since depending on how it is used, the strings can still be in memory.

The prior attempt to add this in #62827 had a number of flaws that this version addresses. That PR used an incomplete view filter. process.report.getReport(), native addons, FFI,
and /proc/self/environ could all still read the real environment. Scrubbing
before any JavaScript runs protects those paths. This PR also fixes a number of other breaking changes the other PR would have introduced:

  • Node.js's own configuration variables are always kept.
  • Writes to process.env and process.loadEnvFile() keep working.
  • Keys defined in --env-file files are allowed.
  • Child processes get --allow-env=*, because the environment they inherit has already been scrubbed.

$ ENV_A=1 ENV_B=2 ./node --permission -pe "process.env"
{
  COLORTERM: 'truecolor',
  TERM_PROGRAM_VERSION: '3.4',
  TMUX: '/tmp/tmux-1000/default,1258,1',
  HOME: '/home/dev',
  LANG: 'C.UTF-8',
  TERM: 'tmux-256color',
  PATH: '/home/dev/bin:/usr/lib/ccache:/opt/rust/cargo/bin:/opt/nvm/versions/node/v26.9.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  TERM_PROGRAM: 'tmux'
}

$ ENV_A=1 ENV_B=2 ./node --permission -pe "process.env" --allow-env=ENV_*
{
  ENV_B: '2',
  ENV_A: '1',
  COLORTERM: 'truecolor',
  TERM_PROGRAM_VERSION: '3.4',
  TMUX: '/tmp/tmux-1000/default,1258,1',
  HOME: '/home/dev',
  LANG: 'C.UTF-8',
  TERM: 'tmux-256color',
  PATH: '/home/dev/bin:/usr/lib/ccache:/opt/rust/cargo/bin:/opt/nvm/versions/node/v26.9.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
  TERM_PROGRAM: 'tmux'
}
$

@jasnell jasnell added semver-major PRs that contain breaking changes and should be released in the next major version. permission Issues and PRs related to the Permission Model. labels Sep 19, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/config
  • @nodejs/gyp
  • @nodejs/security-wg
  • @nodejs/startup

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. labels Sep 19, 2026
@codecov

codecov Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.35644% with 79 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.27%. Comparing base (305cfcd) to head (16fcfb4).
⚠️ Report is 20 commits behind head on main.

Files with missing lines Patch % Lines
src/permission/env_permission.cc 83.81% 7 Missing and 38 partials ⚠️
lib/child_process.js 0.00% 12 Missing ⚠️
src/node.cc 93.00% 3 Missing and 4 partials ⚠️
src/node_env_var.cc 78.12% 3 Missing and 4 partials ⚠️
src/permission/permission.cc 72.00% 3 Missing and 4 partials ⚠️
src/node_dotenv.cc 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66132      +/-   ##
==========================================
- Coverage   90.29%   90.27%   -0.03%     
==========================================
  Files         789      791       +2     
  Lines      272909   273370     +461     
  Branches    52119    52231     +112     
==========================================
+ Hits       246435   246791     +356     
- Misses      16928    17002      +74     
- Partials     9546     9577      +31     
Files with missing lines Coverage Δ
lib/internal/process/permission.js 92.47% <100.00%> (+0.08%) ⬆️
lib/internal/process/pre_execution.js 96.04% <100.00%> (+0.28%) ⬆️
src/env.cc 82.20% <100.00%> (+0.04%) ⬆️
src/node.h 92.15% <100.00%> (+0.49%) ⬆️
src/node_dotenv.h 100.00% <ø> (ø)
src/node_options.cc 80.76% <100.00%> (-0.11%) ⬇️
src/node_options.h 95.63% <ø> (ø)
src/permission/env_permission.h 100.00% <100.00%> (ø)
src/permission/permission.h 100.00% <ø> (ø)
src/permission/permission_base.h 100.00% <ø> (ø)
... and 6 more

... and 35 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.

@jasnell
jasnell marked this pull request as draft September 19, 2026 16:19
@jasnell

This comment was marked as resolved.

@jasnell
jasnell marked this pull request as ready for review September 19, 2026 17:29
@mcollina

Copy link
Copy Markdown
Member

why semver-major?

@jasnell

jasnell commented Sep 20, 2026

Copy link
Copy Markdown
Member Author

It's major because it's filter-by-default when --permission is used. If you just run ./node --permission, then it scrub all env vars but the safelist.

@RafaelGSS RafaelGSS left a comment

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.

We discussed this in nodejs/security-wg#993. I think we should revisit the concerns there before proceeding.

Removing the variables from the actual process environment addresses some of the issues with the previous approach, but I'm still not convinced about the use case. For instance, if my application needs AWS_SECRET_ACCESS_KEY and I allow it, every dependency can still read it. If the application doesn't need it, why not remove it before starting Node.js?

The read-then-drop case seems more useful, but does permission.drop() also remove the value from the initial environment block? From reading RemoveFromEnvironment, it seems that value would still be there on Linux.

I'd like to understand which use case requires this in core and what we intend to guarantee before getting into the implementation details, because as soon as we land this, I do expect a lot of AI-Sloop on H1... sadly.

@jasnell

jasnell commented Sep 21, 2026

Copy link
Copy Markdown
Member Author

... but does permission.drop() also remove the value from the initial environment block?

Yes, it should.

The idea is keep the vars for as long as needed then scrub them.

@jasnell

jasnell commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

@RafaelGSS :

... if my application needs AWS_SECRET_ACCESS_KEY and I allow it, every dependency can still read it.

This is true for all permissions tho. If you allow fs access, every dependency gets fs access. That's why drop(...) exists :-). This doesn't introduce a new dynamic.

...If the application doesn't need it, why not remove it before starting Node.js?

It's entirely possible (and likely) for environment variables to be present without the user even knowing. If the operator has control enough of the system to manage the environment, they are actually less likely to be using the permission system in the first place, choosing instead to rely on OS-level sandboxing or other isolation techniques. The permission system is largely in place for running in less trustworthy environments.

Comment thread src/permission/env_permission.cc Outdated
@RafaelGSS

Copy link
Copy Markdown
Member

Fair enough, I'm +1 with the feature then (semver-major as you already pointed out).

@jasnell
jasnell force-pushed the jasnell/allow-env branch 2 times, most recently from 64b9265 to c50ac3e Compare September 22, 2026 17:22
@jasnell
jasnell requested a review from RafaelGSS September 22, 2026 17:22
@jasnell
jasnell force-pushed the jasnell/allow-env branch 2 times, most recently from 444b524 to ff1e6be Compare September 22, 2026 18:30
@jasnell

jasnell commented Sep 22, 2026

Copy link
Copy Markdown
Member Author

I've updated and strengthened the scrubbing. Access to /proc/{any}/environ is blocked and symlinks are correctly handled now.

@RafaelGSS RafaelGSS left a comment

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.

LGTM!

@jasnell jasnell 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 Sep 22, 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 Sep 22, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@jasnell jasnell added the notable-change PRs with changes that should be highlighted in changelogs. label Sep 23, 2026
@github-actions

Copy link
Copy Markdown
Contributor

The notable-change PRs with changes that should be highlighted in changelogs. label has been added by @jasnell.

Please suggest a text for the release notes if you'd like to include a more detailed summary, then proceed to update the PR description with the text or a link to the notable change suggested text comment. Otherwise, the commit will be placed in the Other Notable Changes section.

@jasnell

jasnell commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

@nodejs/tsc ... as a semver major this needs an additional signoff please :-)

Necessarily semver-major.

When `--permission` is on, every env var not matched by
`--allow-env` is removed at startup. It takes names,
prefix patterns (`PREFIX_*`), or `*`, repeatable or
comma-sep'd.

There are a range of env vars that Node.js itself uses,
and a default range that are generally known to be safe
in common usage. These are never scrubbed. These include
things like `NODE_OPTIONS`, `NODE_EXTRA_CA_CERTS`, `PATH`,
`HOME`, etc. `NODE_ENV` is not in the defaults and must
be allowed explicitly.

Proxy vars (`HTTP_PROXY`, `HTTPS_PROXY`, `NO_PROXY`) are
also not in the defaults since they can carry credentials.
When `--use-env-proxy` or `NODE_USE_ENV_PROXY` is set and
any of them were removed, a single warning naming them is
emitted.

Env vars can be dropped at runtime after reading using
`permission.drop()`. This is a stronger protection than
using `process.env.FOO = undefined` because it will
scrub the env var also from the environment block.

On Linux, the removed entries are overwritten in the
initial environment block. fs reads of any other
process's /proc/<pid>/environ, ancestors included, are
denied regardless of `--allow-fs-read`. A process's own
is readable only with `--allow-env=*`. Symlinks are
resolved before the check so paths like
/dev/fd/../../<ppid>/environ are caught. The check only
canonicalizes paths that statfs() reports are on procfs.

On Windows, removal also clears the C runtime's copy
of the environ using _wputenv_s.

Reading a removed name returns undefined, warns once per
name, and publishes to a diagnostics channel.

Env file keys are allowed. If the user had reason to pass
in an env file the assumption is they meant to allow them.

File-source config (node.config.json and NODE_OPTIONS
from a .env file) can only narrow the allow list.

Embedders must call ScrubProcessEnvironment() themselves
on startup. This is left up to the embedder to determine
the exact timing but needs to be called before startup
actually happens.

Child processes are started with `--allow-env=*`. Those
either receive the explicit env they were started with
or only the env they inherit from the parent. Since the
parent process is scrubbed, and the child cannot read
any other process's /proc/<pid>/environ, it should never
see more than the parent can.

Main part of the impl was done by hand. Docs, tests,
verification pass, and cleanup nits were automated.

Signed-off-by: James M Snell <jasnell@gmail.com>
Assisted-by: Opencode
Signed-off-by: James M Snell <jasnell@gmail.com>
Assisted-by: Opencode
@nodejs-github-bot

This comment was marked as outdated.

@mcollina mcollina left a comment

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.

lgtm

@jasnell jasnell added author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. labels Sep 24, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue-rebase PRs the Commit Queue should land as multiple self-contained commits. lib / src Issues and PRs involving general changes in the lib/ or src/ directories. needs-ci PRs that need a full CI run. notable-change PRs with changes that should be highlighted in changelogs. permission Issues and PRs related to the Permission Model. semver-major PRs that contain breaking changes and should be released in the next major version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants