Skip to content

Commit

Permalink
fix(security): prevent Function calls outside of member expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed Oct 17, 2024
1 parent 0bf1665 commit 5a22e3f
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 9 deletions.
5 changes: 4 additions & 1 deletion dist/index-browser-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ const SafeEval = {
const result = obj[prop];
if (typeof result === 'function') {
if (result === Function) {
throw new Error('Function constructor is disabled');
return result; // Don't bind so can identify and throw later
}
return result.bind(obj); // arrow functions aren't affected by bind.
}
Expand All @@ -1319,6 +1319,9 @@ const SafeEval = {
evalCallExpression(ast, subs) {
const args = ast.arguments.map(arg => SafeEval.evalAst(arg, subs));
const func = SafeEval.evalAst(ast.callee, subs);
if (func === Function) {
throw new Error('Function constructor is disabled');
}
return func(...args);
},
evalAssignmentExpression(ast, subs) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index-browser-esm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-browser-esm.min.js.map

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion dist/index-browser-umd.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,7 @@
const result = obj[prop];
if (typeof result === 'function') {
if (result === Function) {
throw new Error('Function constructor is disabled');
return result; // Don't bind so can identify and throw later
}
return result.bind(obj); // arrow functions aren't affected by bind.
}
Expand All @@ -1325,6 +1325,9 @@
evalCallExpression(ast, subs) {
const args = ast.arguments.map(arg => SafeEval.evalAst(arg, subs));
const func = SafeEval.evalAst(ast.callee, subs);
if (func === Function) {
throw new Error('Function constructor is disabled');
}
return func(...args);
},
evalAssignmentExpression(ast, subs) {
Expand Down
2 changes: 1 addition & 1 deletion dist/index-browser-umd.min.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-browser-umd.min.cjs.map

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion dist/index-node-cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,7 @@ const SafeEval = {
const result = obj[prop];
if (typeof result === 'function') {
if (result === Function) {
throw new Error('Function constructor is disabled');
return result; // Don't bind so can identify and throw later
}
return result.bind(obj); // arrow functions aren't affected by bind.
}
Expand All @@ -1320,6 +1320,9 @@ const SafeEval = {
evalCallExpression(ast, subs) {
const args = ast.arguments.map(arg => SafeEval.evalAst(arg, subs));
const func = SafeEval.evalAst(ast.callee, subs);
if (func === Function) {
throw new Error('Function constructor is disabled');
}
return func(...args);
},
evalAssignmentExpression(ast, subs) {
Expand Down
5 changes: 4 additions & 1 deletion dist/index-node-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ const SafeEval = {
const result = obj[prop];
if (typeof result === 'function') {
if (result === Function) {
throw new Error('Function constructor is disabled');
return result; // Don't bind so can identify and throw later
}
return result.bind(obj); // arrow functions aren't affected by bind.
}
Expand All @@ -1318,6 +1318,9 @@ const SafeEval = {
evalCallExpression(ast, subs) {
const args = ast.arguments.map(arg => SafeEval.evalAst(arg, subs));
const func = SafeEval.evalAst(ast.callee, subs);
if (func === Function) {
throw new Error('Function constructor is disabled');
}
return func(...args);
},
evalAssignmentExpression(ast, subs) {
Expand Down
5 changes: 4 additions & 1 deletion src/Safe-Script.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ const SafeEval = {
const result = obj[prop];
if (typeof result === 'function') {
if (result === Function) {
throw new Error('Function constructor is disabled');
return result; // Don't bind so can identify and throw later
}
return result.bind(obj); // arrow functions aren't affected by bind.
}
Expand All @@ -134,6 +134,9 @@ const SafeEval = {
evalCallExpression (ast, subs) {
const args = ast.arguments.map((arg) => SafeEval.evalAst(arg, subs));
const func = SafeEval.evalAst(ast.callee, subs);
if (func === Function) {
throw new Error('Function constructor is disabled');
}
return func(...args);
},
evalAssignmentExpression (ast, subs) {
Expand Down

0 comments on commit 5a22e3f

Please sign in to comment.