diff --git a/doc/api/child_process.md b/doc/api/child_process.md index e5007ff1497..100579320e8 100644 --- a/doc/api/child_process.md +++ b/doc/api/child_process.md @@ -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 diff --git a/lib/child_process.js b/lib/child_process.js index 5ceed97b57b..0da78973bc3 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -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}`); } } diff --git a/test/parallel/test-child-process-reject-invalid-env-keys.js b/test/parallel/test-child-process-reject-invalid-env-keys.js new file mode 100644 index 00000000000..a392d6dcb3e --- /dev/null +++ b/test/parallel/test-child-process-reject-invalid-env-keys.js @@ -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('='); +} + +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 }); +}