Skip to content

Commit 9aa4e66

Browse files
author
James Halliday
committed
use eval() with early throw instead of Function() to prevent script injection
1 parent 05f2390 commit 9aa4e66

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ module.exports = function (src, file) {
44
if (typeof src !== 'string') src = String(src);
55

66
try {
7-
Function(src);
7+
eval('throw "STOP"; (function () { ' + src + '})()');
88
return;
99
}
1010
catch (err) {
11+
if (err === 'STOP') return undefined;
1112
if (err.constructor.name !== 'SyntaxError') throw err;
1213
return errorInfo(src, file);
1314
}

test/sources/run2.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
}); process.exit(1); (function () {
1+
})();
2+
process.exit(1);
3+
(function () {

0 commit comments

Comments
 (0)