Skip to content
WebInsights

CSP without breaking the site: a policy you have tested

You have a CSP when the browser refuses what you have not allowed — and the site still works.

Content-Security-Policy is an allowlist of what the page may load. Without report-only and a nonce you own, the header is either theatre — or a broken site you call security.

13 min read
Stylised browser window with performance graphs and a layer that only lets approved scripts in — not a header you copied from a blog post.

Web & operations

If you are searching for content security policy, or for CSP header, you are typically in one place: an audit said 'missing CSP', a plugin set `default-src *` and called it green, or you pasted a header from a blog post and now fonts, analytics and Next.js hydration all fail. The security-basics post is the door: HTTPS, updates, admin, secrets, the boring stack in an afternoon. The cookie banner is consent: when scripts may run because the law said yes. Preview secrets are the keys in the shared environment. This one is the allowlist. CSP says: these sources may run script, style, image, connect. If you allow everything, you do not have a policy. If you allow too little without having seen the report, you have a broken site you call hardening.

The browser enforces CSP before your React code gets a chance to apologise. An inline handler, a plugin that injects `<script>`, a font from a CDN you forgot, a websocket to preview — they die quietly in the console, or they break checkout. An honest CSP is boring: report-only first, a nonce or hash on what you yourself ship, third parties you have named, and only then enforce when the report is empty of the violations you do not want. Without that order the header is a badge or an outage.

TL;DR

The main takeaways

  • CSP is an allowlist the browser enforces. `unsafe-inline` and `*` are not a policy — they are writing the header and letting it not care.
  • Report-only first. Enforce when you have read the violations, not when an audit asked for the header yesterday.
  • A nonce or hash on your own scripts beats a copied policy that breaks hydration, fonts and CMS preview.
  • Third parties (analytics, chat, CMP) are named sources — or they are out. A wildcard 'so it works' is theatre.
  • The security-basics post is the door. The cookie banner is consent. Preview secrets are the keys. This one is the allowlist.
  • You have a CSP when a script you have not allowed does not run — and when the site still renders. Not when the header exists.

Why this matters at all

A marketing site that sets `Content-Security-Policy: default-src *` because Lighthouse or a security plugin screamed does not have closed XSS. It has put up a sign. A site that sets `script-src 'self'` without a nonce, on a Next.js app that inlines hydration data, has closed its own render. Both are common. Both come from the header becoming a checkbox, not a contract.

Teams do it because the first copied policy 'worked in Chrome on the homepage'. The WordPress plugin inserted a string. Cloudflare set a managed header. Nobody ran report-only. Nobody opened the contact form, checkout, CMS preview, or the page with the video embed. Monday the header is live. Analytics is empty. Fonts are system. The chat widget is a white hole. Support calls it 'the site is down'. It is CSP you had not tested.

The damage is concrete. A script you trusted that does not run — or a script you did not trust that still runs, because `unsafe-inline` and `unsafe-eval` sat in the string 'so React works'. XSS via a compromised plugin survives a header that allows inline. SEO dies if CSS and images are refused: layout shifts, LCP jumps, the crawl sees something other than the user. The security-basics post said headers are what the browser can refuse for you. This one is that the refusal must be the one you chose — not the one you pasted.

We write this because 'we have CSP' has become a checkbox you tick when the response header exists. An honest setup is boring: report-only, read the report, nonce on your bundles, name third parties, enforce, keep the report running. Without that you have a string. You do not have a policy the browser can use.

What a CSP actually is — and what a security header is not

A CSP is an allowlist per directive: `script-src`, `style-src`, `img-src`, `font-src`, `connect-src`, `frame-src`, `frame-ancestors`. Each is an answer to 'who may'. The default is to refuse what you have not named, once you have set `default-src`. The difference is not whether the header parses. The difference is whether the list matches what the page actually loads, and whether it refuses what you do not want loaded.

It is not the same as the security-basics afternoon check. HTTPS, HSTS, updates, closed admin, keys out of git — that is the door. CSP is what may run when the door is open for an honest user. You can have HSTS and still XSS via an inline script in a theme. You can have a tight CSP and still an open /wp-admin. Two jobs. One does not replace the other.

It is also not the same as the cookie banner. The banner is consent before marketing scripts. CSP is whether the browser will run the script at all, consent or not. A CMP that injects inline to read the choice breaks a tight `script-src` — or you open `unsafe-inline` and have bought a banner that opened the XSS window again. The cookie post is no as one click. This one is that no and yes both have to be able to run inside the list you wrote.

And it is not 'a copied string from OWASP, then we are done'. A policy written for a static HTML site breaks Next.js. A policy written for an SPA breaks WordPress preview. The preview-secrets post is that preview does not get production keys. This one is that preview must also not get a policy you have only tested on production's homepage — and the reverse: production must not inherit `unsafe-eval` because preview tooling needed it.

