What Is a Content Security Policy (CSP)?
A Content Security Policy is an HTTP header that lets a site declare which sources of content (scripts, styles, images, fonts) the browser is allowed to load...
Published November 16, 2026
A Content Security Policy (CSP) is an HTTP header that tells the browser which sources of scripts, styles, images and frames are allowed on your page. It greatly reduces the damage of XSS.
The concept
Directives like script-src and img-src list allowed origins. Anything not listed is blocked, so injected inline scripts will not run.
Roll it out with Content-Security-Policy-Report-Only first, collect reports, then enforce. Nonces or hashes allow specific inline scripts safely.
Allowed vs blocked
script-src 'self' https://cdn.example.com
<script src="/app.js"> ✓ allowed
<script src="https://evil.com/x"> ✗ blocked
<script>steal()</script> ✗ blocked (inline)
Practical examples
Basic policy
Content-Security-Policy: default-src 'self'; img-src 'self' data:; script-src 'self'; object-src 'none'; frame-ancestors 'none'Nonce for an inline script
<script nonce="r4nd0m">init()</script>
<!-- header: script-src 'nonce-r4nd0m' -->Report-only rollout
Content-Security-Policy-Report-Only: default-src 'self'; report-uri /csp-reportCommon mistakes
- Using 'unsafe-inline' and defeating the point.
- Enforcing before testing in report-only mode.
- Forgetting third-party scripts (analytics, ads) in the allow list.
Key takeaways
- Allow-list of content sources.
- Blocks injected scripts.
- Roll out with report-only first.
FAQ
Does CSP replace escaping?
No, it is defence in depth.
How do I set it in a meta tag?
Possible, but headers are preferred and support more directives.