Skip to content

Commit 59a9252

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 d1586f3 commit 59a9252

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

lib/child_process.js

+13-18
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');
@@ -126,13 +128,10 @@ function fork(modulePath, args = [], options) {
126128
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');
135133
}
134+
options = { ...options };
136135

137136
// Prepare arguments for fork:
138137
execArgv = options.execArgv || process.execArgv;
@@ -286,25 +285,21 @@ function execFile(file, args = [], options, callback) {
286285
}
287286
} else if (typeof args === 'function') {
288287
callback = args;
289-
options = {};
288+
options = null;
290289
args = [];
291-
} else {
292-
throw new ERR_INVALID_ARG_VALUE('args', args);
293290
}
294291

295-
if (options == null) {
296-
options = {};
297-
} else if (typeof options === 'function') {
292+
if (typeof options === 'function') {
298293
callback = options;
299-
options = {};
300-
} else if (typeof options !== 'object') {
301-
throw new ERR_INVALID_ARG_VALUE('options', options);
294+
options = null;
295+
} else if (options != null) {
296+
validateObject(options, 'options');
302297
}
303298

304-
if (callback && typeof callback !== 'function') {
305-
throw new ERR_INVALID_ARG_VALUE('callback', callback);
299+
if (callback != null) {
300+
validateFunction(callback, 'callback');
306301
}
307-
302+
308303
options = {
309304
encoding: 'utf8',
310305
timeout: 0,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -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

+12-12
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

@@ -191,4 +191,4 @@ fork(empty, n, o);
191191
fork(empty, a, n);
192192

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

0 commit comments

Comments
 (0)