DevTools Logo

Content Security Policy Cheat Sheet

Quick reference for Content-Security-Policy: source expressions, directive reference, common policy templates, and reporting.

Security Tools
csp
content-security-policy
security

CSP is a browser security header that restricts which resources a page may load. It is the strongest defense-in-depth control against XSS: even if a script is injected, the browser refuses to execute it if the policy does not allow it.

Directive Reference

Table
DirectiveControlsExample
default-srcFallback for all other -src directivesdefault-src 'self'
script-srcJavaScript sourcesscript-src 'self'
style-srcStylesheets and inline stylesstyle-src 'self' 'unsafe-inline'
img-srcImagesimg-src 'self' data: https:
font-srcFontsfont-src 'self' https://fonts.gstatic.com
connect-srcFetch/XHR/WebSocket endpointsconnect-src 'self' https://api.example.com
frame-srcIframesframe-src 'self' https://player.vimeo.com
frame-ancestorsWho may embed this page in an iframeframe-ancestors 'none'
object-src<object>, <embed>, <applet>object-src 'none'
base-uriValid <base> targetsbase-uri 'self'
form-actionValid form submission targetsform-action 'self'
worker-srcWeb workersworker-src 'self' blob:
media-srcAudio/videomedia-src 'self'
upgrade-insecure-requestsAuto-upgrade HTTP to HTTPSupgrade-insecure-requests

Source Expressions

Table
SourceMeaning
'self'Same origin only
'none'Block all
'unsafe-inline'Allow inline scripts/styles (avoid for scripts)
'unsafe-eval'Allow eval() (avoid)
'strict-dynamic'Trust scripts loaded by already-trusted scripts
'nonce-<base64>'Allow a specific inline script/event by nonce
'sha256-<base64>'Allow an inline script matching the hash
https:Any HTTPS origin
https://example.comSpecific origin (scheme+host)
wss:Any secure WebSocket
data:data: URIs
blob:blob: URIs

Starter Policies

http
# Strict, no third-party scripts
Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'

# Allow Google Analytics + self scripts (nonce-based)
Content-Security-Policy: script-src 'self' 'nonce-<RANDOM>' https://www.googletagmanager.com; img-src 'self' https://www.google-analytics.com; connect-src 'self' https://www.google-analytics.com

# Iframe a video player
Content-Security-Policy: default-src 'self'; frame-src 'self' https://player.vimeo.com

Reporting

Report violations without blocking by using Content-Security-Policy-Report-Only first. The report endpoint receives a JSON body listing the violated directive and blocked resource.

http
Content-Security-Policy-Report-Only: default-src 'self'; report-to csp-endpoint
Report-To: { "group": "csp-endpoint", "max_age": 10886400, "endpoints": [{ "url": "https://example.com/csp-reports" }] }

Common Pitfalls

[!WARNING] 'unsafe-inline' in script-src defeats the purpose of CSP — replace inline handlers with nonces or hashes before hardening.

[!TIP] Always set object-src 'none' and base-uri 'self' — they are cheap, high-value hardening that many default templates omit.

References