Skip to content

child_process: disallow args in execFile/spawn when shell option is true #57199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/api/child_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ exec('my.bat', (err, stdout, stderr) => {
});

// Script with spaces in the filename:
const bat = spawn('"my script.cmd"', ['a', 'b'], { shell: true });
const bat = spawn('"my script.cmd" a b', { shell: true });
// or:
exec('"my script.cmd" a b', (err, stdout, stderr) => {
// ...
Expand Down
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3859,13 +3859,16 @@ deprecated, as their values are guaranteed to be identical to that of `process.f

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/57199
description: Runtime deprecation.
- version:
- REPLACEME
pr-url: https://github.com/nodejs/node/pull/57389
description: Documentation-only deprecation.
-->

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.
Expand Down
9 changes: 9 additions & 0 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ function copyProcessEnvToEnv(env, name, optionEnv) {
}
}

let emittedDEP0190Already = false;
function normalizeSpawnArguments(file, args, options) {
validateString(file, 'file');
validateArgumentNullCheck(file, 'file');
Expand Down Expand Up @@ -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') {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-child-process-execfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-child-process-spawn-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-child-process-spawnsync-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading