Bezpieczeństwo

security/no-eval

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

poważnastatycznefe-security

Dlaczego

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.

Poprawka

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.

Przykłady

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

Źródła

Fakty

Identyfikator
security/no-eval
Waga
poważna
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
html jsx vue svelte astro
Uczy jej skill
fe-security Front-end security
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

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