内容 Security Policy (CSP)
一句话解释
CSP 是 HTTP 请求头那个 restricts 哪个 resources (scripts, 样式, 镜像) 浏览器能 加载 on page, preventing XSS attacks.
用大白话说
Content Security Policy (CSP) is a security header that lets you control which sources the browser is allowed to load resources from. It is a powerful defense against XSS and data injection attacks.
A CSP header specifies allowed sources for each resource type: script-src (JavaScript), 样式-src (CSS), img-src (images), 字体-src (fonts), and more. For example, script-src 'self' https://cdn.com allows scripts only from your own domain and cdn.com.
A strict CSP like 默认-src 'self' blocks all external resources and inline scripts by default — a strong security baseline. CSP violations are reported to a specified URL for monitoring. Implementing CSP is one of the most effective XSS mitigations available.
示例
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; img-src 'self' data: https:; style-src 'self' 'unsafe-inline'CSP 那个允许 own-原点 resources 通过默认, scripts 从 self and one CDN, 镜像从 任何 HTTPS 源, and inline 样式.
相关术语
相关资源
相关课程
- Web Security 基础
Web security 包含 CSP.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。