Skip to main content
HighAI codeOWASP A03:2021CWE-94

How to fix: eval() Usage in Page Scripts

An eval() call was found inside the page's <script> blocks. eval() executes arbitrary JavaScript strings and is a common AI-generated mistake when dynamically constructing logic.

Why it matters

If any user-controlled data reaches eval(), it enables full JavaScript code execution in the browser or server context. Even when input is not directly user-controlled, eval() breaks static analysis and makes the application unauditable.

AI code generators often produce eval() when asked to 'dynamically evaluate' configuration, templates, or expressions. The correct alternative is almost always a lookup table, JSON.parse, or a dedicated expression parser.

How to fix it

  1. 1Remove all eval() calls. Use JSON.parse() for parsing JSON, Function() only with extreme caution and never with user input.
  2. 2Replace dynamic dispatch with an explicit lookup: const handlers = { add: (a,b) => a+b }; handlers[op](x, y).
  3. 3Enable eslint's no-eval rule to prevent regressions.
  4. 4If server-side: consider vm2 or isolated-vm for sandboxed evaluation, never raw eval().

Code examples

Remove eval: use a lookup table

JavaScript
// Bad (AI-generated)
const result = eval(`math.${operation}(${a}, ${b})`);

// Good
const ops = { add: (a,b) => a+b, sub: (a,b) => a-b, mul: (a,b) => a*b };
const fn = ops[operation];
if (!fn) throw new Error('Unknown operation');
const result = fn(Number(a), Number(b));

References

Detected automatically by VulnRadar. The check id is vibe-eval-usage, stable across every scan so you can filter or diff on it.

See every ai code check

Questions

How severe is eval() Usage in Page Scripts?
VulnRadar rates this high severity in the ai code category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id vibe-eval-usage, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does eval() Usage in Page Scripts map to?
It maps to CWE-94. In the OWASP Top 10 it falls under A03:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for eval() usage in page scripts

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

How to fix: eval() Usage in Page Scripts | VulnRadar