From d320ca6bb1ff840a91da2ef4476d533c168cdf59 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Thu, 24 Mar 2022 09:37:17 +0800 Subject: [PATCH] fix: only merge non-enumerable global properties that are not in VM scope --- eval.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/eval.js b/eval.js index 76ad90f..8cd4e8f 100644 --- a/eval.js +++ b/eval.js @@ -5,9 +5,7 @@ var requireLike = require('require-like') function merge (a, b) { if (!a || !b) return a - // Include all non-enumerable variables, including console (v10+), - // process (v12+), URL, etc. - var keys = Object.getOwnPropertyNames(b) + var keys = Object.keys(b) for (var k, i = 0, n = keys.length; i < n; i++) { k = keys[i] a[k] = b[k] @@ -37,7 +35,19 @@ module.exports = function (content, filename, scope, includeGlobals) { var _filename = filename || module.parent.filename; if (includeGlobals) { + // Merge enumerable variables on `global` merge(sandbox, global) + // Merge all non-enumerable variables on `global`, including console (v10+), + // process (v12+), URL, etc. We first filter out anything that's already in + // the VM scope (i.e. those in the ES standard) so we don't have two copies + var vmGlobals = Object.getOwnPropertyNames( + new vm.Script("vmGlobals = globalThis").runInNewContext().vmGlobals + ) + Object.getOwnPropertyNames(global).forEach((name) => { + if (!vmGlobals.includes(name)) { + sandbox[name] = global[name] + } + }) sandbox.require = requireLike(_filename) }