Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
fix: only merge non-enumerable global properties that are not in VM s…
Browse files Browse the repository at this point in the history
…cope
  • Loading branch information
Josh-Cena authored Mar 24, 2022
1 parent 34ddc51 commit d320ca6
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit d320ca6

Please sign in to comment.