Eval Injection
eval(), new Function(), and strings passed to setTimeout or setInterval
criticalSecurity
eval-injectionWhy this matters
eval() and new Function() execute arbitrary code. If any part of the string comes from user input, an attacker can run any code on your server.
Example
server/calculate.tsapp.post("/calculate", (req, res) => {
const { expression } = req.body;
const result = eval(expression);
res.json({ result });
});server/calculate.tsimport { evaluate } from "mathjs";
app.post("/calculate", (req, res) => {
const { expression } = req.body;
const result = evaluate(expression);
res.json({ result });
});How to fix
Never use eval() or new Function() with any data that could come from users. Use a safe parser (mathjs for math, JSON.parse for JSON) instead.
Reference
- In the output
- Findings carry
"ruleId": "eval-injection"in--jsonand the same id in SARIF and in the terminal. - Silence one line
// prodlint-disable-next-line eval-injection- Silence a whole file
// prodlint-disable eval-injectionat the top of the file- How it works
- Matches text patterns. Source on GitHub