From bc9d6f34d166958aa526fa0a51239a2322902c99 Mon Sep 17 00:00:00 2001 From: Taejin Kim <60560836+kimtaejin3@users.noreply.github.com> Date: Fri, 6 Dec 2024 15:53:06 +0900 Subject: [PATCH] lib: add validation for options in compileFunction MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/56023 Reviewed-By: Chengzhong Wu Reviewed-By: Juan José Arboleda --- 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..93c3fbaea631ab 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',