Skip to content

Commit 85739b6

Browse files
Gerhuttargos
authored andcommitted
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. This commit prevents those to be transferred to the environment of the child process. PR-URL: #15089 Fixes: #15087 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 858b48b commit 85739b6

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

doc/api/child_process.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,8 @@ If not given, the default is to inherit the current working directory.
437437
Use `env` to specify environment variables that will be visible to the new
438438
process, the default is [`process.env`][].
439439

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

lib/child_process.js

Lines changed: 5 additions & 2 deletions
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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
'use strict';
2323
const common = require('../common');
2424
const assert = require('assert');
25+
const os = require('os');
2526

2627
const spawn = require('child_process').spawn;
2728

2829
const env = {
29-
'HELLO': 'WORLD'
30+
'HELLO': 'WORLD',
31+
'UNDEFINED': undefined,
32+
'NULL': null,
33+
'EMPTY': ''
3034
};
3135
Object.setPrototypeOf(env, {
3236
'FOO': 'BAR'
@@ -53,5 +57,8 @@ child.stdout.on('data', function(chunk) {
5357

5458
process.on('exit', function() {
5559
assert.ok(response.includes('HELLO=WORLD'));
56-
assert.ok(response.includes('FOO=BAR'));
60+
assert.ok(!response.includes('FOO='));
61+
assert.ok(!response.includes('UNDEFINED=undefined'));
62+
assert.ok(response.includes('NULL=null'));
63+
assert.ok(response.includes(`EMPTY=${os.EOL}`));
5764
});

0 commit comments

Comments
 (0)