JavaScript

js/no-eval

Never run strings as code - no eval, new Function, or a string as the first argument of setTimeout or setInterval.

seriousstaticfe-javascript

Why

A string run as code executes with the page's full rights: if any part of it comes from a URL, storage or a server response, it is cross-site scripting. It also forces a Content Security Policy to allow unsafe-eval and stops the engine from optimising the surrounding code.

Fix

Pass functions, not strings: setTimeout(save, 500). Parse data with JSON.parse, look values up in an object or Map instead of building property names as code, and use a template or the DOM for markup.

Examples

// bad
setTimeout('refresh()', 1000)
const total = eval(`${a} + ${b}`)
// good
setTimeout(refresh, 1000)
const total = a + b

Sources

Facts

Id
js/no-eval
Severity
serious
Check
static checked in the source by audit
Applies to
jsx html vue svelte astro
Taught by the skill
fe-javascript JavaScript in the browser
Since version
0.1.0

Rule file on GitHub

Read it in the terminal

npx github:dawidolko/frontend-skills explain js/no-eval