$32
$33
Why Developers Care
Three reasons to actually deploy CSP:
- It catches real bugs. I've had CSP reports reveal XSS vulnerabilities that I'd missed - places where user input was being rendered without escaping in a forgotten corner of the app. The browser literally told me "I blocked this script execution, here's why."
- It's a compliance requirement. PCI DSS, HIPAA, NIST 800-53, and most security audits now expect CSP on any internet-facing app. You'll get dinged for not having one.
- It slows down attackers. Even a buggy site with a strict CSP is harder to exploit than a buggy site without one. The attacker has to find an XSS, then find a way around your CSP, then run their payload.
The cost of deploying CSP correctly is real - usually a few days of work for a moderately complex app, plus ongoing maintenance as you add new third-party scripts. The cost of not deploying it is a wider attack surface and a worse incident postmortem.
Prerequisites
- A web application you control, where you can modify HTTP response headers. Could be served by nginx, Apache, Express, Next.js, Django, Rails - anything that lets you set headers.
- The ability to deploy a "report only" version alongside the enforcing version. This means a separate endpoint or header, and a way to collect reports.
- A modern browser to test with (Chrome, Firefox, Safari, Edge all support CSP).
- Basic understanding of how your app loads resources: which scripts are inline, which are from CDNs, which third-party services you use (analytics, fonts, ads).
- The CSP Evaluator browser extension (from Google) - it'll flag weaknesses in your policy as you iterate.
$34
Architecture: How CSP Reports Flow
┌──────────┐ HTTP response with CSP header ┌──────────────┐
│ Browser │ <─────────────────────────────────── │ Your app │
│ (user) │ │ (nginx/Node)│
│ │ └──────────────┘
│ │ if browser sees a violation: │
│ │ POST /csp-report (JSON) │
│ │ ────────────────────────────────────> ┌──────────────┐
│ │ with details: blocked URI, │ Report │
│ │ violated directive, source file, │ collector │
│ │ line number, sample of violating │ (your │
│ │ code │ endpoint) │
└──────────┘ └──────┬───────┘
│
│ sample / forward
▼
┌──────────────────┐
│ Logging / alerting│
│ (Sentry, ELK, │
│ custom dashboard)│
└──────────────────┘
The reporting pipeline is what makes CSP deployable. You ship a Report-Only policy first, collect violations for a week, fix them, then ship the enforcing policy. Without the report pipeline, you're flying blind - and either shipping too strict a policy that breaks the site, or too loose a policy that doesn't actually defend anything.
$35
$36
$37
$38
$39
$3a
Step 7: Real CSP for a WordPress Site
WordPress is harder because it has plugins that inject inline scripts with wild abandon. The realistic path:
default-src 'self';
script-src 'self' 'unsafe-inline' 'unsafe-eval' https://www.googletagmanager.com https://js.stripe.com;
style-src 'self' 'unsafe-inline' https://fonts.googleapis.com;
img-src 'self' data: https:;
font-src 'self' https://fonts.gstatic.com;
connect-src 'self' https://api.stripe.com;
frame-ancestors 'none';
form-action 'self';
base-uri 'self';
object-src 'none';
upgrade-insecure-requests;
report-uri /csp-report;
The 'unsafe-inline' and 'unsafe-eval' in script-src defeat much of CSP's value for WordPress. The honest assessment: WordPress + CSP is hard. Either accept the weaker policy, or migrate to a static site generator + headless WP. There are WordPress plugins (like "Content Security Policy Pro") that auto-generate hashes for inline scripts, but they're brittle across plugin updates.
Step 8: Subresource Integrity for Third-Party Scripts
If you load scripts from a third-party CDN (<script src="https://cdn.example.com/lib.js">), the CDN could be compromised and the script replaced. CSP doesn't catch this - you've whitelisted the origin, so the browser will run whatever that origin serves.
SRI fixes this. Add an integrity attribute with the script's hash:
<script src="https://cdn.example.com/lib.js"
integrity="sha384-abc123..."
crossorigin="anonymous"></script>
The browser computes the hash of the fetched script and refuses to execute it if it doesn't match. Pair with CSP for defense in depth.
Generate SRI hashes:
curl -s https://cdn.example.com/lib.js | openssl dgst -sha384 -binary | openssl base64 -A
# abc123...
Or use the SRI Hash Generator.
$3b
$3c
$3d
$3e
$3f
$40
Originally published on Mango Developer.
Top comments (0)