Skip to content

Commit 1b5b019

Browse files
authoredMar 21, 2025··
child_process: deprecate passing args to spawn and execFile
Accepting `args` gives the false impression that the args are escaped while really they are just concatenated. This makes it easy to introduce bugs and security vulnerabilities. PR-URL: #57199 Fixes: #57143 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 8b2098f commit 1b5b019

5 files changed

+31
-1
lines changed
 

‎doc/api/deprecations.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -3859,13 +3859,16 @@ deprecated, as their values are guaranteed to be identical to that of `process.f
38593859

38603860
<!-- YAML
38613861
changes:
3862+
- version: REPLACEME
3863+
pr-url: https://github.com/nodejs/node/pull/57199
3864+
description: Runtime deprecation.
38623865
- version:
38633866
- REPLACEME
38643867
pr-url: https://github.com/nodejs/node/pull/57389
38653868
description: Documentation-only deprecation.
38663869
-->
38673870

3868-
Type: Documentation-only
3871+
Type: Runtime
38693872

38703873
When an `args` array is passed to [`child_process.execFile`][] or [`child_process.spawn`][] with the option
38713874
`{ shell: true }`, the values are not escaped, only space-separated, which can lead to shell injection.

‎lib/child_process.js

+9
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ function copyProcessEnvToEnv(env, name, optionEnv) {
535535
}
536536
}
537537

538+
let emittedDEP0190Already = false;
538539
function normalizeSpawnArguments(file, args, options) {
539540
validateString(file, 'file');
540541
validateArgumentNullCheck(file, 'file');
@@ -611,6 +612,14 @@ function normalizeSpawnArguments(file, args, options) {
611612

612613
if (options.shell) {
613614
validateArgumentNullCheck(options.shell, 'options.shell');
615+
if (args.length > 0 && !emittedDEP0190Already) {
616+
process.emitWarning(
617+
'Passing args to a child process with shell option true can lead to security ' +
618+
'vulnerabilities, as the arguments are not escaped, only concatenated.',
619+
'DeprecationWarning',
620+
'DEP0190');
621+
emittedDEP0190Already = true;
622+
}
614623
const command = ArrayPrototypeJoin([file, ...args], ' ');
615624
// Set the shell, switches, and commands.
616625
if (process.platform === 'win32') {

‎test/parallel/test-child-process-execfile.js

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ const fixture = fixtures.path('exit.js');
1212
const echoFixture = fixtures.path('echo.js');
1313
const execOpts = { encoding: 'utf8', shell: true, env: { ...process.env, NODE: process.execPath, FIXTURE: fixture } };
1414

15+
common.expectWarning(
16+
'DeprecationWarning',
17+
'Passing args to a child process with shell option true can lead to security ' +
18+
'vulnerabilities, as the arguments are not escaped, only concatenated.',
19+
'DEP0190');
20+
1521
{
1622
execFile(
1723
process.execPath,

‎test/parallel/test-child-process-spawn-shell.js

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ doesNotExist.on('exit', common.mustCall((code, signal) => {
1818
}));
1919

2020
// Verify that passing arguments works
21+
common.expectWarning(
22+
'DeprecationWarning',
23+
'Passing args to a child process with shell option true can lead to security ' +
24+
'vulnerabilities, as the arguments are not escaped, only concatenated.',
25+
'DEP0190');
26+
2127
const echo = cp.spawn('echo', ['foo'], {
2228
encoding: 'utf8',
2329
shell: true

‎test/parallel/test-child-process-spawnsync-shell.js

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ else
1919
assert.strictEqual(doesNotExist.status, 127); // Exit code of /bin/sh
2020

2121
// Verify that passing arguments works
22+
common.expectWarning(
23+
'DeprecationWarning',
24+
'Passing args to a child process with shell option true can lead to security ' +
25+
'vulnerabilities, as the arguments are not escaped, only concatenated.',
26+
'DEP0190');
27+
2228
internalCp.spawnSync = common.mustCall(function(opts) {
2329
assert.strictEqual(opts.args[opts.args.length - 1].replace(/"/g, ''),
2430
'echo foo');

0 commit comments

Comments
 (0)
Please sign in to comment.