Rolling Out a Content Security Policy Without Breaking Anything

Report-only first, verify, then enforce. How I added a strict CSP to a WordPress site, nonces and all, and proved it works by attacking it.

An open fortress gate on a stone path, with a signpost checking off plan, report, test, tighten and deploy, and a Viking shield leaning on the wall.

A Content Security PolicyCSP Content Security Policy. A header telling the browser which scripts and resources a page may load, so injected code is refused. is one of the most effective defences a website can have, and one of the least used. It’s a single response header that tells the browser which scripts and resources a page is allowed to load. If an attacker manages to inject a script, through a vulnerable plugin, a bad comment filter or a compromised dependency, the browser simply refuses to run it.

So why does almost nobody turn it on? Because a CSP that’s wrong breaks your site, silently, for every visitor. Here’s how I rolled one out on this WordPress site without breaking anything.

Step 1: inventory what actually loads

Before writing a policy, I crawled every page and listed every script, stylesheet, font, image and connection. You can’t allow what you don’t know about. For this site the list was short, by design: the theme’s own files, WordPress core’s scripts, Cloudflare’s small analytics beacon, and a handful of inline scripts WordPress prints itself.

Those inline scripts are the hard part. The easy fix is to allow 'unsafe-inline', which permits every inline script on the page. It also permits an attacker’s. That defeats the point.

Step 2: nonces for the scripts you trust

A noncenonce A number used once. In a Content Security Policy it's a fresh random value per page load that marks which inline scripts the server trusts. is a random value generated fresh for every page load. The server puts it in the CSP header and stamps the same value onto every inline script it trusts. The browser only runs inline scripts carrying the matching nonce. An attacker’s injected script can’t know a value that didn’t exist until this request, so it’s refused.

WordPress makes this possible. Since version 5.7, scripts it prints go through filters (wp_inline_script_attributes and wp_script_attributes) where a small must-use pluginmust-use plugin A WordPress plugin that loads automatically and can't be switched off from the admin screens, which makes it a good home for security settings. can add the nonce. The policy ended up looking like this:

default-src 'self';
script-src 'self' 'nonce-<random>' https://static.cloudflareinsights.com;
style-src 'self' 'unsafe-inline';
img-src 'self' data:; font-src 'self' data:;
connect-src 'self' https://cloudflareinsights.com;
frame-src 'none'; frame-ancestors 'self'; object-src 'none';
base-uri 'self'; form-action 'self'; upgrade-insecure-requests

Inline styles are still allowed. Blocking them would mean rewriting a lot of WordPress, and injected styles are a much smaller risk than injected scripts. Pick the battles that matter.

I also apply the policy to visitors only. When I’m logged in, the admin bar and editor previews behave differently, and the admin area is already behind its own gate.

Step 3: report-only first

Browsers support Content-Security-Policy-Report-Only, which evaluates the policy and logs every violation to the console without blocking anything. I shipped that first. Then I went through every page: checking that each inline script carried the nonce, that nothing loaded from an unexpected host, and that every interactive feature (the terminal, the flag hunt, the easter eggs) ran with zero violations.

It came back clean. If it hadn’t, I’d have fixed the policy while visitors noticed nothing.

On a busier site, checking pages by hand doesn’t scale. CSP can also send violation reports to an endpoint you choose (the report-to directive), so real visitors’ browsers tell you what would have broken. For a small personal site, the console was enough.

Step 4: enforce, then attack yourself

Switching to enforcement was a one-line change. Then I tested it the only way that counts: I simulated an injection by adding a new <script> element to a live page, the way a cross-site scriptingXSS Cross-site scripting. Getting a site to run an attacker's script in a visitor's browser. A Content Security Policy is one strong defence. bug would, and watched the browser refuse to run it while the site’s own features kept working.

A control you haven’t tested is a control you hope works.

Check your own site in five minutes

Curious whether your site sends a policy at all? From any terminal:

curl -sI https://your-site.example | grep -i content-security-policy

No output means no policy. If you add one in report-only mode, open your browser’s developer tools on a few pages and watch the console. Violations show up as messages saying the page refused, or would have refused, to load or run something because it violates a directive, along with the directive and the blocked source. Each one is a line item to allow, fix or remove.

The things that most often break when you enforce:

  • Inline event handlers. Attributes like onclick="..." aren’t covered by nonces. Move them into a script file with addEventListener.
  • Embeds. Videos, maps and social widgets load in frames from other hosts. With frame-src 'none', they simply disappear until you allow their host.
  • Plugins that print their own scripts. Some bypass WordPress’s script functions entirely, so they never get a nonce. Those are worth questioning anyway.
  • Analytics and tag managers. They often load further scripts from hosts you didn’t list. Report-only mode will show you exactly which.

What it costs from here

Some friction. Embedding a YouTube video or adding a third-party widget now means adding its host to the policy first. I think that’s a feature: every new external dependency becomes a deliberate decision instead of something that happens by accident.

If you run a WordPress site, try report-only mode this week. You’ll learn more about what your site actually loads in ten minutes than you have in years.

ᛟ ᛟ ᛟ