CSP/COOP/CORP Hardening Guide with Reporting & Breakage Triage
As Sherlock, I'll walk you through a production-ready hardening strategy for Content Security Policy (CSP), Cross-Origin-Opener-Policy (COOP), and Cross-Origin-Resource-Policy (CORP). These headers mitigate XSS, Spectre-like attacks, and cross-origin leaks. We'll prioritize strict policies, reporting for safe rollout, and a triage workflow to handle breakages.
This guide assumes a modern web app (HTTPS-only). Use report-only modes first to test without breaking production. Tools like report-uri.com or self-hosted endpoints simplify reporting.
Header always set Content-Security-Policy-Report-Only "..."
Header always set Report-To '{"group":"csp-endpoint",...}'
Header always set Cross-Origin-Opener-Policy "same-origin"
# etc.
import json
from collections import Counter
with open('reports.jsonl') as f:
violations = [json.loads(line)['csp-report'] for line in f]
blocked = Counter(v['blocked-uri'] for v in violations if 'blocked-uri' in v)
print("Top Blockers:", blocked.most_common(10))
Phase 3: Enforce Gradually
80/20 Rule: Whitelist top 20% of violations covering 80% traffic.
Flip to CSP (enforce) on 10% traffic (e.g., via Varnish/Cloudflare weighted rules).
Canary: Use COOP/CORP first (low breakage).
Rollback: Keep report-only as fallback; monitor error rates <1%.
Phase 4: Harden Further
Audit: securityheaders.com score 100%+.
Upgrade: upgrade-insecure-requests.
Test: OWASP ZAP, Lighthouse audits.
CI/CD: Pre-deploy CSP validator.
5. Gotchas & Metrics
SharedArrayBuffer: Verify crossOriginIsolated in console.
Edge Cases: Service Workers need worker-src blob:; PWAs may need manifest-src 'self'.