Check 02 · CSP
Content Security Policy
Content-Security-Policy
Content-Security-Policy lists where your pages may load scripts and other resources from. Browsers refuse everything else.
What it does
Most attacks on a page in the browser come down to running a script the site never meant to run: injected through a form field, a comment, a URL parameter or a compromised plugin. A policy tells the browser which scripts are yours, so an injected one is blocked even when the injection itself succeeds.
A policy is a list of directives separated by semicolons. Each directive names a kind of resource (script-src for scripts, object-src for plugins, default-src as the fallback) followed by the sources allowed for it.
Sources you can list
| Source | Allows |
|---|---|
'self' | Your own origin: same scheme, host and port. |
'none' | Nothing at all. |
https://cdn.example.net | One host. Paths and wildcards for subdomains are possible. |
https: | Any host over HTTPS. For scripts, this allows almost anything. |
'nonce-…' | Inline and external scripts carrying the same random value in their nonce attribute. The value must be new on every response. |
'sha256-…' | An inline script whose exact content has this hash. |
'strict-dynamic' | Scripts loaded by an already allowed (nonce or hash) script. Browsers that support it then ignore host and scheme sources. |
'unsafe-inline' | Every inline script and event handler, which is what injected code usually is. Ignored once a nonce or hash is present. |
'unsafe-eval' | Turning strings into code with eval() and similar functions. |
How we score it
| 20 | An enforced policy that restricts scripts and meets all of (a)–(e) below. |
|---|---|
| 10 | An enforced policy that restricts scripts but misses at least one of them. |
| 0 | No enforced policy, or none that restricts scripts (for example only upgrade-insecure-requests). |
| Unable to evaluate | A policy we can't read reliably, unless another readable policy already earns 20. Counts as 0 points. |
- Script sources avoid *, http:, https: and data:.
- No 'unsafe-inline' for scripts, unless a nonce or hash is used.
- No 'unsafe-eval'.
- object-src 'none' (directly, or through default-src 'none').
- base-uri is set.
Scripts are restricted by script-src, or by default-src when there's no script-src. Policies from headers and from a <meta> tag in the page head count; Content-Security-Policy-Report-Only never does. When several policies apply, the strongest is scored. With 'strict-dynamic' plus a nonce or hash, browsers ignore host and scheme sources, so those don't fail (a). A nonce or hash also makes browsers ignore 'unsafe-inline'. We can't read a policy reliably when it contains characters outside plain ASCII, a directive name with invalid characters, a quoted keyword with a missing quote, an unknown quoted keyword, or an empty nonce or hash in a directive we score.
All five checks and the grade scale: How grading works.
Examples
- Content-Security-Policy: default-src 'self'; object-src 'none'; base-uri 'self'
20 / 20 · Strong Scripts are restricted through
default-src, and all five criteria are met. - Content-Security-Policy: script-src 'nonce-R4nd0mV4lue' 'strict-dynamic' https:; object-src 'none'; base-uri 'none'
20 / 20 · Strong A nonce-based policy. With
'strict-dynamic', browsers ignorehttps:, which only remains for very old browsers. - Content-Security-Policy: script-src 'self' 'unsafe-inline'; object-src 'none'; base-uri 'self'
10 / 20 · Partial
'unsafe-inline'without a nonce or hash lets injected inline scripts run (criterion b). - Content-Security-Policy: script-src 'self' https:; object-src 'none'; base-uri 'self'
10 / 20 · Partial
https:allows scripts from any HTTPS host (criterion a). - Content-Security-Policy: default-src 'self'
10 / 20 · Partial A good start, but plugins and
<base>aren't restricted (criteria d and e). - Content-Security-Policy: upgrade-insecure-requests
0 / 20 · Weak A valid policy that says nothing about scripts.
- Content-Security-Policy-Report-Only: default-src 'self'; object-src 'none'; base-uri 'self'
0 / 20 · Missing Report-only policies only report what they would block. Useful while testing, never enforced.
- Content-Security-Policy: script-src 'self; object-src 'none'
– / 20 · Unable to evaluate A missing quote after
'self. We don't guess what was meant.
How to set it
Start from the policy below. It allows scripts, styles and images from your own domain only, blocks plugins, and limits base and framing to your own site.
List every outside host your pages load scripts from (analytics, chat, payments, fonts) in script-src, or move to nonces: generate a fresh random value per response, put it in the header and in each <script nonce="…">, and add 'strict-dynamic'.
Without access to headers, a <meta http-equiv="Content-Security-Policy" content="…"> tag at the top of the <head> counts too. It can't set frame-ancestors or reporting.
Test first. A Content Security Policy can block scripts, styles and embeds your site relies on. Send it first as Content-Security-Policy-Report-Only (same value), check your browser console and reports, adjust, and only then switch to the enforcing header.
Nginx
Put this in the server block. A location block with its own add_header lines doesn’t inherit these, so repeat them there.
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'" always;Apache
In the virtual host or .htaccess. Needs mod_headers (on Debian and Ubuntu: a2enmod headers).
Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'"Caddy
Inside your site block in the Caddyfile.
header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'"Cloudflare
Rules → Create rule → Response Header Transform Rule. Apply to all incoming requests, choose “Set static”, and enter each header below.
Header name: Content-Security-Policy
Value: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'Common mistakes
- Leaving the policy in report-only mode after testing. It never blocks anything there.
- Adding
'unsafe-inline'to make an inline script work. Give that script a nonce or hash, or move it into a file. - Reusing one nonce for every response, for example on a cached page. A nonce only helps when an attacker can't predict it.
- Sending the policy on the homepage only. Injected scripts usually arrive through other pages: site search, comments, account pages.