跳到正文
Skillpedia
安全75k · #2753

avoid-eval

它能做什么

这项技能检查代码中 eval 以及其他将文字当作代码执行的方式。适合希望防范一类常见且严重的网站攻击的开发者。

适用场景

  • ✓当你在代码中发现 eval 或 new Function 时。
  • ✓当定时器被传入一段文字字符串来执行时。
  • ✓当你审查处理用户数据的代码时。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/avoid-eval/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/avoid-eval/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请在我的项目中搜索 eval 调用,并把它们替换为更安全的代码。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会在代码中搜索 eval、new Function 以及以文字字符串作为参数的定时器,然后把它们替换为 JSON 解析、对象查找或函数引用。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Never use eval() or unsafe dynamic code execution

eval() and its equivalents are the root cause of some of the most severe XSS vulnerabilities. If any user-controlled string reaches eval(), an attacker can execute arbitrary JavaScript in your users' browsers — stealing sessions, making requests as the user, or redirecting to malicious sites. Content Security Policy (CSP) blocks eval() but not new Function(), and there is never a legitimate use case that can't be solved without it.

Quick Reference

  • eval() executes arbitrary strings as code — any user input passed to it is a critical security hole
  • new Function() is equally dangerous and not caught by CSP eval restrictions
  • setTimeout('code', delay) and setInterval('code', delay) also evaluate strings — use function references instead
  • Use JSON.parse() instead of eval() for parsing JSON

Check

Search this codebase for any use of eval(), new Function(), or setTimeout/setInterval with string arguments.

Fix

Replace eval() calls with safe alternatives: JSON.parse for data, object lookups for dynamic dispatch, and Function references for timers.

Explain

Explain why eval() is dangerous, how it enables XSS attacks, and what safe alternatives exist for each use case.

Code Review

Review scripts, client components, and browser execution paths related to Never use eval() or unsafe dynamic code execution. Flag exact imports, event handlers, runtime side effects, or blocking operations that violate the rule, and state how the change should be verified in the browser.


For full implementation details, code examples, and framework-specific guidance, see references/rule.md.

Rule page: https://frontendchecklist.io/rules/javascript/avoid-eval

常见问题

eval 为什么危险?

这项技能说明,如果用户输入被传给 eval,攻击者就能在其他访客的浏览器中运行 JavaScript。

内容安全策略能阻止这种情况吗?

这项技能说明,CSP 会阻止 eval,但不会阻止 new Function,因此两者都需要从代码中移除。

解析 JSON 应该用什么?

应使用 JSON.parse,这项技能建议用它代替 eval。

详情

作者的原始描述

Use when reviewing scripts, client components, bundles, or runtime behavior related to Never use eval() or unsafe dynamic code execution. Inspect both source code and the browser execution path so fixes target the real bottleneck or bug.

repo: thedaviddias/Front-End-Checklist

热度

GitHub 星标
75k
排名
#2753
许可证
MIT
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能