Skip to content
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

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -3840,6 +3840,20 @@ Type: Documentation-only
`process.features.tls_alpn`, `process.features.tls_ocsp`, and `process.features.tls_sni` are
deprecated, as their values are guaranteed to be identical to that of `process.features.tls`.

### DEP0190: Passing `args` to `child_process` `execFile`/`spawn` with `shell` option `true`

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

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.

[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
[RFC 8247 Section 2.4]: https://www.rfc-editor.org/rfc/rfc8247#section-2.4
Expand Down
7 changes: 7 additions & 0 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,13 @@ function normalizeSpawnArguments(file, args, options) {

if (options.shell) {
validateArgumentNullCheck(options.shell, 'options.shell');
if (args.length > 0) {
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');
}
const command = ArrayPrototypeJoin([file, ...args], ' ');
// Set the shell, switches, and commands.
if (process.platform === 'win32') {
Expand Down
11 changes: 7 additions & 4 deletions test/parallel/test-child-process-execfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ const execOpts = { encoding: 'utf8', shell: true, env: { ...process.env, NODE: p
{
// Verify the shell option works properly
execFile(
`"${common.isWindows ? execOpts.env.NODE : '$NODE'}"`,
[`"${common.isWindows ? execOpts.env.FIXTURE : '$FIXTURE'}"`, 0],
common.isWindows ? `"${execOpts.env.NODE}" "${execOpts.env.FIXTURE} 0` : `"$NODE" "$FIXTURE" 0`,
execOpts,
common.mustSucceed(),
);
Expand Down Expand Up @@ -117,10 +116,14 @@ const execOpts = { encoding: 'utf8', shell: true, env: { ...process.env, NODE: p
...(common.isWindows ? [] : [{ encoding: 'utf8' }]),
{ shell: true, encoding: 'utf8' },
].forEach((options) => {
const execFileSyncStdout = execFileSync(file, args, options);
const command = options.shell ?
[[file, ...args].join(' ')] :
[file, args];

const execFileSyncStdout = execFileSync(...command, options);
assert.strictEqual(execFileSyncStdout, `foo bar${os.EOL}`);

execFile(file, args, options, common.mustCall((_, stdout) => {
execFile(...command, options, common.mustCall((_, stdout) => {
assert.strictEqual(stdout, execFileSyncStdout);
}));
});
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