Rolling out a Content Security Policy without breaking your site
The safe way to add a content security policy is to deploy it first in Content-Security-Policy-Report-Only mode, collect the violation reports your real pages generate, tighten the directives until the reports go quiet, and only then switch to the enforcing Content-Security-Policy header. That order lets you see what your policy would block before it blocks anything.
A Content Security Policy (CSP) tells the browser which sources of scripts, styles, images and other content a page is allowed to load. Done well, it is one of the strongest defences against cross-site scripting and content injection. Done in a rush, it is the header most likely to break a working site, which is exactly why the report-first approach matters.
This guide walks through a careful rollout for a typical UK small-business site, with a starter policy and the three pitfalls that catch people out: inline scripts, analytics tags and third-party widgets.
Step one: start in report-only mode
Browsers support two headers. Content-Security-Policy enforces the rules and blocks anything that breaks them. Content-Security-Policy-Report-Only applies the exact same rules but blocks nothing: it only records what it would have blocked. Always begin with the report-only version on a live site.
Add a report-only header carrying your draft policy, then browse the site the way a real visitor would: the homepage, key landing pages, a contact form, the checkout if you have one, and the logged-in admin area. Each violation is logged in the browser console, and if you set a reporting endpoint, sent there too. You are building a picture of every source your site genuinely uses before you commit to a rule.
- Content-Security-Policy: enforces and blocks. Use this last.
- Content-Security-Policy-Report-Only: observes and reports only. Use this first.
- Test the pages people actually use, not just the homepage.
A sensible starter policy
A restrictive starting point defaults everything to your own domain, then opens up only what the reports prove you need. A reasonable draft to begin with, written as a single header value, looks like this:
default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'self'; connect-src 'self'; frame-ancestors 'self'; base-uri 'self'; form-action 'self'
Run that in report-only mode and the reports will quickly surface the third-party domains you had forgotten about: a font host, an analytics tag, an embedded map or video, a chat widget. Add each legitimate source to the right directive rather than loosening the whole policy. The frame-ancestors 'self' rule also gives you clickjacking protection, overlapping with X-Frame-Options.
The three pitfalls that break sites
Most CSP headaches come down to three things. Knowing them before you start means the reports will not catch you off guard.
- Inline scripts and styles: a strict script-src blocks inline script blocks and onclick handlers. The clean fix is to move that code into external files. Avoid reaching for 'unsafe-inline', which removes most of the protection; if you must allow a specific inline block, use a nonce or a hash instead.
- Third-party widgets: analytics, tag managers, chat tools, embedded maps and video players each load from their own domains and sometimes inject further scripts. Allow only the exact domains the reports show, and re-test after adding any new widget.
- Over-broad rules: a wildcard like script-src * or a blanket 'unsafe-inline' will silence the reports but defeats the point of having a policy. Tighten, do not surrender.
Free, instant, no account
See your site's security grade in seconds.
Free, instant, and no account needed to see your grade.
Scan my site freePlain-English A to E grade in about 10 seconds. No account needed to see it.
Tighten the policy, then enforce it
Once you have browsed every important page and the report-only violations have dropped to nothing, or only to things you are happy to block, you are ready. Copy the policy from the Content-Security-Policy-Report-Only header into the enforcing Content-Security-Policy header and deploy.
Keep an eye on it for a week or two. A new marketing tag, a new embedded form or a theme update can introduce a source your policy does not allow, which now shows up as a genuinely broken element. Treat the CSP as something you maintain, not a one-off task. Some teams keep a permanent report-only header running alongside the enforcing one to catch new violations early.
How EB Security checks your CSP
The free EB Security scan checks whether your site sends a Content-Security-Policy header at all, alongside the other hardening headers it looks at (HSTS, X-Frame-Options, X-Content-Type-Options, Referrer-Policy and Permissions-Policy). It runs externally in about ten seconds and reads only what your site already serves publicly.
Be clear on the limit: the scan confirms the header is present, not that your policy is well written. A weak policy full of wildcards and 'unsafe-inline' can still pass the presence check while offering little real protection. The scan is a fast first signal that the header exists; judging whether the directives are actually tight is a job for you, your developer or your host. A missing CSP only nudges your grade rather than sinking it, so treat it as a hardening win to work towards, not an emergency.
Roll out a CSP in report-only mode first, read the violation reports, tighten the directives until they go quiet, then switch to the enforcing header. That sequence gives you strong protection against script injection without breaking the pages your visitors rely on.