Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
var unparse = require('escodegen').generate;

module.exports = function (ast, vars) {
module.exports = function (ast, vars, opts) {
if(!opts) opts = {};
var rejectAccessToMethodsOnFunctions = !opts.allowAccessToMethodsOnFunctions;

if (!vars) vars = {};
var FAIL = {};

var result = (function walk (node, noExecute) {
if (node.type === 'Literal') {
return node.value;
Expand Down Expand Up @@ -63,7 +66,7 @@ module.exports = function (ast, vars) {
if (l === FAIL) return FAIL;
var r = walk(node.right, noExecute);
if (r === FAIL) return FAIL;

if (op === '==') return l == r;
if (op === '===') return l === r;
if (op === '!=') return l != r;
Expand All @@ -80,7 +83,7 @@ module.exports = function (ast, vars) {
if (op === '|') return l | r;
if (op === '&') return l & r;
if (op === '^') return l ^ r;

return FAIL;
}
else if (node.type === 'Identifier') {
Expand All @@ -100,7 +103,7 @@ module.exports = function (ast, vars) {
if (callee === FAIL) return FAIL;
if (typeof callee !== 'function') return FAIL;


var ctx = node.callee.object ? walk(node.callee.object, noExecute) : FAIL;
if (ctx === FAIL) ctx = null;

Expand All @@ -119,8 +122,9 @@ module.exports = function (ast, vars) {
}
else if (node.type === 'MemberExpression') {
var obj = walk(node.object, noExecute);
// do not allow access to methods on Function
if((obj === FAIL) || (typeof obj == 'function')){
if((obj === FAIL) || (
(typeof obj == 'function') && rejectAccessToMethodsOnFunctions
)){
return FAIL;
}
if (node.property.type === 'Identifier' && !node.computed) {
Expand All @@ -147,7 +151,7 @@ module.exports = function (ast, vars) {
}
else if (node.type === 'FunctionExpression') {
var bodies = node.body.body;

// Create a "scope" for our arguments
var oldVars = {};
Object.keys(vars).forEach(function(element){
Expand All @@ -168,7 +172,7 @@ module.exports = function (ast, vars) {
}
// restore the vars and scope after we walk
vars = oldVars;

var keys = Object.keys(vars);
var vals = keys.map(function(key) {
return vars[key];
Expand Down Expand Up @@ -196,7 +200,7 @@ module.exports = function (ast, vars) {
}
else return FAIL;
})(ast);

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

Expand Down