Skip to content

Commit 23319e3

Browse files
authored
Revert "[CVE-2017-16088] Sandbox Breakout (Critical Security Fix) - context clear (#13)" (#14)
This reverts commit 5e60f4a.
1 parent 5e60f4a commit 23319e3

File tree

3 files changed

+14
-47
lines changed

3 files changed

+14
-47
lines changed

index.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
var vm = require('vm')
22

3-
function clearContext () {
4-
// eslint-disable-next-line no-global-assign
5-
Function = undefined
6-
const keys = Object.getOwnPropertyNames(this).concat(['constructor'])
7-
keys.forEach((key) => {
8-
const item = this[key]
9-
if (!item) return
10-
if (typeof Object.getPrototypeOf(item).constructor === 'function') {
11-
Object.getPrototypeOf(item).constructor = undefined
12-
}
13-
if (typeof item.constructor === 'function') {
14-
this[key].constructor = undefined
15-
}
16-
})
17-
}
18-
193
module.exports = function safeEval (code, context, opts) {
204
var sandbox = {}
215
var resultKey = 'SAFE_EVAL_' + Math.floor(Math.random() * 1000000)
226
sandbox[resultKey] = {}
23-
var clearContextCall = `(${clearContext.toString()})();`
24-
code = `${clearContextCall}${resultKey}=${code}`
7+
var clearContext = `
8+
(function() {
9+
Function = undefined;
10+
const keys = Object.getOwnPropertyNames(this).concat(['constructor']);
11+
keys.forEach((key) => {
12+
const item = this[key];
13+
if (!item || typeof item.constructor !== 'function') return;
14+
this[key].constructor = undefined;
15+
});
16+
})();
17+
`
18+
code = clearContext + resultKey + '=' + code
2519
if (context) {
2620
Object.keys(context).forEach(function (key) {
27-
if (context[key] === Function) return
2821
sandbox[key] = context[key]
2922
})
3023
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "safe-eval",
3-
"version": "0.4.2",
3+
"version": "0.4.1",
44
"description": "Safer version of eval()",
55
"main": "index.js",
66
"scripts": {

test/test.js

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,7 @@ describe('safe-eval', function () {
4444
})
4545
})
4646

47-
it('should not have access to Node.js objects using context (CWE-265)', function () {
48-
var code = 'test(\'return process\')()'
49-
assert.throws(function () {
50-
safeEval(code, {
51-
// eslint-disable-next-line no-new-func
52-
test: new Function().constructor
53-
})
54-
})
55-
})
56-
57-
it('should not have access to Node.js objects using Object.getPrototypeOf (CWE-265)', function () {
58-
var code = `Object.getPrototypeOf(Object).constructor('return process')();`
59-
assert.throws(function () {
60-
safeEval(code)
61-
})
62-
})
63-
64-
it('should not have access to Node.js objects using Object.getPrototypeOf with context (CWE-265)', function () {
65-
var code = `Object.getPrototypeOf(obj).constructor.constructor("return process")();`
66-
assert.throws(function () {
67-
safeEval(code, {
68-
obj: Object
69-
})
70-
})
71-
})
72-
73-
it('should not have access to Node.js objects using this.constructor (CWE-265)', function () {
47+
it('should not have access to Node.js objects (CWE-265)', function () {
7448
var code = 'this.constructor.constructor(\'return process\')()'
7549
assert.throws(function () {
7650
safeEval(code)

0 commit comments

Comments
 (0)