CSP Bypasses and Common Weaknesses
You'll learn how attackers can circumvent Content Security Policies through predictable vulnerabilities and misconfigurations.
Even well-intentioned Content Security Policies can fail if they contain common weaknesses. Understanding these bypasses helps you build stronger defenses.
The Three Common Weak Spots
unsafe-inline: The Obvious Hole
When a CSP includes unsafe-inline in script-src, it defeats the entire purpose. This directive tells the browser "allow any inline JavaScript," meaning XSS payloads injected directly into HTML will execute freely. It's like installing a fortress wall but leaving the front gate wide open.
Content-Security-Policy: script-src 'unsafe-inline' 'self'
With this policy, an attacker's <script>alert(document.cookie)</script> payload works perfectly—CSP won't block it.
JSONP Endpoints: Legitimate Scripts, Malicious Use
If your CSP allows a domain that hosts JSONP endpoints (many CDNs and APIs do), attackers can inject script tags pointing to these endpoints with callback parameters they control:
<script src="https://trusted-cdn.com/api?callback=evilFunction"></script>
The trusted domain loads, CSP allows it, but the attacker controls what executes. It's like giving someone keys to your house because they work for a company you trust—except you haven't verified their identity.
Weak Allowlists: Too Permissive Domains
Allowing broad domains like https://*.googleapis.com or https://cdnjs.cloudflare.com opens doors. These domains host thousands of scripts—some exploitable, some with vulnerabilities. Attackers can reference obscure libraries or outdated files to find gadgets that enable XSS.
Prevention
Use nonces or hashes instead of unsafe-inline, carefully vet every allowed domain, and maintain strict allowlists with only the specific resources you actually need.
Key Takeaway: CSP bypasses exploit predictable weaknesses like unsafe-inline, JSONP endpoints on trusted domains, and overly broad allowlists—tighten your policies to eliminate these escape routes.