Skip to content

Commit 5e37647

Browse files
committed
child_process: ignore undef/proto values of env
At present, undefined values of env option will be transferred as a "undefined" string value, and values in prototype will also be included, which are not usual behaviors. Since non-string env values & prototype values are undocumented, this change may be treated as a bugfix or a breaking change. Tested on Mac, Windows not yet. Fixes: nodejs#15087
1 parent abd5d95 commit 5e37647

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

doc/api/child_process.md

+2
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,8 @@ If not given, the default is to inherit the current working directory.
438438
Use `env` to specify environment variables that will be visible to the new
439439
process, the default is [`process.env`][].
440440

441+
`undefined` values in `env` will be ignored.
442+
441443
Example of running `ls -lh /usr`, capturing `stdout`, `stderr`, and the
442444
exit code:
443445

lib/child_process.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -504,8 +504,11 @@ function normalizeSpawnArguments(file, args, options) {
504504
var env = options.env || process.env;
505505
var envPairs = [];
506506

507-
for (var key in env) {
508-
envPairs.push(`${key}=${env[key]}`);
507+
for (const key of Object.keys(env)) {
508+
const value = env[key];
509+
if (value !== undefined) {
510+
envPairs.push(`${key}=${value}`);
511+
}
509512
}
510513

511514
_convertCustomFds(options);

test/parallel/test-child-process-env.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ const assert = require('assert');
2626
const spawn = require('child_process').spawn;
2727

2828
const env = {
29-
'HELLO': 'WORLD'
29+
'HELLO': 'WORLD',
30+
'UNDEFINED': undefined,
31+
'NULL': null,
32+
'EMPTY': ''
3033
};
3134
Object.setPrototypeOf(env, {
3235
'FOO': 'BAR'
@@ -53,5 +56,8 @@ child.stdout.on('data', function(chunk) {
5356

5457
process.on('exit', function() {
5558
assert.ok(response.includes('HELLO=WORLD'));
56-
assert.ok(response.includes('FOO=BAR'));
59+
assert.ok(!response.includes('FOO='));
60+
assert.ok(!response.includes('UNDEFINED=undefined'));
61+
assert.ok(response.includes('NULL=null'));
62+
assert.ok(response.includes('EMPTY=\n'));
5763
});

0 commit comments

Comments
 (0)