A Content-Security-Policy tells the browser which scripts, styles and other resources a page may use — so a script an attacker manages to inject simply does not run. It is the most valuable security header and the one that needs the most care: start in report-only mode, read what it would block, then enforce.
| Defined in | CSP Level 3 (W3C working draft, September 2026) |
| Stops | Injected scripts (XSS), framing, insecure loads |
| Start with | Content-Security-Policy-Report-Only |
| Reporting | report-to with a Reporting-Endpoints header |
| Browser support | Every current browser |
Check Content-Security-Policy on your site
Enter a domain: we fetch its homepage and read every security header, this one first.
What it does
A policy is a list of directives, each saying where one kind of resource may come from: script-src for scripts, style-src for styles, img-src for images, and so on, with default-src as the fallback for the ones you leave out. Anything the policy does not allow is blocked, and the browser can report each block to you.
Google’s recommended form is a strict CSP: instead of listing hosts — which can be bypassed — each response carries a fresh random nonce, and only scripts carrying that nonce run. Pages that are cached or static can list hashes of their inline scripts instead. A CSP is defence in depth, not a replacement for fixing XSS bugs.
Directives and values
| Directive or value | What it does |
|---|---|
default-src | The fallback for the resource types you do not set. There is no inheritance: once script-src is set, default-src no longer applies to scripts. |
script-src | Where scripts may come from, and whether inline scripts and eval() run. |
style-src / img-src / connect-src | Stylesheets; images; the addresses fetch(), XHR and WebSockets may reach. |
object-src 'none' | Blocks plugin content (<object>, <embed>). Recommended in every policy. |
base-uri 'none' or 'self' | Stops an injected <base> tag from redirecting your relative script URLs. Not covered by default-src. |
frame-ancestors | Who may put the page in a frame — the modern form of X-Frame-Options. Not covered by default-src, and ignored in a <meta> tag. |
form-action | Where forms may submit to. Not covered by default-src. |
'nonce-…' / 'sha256-…' | Allow one script or style by a random value generated per response, or by the hash of its content. |
'strict-dynamic' | Scripts allowed by nonce or hash may load further scripts; host lists and ‘unsafe-inline’ are then ignored. |
'unsafe-inline' / 'unsafe-eval' | Allow inline scripts or eval(). MDN: they defeat much of the purpose of having a CSP. |
report-to | Sends violation reports to an endpoint named in the Reporting-Endpoints header. It replaces the deprecated report-uri. |
What to send
Content-Security-Policy: script-src 'nonce-{RANDOM}' 'strict-dynamic'; object-src 'none'; base-uri 'none';
That is web.dev’s strict CSP; {RANDOM} is a new random value — 128 bits or more, base64 — generated for every response and added to each <script nonce=”…”> tag. For a static or cached site, list hashes of the inline scripts instead: script-src ‘sha256-…’ ‘strict-dynamic’. If nonces or hashes are out of reach, OWASP’s basic policy is a start: default-src ‘self’; frame-ancestors ‘self’; form-action ‘self’.
Send it first as Content-Security-Policy-Report-Only with report-to, so nothing is blocked while you read the reports; switch the header name to enforce once they are clean.
How to set it
A nonce must be new on every response, so a nonce-based policy is sent by the application, not written into the server configuration. What the server can send is a starting policy in report-only mode — add report-to and a Reporting-Endpoints header naming the address that collects the reports.
Apache
Header always set Content-Security-Policy-Report-Only "default-src 'self'; frame-ancestors 'self'; form-action 'self'"
In the virtual host or .htaccess; needs mod_headers (and AllowOverride FileInfo for .htaccess). always also covers error pages and redirects, which plain Header set skips.
nginx
add_header Content-Security-Policy-Report-Only "default-src 'self'; frame-ancestors 'self'; form-action 'self'" always;
In the server block. A location with any add_header of its own drops every add_header from the server block — repeat them there, or from nginx 1.29.3 use add_header_inherit merge;.
IIS (web.config)
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Content-Security-Policy-Report-Only" value="default-src 'self'; frame-ancestors 'self'; form-action 'self'" />
</customHeaders>
</httpProtocol>
</system.webServer>
Caddy
header Content-Security-Policy-Report-Only "default-src 'self'; frame-ancestors 'self'; form-action 'self'"
Cloudflare
Rules → Overview → Create rule → Response Header Transform Rule
Set static — header name: Content-Security-Policy-Report-Only — value: default-src 'self'; frame-ancestors 'self'; form-action 'self'
On every plan, for records that are proxied through Cloudflare. Set replaces the header if your server already sends one.
WordPress
add_action( 'send_headers', function () {
header( "Content-Security-Policy-Report-Only: default-src 'self'; frame-ancestors 'self'; form-action 'self'" );
} );
In a small plugin or the theme’s functions.php. PHP only runs for pages WordPress builds, so images, scripts and other static files do not get the header — set it at the server or CDN to cover everything.
Common mistakes
- ‘unsafe-inline’ or ‘unsafe-eval’ in script-src, which lets most injected scripts through.
- Relying on a list of allowed hosts: a script-hosting host on the list is enough to bypass it.
- Leaving out object-src and base-uri — default-src does not cover base-uri, form-action or frame-ancestors.
- Reusing one nonce, or middleware that stamps the nonce on every script tag — injected scripts then get it too.
- Sending a second CSP header to loosen the first: every policy must pass, so a second one can only restrict further.
- Putting frame-ancestors, sandbox or report-uri in a <meta> tag, where they are ignored.
What we recommend
Aim for a strict, nonce- or hash-based policy with object-src ‘none’ and base-uri ‘none’, rolled out in report-only mode first. If the site is a WordPress site with plugins you do not control, start with report-only and OWASP’s basic policy, and tighten as the reports allow.
Questions
What is Content-Security-Policy-Report-Only?
The same policy, not enforced: the browser reports what it would have blocked and blocks nothing. It is how to test a policy on a live site. It needs report-to for the reports to be sent, and it cannot be set in a <meta> tag.
Can I set a CSP in a <meta> tag?
Partly. <meta http-equiv=”Content-Security-Policy”> works, but frame-ancestors, sandbox, report-uri and report-only mode are ignored there, and it covers only what comes after it. Prefer the header.
Sources
Checked on 25 September 2026 against these official sources:
- CSP3 — w3.org
- Content Security Policy (CSP) — developer.mozilla.org
- Content-Security-Policy (CSP) header — developer.mozilla.org
- Mitigate cross-site scripting (XSS) with a strict Content Security Policy (CSP) — web.dev
- Content Security Policy — cheatsheetseries.owasp.org