← Back to the blog

Security • 2026-08-27 • 3 min read

A Content Security Policy You Can Actually Deploy

Most CSP headers in the wild contain unsafe-inline, which removes the reason for having one. How to ship a strict policy on a React app without a week of broken pages.

A Content Security Policy You Can Actually Deploy — Security

A Content Security Policy is the control that turns a cross-site scripting bug from a compromise into a blocked console message. Most deployed policies contain unsafe-inline, which switches that off while leaving the header in place to reassure whoever asked for it.

Nonces are the way in

A strict policy refuses inline scripts. A real application has some — the bootstrap payload a server-rendered page hands to the client, for one. Nonces solve this: the server generates a random value per response, puts it in the header and on each script tag it is willing to vouch for.

// text
Content-Security-Policy:
  default-src 'self';
  script-src 'self' 'nonce-{RANDOM}' 'strict-dynamic';
  style-src 'self' 'nonce-{RANDOM}';
  img-src 'self' https://cdn.example.com data:;
  connect-src 'self';
  frame-ancestors 'none';
  base-uri 'none';
  object-src 'none';
  form-action 'self';
  report-uri /csp-report

The nonce must be per response and unpredictable — 128 bits from a CSP-safe random source. A nonce reused across responses, or derived from the session, is a nonce an attacker can obtain and reuse.

'strict-dynamic' is what makes this workable with a bundler: a script you trusted with a nonce may load further scripts, which is exactly what a module loader does, without you enumerating every chunk filename.

The four directives people forget

  • frame-ancestors 'none' — this, not X-Frame-Options, is the modern clickjacking control
  • base-uri 'none' — without it, an injected <base> tag redirects every relative script URL
  • object-src 'none' — legacy plugin content, pure attack surface
  • form-action 'self' — stops an injected form from posting credentials elsewhere

Rolling it out

Report-only first. Content-Security-Policy-Report-Only sends violation reports and blocks nothing, so you learn what breaks without breaking it. Run it for a week of real traffic, including the pages only finance opens on the last day of the month.

Expect noise. Browser extensions generate violations constantly and you cannot fix those; filter by source-file before the reports reach anyone. When the remaining reports are quiet, swap the header name.

Keep it in code, not in a config file

Our policy is assembled in middleware, with the one dynamic part — the media origin — read from the same place the application reads it. The bucket URL appears in exactly one place, so moving the CDN cannot leave the policy blocking the images it is meant to allow. A policy maintained separately from the thing it describes drifts, and the failure mode is a blank page in production.

Measure it afterwards

Keep the report endpoint after enforcement begins. A sudden rise in violations on a page is a strong signal: either a deploy introduced an inline handler, or something is injecting into your page. Both are worth knowing within minutes.

Read next

COOKIES // MEASUREMENT

We count page views through Google Tag Manager. Say yes and it may store a cookie in your browser; say no and it still counts the visit, without storing anything and without an identifier. Nothing here is used for advertising.

Cookie Policy — you can change this at any time from the footer of any page.