Skip to main content
HighClient-sideOWASP A03:2021CWE-94

How to fix: eval() in Client-Side JavaScript

eval() or the Function() constructor was found in client-side JavaScript. These functions execute strings as code and are a top DOM XSS sink.

Why it matters

If any attacker-controlled data reaches eval() or new Function(), it results in arbitrary JavaScript execution. This is rated as one of the most dangerous DOM XSS sinks because it can bypass CSP 'unsafe-inline' in some browsers.

eval() and new Function(string) convert strings into executable code. When combined with user input; URL parameters, postMessage data, or third-party data; they enable arbitrary code execution.

How to fix it

  1. 1Remove all eval() and new Function(string) calls.
  2. 2Replace eval(jsonString) with JSON.parse(jsonString).
  3. 3Replace eval(mathExpression) with a safe math parser library.
  4. 4Enable the CSP 'unsafe-eval' restriction to block these at the browser level.

Code examples

Replace eval with safe alternatives

JavaScript
// Bad
const data = eval('(' + serverResponse + ')');

// Good
const data = JSON.parse(serverResponse);

// Bad: dynamic function creation
const fn = new Function('x', 'return x * ' + userInput);

// Good: explicit safe computation
const multiplier = Number(userInput);
if (isNaN(multiplier)) throw new Error('Invalid multiplier');
const fn = (x) => x * multiplier;

References

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

See every client-side check

Questions

How severe is eval() in Client-Side JavaScript?
VulnRadar rates this high severity in the client-side 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 eval-in-client-script, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does eval() in Client-Side JavaScript 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() in client-side javascript

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() in Client-Side JavaScript | VulnRadar