Content Security Policy (CSP)
Last updated: April 2026
Content Security Policy is an HTTP response header that tells browsers which sources are trusted for loading scripts, styles, images, fonts, and other resources. It is the primary browser-level control for reducing cross-site scripting (XSS) risk.
Basic CSP header
Content-Security-Policy: default-src 'self'; script-src 'self' https://cdn.example.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; frame-ancestors 'none'; object-src 'none'
Key directives
default-src โ fallback for all resource types not explicitly listed.
script-src โ controls JavaScript sources. The most security-critical directive.
style-src โ controls CSS sources.
img-src โ controls image sources.
connect-src โ controls fetch, XHR, and WebSocket destinations.
frame-ancestors โ controls which pages can embed yours in an iframe. Replaces X-Frame-Options.
object-src โ controls plugins. Set to 'none' to block Flash and other plugins.
Test without breaking โ report-only mode
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self'; report-uri /csp-report
Inline scripts โ nonce vs unsafe-inline
# Nonce (recommended) โ generates a random value per request Content-Security-Policy: script-src 'self' 'nonce-RANDOM_BASE64' # In HTML: <script nonce="RANDOM_BASE64">...</script> # Hash โ for static inline scripts Content-Security-Policy: script-src 'self' 'sha256-HASH_OF_SCRIPT_CONTENT' # unsafe-inline โ avoid, defeats XSS protection Content-Security-Policy: script-src 'self' 'unsafe-inline'