Skip to content
Dorian FINKEL
Web securityCSPCloudflare WorkersLessons learned · 2026 · 3 min read · Updated 9 October 2026

A strict CSP that doesn’t break the site: the hash computed at build time

A content security policy that forbids every inline script except one: the theme script, allowed by its SHA-256 hash. Recomputed at every build, it never breaks the site when the script changes.

01Why a CSP

The Content-Security-Policy header tells the browser where a resource may come from. With script-src limited to the site itself, a script injected into the page (the basis of an XSS attack) doesn’t run, even if it manages to slip in. It is a second line of defence: it doesn’t replace properly escaping displayed data.

02The troublesome script

To avoid a white flash on load, a small inline script applies the light or dark theme before first paint. Inline, it is blocked by a strict CSP. Allowing it with unsafe-inline would open the door to every inline script, hence to the very attack we want to prevent.

CSP accepts another form: the SHA-256 hash of the exact script. Only that text, character for character, spaces and line breaks included, is allowed.

03The hash computed at build time

Written by hand, the hash breaks the site at the first edit of the script. It is therefore computed at build time from the generated page, then written into the headers file served by Cloudflare.

Example · JavaScript
import { createHash } from 'node:crypto';

// For each executable inline script in the generated page:
const hash = (code) => `'sha256-${createHash('sha256').update(code).digest('base64')}'`;
const csp = [
  "default-src 'self'",
  `script-src 'self' ${scripts.map(hash).join(' ')}`,
  "object-src 'none'",
  "base-uri 'none'",
  "frame-ancestors 'none'",
].join('; ');

04The directives people forget

script-src is not enough. object-src 'none' blocks legacy plugins, base-uri 'none' prevents hijacking relative paths with an injected base tag, and frame-ancestors 'none' forbids displaying the site in an iframe elsewhere (the modern version of X-Frame-Options).

05Watch before enforcing

The Content-Security-Policy-Report-Only header applies the same policy without blocking anything: the browser only reports what it would have refused. It is the right way to try a new rule on a production site before making it blocking.

06The traps

Every third-party service must be declared: the form’s anti-bot widget, the map loaded on click. An omission only shows up at runtime, in the console.

The prerendered HTML must contain no unexpected inline script: some rendering mechanisms add them silently, and they would be blocked. The build checks they are absent and fails otherwise.

End-to-end tests fail on the slightest CSP violation reported in the console: a rule that is too strict is caught before going live.

07Verify

A tool like securityheaders.com grades the page’s headers, and Google’s CSP Evaluator flags weaknesses in the policy. In the browser, the Console tab reports every refused resource.

Example · Bash
curl -sI https://www.example.com | grep -i content-security-policy

Key takeaways

  • A strict CSP blocks injected scripts, even if they get into the page.
  • unsafe-inline cancels the protection; a SHA-256 hash allows one specific script.
  • Compute the hash at build time: it follows the script automatically.
  • object-src, base-uri, frame-ancestors: close them too.
  • Report-Only to test, end-to-end tests to never regress.

Official sources