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.

How to fix5 min read

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 free

Plain-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.

Common questions

What is the difference between Content-Security-Policy and Content-Security-Policy-Report-Only?
They apply identical rules, but the report-only header blocks nothing. It only records what it would have blocked, which lets you test a draft policy on a live site safely. Use report-only to find problems, then switch to the enforcing Content-Security-Policy header once the reports are clean.
Will adding a CSP break my website?
It can if you enforce it straight away, because a strict policy may block inline scripts, analytics tags or third-party widgets your site relies on. Starting in report-only mode avoids this: you see every would-be block as a report first and fix the policy before anything actually breaks for visitors.
How do I handle inline scripts when I add a content security policy?
The cleanest fix is to move inline scripts and event handlers into external .js files that your script-src allows. If you genuinely cannot, use a per-request nonce or a content hash to permit specific inline blocks. Avoid 'unsafe-inline', as it removes most of the protection a CSP gives you.
Does the EB Security scanner check whether my CSP is any good?
It checks whether a Content-Security-Policy header is present, not whether the policy itself is strong. A weak policy full of wildcards can still pass the presence check. Use the scan as a quick confirmation the header exists, then review the directives yourself or with a developer to judge their quality.
Where do I add the CSP header?
It is an HTTP response header, so set it on the server or application that serves your pages: a web server config such as Apache or Nginx, a WordPress plugin or functions snippet, or a CDN edge rule. Apply it site-wide so every page is covered, and update it whenever you add a new third-party tool.

How safe is your website?

A free A to E grade in about 10 seconds. No account needed.

Check my site