What typically goes wrong when someone says CSP is set

The first failure is enforcing before you have seen the report. You set the header on Friday because a scan said A+. Monday checkout is dead, because Stripe.js or a 3-D Secure iframe was not on the list. Report-only (`Content-Security-Policy-Report-Only`) sends the violations to an endpoint you read. Enforce is the second step. The security-basics post is measuring headers. This one is measuring the violations before you refuse.

The second failure is `unsafe-inline` and `unsafe-eval` 'so the framework works'. Next.js can run with a nonce in middleware. WordPress can run with hashes on the few inline blocks you yourself own — or with a plugin you have actually read. Opening inline to skip the work is keeping the XSS window and keeping the header. The audit sees a CSP. The attacker sees a theme that can still inject script.

The third failure is third parties as a wildcard. `script-src https:` or `*.google.com` because analytics 'probably lives there'. CMP, chat, video, tag manager, A/B tool — each is a name you must be able to point at. Tag Manager with `unsafe-inline` is moving XSS into a container marketing owns. The cookie banner says when they may run. CSP says whether they can at all. Name them. Or drop the tool. A wildcard is theatre.

The fourth failure is forgetting `connect-src`, fonts, frames and the report endpoint. Script may run, but fetch to the API is refused. Fonts come from a CDN you did not list — layout shifts, and the Core Web Vitals post becomes your Monday. An embed in `frame-src` is refused, and you open `*` to 'fix video'. Report-URI points at an endpoint you do not have, so you never see violations. A policy without eyes is a policy you do not know is working.

A tested allowlist vs. a header you are hoping for

The honest CSP

Report-only, nonce on your bundles, named third parties, enforce when the report is quiet.

  • The browser refuses script you have not allowed — and the site still renders
  • No unsafe-inline as a habit, no wildcard 'so it works'
  • A report endpoint you read, also after enforce
  • Is boring to maintain, because every new script is a line you choose

The hoped-for header

Copied string, unsafe-inline, default-src *, and a scan that went green.

  • The homepage worked in Chrome; checkout, fonts and CMS preview did not
  • Third parties as https: or a star you never review
  • No report, or a report you never open
  • Is a badge you call security — or a broken site you call an accident
csp-report-then-enforce.tsts
// Right: report-only, nonce on your own scripts, named third party
const csp = [
  "default-src 'self'",
  "script-src 'self' 'nonce-abc123' https://js.stripe.com",
  "connect-src 'self' https://api.stripe.com",
  "report-uri /csp-report",
].join("; ");
// Wrong: copied header that either allows everything or breaks hydration
// Content-Security-Policy: default-src *; script-src 'unsafe-inline' 'unsafe-eval'

How to make CSP an allowlist — before you enforce it

Without this order you get another header you call security.

  1. 01

    Set report-only, and read the violations for a week before you refuse anything

    The same policy, a different header name. An endpoint you own. If you cannot point at the report, you have no basis for enforce. The security-basics post is measuring. This one is measuring what the browser would have refused.

  2. 02

    Give your own scripts a nonce or a hash you rotate per response

    Next.js middleware can set a nonce and pass it into the document. WordPress: the inline blocks you yourself own, hashes — the rest out. `unsafe-inline` is not a step on the way. It is stopping.

  3. 03

    Name each third party, or drop it

    Analytics, CMP, chat, embeds, payment. Each host on `script-src` and `connect-src` and `frame-src` as needed. The cookie banner is when. This one is whether. A Tag Manager with a free hand is outsourcing the allowlist to the next pixel someone pastes in.

  4. 04

    Test the homepage, a form, a preview, an embed and a payment flow

    Not only the homepage. Hydration, fonts, images, websockets, 3-D Secure. What breaks is what you did not open. The preview-secrets post is the keys. This one is that preview's policy must not be looser 'so it is easy' if preview runs the same script surface.

  5. 05

    Enforce, and keep the report running

    When the violations are only what you have chosen. Then switch to `Content-Security-Policy`. Leave report-uri in place. New scripts arrive. A policy without eyes rots into either `*` or a break you discover in support.

“You have a CSP when the browser refuses what you have not allowed — and the site still works.”

Next.js, WordPress, third parties and frames — the same words, four jobs

Next.js is the job that dies on inline hydration and on CSS-in-JS if you copied a static policy. Nonce in middleware, `strict-dynamic` if you understand it, hashes on what you can pin. App Router and Pages Router have different inline surfaces. Test both if you have both. A policy that worked on a marketing site in `app/` does not automatically survive a dashboard. The security-basics post said Next.js breaks on secrets. Here it breaks on the header you did not have for the framework.

