Implementing Strict Content Security Policy (CSP) with Nonces and 'strict-dynamic' in Modern SPAs
How to move beyond brittle domain allowlists to cryptographic nonces, trust propagation for dynamic code-splitting, and XSS defense.
Traditional Content Security Policy (CSP) configurations relied on domain allowlists (`script-src 'self' https://cdn.example.com`). Security research has proven that domain allowlists are easily bypassed via open JSONP endpoints, Angular sandboxes, or user-upload CDNs. A modern Strict CSP abandons domain allowlists in favor of per-request cryptographic nonces paired with the `'strict-dynamic'` keyword, which automatically propagates trust to dynamically code-split bundle chunks.
THE PRODUCTION PROBLEM STATEMENT
Domain-based CSPs break frequently when third-party CDNs change URLs, while failing to stop attackers who inject malicious payloads hosted on whitelisted domains.
How 'strict-dynamic' Solves SPA Code-Splitting
When an initial script tag possesses a valid cryptographically random nonce (`<script nonce="rAnd0m123" src="/main.js">`), the browser marks that script execution context as trusted.
With `'strict-dynamic'`, any subsequent script tag created and injected by that trusted script (such as lazy-loaded React chunks or router splits) is automatically trusted. You do not need to generate new nonces for every asynchronously fetched module.
Content-Security-Policy:
base-uri 'none';
object-src 'none';
script-src 'nonce-RANDOM_BASE64_VALUE' 'strict-dynamic' 'unsafe-inline' https: http:;
style-src 'self' 'nonce-RANDOM_BASE64_VALUE';
require-trusted-types-for 'script';[PROBABLE CAUSE] A script tag in index.html lacks the matching nonce attribute, or an inline event handler (like `onclick="..."`) was used.
[VERIFIED RESOLUTION] Attach `nonce={requestNonce}` to all root scripts and replace inline HTML event attributes with JavaScript event listeners.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
Browsers that support CSP Level 2 and 3 automatically ignore 'unsafe-inline' when a nonce or 'strict-dynamic' is present. It serves purely as a fallback for ancient browsers.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Definitive Web Authentication: Secure HttpOnly Session Cookies vs LocalStorage JWT Storage
Why storing access tokens in localStorage exposes users to token exfiltration, and how to structure secure, revocable HttpOnly cookie sessions.
The Modern Web Build Pipeline: Decoupling Bundling, Transpilation, and Hot Module Replacement
How modern frontend toolchains evolved from monolithic webpack bundling to native ESM dev servers, Go-compiled transpilation, and optimal production chunking.