Strict-Dynamic and Nonce Propagation
You'll learn how the 'strict-dynamic' CSP directive empowers nonce-authorized scripts to dynamically load additional scripts without explicitly allowlisting their sources.
The Challenge of Dynamic Script Loading
Imagine you've carefully nonce-protected your initial scripts. But what if those scripts need to load other scripts at runtime—like analytics libraries, A/B testing tools, or third-party widgets? Traditionally, you'd need to allowlist every possible source domain in your CSP, which quickly becomes unmanageable and defeats the purpose of using nonces.
What 'strict-dynamic' Does
The 'strict-dynamic' directive solves this by trusting the script rather than the source. When you add it to your script-src directive, any script authorized with a valid nonce can dynamically create and load additional scripts—and those child scripts inherit the trust.
Think of it like a security clearance that passes down: if you trust the parent script (verified by nonce), you automatically trust scripts it consciously loads.
Example CSP header:
Content-Security-Policy: script-src 'nonce-xyz123' 'strict-dynamic'
With this policy, your nonce-protected script can use methods like document.createElement('script') or dynamic imports to load third-party code. The browser will allow it because the parent script was nonce-verified.
What It Blocks
Importantly, 'strict-dynamic' ignores allowlist-based sources (like https://cdn.example.com) and blocks parser-inserted scripts without nonces. It also disables 'unsafe-inline', creating a more secure default.
Best Practice
Use 'strict-dynamic' when your nonce-protected scripts need to dynamically load dependencies, but ensure your initial bootstrapping script has a proper nonce.
Key Takeaway: 'strict-dynamic' lets nonce-authorized scripts safely load additional scripts at runtime by propagating trust through the script execution chain, eliminating the need to manually allowlist every dynamic dependency source.