On this page
  1. How does CSP stop XSS?
  2. What do the common directives mean?
  3. How do I roll out CSP without breaking my site?
  4. Where AI coding assistants get this wrong
  5. Checklist
  6. FAQ
    1. Does CSP work in all browsers?
    2. What is the difference between CSP and a WAF?
  7. Related topics
  8. Sources
concept

What Is Content Security Policy (CSP)?

CSP is a header that tells the browser which scripts and resources a page may load, stopping injected code. Learn how to set it up without breaking your site.

Quick answer

  • CSP (Content Security Policy) is a response header that restricts what your page can load — scripts, styles, images, frames.
  • Its main job is stopping XSS: if inline or remote scripts are disallowed, injected script code can’t run.
  • Roll it out in report-only mode first, collect violations, then enforce — a strict policy that breaks your site is worse than none.

How does CSP stop XSS?

XSS works by getting the browser to execute attacker-controlled script. CSP gives the browser an allowlist of what it may execute: script-src ‘self’ means only scripts loaded from your own origin run; everything else — inline event handlers, javascript: URLs, scripts from other domains — is blocked with a console error. An injected