lib: add validation for options in compileFunction

PR-URL: https://github.com/nodejs/node/pull/56023
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
pull/56191/head
Taejin Kim 2024-12-06 15:53:06 +09:00 committed by GitHub
parent 7904bc0976
commit 56c8360f87
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 1 deletions

View File

@ -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');
}

View File

@ -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',