CSP 指令
script-src:控制可运行的脚本style-src:控制可加载的样式表font-src:控制可加载的字体img-src:控制可加载的图像、图标和徽标connect-src:控制可连接的用于 API 调用和 WebSocket 连接的 URLframe-src:控制可嵌入 frame 或 iframe 的 URLdefault-src:未显式设置其他指令时的回退项
域名允许列表
示例 CSP 配置
仅包含你实际使用的服务的 domain。删除你尚未为文档配置的任何 Analytics 相关 domain。
按代理类型划分的常见配置
Cloudflare 配置
- 在 Cloudflare 控制台,进入 Rules > Overview。
- 选择 Create rule > Response Header Transform Rule。
- 配置该规则:
- Modify response header:设置为 static
- Header name:
Content-Security-Policy - Header value:
- Header name:
- 部署该规则。
AWS CloudFront 配置
Vercel 配置
vercel.json:
疑难解答
- 打开浏览器的开发者工具。
- 切换到 Console 标签页。
- 查找以下前缀的错误信息:
Content Security Policy: The page's settings blocked the loading of a resourceRefused to load the script/stylesheet because it violates the following Content Security Policy directiveRefused to connect to because it violates the following Content Security Policy directive