Content Security Policy
CSP is an HTTP response header (
Content-Security-Policy) that restricts which script/resource origins a page may load and execute — the defense-in-depth net meant to turn an HTML injection into a non-event, and routinely misconfigured so it isn’t.
How it works
The browser enforces the policy’s directives against every resource load and script execution. Script execution is gated by the fallback chain script-src-elem → script-src → default-src; if none is present, scripts are unrestricted. Key knobs:
- Source lists:
'self', host/scheme allowlists,*wildcards. 'unsafe-inline'(allows inline<script>/handlers),'unsafe-eval'(allowseval/string timers) — both defeat CSP’s purpose for scripts.'nonce-…'(per-response random, must match the tag’snonce),'sha256-…'hashes,'unsafe-hashes'(CSP3, allows hashed event handlers).'strict-dynamic'— trust propagates from a nonced/hashed script to scripts it loads; makes the browser ignore the host allowlist.require-trusted-types-for 'script'+trusted-types— forces DOM sinks to receive typed, sanitized values (structural DOM-Based XSS defense).form-action,base-uri,frame-ancestors,object-src. Noteform-actiondoes not fall back todefault-src.Content-Security-Policy-Report-Onlyenforces nothing — purely informational.
Techniques / payloads (bypasses)
- No/weak policy: missing CSP, report-only only, or
script-srcwith*,'unsafe-inline',https:, ordata:→ inject directly (<script src="data:;base64,YWxlcnQoMSk=">). - Allowlisted-host gadgets: a trusted CDN hosts a JSONP endpoint (
?callback=alert(document.domain)//) or a vulnerable library (AngularJS, old Bootstrap) usable as a script gadget — run arbitrary JS from an allowed origin. Datasets of 350+ known gadgets exist (cspbypass.com); match the allowlist with CSP Evaluator.strict-dynamicneutralizes host gadgets (host list ignored), so check for it first. - Arbitrary file upload on an allowlisted host: upload a file served as JS from a whitelisted origin (common with shared S3/CDN domains anyone can upload to).
- Predictable/reused/static nonces: nonce derived from timestamp+UA, or reused across responses, is forgeable.
- Policy injection: a reflected parameter lands inside the CSP header/
<meta>→ inject; script-src * 'unsafe-inline'to loosen it. - URL-parser confusion (Netlify image CDN): the component that sets a strict CSP (
script-src 'none') and the one that serves the resource normalize the path differently;/./.netlify%2Fimages?url=…evaded the nginxlocationmatch so a relaxed CSP was returned with the same body → XSS, leaking the GitHub OAuthaccess_token. - No-JS exfil via
form-action: even underdefault-src 'none', ifform-actionis absent or'self', an injected self-posting login form steals credentials with zero script execution (PortSwigger Burp AT research; AFINE).
Notable cases
- Netlify image CDN — strict
script-src 'none'bypassed by path-parser confusion → account-token theft. - Basecamp CSP-bypassing XSS (HackerOne roundup) allowing unauthorized profile editing.
- PayPal DOM polyglot XSS executed by abusing in-app script gadgets (Bootstrap, custom
youtube.js) despite CSP.
Defenses (deploying CSP well)
- Nonce-based,
strict-dynamic, no'unsafe-inline'/'unsafe-eval'; setobject-src 'none'andbase-uri 'none'. - Set
form-action 'self'and validate it covers the self-post case; don’t rely ondefault-srcto cover forms. - Minimize the host allowlist; never allowlist a host that accepts public uploads or serves JSONP; audit with Google CSP Evaluator.
- Add
require-trusted-types-for 'script'to kill DOM-sink injection. - Treat CSP as layer 2 — primary control remains output encoding/sanitization. A CSP bypass is report-worthy chiefly when chained to an actual injection.
Related
- Cross-Site Scripting · DOM-Based XSS · Reflected XSS · XSS Filter Bypasses · SVG and File Upload XSS · Account Takeover
Sources
- CSP Bypasses: Advanced Exploitation Guide · original
- CSP Bypass Techniques and Security Tips · original
- CSP explained including common bypasses · original
- CSP Inline Scripts · original
- CSP Bypass Techniques Collection · original
- Blind trust: stealing credentials through CSP · original
- Bypassing CSP via URL Parser Confusions (Netlify) · original
- CSPBypass tool · original