In short
The Content Security Policy (CSP) is an HTTP header that tells the browser which content sources a page is allowed to load and execute: scripts, styles, images, frames, and connections. When properly configured, it blocks the execution of injected scripts and significantly reduces the impact of an XSS vulnerability. It complements data escaping in the code without replacing it, and is deployed gradually using a test mode that flags blockages without actually applying them.
Essential Directives
| Directive | What it controls |
|---|---|
| default-src | The default value for unspecified resource types |
| script-src | Authorized scripts: origins, nonces, or hashes |
| style-src, img-src, font-src | Style sheets, images, and fonts |
| connect-src | Destinations for fetch, XHR, and WebSocket requests |
| frame-ancestors | Sites authorized to display the page in a frame (clickjacking protection) |
| object-src | The plugins (object, embed); set to 'none' |
| base-uri | The addresses authorized in the `base` tag, which an attacker can hijack to load relative scripts from their server |
| form-action | Authorized destinations for form submissions |
| report-to, report-uri | The address to which the browser sends violation reports |
A strict policy rather than a list of domains
A CSP based on a list of allowed domains can often be bypassed: a single overly permissive domain—such as a public CDN hosting exploitable libraries—is enough for an attacker. OWASP recommends a “strict” CSP based on nonces: each response generates a random value, which is added to legitimate script tags, and only those scripts execute.
Example of a strict policy: script-src 'nonce-{random value}' 'strict-dynamic'; object-src 'none'; base-uri 'none'. The nonce value must change every time the page loads.
The 'unsafe-inline' keyword in script-src allows any inline script, including those injected by an attacker: it nullifies most of the protection against XSS. 'unsafe-eval' also weakens the policy.
Deploying a CSP Without Breaking the Site
- Start with the Content-Security-Policy-Report-Only header: the browser reports violations without blocking anything.
- Analyze the reports, adjust the policy (online scripts to be moved, third-party services to be authorized), and then switch to blocking mode.
- Send the CSP in an HTTP header: certain directives, such as `frame-ancestors`, do not work in a `meta` tag.
- Monitor reports after deployment to identify regressions and injection attempts.
Frequently asked questions
Is a CSP sufficient to prevent XSS?
No. It often blocks the execution of an injected script and limits what it can do, but the fundamental fix remains data escaping and the absence of raw HTML insertion. CSP is a second line of defense.
Can a CSP be defined in a meta tag?
Yes, using an `http-equiv` meta tag, but with limitations: the `frame-ancestors`, `report-uri`, and `sandbox` directives are not supported. The HTTP header remains the recommended method.
Sources
Find out where an attacker would get in.
Our AI agents actually attack your scope, fully automated, and hand you the proof of every vulnerability.
