Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,9 @@ process, the default is [`process.env`][].

`undefined` values in `env` will be ignored.

Keys in `env` must not be empty and must not contain `=`, otherwise
`ERR_INVALID_ARG_VALUE` is thrown. On Windows, a leading `=` is allowed.

> Using the `signal` option to destroy a long-lived child process as a resource
> cleanup mechanism is deprecated. The `signal` option remains appropriate for
> cancellation, externally propagated aborts, and timeouts. See
Expand Down
7 changes: 7 additions & 0 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,13 @@ function normalizeSpawnArguments(file, args, options) {
if (value !== undefined) {
validateArgumentNullCheck(key, `options.env['${key}']`);
validateArgumentNullCheck(value, `options.env['${key}']`);
// On Windows a leading '=' is allowed, as it is used by per-drive variables.
const isWindows = process.platform === 'win32';
if (key === '' || (isWindows && key === '=') ||
StringPrototypeIndexOf(key, '=', isWindows ? 1 : 0) !== -1) {
throw new ERR_INVALID_ARG_VALUE(
`options.env['${key}']`, key, 'must be a valid environment variable name');
}
ArrayPrototypePush(envPairs, `${key}=${value}`);
}
}
Expand Down
39 changes: 39 additions & 0 deletions test/parallel/test-child-process-reject-invalid-env-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';
const common = require('../common');

const assert = require('assert');
const { exec, execFile, execFileSync, execSync, fork, spawn, spawnSync } = require('child_process');

const invalidKeys = ['', 'AAA=BBB', 'AAA=', 'A=', '=A=B'];
if (!common.isWindows) {
// A leading '=' is only valid on Windows, where it is used by per-drive
// variables such as '=C:'.
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.

}

const expected = {
code: 'ERR_INVALID_ARG_VALUE',
name: 'TypeError',
message: /must be a valid environment variable name/,
};

for (const key of invalidKeys) {
const options = { env: { [key]: 'value' } };
assert.throws(() => spawn(process.execPath, options), expected);
assert.throws(() => spawnSync(process.execPath, options), expected);
assert.throws(() => exec(process.execPath, options, common.mustNotCall()), expected);
assert.throws(() => execSync(process.execPath, options), expected);
assert.throws(() => execFile(process.execPath, options, common.mustNotCall()), expected);
assert.throws(() => execFileSync(process.execPath, options), expected);
assert.throws(() => fork(__filename, options), expected);
}

if (common.isWindows) {
const env = { ...process.env, '=C:': 'C:\\foo', '=D:': 'D:\\bar' };
const child = spawnSync(process.execPath, ['-e', ''], { env });
assert.strictEqual(child.status, 0);
execFileSync(process.execPath, ['-e', ''], { env });
}
Loading