CSP/CORP/COEP Hardening Guide
Hardening Content Security Policy (CSP), Cross-Origin Resource Policy (CORP), and Cross-Origin Embedder Policy (COEP) is essential for modern web security. These headers mitigate risks like XSS, data injection, and cross-origin attacks (e.g., Spectre/Meltdown variants). CSP controls resource loading, CORP blocks cross-origin reads, and COEP enables cross-origin isolation for SharedArrayBuffer and high-granularity performance APIs.
Key goal: Progressive enforcement to avoid breakage while achieving strict-dynamic, require-corp, and cross-origin-isolated states.
1. Asset Policy
Define granular policies for assets (scripts, styles, images, fonts, etc.) to minimize attack surface. Start with report-only mode.
Recommended Policies
| Asset Type | CSP Directive | CORP | COEP Implication | Example Value |
|---|
| Scripts | script-src | same-origin (or stricter) | Require CORP for embeds | 'self' 'strict-dynamic' https://trusted.cdn.com nonce-abc123 |
| Styles | style-src | N/A | Inline styles blocked | 'self' 'unsafe-inline' (migrate to nonces/hashes) |
| Images | img-src | same-site | Data URLs blocked | data: https: 'self' |
| Fonts | font-src | same-origin | Embedder policy checks | 'self' https://fonts.gstatic.com |
| Connect | connect-src | Enforced via CORP | Fetch/XHR restricted | 'self' https://api.example.com |
| Media | media-src | same-site | Cross-origin muted | 'self' https://videos.example.com |
| Workers | worker-src | same-origin | Blob workers allowed | 'self' blob: |
Implementation Steps
# report-only-csp: CSP-Report-Only: default-src 'none'; script-src 'self' 'strict-dynamic'; ...
# Full CSP: Content-Security-Policy: upgrade-insecure-requests; block-all-mixed-content;
CORP: same-origin
COEP: require-corp # Requires CORP: cross-origin or stricter on all cross-origin loads
Cross-Origin-Opener-Policy: same-origin # Pairs with COEP for isolation
Cross-Origin-Embedder-Policy-Report-Only: require-corp # Progressive
- Audit Assets: Use browser devtools (Network tab) or tools like
lighthouse --preset=security to inventory.
- Nonces/Hashes: Migrate inline scripts/styles:
sha256-<base64-hash> or nonce-<random>.
- Trusted CDNs: Pin via
https://example.com + SRI (integrity="sha384-...").
2. Breakage Triage
Strict policies break legacy/inline content. Triage systematically to fix root causes.
Triage Workflow
-
Monitor in Report-Only Mode (CSP-RO, COEP-RO):
Content-Security-Policy-Report-Only: ...; report-to csp-endpoint;
Report-To: {"group":"csp-endpoint","max_age":10886400,"endpoints":[{"url":"https://your-report-uri.com"}]}
-
Collect Violations:
- Browser console: CSP/COEP error logs.
- Server-side: Aggregate reports (e.g., via
report-uri.com, Sentry, or custom endpoint).
-
Categorize Breaks:
| Break Type | Symptom | Fix Priority | Resolution |
|---|
| Inline Script/Style | Refused to execute inline | High | Nonce/hash or extract to file. |
| Cross-Origin Load | Blocked loading mixed active content | High | HTTPS upgrade or self-host. |
| Worker/Fetch | require-corp violation | Med | Add CORP header to 3P resources. |
| Third-Party | Unsafe eval | Low | Audit/remove or sandbox via sandbox directive. |
-
Tools:
- CSP Evaluator (csp-evaluator.withgoogle.com): Score your policy.
- Lighthouse CI: Automate audits.
- Puppeteer/Playwright: Scripted testing:
page.on('cspviolation', log).
-
Rollback Plan: Feature flag policies (e.g., via Varnish/Cloudflare Workers).
3. Reporting
Enable violation reports for proactive detection.
Setup
Handling Reports
- JSON Structure (CSP):
{
"csp-report": {
"document-uri": "https://example.com",
"violated-directive": "script-src-elem",
"blocked-uri": "https://evil.com/script.js",
"effective-directive": "script-src"
}
}
- Dashboards: Sentry, Datadog, ELK stack. Alert on >5% violation rate.
- Privacy: Use endpoint-only reporting (no
report-uri fallback to avoid leakage).
4. Progressive Lock-Down
Roll out in phases over 4-12 weeks to minimize downtime.
Phases
| Phase | Duration | Actions | Success Metrics |
|---|
| 0: Baseline | 1 week | Deploy CSP/CORP/COEP in report-only. Monitor logs. | <1% violations. |
| 1: Basic Block | 2 weeks | Enforce CSP Level 1 (default-src 'self'). CORP same-site. | No prod breaks; fix inlines. |
| 2: Strict Dynamic | 3 weeks | Add script-src 'strict-dynamic' nonce-*. COEP-RO require-corp. | 90% nonce adoption. |
| 3: Full Isolation | 4 weeks | Enforce COEP require-corp, CORP same-origin. strict-dynamic + hashes. | Cross-origin isolated (devtools check). |
| 4: Lock & Audit | Ongoing | Quarterly audits. Automate nonce rotation. | 100% compliance; zero high-sev violations. |
Best Practices
Verification
- DevTools: Application > Manifest > "Cross Origin Isolated".
fetch('/').then(r => r.headers.get('cross-origin-embedder-policy-report-only')).
- Test with
SharedArrayBuffer allocation.
This strategy achieves COOP/COEP/CORP compliance (Chrome 100+ requirement for performance APIs) with minimal breakage. For custom setups, share your current headers/logs for tailored advice! 🔍