# 内容安全策略（CSP） (content-security-policy)

> 检查并实施严格的 CSP 响应头，配合 HTML 清理和 Trusted Types 防范 XSS 与注入。

- 分类: [安全加固](https://www.myskills.tools/zh/categories/hardening/)
- 来源: 社区 · David Dias
- 代码仓库: https://github.com/thedaviddias/Front-End-Checklist/tree/main/skills/content-security-policy (commit 4b0ddb9)
- SKILL.md: https://github.com/thedaviddias/Front-End-Checklist/blob/main/skills/content-security-policy/SKILL.md
- 安装命令: `npx skills add thedaviddias/Front-End-Checklist --skill content-security-policy`
- 许可证: MIT
- 兼容工具: Any SKILL.md agent
- 权限: read-repo ✓, write-files ✓, shell ✗, network ✓
- 状态: 扫描通过
- 规则扫描无法覆盖所有风险，安装前请阅读此技能的 SKILL.md，确认权限后再使用。

## 核心规则

CSP 只缩小影响面，不能替代输出编码和 HTML 清理。

## 执行阶段

1. **检查现状** — 检查是否存在 CSP 头及其指令，并审查 innerHTML、dangerouslySetInnerHTML 等注入点。
2. **先用仅报告模式** — 先用 Content-Security-Policy-Report-Only 测试，收集违规报告，避免直接破坏站点。
3. **收紧策略** — 设置严格的 default-src 再逐项放行；内联脚本用 nonce 或 hash，避免 unsafe-inline 和 unsafe-eval。
4. **在类生产环境验证** — 在接近生产的环境中核对实际响应；较大的应用可加 Trusted Types 作为纵深防御。

## 权限说明（据 SKILL.md 整理）

| 能力 | 是否需要 | 说明 |
|---|---|---|
| 文件写入 | 需要 | 修改服务器或框架配置以设置 CSP 头，并在注入点加入清理逻辑。 |
| 命令执行 | 不需要 | 技能未要求运行特定命令。 |
| 网络外发 | 部分需要 | 仅为核对目标站点的实际响应头而访问该站点。 |

## SKILL.md 头部信息（YAML）

```yaml
---
name: content-security-policy
description: "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."
metadata:
  category: security
  priority: high
  difficulty: advanced
  estimatedTime: "45"
  source: frontendchecklist.io
  url: https://frontendchecklist.io/rules/security/content-security-policy
---
```

首页: https://www.myskills.tools/zh/ · URL: https://www.myskills.tools/zh/skills/content-security-policy/
