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' (allows eval/string timers) — both defeat CSP’s purpose for scripts.
  • 'nonce-…' (per-response random, must match the tag’s nonce), '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. Note form-action does not fall back to default-src.
  • Content-Security-Policy-Report-Only enforces nothing — purely informational.

Techniques / payloads (bypasses)

  • No/weak policy: missing CSP, report-only only, or script-src with *, 'unsafe-inline', https:, or data: → 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-dynamic neutralizes 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 nginx location match so a relaxed CSP was returned with the same body → XSS, leaking the GitHub OAuth access_token.
  • No-JS exfil via form-action: even under default-src 'none', if form-action is 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'; set object-src 'none' and base-uri 'none'.
  • Set form-action 'self' and validate it covers the self-post case; don’t rely on default-src to 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.

Sources