Skip to content

Commit 758db14

Browse files
author
Dandelion Mané
committed
ensure that globals includes process and console
The code attaches global properties to the sandbox context by iterating over all the enumerable properties of `global`. However, in node v10, `console` switched [to being non-enmuerable][1]. This means that for users of this library with node>10, any `console.log`s in evaluated scripts will fail. This commit fixes this issue by manually attaching console to the sandbox (when globals are being used). A test has been added. Prior to the change to eval.js, the test would pass in node v8 but fail in v10 and v12. Also, the tests were already failing in v12, because in v12 `process` also became non-enumerable. I've applied a similar fix to `process` to ensure that it's always available too. [1]: nodejs/node#17708
1 parent ae48528 commit 758db14

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

eval.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ module.exports = function (content, filename, scope, includeGlobals) {
3636

3737
if (includeGlobals) {
3838
merge(sandbox, global)
39+
// console is non-enumerable in node v10 and above
40+
sandbox.console = global.console
41+
// process is non-enumerable in node v12 and above
42+
sandbox.process = global.process
3943
sandbox.require = requireLike(_filename)
4044
}
4145

test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@ assert.throws(function () {
2727
_eval('require("fs")')
2828
})
2929

30+
// Verify that the console is available when globals are passed
31+
res = _eval('exports.x = console', true)
32+
assert.deepEqual(res.x, console)
33+
3034
console.log('All tests passed')

0 commit comments

Comments
 (0)