Skip to content

Commit

Permalink
Revert "[CVE-2017-16088] Sandbox Breakout (Critical Security Fix) - c…
Browse files Browse the repository at this point in the history
…ontext clear (#13)" (#14)

This reverts commit 5e60f4a.
  • Loading branch information
hacksparrow authored Dec 14, 2018
1 parent 5e60f4a commit 23319e3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 47 deletions.
31 changes: 12 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
var vm = require('vm')

function clearContext () {
// eslint-disable-next-line no-global-assign
Function = undefined
const keys = Object.getOwnPropertyNames(this).concat(['constructor'])
keys.forEach((key) => {
const item = this[key]
if (!item) return
if (typeof Object.getPrototypeOf(item).constructor === 'function') {
Object.getPrototypeOf(item).constructor = undefined
}
if (typeof item.constructor === 'function') {
this[key].constructor = undefined
}
})
}

module.exports = function safeEval (code, context, opts) {
var sandbox = {}
var resultKey = 'SAFE_EVAL_' + Math.floor(Math.random() * 1000000)
sandbox[resultKey] = {}
var clearContextCall = `(${clearContext.toString()})();`
code = `${clearContextCall}${resultKey}=${code}`
var clearContext = `
(function() {
Function = undefined;
const keys = Object.getOwnPropertyNames(this).concat(['constructor']);
keys.forEach((key) => {
const item = this[key];
if (!item || typeof item.constructor !== 'function') return;
this[key].constructor = undefined;
});
})();
`
code = clearContext + resultKey + '=' + code
if (context) {
Object.keys(context).forEach(function (key) {
if (context[key] === Function) return
sandbox[key] = context[key]
})
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "safe-eval",
"version": "0.4.2",
"version": "0.4.1",
"description": "Safer version of eval()",
"main": "index.js",
"scripts": {
Expand Down
28 changes: 1 addition & 27 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,33 +44,7 @@ describe('safe-eval', function () {
})
})

it('should not have access to Node.js objects using context (CWE-265)', function () {
var code = 'test(\'return process\')()'
assert.throws(function () {
safeEval(code, {
// eslint-disable-next-line no-new-func
test: new Function().constructor
})
})
})

it('should not have access to Node.js objects using Object.getPrototypeOf (CWE-265)', function () {
var code = `Object.getPrototypeOf(Object).constructor('return process')();`
assert.throws(function () {
safeEval(code)
})
})

it('should not have access to Node.js objects using Object.getPrototypeOf with context (CWE-265)', function () {
var code = `Object.getPrototypeOf(obj).constructor.constructor("return process")();`
assert.throws(function () {
safeEval(code, {
obj: Object
})
})
})

it('should not have access to Node.js objects using this.constructor (CWE-265)', function () {
it('should not have access to Node.js objects (CWE-265)', function () {
var code = 'this.constructor.constructor(\'return process\')()'
assert.throws(function () {
safeEval(code)
Expand Down

0 comments on commit 23319e3

Please sign in to comment.