Files
node/test/parallel/test-permission-env-file.js
James M Snell f378cfcfbf src,lib: add --allow-env permission
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
PR-URL: https://github.com/nodejs/node/pull/66132
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2026-09-26 21:18:43 +00:00

72 lines
1.7 KiB
JavaScript

'use strict';
const common = require('../common');
const { isMainThread } = require('worker_threads');
if (!isMainThread) {
common.skip('This test only works on a main thread');
}
const assert = require('assert');
const fs = require('fs');
const tmpdir = require('../common/tmpdir');
const { spawnSyncAndAssert } = require('../common/child_process');
tmpdir.refresh();
const envFile = tmpdir.resolve('permission.env');
fs.writeFileSync(
envFile,
'PERMISSION_ENV_FROM_FILE=file\nPERMISSION_ENV_BOTH=file\n');
const env = {
...process.env,
PERMISSION_ENV_BOTH: 'inherited',
};
const script = `console.log(JSON.stringify({
fromFile: process.env.PERMISSION_ENV_FROM_FILE,
both: process.env.PERMISSION_ENV_BOTH,
has: [
process.permission.has('env', 'PERMISSION_ENV_FROM_FILE'),
process.permission.has('env', 'PERMISSION_ENV_BOTH'),
],
}))`;
// Variables defined in an env file are allowed. Only the file's values are
// visible: an inherited value for the same name was removed at startup.
spawnSyncAndAssert(
process.execPath,
['--permission', `--env-file=${envFile}`, '-e', script],
{ env },
{
stdout(output) {
assert.deepStrictEqual(JSON.parse(output), {
fromFile: 'file',
both: 'file',
has: [true, true],
});
},
});
// A name that --allow-env grants access to keeps the usual precedence of the
// inherited value over the file's.
spawnSyncAndAssert(
process.execPath,
[
'--permission',
'--allow-env=PERMISSION_ENV_BOTH',
`--env-file=${envFile}`,
'-e', script,
],
{ env },
{
stdout(output) {
assert.deepStrictEqual(JSON.parse(output), {
fromFile: 'file',
both: 'inherited',
has: [true, true],
});
},
});