Eval Injection

eval(), new Function(), and strings passed to setTimeout or setInterval

criticalSecurityeval-injection

Why 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

Flaggedserver/calculate.ts
app.post("/calculate", (req, res) => {
  const { expression } = req.body;
  const result = eval(expression);
  res.json({ result });
});
Fixedserver/calculate.ts
import { 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 --json and 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-injection at the top of the file
How it works
Matches text patterns. Source on GitHub