WordPress is the job themes and plugins love to inject inline into. A security plugin that sets CSP and a page-builder plugin that writes `onclick` is an argument the site loses. Either you own those inline blocks (hash), or you remove the plugin, or you do not have a CSP. Letting the page builder win with `unsafe-inline` is keeping the header name. Admin and preview must be in the test. A policy that is only set on the cached homepage is not set.

Third parties are the job the cookie banner and CSP share. The banner turns scripts off until yes. CSP turns off scripts that are not on the list, even after yes. Consent to a host you have not listed is a yes to something the browser refuses — and empty analytics you debug for three days. List the host, or drop the consent. The two lists must be explainable as the same set of tools.

Frames and `frame-ancestors` are the job you mix up with `X-Frame-Options`. `frame-src` is what you may embed. `frame-ancestors` is who may embed you. Clickjacking is the second. Video is the first. Opening `frame-src *` because one YouTube embed failed is allowing any iframe. Name it. The security-basics post mentioned open redirects. This one is the iframe you had not thought of as a door.

What you should typically do in the situation you are in

The left is what you typically say. The right is the next step we typically recommend.

  • Situation

    A scan has said 'missing CSP', and you want to set a header today

    Next step

    Report-only first. Enforce is the second step. A copied string is a break or theatre

  • Situation

    The site broke after you set script-src 'self' without a nonce

    Next step

    Nonce or hash on your bundles. Unsafe-inline is not the fix. It is opening the window again

  • Situation

    Analytics, chat or checkout died, and you are considering default-src *

    Next step

    Name the host on the right directive. A star is deleting the policy and keeping the name

  • Situation

    WordPress preview or Next hydration only works with unsafe-eval

    Next step

    The policy does not match the framework. Fix the integration. Eval in production is not a compromise you should win

  • Situation

    You have a CSP but no report, and you do not know if it refuses anything

    Next step

    Set report-uri. A policy without eyes rots. The security-basics post is measuring headers. This one is measuring violations

Checklist before you call it a CSP

If you are missing several of these, the sentence is we have CSP — the setup is a header you are hoping for.

  • Report-only has run, and you have read the violations, before you enforced
  • Your own scripts have a nonce or hash — not unsafe-inline as a habit
  • Each third party is named on the directives it actually uses
  • Homepage, form, preview, embed and payment flow render with the policy on
  • Report-uri still runs after enforce, so new scripts do not land quietly
  • The security-basics post is the door. The cookie banner is consent. Preview secrets are the keys. This one is the allowlist

Questions we get again and again

  • Is this the same as website security, the cookie banner or preview secrets?

    No. The security-basics post is the door: HTTPS, updates, admin, secrets, the boring stack. The cookie banner is consent: when marketing scripts may run. Preview secrets are that the shared environment does not get production keys. This one is the allowlist: what the browser may load when the page runs. You can have HSTS and still XSS via inline. You can have an honest banner and a CSP that refuses what the user said yes to. Four jobs. One does not replace the others.

  • May we start with unsafe-inline so the site does not break?

    As an enforce policy: no. As a temporary report-only window you look at to see where inlines sit: that is a mirror, not a destination. The destination is a nonce or hash on what you ship, and named third parties. Unsafe-inline in production is keeping the XSS window. The header becomes a name. The audit goes green. The attack does not change.

  • Is Content-Security-Policy-Report-Only enough forever?

    No. It is the mirror. It refuses nothing. Useful until you have read the violations and fixed the site. Then enforce, and keep the report. A site that runs report-only for years because 'we do not dare' has the same attack surface as before the header. The security-basics post said a plugin is not a threat model. A report you never act on is the same.

  • What about Cloudflare, Vercel headers and a WordPress plugin at once?

    One policy. Several layers that each set CSP are an argument — the strictest typically wins, and you do not know which. Choose the source: platform header, middleware, or plugin — not all three. The security-basics post said measure headers. Curl the response. Read which CSP actually lands. If you cannot point at the one string, you do not have a policy. You have three.

  • Does CSP break SEO if it refuses CSS or images?

    Yes, if you enforce a list that does not match the page. The crawl and the user should see the same thing. A policy that refuses fonts and hero images gives layout shift and an LCP you had not thought of as a security problem. Report-only shows it before the index does. The image-pipeline post is the images. This one is that the list must know the hosts the images actually come from.

If CSP is a header you call hardening

Let us separate a boring allowlist from a copied string.

Report-only and a nonce beat a plugin that sets stars — or a site that broke on Monday.

Why we wrote this

This is how we think — and it's what we build.

Our insights are about the work we actually do. If this hit something you're working on, there's a concrete service that lines up.