Content Security Policy: Nonces, Hashes and Report-Only Rollouts

Content Security Policy: Nonces, Hashes and Report-Only Rollouts

Most teams add a Content Security Policy header once, copy it from a tutorial, and then forget about it for two years. That is a shame, because a well-written policy is one of the cheapest defences against cross-site scripting, and getting one to work with a real application is less painful than the internet suggests. The sticking point is almost always inline scripts, and that is where nonces, hashes and a report-only rollout come in.

Why inline scripts cause so much trouble

The quickest way to get a policy working is to add unsafe-inline to script-src and move on with your day. It also, more or less, switches the protection off. Once unsafe-inline is there, the browser has no way to tell your carefully written bootstrap snippet apart from the code an attacker managed to inject into a comment field. Nonces and hashes exist precisely to solve that: they let you allow specific inline scripts without opening the door to everything else.

Nonces: one ticket per response

A nonce is a random value generated for each HTTP response. You put it in the policy, add the same value as an attribute on your script tags, and the browser only runs scripts carrying the matching ticket. The theory is simple; the practice has traps. It is also worth checking view the collection before you decide.

  • Use a cryptographically secure generator and at least 128 bits of randomness. A guessable nonce is worse than none, because it looks like protection.
  • Generate it per request, not per deployment. A nonce that repeats across thousands of responses is a nonce an attacker can copy.
  • Watch your caching. If a CDN or full-page cache serves the same HTML to everyone, the nonce stops being unique and the scheme quietly degrades.
  • Thread it through your templates properly. The day someone adds an inline script and forgets the attribute, it works in report-only and breaks in enforce.

Hashes: for scripts that never change

A hash is a base64 digest of the exact script contents, typically SHA-256. You compute it, paste it into script-src, and the browser compares what it finds in the page against the list. There is no per-request work, which makes hashes a good fit for static pages and for small, stable snippets such as an analytics bootstrapper or a pre-rendered configuration object.

They are brittle in a different way. A changed whitespace character, a swapped quote, or a templating engine that reformats your output produces a different hash and the script simply stops running. That is fine when your build step computes hashes automatically. It is miserable when a human maintains them by hand, so automate or avoid.

Choosing between them

  • Pages rendered per request: nonces, passed through the template layer.
  • Static content or anything generated once: hashes, calculated at build time.
  • Both at once: perfectly legal, and often the pragmatic path during a migration. Pin legacy snippets with hashes and write new code against a nonce.

Report-Only is your friend

Content-Security-Policy-Report-Only behaves exactly like the real header, except nothing is blocked. Violations are sent to the endpoint you specify instead, which gives you a map of everything your policy would break before it breaks it.

Treat the first fortnight of reports as a survey rather than a to-do list. Browser extensions inject scripts into pages, corporate proxies rewrite markup, and plenty of noise comes from things you neither control nor care about. Filter by directive and by blocked URI, find the patterns, and fix only what is genuinely your code.

It is also worth running a report-only copy of your next, stricter policy alongside the one you actually enforce, so you learn about breakage from logs instead of from users.

Rolling out without heroics

  • Start with the cheap wins: object-src none and base-uri self break very little and close real gaps.
  • Add a report-only script-src, collect data for a week or two, then fix the genuine offenders.
  • Enforce at that level before tightening further. A smaller policy that ships beats a perfect one that never leaves the branch.
  • Move to nonces and strict-dynamic when you are ready, and keep report-only running so regressions surface early.

Details that catch people out

  • frame-ancestors does not fall back to default-src. If you rely on it, set it explicitly.
  • Inline style attributes and event handler attributes are a separate problem; nonces do not cover them, and unsafe-hashes is usually the way out.
  • Add report-sample if your endpoint supports it, otherwise the reports tell you a script was blocked but not what it contained.
  • Third-party widgets are the usual reason a rollout stalls. Ask vendors for nonce support, or isolate the widget in an iframe with its own policy.

None of this is glamorous work, and it rarely earns anyone a promotion. But a policy that blocks injected scripts, written once and tightened gradually, pays for itself the first time it stops something. Start in report-only, read the logs, and tighten until it stings a little.

3 comments

Also Popular