From bda151e46dc2e720a4dc482b713fa7e55376794d Mon Sep 17 00:00:00 2001 From: Taejin Kim Date: Wed, 27 Nov 2024 23:25:47 +0900 Subject: [PATCH] lib: add validation for options in compileFunction --- lib/vm.js | 1 + test/parallel/test-vm-basic.js | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/vm.js b/lib/vm.js index 3eea66b3f07437..ae710806201893 100644 --- a/lib/vm.js +++ b/lib/vm.js @@ -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'); } diff --git a/test/parallel/test-vm-basic.js b/test/parallel/test-vm-basic.js index f2424128b66e9f..bebe978e6fe680 100644 --- a/test/parallel/test-vm-basic.js +++ b/test/parallel/test-vm-basic.js @@ -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',