content-security-policy
它能做什么
这项技能检查并构建内容安全策略,即一组阻止未经批准代码和资源的浏览器规则。适合希望多一层防护、防范跨站脚本攻击的开发者。
适用场景
- ✓当你的网站没有内容安全策略标头时。
- ✓当页面使用了内联脚本或不安全的设置时。
- ✓当你的应用会把用户提供的 HTML 插入页面时。
技能链接
https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/content-security-policy/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请为我的网站设置一个内容安全策略,阻止未知脚本,并在正式强制执行前先测试。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会提出一个先以仅报告模式运行的策略,为内联脚本使用 nonce 或哈希,避免不安全的设置,并为不可信的 HTML 添加清理处理。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Implement a content security policy
Content Security Policy prevents cross-site scripting (XSS), clickjacking, and data injection attacks by controlling which resources can be loaded and executed on your pages. It reduces blast radius, but it does not replace output encoding and sanitization.
Quick Reference
- Start with Content-Security-Policy-Report-Only to test without breaking your site
- Use nonces or hashes for inline scripts instead of unsafe-inline
- Avoid unsafe-eval unless absolutely necessary
- Set strict default-src then allow specific sources
- Monitor CSP reports to catch violations
- Sanitize untrusted HTML before rendering it into the DOM
- Use Trusted Types in larger apps as defense in depth for DOM XSS sinks
Check
Check if this website implements a Content Security Policy header and analyze its directives. Also review any use of innerHTML, dangerouslySetInnerHTML, HTML template injection, and Trusted Types enforcement for large applications.
Fix
Implement a strict Content Security Policy to prevent XSS attacks and control resource loading. Sanitize untrusted HTML before rendering and add Trusted Types enforcement where the application has enough DOM injection surface to benefit from it.
Explain
Explain how CSP provides a security layer that helps detect and mitigate XSS and data injection attacks, and why sanitization is still required before rendering untrusted HTML.
Code Review
Review server config, headers, forms, and integration points related to Implement a content security policy. Flag exact responses, cookies, or browser behaviors that violate the rule, and verify them against the effective production-like response. Inspect HTML injection sinks and note whether Trusted Types or sanitization protects them.
For full implementation details, code examples, and framework-specific guidance,
see references/rule.md.
Rule page: https://frontendchecklist.io/rules/security/content-security-policy
常见问题
应该立即强制执行这个策略吗?
不应该。这项技能建议先以仅报告模式启动,在不破坏网站的前提下进行测试。
内容安全策略能替代清理处理吗?
不能。这项技能说明,它可以降低攻击的影响,但不能替代输出编码和清理处理。
什么是 Trusted Types?
这项技能把它描述为一种额外的防护,适用于将 HTML 插入页面的较大型应用。
详情
作者的原始描述
Use when reviewing headers, forms, cookies, or third-party integrations related to Implement a content security policy. Validate the effective browser and HTTP behavior in a production-like environment.
热度
- GitHub 星标
- 75k
- 排名
- #2785
- 许可证
- MIT
- 更新时间
- 2026/10/31
评论
- …
同一仓库的更多技能
frontendchecklist
75k使用命令行工具,依据 380 多条关于无障碍、速度、SEO 和安全的规则,检查前端代码和线上页面。
frontend-checklist-global
75k依据全部 386 条 Front-End Checklist 规则审查前端项目,并为发现的每个问题给出具体修复方法。
404-page
75k检查你的页面缺失提示是否提供有用的链接,并返回真正的 404 状态,帮助迷路的访客找到方向。
about-page
75k审查你的网站是否有清晰的关于页面,说明由谁运营,以建立访客和搜索引擎的信任。