Skip to content

Commit 9ad5106

Browse files
committed
child_process: harden fork arguments validation
Ensure that the first argument `modulePath` of `fork` method must be provided and be of type string. PR-URL: #27039 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 56354d4 commit 9ad5106

File tree

3 files changed

+106
-37
lines changed

3 files changed

+106
-37
lines changed

lib/child_process.js

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ function stdioStringToArray(option) {
6262
}
6363

6464
exports.fork = function fork(modulePath /* , args, options */) {
65+
validateString(modulePath, 'modulePath');
6566

6667
// Get options and args arguments.
6768
var execArgv;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
'use strict';
2+
const common = require('../common');
3+
const fixtures = require('../common/fixtures');
4+
const assert = require('assert');
5+
const { fork } = require('child_process');
6+
7+
// This test check the arguments of `fork` method
8+
// Refs: https://github.com/nodejs/node/issues/20749
9+
const expectedEnv = { foo: 'bar' };
10+
11+
// Ensure that first argument `modulePath` must be provided
12+
// and be of type string
13+
{
14+
const invalidModulePath = [
15+
0,
16+
true,
17+
undefined,
18+
null,
19+
[],
20+
{},
21+
() => {},
22+
Symbol('t')
23+
];
24+
invalidModulePath.forEach((modulePath) => {
25+
common.expectsError(() => fork(modulePath), {
26+
code: 'ERR_INVALID_ARG_TYPE',
27+
type: TypeError,
28+
message: /^The "modulePath" argument must be of type string/
29+
});
30+
});
31+
32+
const cp = fork(fixtures.path('child-process-echo-options.js'));
33+
cp.on(
34+
'exit',
35+
common.mustCall((code) => {
36+
assert.strictEqual(code, 0);
37+
})
38+
);
39+
}
40+
41+
// Ensure that the second argument of `fork`
42+
// and `fork` should parse options
43+
// correctly if args is undefined or null
44+
{
45+
const invalidSecondArgs = [
46+
0,
47+
true,
48+
() => {},
49+
Symbol('t')
50+
];
51+
invalidSecondArgs.forEach((arg) => {
52+
common.expectsError(
53+
() => {
54+
fork(fixtures.path('child-process-echo-options.js'), arg);
55+
},
56+
{
57+
code: 'ERR_INVALID_ARG_VALUE',
58+
type: TypeError
59+
}
60+
);
61+
});
62+
63+
const argsLists = [undefined, null, []];
64+
65+
argsLists.forEach((args) => {
66+
const cp = fork(fixtures.path('child-process-echo-options.js'), args, {
67+
env: Object.assign({}, process.env, expectedEnv)
68+
});
69+
70+
cp.on(
71+
'message',
72+
common.mustCall(({ env }) => {
73+
assert.strictEqual(env.foo, expectedEnv.foo);
74+
})
75+
);
76+
77+
cp.on(
78+
'exit',
79+
common.mustCall((code) => {
80+
assert.strictEqual(code, 0);
81+
})
82+
);
83+
});
84+
}
85+
86+
// Ensure that the third argument should be type of object if provided
87+
{
88+
const invalidThirdArgs = [
89+
0,
90+
true,
91+
() => {},
92+
Symbol('t')
93+
];
94+
invalidThirdArgs.forEach((arg) => {
95+
common.expectsError(
96+
() => {
97+
fork(fixtures.path('child-process-echo-options.js'), [], arg);
98+
},
99+
{
100+
code: 'ERR_INVALID_ARG_VALUE',
101+
type: TypeError
102+
}
103+
);
104+
});
105+
}

test/parallel/test-child-process-fork-options.js

-37
This file was deleted.

0 commit comments

Comments
 (0)