On this page
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