Chapter 66: Content Security Policy (CSP)

Modern Strict CSP Architecture with 'strict-dynamic'

**Part 14: Security & Best Practices** — Chapter 66: Content Security Policy (CSP)

LEARNING OBJECTIVES
  • Understand why domain-based allowlists (script-src https://*.googleapis.com) are vulnerable to JSONP and open-redirect bypasses.
  • Implement modern Strict CSP using nonces and 'strict-dynamic'.
  • Allow trusted scripts to dynamically load secondary child scripts without modifying CSP headers.
  • Deploy backward-compatible CSP Level 3 policies across all browsers.
🎬 INTERACTIVE VISUAL PIPELINE Core Architecture Simulation
🌐
1. Input
Directives & Tags
⚙️
2. Parse
Tokenizer & AST
🌳
3. Layout
Box Model & Flow
🎨
4. Render
GPU Paint & Composite
PHASE 1: INPUT & DIRECTIVES
Browser receives declarative markup stream, parsing tag tokens and initializing component state.

💻 Interactive Code Playground

How It Works:

  1. Modern Browsers (CSP Level 3): 'strict-dynamic' ignores 'unsafe-inline' and https:, trusting ONLY scripts that carry the correct nonce and any child scripts dynamically appended by those trusted scripts.
  2. Older Browsers (CSP Level 2/1 Fallback): Gracefully falls back to https: and 'unsafe-inline'.

Content-Security-Policy: 
  script-src 'nonce-rAnd0m123' 'strict-dynamic' https: 'unsafe-inline'; 
  object-src 'none'; 
  base-uri 'none';

📌 Key Takeaways

  • Strict CSP with 'strict-dynamic' eliminates the maintenance nightmare of managing long lists of third-party domains.
  • Combine 'strict-dynamic' with object-src 'none' and base-uri 'none' for bulletproof XSS mitigation.
  • --

❓ Knowledge Check

1. Which of the following is correct?

2. Which of the following is correct?