Skip to content

child_process: reject invalid environment variable names - #67659

Open
ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:child-process-reject-invalid-env-keys
Open

ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:child-process-reject-invalid-env-keys

Conversation

@ShogunPanda

Copy link
Copy Markdown
Contributor

child_process.spawn() and friends build the child environment as
KEY=VALUE strings. A key in options.env that is empty or that
contains = cannot be represented that way: for example
{ 'A=B': 'C' } produced A=B=C, which the child reads as variable A
with value B=C.

This throws ERR_INVALID_ARG_VALUE for such keys in
normalizeSpawnArguments(), next to the existing null byte check. On
Windows a leading = is still accepted, because process.env contains
per-drive entries like =C: and env: process.env must keep working.
On POSIX, process.env does not expose entries starting with =, so
env: process.env is unaffected.

This is a bug fix and not an API change: such input has never been
representable, and the child always received a different variable than
the one requested. The same applies to the null byte validation of
env, which landed without a semver label and was backported to
v18.x and v19.x (#44782).

Includes a test covering spawn, spawnSync, exec, execSync,
execFile, execFileSync and fork, a positive case for a leading =
on Windows, and a one line doc note.

Keys in the `env` option that are empty or that contain `=` cannot be
represented in the `KEY=VALUE` form used by the child process
environment. They were passed through unchanged, so the child received
a different variable than the one the caller wrote. Throw
`ERR_INVALID_ARG_VALUE` for them, as is already done for keys that
contain null bytes.

On Windows, a leading `=` is allowed, as it is used by per-drive
variables such as `=C:`.

Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@nodejs-github-bot nodejs-github-bot added child_process Issues and PRs related to the child_process subsystem. needs-ci PRs that need a full CI run. labels Nov 20, 2026
invalidKeys.push('=C:');
} else {
// A lone '=' is not a valid name on Windows either.
invalidKeys.push('=');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I think it might be worth testing the lone = unconditionally, not just for Windows.

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.73%. Comparing base (030745c) to head (5285e5b).
⚠️ Report is 85 commits behind head on main.

Files with missing lines Patch % Lines
lib/child_process.js 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67659      +/-   ##
==========================================
+ Coverage   90.72%   90.73%   +0.01%     
==========================================
  Files         801      801              
  Lines      280960   280967       +7     
  Branches    54260    54270      +10     
==========================================
+ Hits       254892   254932      +40     
+ Misses      16248    16199      -49     
- Partials     9820     9836      +16     
Files with missing lines Coverage Δ
lib/child_process.js 91.51% <85.71%> (-0.04%) ⬇️

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

@sepehrst

sepehrst commented Nov 20, 2026 •

Copy link
Copy Markdown

On Windows, in addition to the lone = also anything starting with == is invalid
Non-standard drive letters can be created and used in CMD (for example, subst #: C:\ creates #:), whereas while creating =: succeeds it fails to retain the current directory information as Windows cannot create the required environment variable ==:.
So it might be worth also excluding anything starting with == on Windows:

[C:\]
$ subst ^=: C:\

[C:\]
$ ^=:

[=:\]
$ cd Windows
Not enough memory resources are available to process this command.

[=:\Windows]
$ C:

[C:\]
$ ^=:

[=:\]
$

For comparison:

[C:\]
$ subst #: C:\

[C:\]
$ #:

[#:\]
$ cd Windows

[#:\Windows]
$ C:

[C:\]
$ #:

[#:\Windows]
$

Edit:
Just noticed StringPrototypeIndexOf(key, '=', isWindows ? 1 : 0) !== -1 already rejects anything starting with ==. So the whole comment might be irrelevant, but I kept it anyway just in case adding an explicit test case for ^== (especially on Windows) seems worthwhile to guard against future refactors.

@jasnell jasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

child_process Issues and PRs related to the child_process subsystem. needs-ci PRs that need a full CI run. 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