-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
child_process: validate execFile & fork arguments #4508
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,15 +20,10 @@ exports.fork = function(modulePath /*, args, options*/) { | |
|
||
// Get options and args arguments. | ||
var options, args, execArgv; | ||
if (Array.isArray(arguments[1])) { | ||
args = arguments[1]; | ||
options = util._extend({}, arguments[2]); | ||
} else if (arguments[1] && typeof arguments[1] !== 'object') { | ||
throw new TypeError('Incorrect value of args option'); | ||
} else { | ||
args = []; | ||
options = util._extend({}, arguments[1]); | ||
} | ||
|
||
var opts = normalizeForkArgs(arguments, options); | ||
args = opts.args; | ||
options = opts.options; | ||
|
||
// Prepare arguments for fork: | ||
execArgv = options.execArgv || process.execArgv; | ||
|
@@ -114,6 +109,82 @@ exports.exec = function(command /*, options, callback*/) { | |
opts.callback); | ||
}; | ||
|
||
function normalizeArgsOptions(allArgs, defaultOption) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it is going to be a nightmare to maintain. If we can't do the validation more simply, I think we'd be better off skipping it or loosening its strictness. |
||
var pos = 1, arrayPos = -1, objectPos = -1, args; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we're using |
||
if (pos < allArgs.length && Array.isArray(allArgs[pos])) { | ||
arrayPos = pos++; | ||
args = allArgs[arrayPos]; | ||
} else if (pos < allArgs.length && allArgs[pos] == null) { | ||
arrayPos = pos++; | ||
} | ||
|
||
if (pos < allArgs.length && typeof allArgs[pos] === 'object') { | ||
objectPos = pos++; | ||
defaultOption = util._extend(defaultOption, allArgs[objectPos]); | ||
} else if (pos < allArgs.length && allArgs[pos] == null) { | ||
objectPos = pos++; | ||
} | ||
|
||
return { | ||
pos: pos, | ||
args: args || [], | ||
options: defaultOption, | ||
arrayPos: arrayPos, | ||
objectPos: objectPos | ||
}; | ||
} | ||
|
||
function normalizeForkArgs(allArgs, defaultOption) { | ||
if (!defaultOption) { | ||
defaultOption = {}; | ||
} | ||
|
||
var opts = normalizeArgsOptions(allArgs, defaultOption); | ||
|
||
if (opts.pos === 2 && | ||
allArgs.length > 2 && | ||
opts.arrayPos > -1) { | ||
throw new TypeError('Incorrect value of options option'); | ||
} | ||
|
||
if (opts.pos === 1 && allArgs.length > 1) { | ||
throw new TypeError('Incorrect value of args option'); | ||
} | ||
|
||
return opts; | ||
} | ||
|
||
function normalizeExecFileArgs(allArgs, defaultOption) { | ||
var opts = normalizeArgsOptions(allArgs, defaultOption); | ||
|
||
if (opts.pos < allArgs.length && typeof allArgs[opts.pos] === 'function') { | ||
opts.callback = allArgs[opts.pos++]; | ||
} | ||
|
||
if (opts.pos === 3 && | ||
allArgs.length > 3 && | ||
typeof opts.callback !== 'function' && | ||
allArgs[3] !== undefined && | ||
allArgs[3] !== null) { | ||
throw new TypeError('Incorrect value of callback option'); | ||
} | ||
|
||
if (opts.pos === 2 && allArgs.length > 2) { | ||
if (opts.arrayPos > -1) { | ||
throw new TypeError('Incorrect value of options option'); | ||
} else if (opts.objectPos > -1 && | ||
allArgs[opts.pos] !== null && | ||
allArgs[opts.pos] !== undefined) { | ||
throw new TypeError('Incorrect value of callback option'); | ||
} | ||
} | ||
|
||
if (opts.pos === 1 && allArgs.length > 1) { | ||
throw new TypeError('Incorrect value of args option'); | ||
} | ||
|
||
return opts; | ||
} | ||
|
||
exports.execFile = function(file /*, args, options, callback*/) { | ||
var args = [], callback; | ||
|
@@ -126,27 +197,10 @@ exports.execFile = function(file /*, args, options, callback*/) { | |
env: null | ||
}; | ||
|
||
// Parse the optional positional parameters. | ||
var pos = 1; | ||
if (pos < arguments.length && Array.isArray(arguments[pos])) { | ||
args = arguments[pos++]; | ||
} else if (pos < arguments.length && arguments[pos] == null) { | ||
pos++; | ||
} | ||
|
||
if (pos < arguments.length && typeof arguments[pos] === 'object') { | ||
options = util._extend(options, arguments[pos++]); | ||
} else if (pos < arguments.length && arguments[pos] == null) { | ||
pos++; | ||
} | ||
|
||
if (pos < arguments.length && typeof arguments[pos] === 'function') { | ||
callback = arguments[pos++]; | ||
} | ||
|
||
if (pos === 1 && arguments.length > 1) { | ||
throw new TypeError('Incorrect value of args option'); | ||
} | ||
var opts = normalizeExecFileArgs(arguments, options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't passing |
||
args = opts.args; | ||
options = opts.options; | ||
callback = opts.callback; | ||
|
||
var child = spawn(file, args, { | ||
cwd: options.cwd, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need a
normalizeForkArgs()
, as this code is specific tofork()
. If the logic needs to be tightened up, it can be done here.fork()
is one of the simpler cases, as we only need to validate an optional array and an optional object.