Content Security Policy Nonces and strict-dynamic
HTTP-header script execution allowlist using per-response cryptographic nonces and 'strict-dynamic' trust propagation to block injected scripts without brittle domain allowlists.
About this page
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.
Where this connects
Prerequisites
Preload Scanner and fetchpriority Attribute
The browser HTML parser and preload scanner evaluate script nonces before executing fetched scripts.
DOM XSS Prevention with Trusted Types
Combining nonce-based CSP with Trusted Types blocks both initial script injection and runtime DOM sinks.
Seen in Architecture Autopsies
Autopsy #11: The Chat Bubble That Left With the Session Tokens
Blocks injected inline scripts and restricts execution to nonce-authenticated bundles.
Mechanical Comparisons
- Contrasts WithAsync Clipboard API and User Activation vs Content Security Policy Nonces and strict-dynamic
Clipboard sanitization strips executable markup from pasted HTML rather than blocking scripts via CSP.