Skip to content
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

lib: add validation for options in compileFunction #56023

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ function runInThisContext(code, options) {

function compileFunction(code, params, options = kEmptyObject) {
validateString(code, 'code');
validateObject(options, 'options');
if (params !== undefined) {
validateStringArray(params, 'params');
}
Expand Down
25 changes: 24 additions & 1 deletion test/parallel/test-vm-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,30 @@ const vm = require('vm');
'Received null'
});

// vm.compileFunction('', undefined, null);
// Test for invalid options type
assert.throws(() => {
vm.compileFunction('', [], null);
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type object. Received null'
});

assert.throws(() => {
vm.compileFunction('', [], 'string');
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type object. Received type string (\'string\')'
});

assert.throws(() => {
vm.compileFunction('', [], 123);
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options" argument must be of type object. Received type number (123)'
});

const optionTypes = {
'filename': 'string',
Expand Down
Loading