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: validate options.shell and correctly enforce shell invocation in exec/execSync #54623

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ function normalizeExecArgs(command, options, callback) {

// Make a shallow copy so we don't clobber the user's options object.
options = { __proto__: null, ...options };
options.shell = typeof options.shell === 'string' ? options.shell : true;

// Validate the shell, if present, and ensure a truthy value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this comment is needed, it might be implied, but if not, it's validating that it's a string, not a truthy value.

Suggested change
// Validate the shell, if present, and ensure a truthy value.
Suggested change
// Validate the shell, if present, and ensure a truthy value.
// Verify that the shell, if present, is a string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation ensures that it's a string or nullish, and then the or-assignment ensures that it's a truthy value. The comment is designed to describe both.

if (options.shell != null) {
validateString(options.shell, 'options.shell');
}
options.shell ||= true;

return {
file: command,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const testCopy = (shellName, shellPath) => {
const system32 = `${process.env.SystemRoot}\\System32`;

// Test CMD
test(true);
test();
test('cmd');
testCopy('cmd.exe', `${system32}\\cmd.exe`);
test('cmd.exe');
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-child-process-exec-enforce-shell.js
Renegade334 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { exec, execSync } = require('child_process');

const invalidArgTypeError = {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError'
};

exec('echo should-be-passed-as-argument', { shell: '' }, common.mustSucceed((stdout, stderr) => {
assert.match(stdout, /should-be-passed-as-argument/);
assert.ok(!stderr);
}));

{
const ret = execSync('echo should-be-passed-as-argument', { encoding: 'utf-8', shell: '' });
assert.match(ret, /should-be-passed-as-argument/);
}

for (const fn of [exec, execSync]) {
assert.throws(() => fn('should-throw-on-boolean-shell-option', { shell: false }), invalidArgTypeError);
}
Loading