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.

@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.

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.

Thanks. The validation already rejects a lone = on every platform: on POSIX indexOf(key, '=', 0) matches it, and on Windows we have the explicit key === '=' check because the search starts at index 1. == is also rejected on Windows, since indexOf(key, '=', 1) finds the second =.

You're right that the test only covers = on Windows. I'll add = and == to the shared invalidKeys list so this doesn't regress if that start index is refactored.

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.58%. Comparing base (030745c) to head (447c810).
⚠️ Report is 320 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67659      +/-   ##
==========================================
- Coverage   90.72%   90.58%   -0.14%     
==========================================
  Files         801      802       +1     
  Lines      280960   286942    +5982     
  Branches    54260    55415    +1155     
==========================================
+ Hits       254892   259926    +5034     
- Misses      16248    17110     +862     
- Partials     9820     9906      +86     
Files with missing lines Coverage Δ
lib/child_process.js 90.05% <100.00%> (-1.50%) ⬇️

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

This comment was marked as resolved.

@jasnell jasnell added the semver-major PRs that contain breaking changes and should be released in the next major version. label Nov 22, 2026
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>
@ShogunPanda
ShogunPanda force-pushed the child-process-reject-invalid-env-keys branch from 5285e5b to 447c810 Compare December 3, 2026 11:41
@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 Dec 3, 2026

@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

@mcollina mcollina added request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. and removed request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. labels Dec 3, 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 Dec 3, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@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

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.

5 participants