Skip to content

Commit 55ad9b1

Browse files
committed
child_process: improve argument validation
For execFile() and fork(), use INVALID_ARG_TYPE as appropriate instead of INVALID_ARG_VALUE. Use validator functions where sensible.
1 parent 5cc4b69 commit 55ad9b1

File tree

3 files changed

+34
-42
lines changed

3 files changed

+34
-42
lines changed

lib/child_process.js

+19-27
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ const { getValidatedPath } = require('internal/fs/utils');
7575
const {
7676
isInt32,
7777
validateAbortSignal,
78+
validateArray,
7879
validateBoolean,
80+
validateFunction,
7981
validateObject,
8082
validateString,
8183
} = require('internal/validators');
@@ -119,20 +121,17 @@ function fork(modulePath, args = [], options) {
119121

120122
if (args == null) {
121123
args = [];
122-
} else if (typeof args !== 'object') {
123-
throw new ERR_INVALID_ARG_VALUE('args', args);
124-
} else if (!ArrayIsArray(args)) {
124+
} else if (typeof args === 'object' && !ArrayIsArray(args)) {
125125
options = args;
126126
args = [];
127+
} else {
128+
validateArray(args, 'args');
127129
}
128130

129-
if (options == null) {
130-
options = {};
131-
} else if (typeof options !== 'object') {
132-
throw new ERR_INVALID_ARG_VALUE('options', options);
133-
} else {
134-
options = { ...options };
131+
if (options != null) {
132+
validateObject(options, 'options', { allowArray: true });
135133
}
134+
options = { ...options };
136135

137136
// Prepare arguments for fork:
138137
execArgv = options.execArgv || process.execArgv;
@@ -276,33 +275,26 @@ ObjectDefineProperty(exec, promisify.custom, {
276275
* @returns {ChildProcess}
277276
*/
278277
function execFile(file, args = [], options, callback) {
279-
if (args == null) {
278+
if (args != null && typeof args === 'object' && !ArrayIsArray(args)) {
279+
callback = options;
280+
options = args;
280281
args = [];
281-
} else if (typeof args === 'object') {
282-
if (!ArrayIsArray(args)) {
283-
callback = options;
284-
options = args;
285-
args = [];
286-
}
287282
} else if (typeof args === 'function') {
288283
callback = args;
289-
options = {};
284+
options = null;
290285
args = [];
291-
} else {
292-
throw new ERR_INVALID_ARG_VALUE('args', args);
293286
}
294287

295-
if (options == null) {
296-
options = {};
297-
} else if (typeof options === 'function') {
288+
if (typeof options === 'function') {
298289
callback = options;
299-
options = {};
300-
} else if (typeof options !== 'object') {
301-
throw new ERR_INVALID_ARG_VALUE('options', options);
290+
options = null;
291+
} else if (options != null) {
292+
// TODO: Should this have allowArray: true set?
293+
validateObject(options, 'options');
302294
}
303295

304-
if (callback && typeof callback !== 'function') {
305-
throw new ERR_INVALID_ARG_VALUE('callback', callback);
296+
if (callback != null) {
297+
validateFunction(callback, 'callback');
306298
}
307299

308300
options = {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const expectedEnv = { foo: 'bar' };
5454
fork(fixtures.path('child-process-echo-options.js'), arg);
5555
},
5656
{
57-
code: 'ERR_INVALID_ARG_VALUE',
57+
code: 'ERR_INVALID_ARG_TYPE',
5858
name: 'TypeError'
5959
}
6060
);
@@ -97,7 +97,7 @@ const expectedEnv = { foo: 'bar' };
9797
fork(fixtures.path('child-process-echo-options.js'), [], arg);
9898
},
9999
{
100-
code: 'ERR_INVALID_ARG_VALUE',
100+
code: 'ERR_INVALID_ARG_TYPE',
101101
name: 'TypeError'
102102
}
103103
);

test/parallel/test-child-process-spawn-typeerror.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -158,17 +158,17 @@ execFile(cmd, c, n);
158158
// String is invalid in arg position (this may seem strange, but is
159159
// consistent across node API, cf. `net.createServer('not options', 'not
160160
// callback')`.
161-
assert.throws(function() { execFile(cmd, s, o, c); }, invalidArgValueError);
162-
assert.throws(function() { execFile(cmd, a, s, c); }, invalidArgValueError);
163-
assert.throws(function() { execFile(cmd, a, o, s); }, invalidArgValueError);
164-
assert.throws(function() { execFile(cmd, a, s); }, invalidArgValueError);
165-
assert.throws(function() { execFile(cmd, o, s); }, invalidArgValueError);
166-
assert.throws(function() { execFile(cmd, u, u, s); }, invalidArgValueError);
167-
assert.throws(function() { execFile(cmd, n, n, s); }, invalidArgValueError);
168-
assert.throws(function() { execFile(cmd, a, u, s); }, invalidArgValueError);
169-
assert.throws(function() { execFile(cmd, a, n, s); }, invalidArgValueError);
170-
assert.throws(function() { execFile(cmd, u, o, s); }, invalidArgValueError);
171-
assert.throws(function() { execFile(cmd, n, o, s); }, invalidArgValueError);
161+
assert.throws(function() { execFile(cmd, s, o, c); }, invalidArgTypeError);
162+
assert.throws(function() { execFile(cmd, a, s, c); }, invalidArgTypeError);
163+
assert.throws(function() { execFile(cmd, a, o, s); }, invalidArgTypeError);
164+
assert.throws(function() { execFile(cmd, a, s); }, invalidArgTypeError);
165+
assert.throws(function() { execFile(cmd, o, s); }, invalidArgTypeError);
166+
assert.throws(function() { execFile(cmd, u, u, s); }, invalidArgTypeError);
167+
assert.throws(function() { execFile(cmd, n, n, s); }, invalidArgTypeError);
168+
assert.throws(function() { execFile(cmd, a, u, s); }, invalidArgTypeError);
169+
assert.throws(function() { execFile(cmd, a, n, s); }, invalidArgTypeError);
170+
assert.throws(function() { execFile(cmd, u, o, s); }, invalidArgTypeError);
171+
assert.throws(function() { execFile(cmd, n, o, s); }, invalidArgTypeError);
172172

173173
execFile(cmd, c, s); // Should not throw.
174174

@@ -190,5 +190,5 @@ fork(empty, n, n);
190190
fork(empty, n, o);
191191
fork(empty, a, n);
192192

193-
assert.throws(function() { fork(empty, s); }, invalidArgValueError);
194-
assert.throws(function() { fork(empty, a, s); }, invalidArgValueError);
193+
assert.throws(function() { fork(empty, s); }, invalidArgTypeError);
194+
assert.throws(function() { fork(empty, a, s); }, invalidArgTypeError);

0 commit comments

Comments
 (0)