src/content/docs/linter/rules/no-global-eval.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.5.0` - Diagnostic Category: [`lint/security/noGlobalEval`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - Sources: - Same as [`no-eval`](https://eslint.org/docs/latest/rules/no-eval){
"linter": {
"rules": {
"security": {
"noGlobalEval": "error"
}
}
}
}
Disallow the use of global eval().
The eval() function evaluates the passed string as a JavaScript code.
The executed code can access and mutate variables in the scope where the function is called.
The use of eval() exposes to security risks and performance issues.
If the executed code is somehow affected by a malicious party,
then you may end up executing malicious code with the privileges of the caller.
Moreover, changing variables in the caller's scope is expensive in modern JavaScript interpreters.
eval("var a = 0");
(0, globalThis.eval)("var a = 0")
f(eval);
const aliasedEval = eval;
function f(eval) {
eval("let a = 0;");
}
The rule is not able to detect cases where the global object is aliased:
let foo = globalThis;
foo.eval("let a = 0;");