# 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.
## Related
- [[Cross-Site Scripting]] · [[DOM-Based XSS]] · [[Reflected XSS]] · [[XSS Filter Bypasses]] · [[SVG and File Upload XSS]] · [[Account Takeover]]
## Sources
- [[raw/intigriti.com/csp-bypasses-advanced-exploitation-guide-3079|CSP Bypasses: Advanced Exploitation Guide]]
- [[raw/vaadata.com/content-security-policy-bypass-techniques-and-security-tips-3077|CSP Bypass Techniques and Security Tips]]
- [[raw/blog.detectify.com/content-security-policy-csp-explained-including-common-bypasses-5066|CSP explained including common bypasses]]
- [[raw/content-security-policy.com/csp-inline-scripts-5058|CSP Inline Scripts]]
- [[raw/github.com/content-security-policy-bypass-techniques-collection-4171|CSP Bypass Techniques Collection]]
- [[raw/portswigger.net/blind-trust-stealing-credentials-through-csp-14199|Blind trust: stealing credentials through CSP]]
- [[raw/sudhanshur705.medium.com/bypassing-csp-via-url-parser-confusions-xss-on-netlify-s-image-cdn-5023|Bypassing CSP via URL Parser Confusions (Netlify)]]
- [[raw/github.com/cspbypass-tool-to-bypass-content-security-policies-4172|CSPBypass tool]]