1. What is Content Security Policy?
Content Security Policy (CSP) is an HTTP response header (standardized by the W3C) that allows website administrators to declare approved sources of content that browsers are permitted to load and execute.
By restricting where scripts, styles, images, and network calls can originate, CSP serves as the web's most powerful defense against Cross-Site Scripting (XSS) and data exfiltration vulnerabilities.
2. Core Directives Breakdown
| DIRECTIVE | CONTROLS | SECURE DEFAULT |
|---|---|---|
| default-src | Fallback for all unspecified fetch directives | 'self' |
| script-src | Permitted JavaScript execution sources | 'self' 'nonce-...' |
| style-src | Permitted stylesheet origins and inline styles | 'self' 'unsafe-inline' |
| connect-src | Allowed Fetch, XHR, and WebSocket destinations | 'self' https://api.domain.com |
| frame-ancestors | Domains permitted to embed this page in <iframe> | 'none' |
3. Nonce vs Hash-based Policies
Legacy CSP relied on domain allowlisting (e.g. script-src https://cdn.example.com). However, modern security research has shown that domain allowlists can frequently be bypassed via open redirects or JSONP endpoints hosted on the allowlisted CDN.
Strict CSP (Level 3) solves this by using per-request cryptographic nonces:
4. Frame Ancestors vs X-Frame-Options
While X-Frame-Options: DENY is still supported for backwards compatibility, CSP's frame-ancestors directive is the modern standard because it allows fine-grained control over which parent origins can frame your application.
5. Step-by-Step CSP Rollout Plan
Content-Security-Policy-Report-Only with a report-to endpoint to discover legacy inline scripts without breaking production traffic.onclick="..." attributes to external script event listeners.Content-Security-Policy with object-src 'none'.