Skip to content
Open
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
19 changes: 15 additions & 4 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const {
ArrayPrototypeForEach,
ArrayPrototypePush,
ObjectFreeze,
ObjectPrototype,
PromiseReject,
Expand Down Expand Up @@ -51,7 +52,6 @@ const {
validateStringArray,
validateUint32,
kValidateObjectAllowArray,
kValidateObjectAllowNullable,
} = require('internal/validators');
const {
emitExperimentalWarning,
Expand Down Expand Up @@ -318,10 +318,20 @@ function runInThisContext(code, options) {
return createScript(code, options).runInThisContext(options);
}

function copyArray(array) {
const copy = [];
for (let i = 0; i < array.length; ++i) {
ArrayPrototypePush(copy, array[i]);
}
return copy;
}

function compileFunction(code, params, options = kEmptyObject) {
validateString(code, 'code');
validateObject(options, 'options');
if (params !== undefined) {
validateArray(params, 'params');
params = copyArray(params);
validateStringArray(params, 'params');
}
const {
Expand Down Expand Up @@ -355,17 +365,18 @@ function compileFunction(code, params, options = kEmptyObject) {
}
}
validateArray(contextExtensions, 'options.contextExtensions');
ArrayPrototypeForEach(contextExtensions, (extension, i) => {
const extensions = copyArray(contextExtensions);
ArrayPrototypeForEach(extensions, (extension, i) => {
const name = `options.contextExtensions[${i}]`;
validateObject(extension, name, kValidateObjectAllowNullable);
validateObject(extension, name);
});

const hostDefinedOptionId =
getHostDefinedOptionId(importModuleDynamically, filename);

return internalCompileFunction(
code, filename, lineOffset, columnOffset,
cachedData, produceCachedData, parsingContext, contextExtensions,
cachedData, produceCachedData, parsingContext, extensions,
params, hostDefinedOptionId, importModuleDynamically,
).function;
}
Expand Down
84 changes: 84 additions & 0 deletions test/parallel/test-vm-compile-function-validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
'use strict';

// Refs: https://github.com/nodejs/node/issues/67366

const common = require('../common');
const assert = require('assert');
const vm = require('vm');

assert.throws(() => {
vm.compileFunction('return 1', [], { contextExtensions: [null] });
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.contextExtensions[0]" property must be of type ' +
'object. Received null',
});

assert.throws(() => {
vm.compileFunction('return 1', [], { contextExtensions: [{}, null] });
}, {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "options.contextExtensions[1]" property must be of type ' +
'object. Received null',
});

{
let reads = 0;
const params = [];
Object.defineProperty(params, 0, {
__proto__: null,
enumerable: true,
configurable: true,
get: common.mustCall(() => (++reads === 1 ? 'x' : 42)),
});

const fn = vm.compileFunction('return x', params);
assert.strictEqual(fn(5), 5);
}

{
let reads = 0;
const contextExtensions = [];
Object.defineProperty(contextExtensions, 0, {
__proto__: null,
enumerable: true,
configurable: true,
get: common.mustCall(() => (++reads === 1 ? { a: 5 } : null)),
});

const fn = vm.compileFunction('return a', [], { contextExtensions });
assert.strictEqual(fn(), 5);
}

{
const params = [];
Object.defineProperty(params, 0, {
__proto__: null,
enumerable: true,
configurable: true,
get: common.mustCall(() => 42),
});

assert.throws(() => vm.compileFunction('return 1', params), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "params[0]" argument must be of type string. ' +
'Received type number (42)',
});
}

{
let reads = 0;
const params = new Proxy(['x'], {
get(target, key, receiver) {
if (key === '0') return ++reads === 1 ? 'x' : 42;
return Reflect.get(target, key, receiver);
},
});

const fn = vm.compileFunction('return x', params);
assert.strictEqual(fn(7), 7);
assert.strictEqual(reads, 1);
}