Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b885590

Browse files
authored
Use execFileSync to launch child process (shelljs#790)
This uses `child_process.execFileSync` instead of `execSync` to launch the child process. This further reduces the attack surface, removing a possible point for command injection in the ShellJS implementation. This does not affect backwards compatibility for the `shell.exec` API (the behavior is determined by the call to `child_process.exec` within `src/exec-child.js`). Issue shelljs#782
1 parent e9461dc commit b885590

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

src/exec-child.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ if (require.main !== module) {
55
var childProcess = require('child_process');
66
var fs = require('fs');
77

8-
// Note: this will break if `paramFilePath` contains special characters ( '\n',
9-
// '\t', etc.). Such characters are possible if $TMP gets modified. We already
10-
// rely on tempdir() to work for other things, so this is an acceptable risk.
118
var paramFilePath = process.argv[2];
129

1310
var serializedParams = fs.readFileSync(paramFilePath, 'utf8');

src/exec.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ function execSync(cmd, opts, pipe) {
5555

5656
fs.writeFileSync(paramsFile, JSON.stringify(paramsToSerialize), 'utf8');
5757

58-
var execCommand = [
59-
JSON.stringify(common.config.execPath),
60-
JSON.stringify(path.join(__dirname, 'exec-child.js')),
61-
JSON.stringify(paramsFile),
62-
].join(' ');
58+
var execArgs = [
59+
path.join(__dirname, 'exec-child.js'),
60+
paramsFile,
61+
];
6362

6463
/* istanbul ignore else */
6564
if (opts.silent) {
@@ -70,7 +69,11 @@ function execSync(cmd, opts, pipe) {
7069

7170
// Welcome to the future
7271
try {
73-
child.execSync(execCommand, opts);
72+
// Bad things if we pass in a `shell` option to child_process.execFileSync,
73+
// so we need to explicitly remove it here.
74+
delete opts.shell;
75+
76+
child.execFileSync(common.config.execPath, execArgs, opts);
7477
} catch (e) {
7578
// Clean up immediately if we have an exception
7679
try { common.unlinkSync(codeFile); } catch (e2) {}

0 commit comments

Comments
 (0)