eval
This is how the comment reads inline, under the flagged line.
Why it matters
If any part of the string comes from input, this is remote code execution. Even when it doesn't, it defeats static analysis and CSP.
What to do
Parse the data (JSON.parse, ast.literal_eval) or dispatch through a lookup table.
Examples
const fn = eval(userFormula);
const fn = FORMULAS[name];
What it catches
It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript, Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
const r = eval(s);
src/app.py
exec(code)
Left alone
const m = re.exec(s);
const r = evaluate(s);
class A {
override async exec(o) {}
}src/app.py
def exec(self, cmd):
return cmd
Research
`eval`, `exec` and `new Function`. Dynamic code execution turns input into code.
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"eval-call"
],
"severity": {
"eval-call": "low"
}
}
}
evalruns a string as code.