CSP
A response header that tells the browser which scripts, styles, frames and connections a page may load, limiting the impact of XSS.
CSP is delivered as the Content-Security-Policy header (Level 3 is the current W3C draft). Directives such as script-src, object-src, base-uri, frame-ancestors and connect-src restrict sources. Content-Security-Policy-Report-Only tests a policy without enforcing it, and violations are sent to a report-to endpoint.
Allow-list policies (script-src 'self' cdn.example.com) are routinely bypassed through JSONP endpoints and script gadgets on allowed hosts. Google's research showed most such policies gave no real protection, which led to strict CSP: nonces or hashes plus strict-dynamic.
CSP is defence in depth, not an XSS fix. Common weaknesses: 'unsafe-inline', missing object-src 'none' and base-uri 'none', nonces reused across responses, and policies stuck in report-only for years.
Content-Security-Policy: script-src 'nonce-r4nd0mPerResponse' 'strict-dynamic';
object-src 'none'; base-uri 'none'; frame-ancestors 'self'; report-to cspLearn it properly
Also used in
- DNS and email securitySecEng · Network & Infrastructure SecurityFree
- The bugs that actually shipSecEng · Application Security & DevSecOpsFree
- Cookies and session hardening in the browserSecEng · Browser & web platform securityFree
- Cross-site scripting for defendersSecEng · Offensive web security for defendersFree
- Design a secure file upload serviceHLD · First security designsFree
- Design a CSRF token serviceLLD · Problems: first security componentsFree
- Prompt injection, direct and indirectSecEng · AI & LLM security engineeringPro
- Design a safe file-type detectorLLD · Problems: parsing and sandboxing componentsPro