Security & AccessibilityBaseline · Widely

Content Security Policy Nonces and strict-dynamic

Mental Model

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.

Interactive Simulator

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.

Entry Script (nonce=r4nd0m)
Injected <script>alert(1)</script>
Step 1 / 3: 1. Server Mints Per-Response Nonce

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';
Internal State / Memory
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.

  1. Header vs Parser: Before the Preload Scanner or JS engine runs a <script>, it verifies that the element's nonce attribute matches the HTTP response header token.
  2. 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.
  3. 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.
HTTP
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-inline broke 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

Production Pitfall
  • Caching HTML with a baked-in nonce: If a CDN serves the same nonce to multiple users, attackers can read it and craft matching <script nonce="..."> payloads; mint nonces dynamically per response.
  • Omitting base-uri 'none' and object-src 'none': Without base-uri 'none', an injected <base href="https://evil.test"> hijacks relative script URLs even under a strict nonce policy.
When Not to Use
  • 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

Security & AccessibilityContent Security Policy Nonces and strict-dynamic

Seen in Architecture Autopsies

Mechanical Comparisons