Content Security Policy Nonces and strict-dynamic
A nonce-based Content Security Policy (script-src 'nonce-<random>' 'strict-dynamic') replaces porous domain allowlists with a per-response cryptographic token. Only scripts carrying the matching nonce (and child scripts they dynamically load) execute, neutralizing injected <script> tags and inline event handlers.
CSP Nonce & 'strict-dynamic' Trust Propagation
Trace how a per-request nonce trusts the entry bundle, propagates to lazy chunks, and blocks injected inline scripts.
Edge middleware generates nonce='r4nd0m' in the Content-Security-Policy header and stamps it on the root <script>.
Content-Security-Policy: script-src 'nonce-r4nd0m' 'strict-dynamic';- headerNonce
- r4nd0m
- strictDynamic
- true
- blockedScripts
- 0
Under the Hood
Domain-allowlist CSPs are like checking guest IDs only by hometown ("anyone from *.googleapis.com may enter"); 'nonce-...' 'strict-dynamic' issues a single-use wristband at the door and lets badged guests escort their own sub-bundles inside.
- Header vs Parser: Before the Preload Scanner or JS engine runs a
<script>, it verifies that the element'snonceattribute matches the HTTP response header token. - Trust Propagation:
'strict-dynamic'lets a nonce-verified root bundle append dynamic<script>chunks (such as Vite/Next.js code-split bundles) without stamping every chunk URL, pairing cleanly with Trusted Types. - Allowlist Bypass Closure: When
'strict-dynamic'is active, legacy host allowlists and'unsafe-inline'are ignored, closing JSONP and Angular-template bypasses on whitelisted CDNs.
Content-Security-Policy:
default-src 'self';
script-src 'nonce-8fK2pQ9xL4mN7vR1' 'strict-dynamic';
object-src 'none';
base-uri 'none';Problem Genealogy
- 2012 (CSP Level 1 Host Allowlists): Teams whitelisted CDN domains, only to discover hosted JSONP endpoints and
unsafe-inlinebroke the policy. - 2016 (CSP Level 3
'strict-dynamic'): Introduced cryptographic nonces + transitive trust for dynamic module loaders. - 2022+ (Edge Middleware Nonces): Per-request nonces became standard in Next.js, Cloudflare Workers, and SSR frameworks.
Production Pitfalls
- Caching HTML with a baked-in nonce: If a CDN serves the same
nonceto multiple users, attackers can read it and craft matching<script nonce="...">payloads; mint nonces dynamically per response. - Omitting
base-uri 'none'andobject-src 'none': Withoutbase-uri 'none', an injected<base href="https://evil.test">hijacks relative script URLs even under a strict nonce policy.
- When not to use CSP nonces: Static SPAs served from an immutable CDN without edge compute should use SHA-256 script hashes (
'sha256-...') instead of nonces.
Connections & Logical Next Steps
The browser HTML parser and preload scanner evaluate script nonces before executing fetched scripts.
Combining nonce-based CSP with Trusted Types blocks both initial script injection and runtime DOM sinks.
Trusted Types are activated and policy-allowlisted via Content-Security-Policy headers.
Clipboard sanitization strips executable markup from pasted HTML rather than blocking scripts via CSP.