Advanced 26 min readModule: Module 16: Content Security Policy (CSP3), Trusted Types & SRI
Content Security Policy Level 3 & Trusted Types Hardening
Defend HTML applications against DOM-based Cross-Site Scripting (XSS) and code injection using CSP3 nonces, Trusted Types policies, and Subresource Integrity (SRI) hashes.
What You Will Learn in This Lesson
- Writing strict Content Security Policy Level 3 headers with cryptographic nonces
- Eliminating DOM XSS vulnerabilities by enforcing the Trusted Types API (`require-trusted-types-for 'script'`)
- Verifying CDN asset integrity with Subresource Integrity (SRI) SHA-384 hashes
- Restricting framing and clickjacking attacks using `frame-ancestors 'none'`
Introduction & Core Concept
HTML applications are subject to injection vulnerabilities when untrusted user input is parsed as code. Content Security Policy (CSP Level 3) and the W3C Trusted Types API represent the highest standard of client-side web security, forcing the browser to reject untrusted scripts, inline styles, and unverified DOM string injections at the parser level.
WHY DOES THIS MATTER IN THE REAL WORLD?
Cross-Site Scripting (XSS) is historically among the most exploited web vulnerabilities. Enforcing strict CSP nonces and Trusted Types guarantees that even if an attacker injects a malicious <script> tag into your HTML, the browser will refuse to execute it.
Syntax & Structure
html
<meta http-equiv="Content-Security-Policy" content="script-src 'nonce-rAnd0m' 'strict-dynamic';"><script src="https://cdn.example.com/lib.js" integrity="sha384-..." crossorigin="anonymous"></script>Securing HTML with Cryptographic Nonce CSP and SRI Hashes
htmlhtml
123456789101112131415161718192021222324252627282930313233343536<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><!-- Content Security Policy Level 3 Meta Definition --><meta http-equiv="Content-Security-Policy" content="default-src 'self';script-src 'self' 'nonce-kwasSecureRandom2026' https://cdn.jsdelivr.net;style-src 'self' 'unsafe-inline';object-src 'none';base-uri 'self';frame-ancestors 'none';require-trusted-types-for 'script';"><title>KWAS Academy Security Baseline</title><!-- External Library with Subresource Integrity (SRI) Hash --><scriptsrc="https://cdn.jsdelivr.net/npm/lucide@latest/dist/umd/lucide.min.js"integrity="sha384-O6jQJ9hO2hT3oYnI6q+zK9a7Gv8wE4uC+J7uV6QxO+H7E4E9I5Q7G3x0z8E3B6"crossorigin="anonymous"></script></head><body><h1>Hardened Enterprise HTML Environment</h1><div id="secure-content">Strict CSP Active</div><!-- Valid Nonce: Browser Executes Safely --><script nonce="kwasSecureRandom2026">console.log("✅ Authenticated script executed with valid cryptographic nonce.");</script><!-- Missing Nonce: Browser Blocks and Reports Violation --><!-- <script>alert('Blocked by browser CSP!');</script> --></body></html>
Line-by-Line Technical Breakdown
1Trusted Types API: When Trusted Types is enabled, assigning raw strings to `element.innerHTML` throws a TypeError. Developers must create a sanitization policy (`trustedTypes.createPolicy(...)`) that explicitly validates and sanitizes input before passing it into DOM sinks.
Try It Yourself (Interactive Editor)
Modify the code in real-time and click Run to test live browser output and console logs.
Intelligent Code Runner & Live Sandbox[HTML]
HTML SOURCE EDITOR
Interactive Live CodeCommon Mistakes & How to Avoid Them
#1: Using 'unsafe-inline' and 'unsafe-eval' in script-src directives, defeating CSP protection.
'unsafe-inline' allows any injected <script> tag to execute freely, rendering CSP ineffective against XSS.
Incorrect / Antipattern
script-src 'self' 'unsafe-inline' 'unsafe-eval';Correct / Professional Solution
script-src 'self' 'nonce-RANDOM_VALUE' 'strict-dynamic';Industry Best Practices & Professional Standards
- Generate unique, cryptographically random nonces on the server for every single HTTP response.
- Always attach SRI `integrity` hashes to external CDN script tags.
- Use `report-uri` or `report-to` directives to log CSP violation telemetry to your security monitoring service.
Lesson Summary & Core Takeaways
- CSP Level 3 uses cryptographic nonces to defeat Cross-Site Scripting (XSS).
- Trusted Types prevents DOM-based injection vulnerabilities at dangerous DOM sinks.
- Subresource Integrity (SRI) guarantees external CDN binaries have not been altered.