CSP (Content Security Policy) là HTTP header whitelist trusted sources cho scripts, styles, images, fonts — defense in depth chống XSS. default-src: fallback cho directives không specify; script-src: chỉ load scripts từ approved sources; style-src, img-src, font-src, connect-src (fetch/XHR), frame-src, form-action.
- Directives: 'self' (same origin), 'none' (block all), specific domains, 'unsafe-inline' (allow inline — weakens CSP), 'unsafe-eval' (allow eval — dangerous).
- Strict CSP với nonces: script-src 'nonce-{random}' — mỗi request tạo random nonce, inline scripts chỉ chạy nếu có matching nonce attribute; phòng chống injection tốt hơn domain whitelist.
- Hash-based: sha256-{base64hash} thay vì nonce cho static inline scripts.
- Report-Only mode: Content-Security-Policy-Report-Only header — observe violations mà không block, collect reports để debug trước khi enforce. report-uri/report-to: endpoint nhận violation reports dạng JSON — essential để monitor và catch issues sau deploy (ví dụ third-party script bị block).
- Trusted Types API: ngăn DOM XSS bằng cách enforce typed DOM operations — require-trusted-types-for 'script'.
Pitfall: CSP với 'unsafe-inline' và 'unsafe-eval' cho toàn bộ site thực tế vô dụng — common mistake khi copy từ examples mà không hiểu.