From c6bca3fd3467acfbb5a4c3e2cf192a5a3ad2f201 Mon Sep 17 00:00:00 2001 From: Daniel Venable <37315423+DanielVenable@users.noreply.github.com> Date: Fri, 21 Mar 2025 09:15:18 -0700 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/57199 Fixes: https://github.com/nodejs/node/issues/57143 Reviewed-By: Yagiz Nizipli Reviewed-By: Antoine du Hamel Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- doc/api/deprecations.md | 5 ++++- lib/child_process.js | 9 +++++++++ test/parallel/test-child-process-execfile.js | 6 ++++++ test/parallel/test-child-process-spawn-shell.js | 6 ++++++ test/parallel/test-child-process-spawnsync-shell.js | 6 ++++++ 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index ebc04cc0795..f97dbb14b64 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -3856,13 +3856,16 @@ deprecated, as their values are guaranteed to be identical to that of `process.f -Type: Documentation-only +Type: Runtime When an `args` array is passed to [`child_process.execFile`][] or [`child_process.spawn`][] with the option `{ shell: true }`, the values are not escaped, only space-separated, which can lead to shell injection. diff --git a/lib/child_process.js b/lib/child_process.js index 3fb21f755be..5c853716a26 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -535,6 +535,7 @@ function copyProcessEnvToEnv(env, name, optionEnv) { } } +let emittedDEP0190Already = false; function normalizeSpawnArguments(file, args, options) { validateString(file, 'file'); validateArgumentNullCheck(file, 'file'); @@ -611,6 +612,14 @@ function normalizeSpawnArguments(file, args, options) { if (options.shell) { validateArgumentNullCheck(options.shell, 'options.shell'); + if (args.length > 0 && !emittedDEP0190Already) { + process.emitWarning( + 'Passing args to a child process with shell option true can lead to security ' + + 'vulnerabilities, as the arguments are not escaped, only concatenated.', + 'DeprecationWarning', + 'DEP0190'); + emittedDEP0190Already = true; + } const command = ArrayPrototypeJoin([file, ...args], ' '); // Set the shell, switches, and commands. if (process.platform === 'win32') { diff --git a/test/parallel/test-child-process-execfile.js b/test/parallel/test-child-process-execfile.js index c4dba6b3f94..80d94cc5626 100644 --- a/test/parallel/test-child-process-execfile.js +++ b/test/parallel/test-child-process-execfile.js @@ -12,6 +12,12 @@ const fixture = fixtures.path('exit.js'); const echoFixture = fixtures.path('echo.js'); const execOpts = { encoding: 'utf8', shell: true, env: { ...process.env, NODE: process.execPath, FIXTURE: fixture } }; +common.expectWarning( + 'DeprecationWarning', + 'Passing args to a child process with shell option true can lead to security ' + + 'vulnerabilities, as the arguments are not escaped, only concatenated.', + 'DEP0190'); + { execFile( process.execPath, diff --git a/test/parallel/test-child-process-spawn-shell.js b/test/parallel/test-child-process-spawn-shell.js index a63e92f29d0..0fd530988c7 100644 --- a/test/parallel/test-child-process-spawn-shell.js +++ b/test/parallel/test-child-process-spawn-shell.js @@ -18,6 +18,12 @@ doesNotExist.on('exit', common.mustCall((code, signal) => { })); // Verify that passing arguments works +common.expectWarning( + 'DeprecationWarning', + 'Passing args to a child process with shell option true can lead to security ' + + 'vulnerabilities, as the arguments are not escaped, only concatenated.', + 'DEP0190'); + const echo = cp.spawn('echo', ['foo'], { encoding: 'utf8', shell: true diff --git a/test/parallel/test-child-process-spawnsync-shell.js b/test/parallel/test-child-process-spawnsync-shell.js index 9db8a53366f..8c6f180accc 100644 --- a/test/parallel/test-child-process-spawnsync-shell.js +++ b/test/parallel/test-child-process-spawnsync-shell.js @@ -19,6 +19,12 @@ else assert.strictEqual(doesNotExist.status, 127); // Exit code of /bin/sh // Verify that passing arguments works +common.expectWarning( + 'DeprecationWarning', + 'Passing args to a child process with shell option true can lead to security ' + + 'vulnerabilities, as the arguments are not escaped, only concatenated.', + 'DEP0190'); + internalCp.spawnSync = common.mustCall(function(opts) { assert.strictEqual(opts.args[opts.args.length - 1].replace(/"/g, ''), 'echo foo');