Skip to content

Commit 33245c6

Browse files
update static-eval, closes #34, #35 (#38)
* deps(package): Update static-eval to 2.0 Fixes https://nodesecurity.io/advisories/548 * use proxy value for callbacks when static-eval fails builds on #35, but when static-eval cannot evaluate a callback function because it is unsafe, this passes a proxy value. when the proxy callback function is called, it throws an error, but when it is stringified (eg in the generated output) it'll work. this works with brfs, i haven't tried others yet. * make sure `callee` exists * ci: remove node 0.8, add new versions
1 parent f48557f commit 33245c6

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
language: node_js
22
node_js:
3-
- "0.8"
3+
- 9
4+
- 8
5+
- 6
6+
- 4
47
- "0.10"

index.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,31 @@ module.exports = function parse (modules, opts) {
287287

288288
var xvars = copy(vars);
289289
xvars[node.name] = val;
290-
290+
291291
var res = evaluate(cur, xvars);
292+
if (res === undefined && cur.type === 'CallExpression') {
293+
// static-eval can't safely evaluate code with callbacks, so do it manually in a safe way
294+
var callee = evaluate(cur.callee, xvars);
295+
var args = cur.arguments.map(function (arg) {
296+
// Return a function stub for callbacks so that `static-module` users
297+
// can do `callback.toString()` and get the original source
298+
if (arg.type === 'FunctionExpression' || arg.type === 'ArrowFunctionExpression') {
299+
var fn = function () {
300+
throw new Error('static-module: cannot call callbacks defined inside source code');
301+
};
302+
fn.toString = function () {
303+
return body.slice(arg.start, arg.end);
304+
};
305+
return fn;
306+
}
307+
return evaluate(arg, xvars);
308+
});
309+
310+
if (callee !== undefined) {
311+
res = callee.apply(null, args);
312+
}
313+
}
314+
292315
if (res !== undefined) {
293316
updates.push({
294317
start: cur.start,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"quote-stream": "~1.0.2",
1414
"readable-stream": "~2.3.3",
1515
"shallow-copy": "~0.0.1",
16-
"static-eval": "~0.2.0",
16+
"static-eval": "^2.0.0",
1717
"through2": "~2.0.3"
1818
},
1919
"devDependencies": {

0 commit comments

Comments
 (0)