Security

security/no-eval

Front-end code never runs strings as code - no eval, new Function or string arguments to setTimeout and setInterval.

seriousstaticfe-security

Why

Any of these turns a string into running code. If any part of the string can come from a URL, storage, a message or an API, an attacker can run script in the page. They also force a Content Security Policy to allow 'unsafe-eval', which weakens it for everything else.

Fix

Parse data with JSON.parse, look functions up in an object (handlers[name]()), and pass functions to timers (setTimeout(() => refresh(), 1000)). Keep 'unsafe-eval' out of the CSP.

Examples

// bad
const settings = eval('(' + raw + ')')
setTimeout('refresh()', 1000)
// good
const settings = JSON.parse(raw)
setTimeout(refresh, 1000)

Sources

Facts

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

Rule file on GitHub

Read it in the terminal

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