1
1
var unparse = require ( 'escodegen' ) . generate ;
2
2
3
- module . exports = function ( ast , vars ) {
3
+ module . exports = function ( ast , vars , opts ) {
4
+ if ( ! opts ) opts = { } ;
5
+ var rejectAccessToMethodsOnFunctions = ! opts . allowAccessToMethodsOnFunctions ;
6
+
4
7
if ( ! vars ) vars = { } ;
5
8
var FAIL = { } ;
6
-
9
+
7
10
var result = ( function walk ( node , noExecute ) {
8
11
if ( node . type === 'Literal' ) {
9
12
return node . value ;
@@ -63,7 +66,7 @@ module.exports = function (ast, vars) {
63
66
if ( l === FAIL ) return FAIL ;
64
67
var r = walk ( node . right , noExecute ) ;
65
68
if ( r === FAIL ) return FAIL ;
66
-
69
+
67
70
if ( op === '==' ) return l == r ;
68
71
if ( op === '===' ) return l === r ;
69
72
if ( op === '!=' ) return l != r ;
@@ -80,7 +83,7 @@ module.exports = function (ast, vars) {
80
83
if ( op === '|' ) return l | r ;
81
84
if ( op === '&' ) return l & r ;
82
85
if ( op === '^' ) return l ^ r ;
83
-
86
+
84
87
return FAIL ;
85
88
}
86
89
else if ( node . type === 'Identifier' ) {
@@ -100,7 +103,7 @@ module.exports = function (ast, vars) {
100
103
if ( callee === FAIL ) return FAIL ;
101
104
if ( typeof callee !== 'function' ) return FAIL ;
102
105
103
-
106
+
104
107
var ctx = node . callee . object ? walk ( node . callee . object , noExecute ) : FAIL ;
105
108
if ( ctx === FAIL ) ctx = null ;
106
109
@@ -119,8 +122,9 @@ module.exports = function (ast, vars) {
119
122
}
120
123
else if ( node . type === 'MemberExpression' ) {
121
124
var obj = walk ( node . object , noExecute ) ;
122
- // do not allow access to methods on Function
123
- if ( ( obj === FAIL ) || ( typeof obj == 'function' ) ) {
125
+ if ( ( obj === FAIL ) || (
126
+ ( typeof obj == 'function' ) && rejectAccessToMethodsOnFunctions
127
+ ) ) {
124
128
return FAIL ;
125
129
}
126
130
if ( node . property . type === 'Identifier' && ! node . computed ) {
@@ -147,7 +151,7 @@ module.exports = function (ast, vars) {
147
151
}
148
152
else if ( node . type === 'FunctionExpression' ) {
149
153
var bodies = node . body . body ;
150
-
154
+
151
155
// Create a "scope" for our arguments
152
156
var oldVars = { } ;
153
157
Object . keys ( vars ) . forEach ( function ( element ) {
@@ -168,7 +172,7 @@ module.exports = function (ast, vars) {
168
172
}
169
173
// restore the vars and scope after we walk
170
174
vars = oldVars ;
171
-
175
+
172
176
var keys = Object . keys ( vars ) ;
173
177
var vals = keys . map ( function ( key ) {
174
178
return vars [ key ] ;
@@ -196,7 +200,7 @@ module.exports = function (ast, vars) {
196
200
}
197
201
else return FAIL ;
198
202
} ) ( ast ) ;
199
-
203
+
200
204
return result === FAIL ? undefined : result ;
201
205
} ;
202
206
0 commit comments