Six ways a nonce-based CSP breaks quietly
Websites have a security rule that only lets "approved" code run on the page, like a bouncer checking IDs at a door, so hackers can't sneak in fake code that steals your information. The tricky part is that it's easy to accidentally lock out your OWN real code too, and the page can look completely fine while secretly being broken for every visitor. This write-up covers six ways that happens by accident, and how to catch it before real people do. Useful for anything that handles logins, payments, or personal information, basically any real website or app a business actually depends on
A nonce-based Content Security Policy is one of the strongest defenses against injected scripts, and one of the easiest to get subtly wrong. Each failure below is silent: the build passes, the unit tests pass, and the page still ships broken. I hit all six on real projects, then reproduced the browser behavior in Chrome to confirm it
1. A static page gets no nonce. A nonce must be fresh on every request, so the server has to render the page per request. Next.js documents that static pages can't get one. In one app, a simple form page fetched no data, so it built as static and every framework script on it was blocked. The page rendered and no button worked. Read the route table from a real build and check every page a first-time visitor can reach
2. A framework's own startup script has no nonce. Adding an Astro island with client:load to a nonce-only page passed the build, the type check, and every unit test. The island's inline bootstrap scripts carried no nonce, so a real browser would have blocked them and the island would never have woken up. We only saw it by reading the raw HTML of a real response. Our fix was pinning the scripts' SHA-256 hashes in the policy, which breaks whenever a version bump changes those bytes. Astro 6 now has a built-in hash-based option (a meta tag, not a header), so check that before pinning hashes by hand
3. A CDN optimizer rewrites your scripts. Cloudflare's Rocket Loader rewrites the type of every script tag and re-runs them through its own script. Cloudflare's documented CSP fix is to allow ajax.cloudflare.com in script-src. But 'strict-dynamic' makes browsers ignore host allowlists, so that fix does not apply to a nonce plus strict-dynamic policy. A batch of "Speed" recommendations took one of our sites from working to a half-loaded page within minutes: the HTML arrived and nothing ran. We turned Rocket Loader off. Treat any "auto-optimize" feature that rewrites markup or scripts as suspect. Plain compression and caching toggles are fine
4. Inline style= attributes are dropped. A nonce covers <style> and <link> elements, never a style attribute. Under a nonce-only style-src, every inline style prop is discarded. The page renders with browser defaults and nothing fails in the build. Move those rules into a real stylesheet
5. img-src 'self' blocks data: images. An inline SVG or any image delivered as a data: URI simply disappears. Search the app for every image source before writing the policy, including data: and blob:
6. Your test reads the nonce wrong. Browsers hide the nonce from the nonce attribute once the page is parsed, so getAttribute("nonce") returns an empty string. Read the .nonce property instead. A test that uses the attribute can compare an empty string to an empty string and pass with no policy at all
One check catches most of this. Build tools and jsdom never enforce a CSP, so load the page in a real browser and fail on any violation:
await page.addInitScript(() => {
window.__csp = [];
document.addEventListener("securitypolicyviolation", (e) =>
window.__csp.push(e.effectiveDirective + " blocked " + (e.blockedURI || "inline")));
});
await page.goto(url);
expect(await page.evaluate(() => window.__csp)).toEqual([]);Tested, not just remembered: on a small test page in Chrome with a nonce plus strict-dynamic policy, a script without a nonce was blocked even though 'self' was listed, a script added by a nonced script still ran, an inline style attribute was ignored while a nonced style element applied, a data: image was blocked while a same-origin image loaded, and getAttribute("nonce") returned an empty string while .nonce held the value. I saw Rocket Loader on a live Cloudflare zone, not on the test page