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

content-security-policy

它能做什么

这项技能检查并构建内容安全策略,即一组阻止未经批准代码和资源的浏览器规则。适合希望多一层防护、防范跨站脚本攻击的开发者。

适用场景

  • ✓当你的网站没有内容安全策略标头时。
  • ✓当页面使用了内联脚本或不安全的设置时。
  • ✓当你的应用会把用户提供的 HTML 插入页面时。

技能链接

https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/content-security-policy/SKILL.md

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

使用方法

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

可直接粘贴的消息

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

可以这样问 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.

repo: thedaviddias/Front-End-Checklist

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能