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: https://github.com/nodejs/node/pull/27039 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
56354d480d
commit
9ad5106934
@ -62,6 +62,7 @@ function stdioStringToArray(option) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.fork = function fork(modulePath /* , args, options */) {
|
exports.fork = function fork(modulePath /* , args, options */) {
|
||||||
|
validateString(modulePath, 'modulePath');
|
||||||
|
|
||||||
// Get options and args arguments.
|
// Get options and args arguments.
|
||||||
var execArgv;
|
var execArgv;
|
||||||
|
105
test/parallel/test-child-process-fork-args.js
Normal file
105
test/parallel/test-child-process-fork-args.js
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
'use strict';
|
||||||
|
const common = require('../common');
|
||||||
|
const fixtures = require('../common/fixtures');
|
||||||
|
const assert = require('assert');
|
||||||
|
const { fork } = require('child_process');
|
||||||
|
|
||||||
|
// This test check the arguments of `fork` method
|
||||||
|
// Refs: https://github.com/nodejs/node/issues/20749
|
||||||
|
const expectedEnv = { foo: 'bar' };
|
||||||
|
|
||||||
|
// Ensure that first argument `modulePath` must be provided
|
||||||
|
// and be of type string
|
||||||
|
{
|
||||||
|
const invalidModulePath = [
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
undefined,
|
||||||
|
null,
|
||||||
|
[],
|
||||||
|
{},
|
||||||
|
() => {},
|
||||||
|
Symbol('t')
|
||||||
|
];
|
||||||
|
invalidModulePath.forEach((modulePath) => {
|
||||||
|
common.expectsError(() => fork(modulePath), {
|
||||||
|
code: 'ERR_INVALID_ARG_TYPE',
|
||||||
|
type: TypeError,
|
||||||
|
message: /^The "modulePath" argument must be of type string/
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
const cp = fork(fixtures.path('child-process-echo-options.js'));
|
||||||
|
cp.on(
|
||||||
|
'exit',
|
||||||
|
common.mustCall((code) => {
|
||||||
|
assert.strictEqual(code, 0);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that the second argument of `fork`
|
||||||
|
// and `fork` should parse options
|
||||||
|
// correctly if args is undefined or null
|
||||||
|
{
|
||||||
|
const invalidSecondArgs = [
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
() => {},
|
||||||
|
Symbol('t')
|
||||||
|
];
|
||||||
|
invalidSecondArgs.forEach((arg) => {
|
||||||
|
common.expectsError(
|
||||||
|
() => {
|
||||||
|
fork(fixtures.path('child-process-echo-options.js'), arg);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'ERR_INVALID_ARG_VALUE',
|
||||||
|
type: TypeError
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
const argsLists = [undefined, null, []];
|
||||||
|
|
||||||
|
argsLists.forEach((args) => {
|
||||||
|
const cp = fork(fixtures.path('child-process-echo-options.js'), args, {
|
||||||
|
env: Object.assign({}, process.env, expectedEnv)
|
||||||
|
});
|
||||||
|
|
||||||
|
cp.on(
|
||||||
|
'message',
|
||||||
|
common.mustCall(({ env }) => {
|
||||||
|
assert.strictEqual(env.foo, expectedEnv.foo);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
cp.on(
|
||||||
|
'exit',
|
||||||
|
common.mustCall((code) => {
|
||||||
|
assert.strictEqual(code, 0);
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that the third argument should be type of object if provided
|
||||||
|
{
|
||||||
|
const invalidThirdArgs = [
|
||||||
|
0,
|
||||||
|
true,
|
||||||
|
() => {},
|
||||||
|
Symbol('t')
|
||||||
|
];
|
||||||
|
invalidThirdArgs.forEach((arg) => {
|
||||||
|
common.expectsError(
|
||||||
|
() => {
|
||||||
|
fork(fixtures.path('child-process-echo-options.js'), [], arg);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'ERR_INVALID_ARG_VALUE',
|
||||||
|
type: TypeError
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
@ -1,37 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
const common = require('../common');
|
|
||||||
const fixtures = require('../common/fixtures');
|
|
||||||
|
|
||||||
// This test ensures that fork should parse options
|
|
||||||
// correctly if args is undefined or null
|
|
||||||
|
|
||||||
const assert = require('assert');
|
|
||||||
const { fork } = require('child_process');
|
|
||||||
|
|
||||||
const expectedEnv = { foo: 'bar' };
|
|
||||||
|
|
||||||
{
|
|
||||||
const cp = fork(fixtures.path('child-process-echo-options.js'), undefined,
|
|
||||||
{ env: Object.assign({}, process.env, expectedEnv) });
|
|
||||||
|
|
||||||
cp.on('message', common.mustCall(({ env }) => {
|
|
||||||
assert.strictEqual(env.foo, expectedEnv.foo);
|
|
||||||
}));
|
|
||||||
|
|
||||||
cp.on('exit', common.mustCall((code) => {
|
|
||||||
assert.strictEqual(code, 0);
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
const cp = fork(fixtures.path('child-process-echo-options.js'), null,
|
|
||||||
{ env: Object.assign({}, process.env, expectedEnv) });
|
|
||||||
|
|
||||||
cp.on('message', common.mustCall(({ env }) => {
|
|
||||||
assert.strictEqual(env.foo, expectedEnv.foo);
|
|
||||||
}));
|
|
||||||
|
|
||||||
cp.on('exit', common.mustCall((code) => {
|
|
||||||
assert.strictEqual(code, 0);
|
|
||||||
}));
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user