Skip to content

Commit

Permalink
Merge pull request #27 from browserify/prevent-constructor-access
Browse files Browse the repository at this point in the history
disallow accessing Function constructor
  • Loading branch information
goto-bus-stop committed Nov 19, 2019
2 parents 7d7bdc5 + a18a308 commit 0bcd9dc
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
10 changes: 8 additions & 2 deletions index.js
Expand Up @@ -101,11 +101,13 @@ module.exports = function (ast, vars) {
if((obj === FAIL) || (typeof obj == 'function')){
return FAIL;
}
if (node.property.type === 'Identifier') {
if (node.property.type === 'Identifier' && !node.computed) {
if (isUnsafeProperty(node.property.name)) return FAIL;
return obj[node.property.name];
}
var prop = walk(node.property);
if (prop === FAIL) return FAIL;
if (prop === null || prop === FAIL) return FAIL;
if (isUnsafeProperty(prop)) return FAIL;
return obj[prop];
}
else if (node.type === 'ConditionalExpression') {
Expand Down Expand Up @@ -176,3 +178,7 @@ module.exports = function (ast, vars) {

return result === FAIL ? undefined : result;
};

function isUnsafeProperty(name) {
return name === 'constructor' || name === '__proto__';
}
43 changes: 42 additions & 1 deletion test/eval.js
Expand Up @@ -79,4 +79,45 @@ test('MemberExpressions from Functions unresolved', function(t) {
var ast = parse(src).body[0].expression;
var res = evaluate(ast, {});
t.equal(res, undefined);
});
});

test('disallow accessing constructor or __proto__', function (t) {
t.plan(4)

var someValue = {};

var src = 'object.constructor';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, { vars: { object: someValue } });
t.equal(res, undefined);

var src = 'object["constructor"]';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, { vars: { object: someValue } });
t.equal(res, undefined);

var src = 'object.__proto__';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, { vars: { object: someValue } });
t.equal(res, undefined);

var src = 'object["__pro"+"t\x6f__"]';
var ast = parse(src).body[0].expression;
var res = evaluate(ast, { vars: { object: someValue } });
t.equal(res, undefined);
});


test('constructor at runtime only', function(t) {
t.plan(2)

var src = '(function myTag(y){return ""[!y?"__proto__":"constructor"][y]})("constructor")("console.log(process.env)")()'
var ast = parse(src).body[0].expression;
var res = evaluate(ast);
t.equal(res, undefined);

var src = '(function(prop) { return {}[prop ? "benign" : "constructor"][prop] })("constructor")("alert(1)")()'
var ast = parse(src).body[0].expression;
var res = evaluate(ast);
t.equal(res, undefined);
});

0 comments on commit 0bcd9dc

Please sign in to comment.