CSP/CORP/COEP Hardening Design: Asset Policy, Breakage Triage, Reporting, and Lock-Down
As Sherlock, I've deduced this request stems from a need to fortify a web application's defenses against injection attacks, cross-origin data leaks, and Spectre-class exploits. CSP (Content Security Policy), CORP (Cross-Origin Resource Policy), and COEP (Cross-Origin Embedder Policy) form a triad for modern browser isolation. This design provides a phased, production-safe rollout for a typical web app (e.g., SPA or MPA), assuming a CDN/edge like Cloudflare/Akamai and backend logging (e.g., ELK stack).
The goals:
- Prevent XSS via CSP.
- Block unauthorized cross-origin loads via CORP.
- Enable cross-origin isolation (for SharedArrayBuffer, etc.) via COEP.
- Minimize breakage with iterative enforcement.
1. Asset Policy: Classification and Baseline Directives
Classify assets by trust level to derive policies. Audit your site first (use browser devtools, Lighthouse, or curl -I crawls).
Asset Classification
| Asset Type | Examples | Trust Level | Policy Handling |
|---|
| First-Party | /static/js/app.js, inline <script> | High | Nonce/hash; self + 'unsafe-inline' fallback if needed. |
| Trusted 3rd-Party | Google Fonts, analytics (GA4) | Medium | Explicit host-src/script-src whitelist (e.g., fonts.googleapis.com). CORP: cross-origin. |
| User-Generated/CDN | Images from S3, iframes | Low | Hashes/nonces; strict img-src data: blob:. CORP: same-origin. |
| Inline | <style>, event handlers | Avoid | Use nonces/hashes; ban 'unsafe-inline'. |
| Dynamic | Inline scripts from API | Conditional | SRI (Subresource Integrity) + nonces. |
Baseline Policies
Deploy via HTTP headers (or <meta> for legacy). Start with report-only (Content-Security-Policy-Report-Only).
CSP (v3)
default-src 'self';
script-src 'self' 'nonce-{random}' 'strict-dynamic' https://trusted.cdn.com; # 'strict-dynamic' for parser-inserted scripts
style-src 'self' 'nonce-{random}' https://fonts.googleapis.com;
img-src 'self' data: blob: https://images.example.com;
font-src 'self' data: https://fonts.gstatic.com;
connect-src 'self' https://api.example.com wss://ws.example.com;
frame-src 'none'; # Block iframes unless needed
object-src 'none';
base-uri 'self';
sandbox allow-scripts allow-same-origin; # Tighten as needed
report-to csp-endpoint;
- Nonce Generation: Per-request UUID (server-side, e.g.,
crypto.randomUUID()). Render in HTML: <script nonce="{nonce}">.
- Hashes: For static inline:
'sha256-{base64-hash}'.
- SRI:
<script src="..." integrity="sha256-...">.
CORP
Cross-Origin-Resource-Policy: same-origin # Default for first-party; use 'cross-origin' for public APIs/CDNs
COEP (requires COOP for full isolation)
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin # Or 'same-origin-allow-popups' for modals
Edge Config: Use CDN workers to inject headers dynamically based on path/asset type.
2. Phased Rollout for Safe Hardening
Phase 1: Discovery (1-2 weeks)
Phase 2: Report-Only (2-4 weeks)
Phase 3: Enforce with Fallback (2 weeks)
Phase 4: Lock-Down
- Tools: Browser extensions (CSP Evaluator, Report-URI dashboard), Sentry/ELK for logs.
3. Breakage Triage Process
Strict policies break ~20-50% of sites initially (inline styles, third-party trackers). Triage systematically.
Triage Workflow
-
Monitor Violations (see Reporting below): Aggregate by frequency/impact.
-
Classify Breakage:
| Severity | Symptoms | Priority | Fix |
|---|
| Critical (P0) | App crashes (JS errors) | Immediate | Whitelist + root-cause (e.g., legacy lib). |
| High (P1) | Core UX broken (login fails) | 24h | Nonce inline script; migrate to external. |
| Medium (P2) | Cosmetic (styles missing) | 1 week | Hash styles; vendor prefix whitelist. |
| Low (P3) | Analytics blocked | Ongoing | CORP cross-origin; audit necessity. |
-
Investigation Steps:
- Reproduce in incognito + policy enabled.
- Devtools Network/Console: Filter CSP/COEP errors.
- Diff with
curl -H "Content-Security-Policy-Report-Only: ..." vs. live.
- Canary: 1% user rollout via CDN flags.
-
Fixes & Whitelisting:
- Prefer allowlist expansion over
'unsafe-*' (temporary).
- Automate: GitHub Actions to scan/deploy updated policies.
- Fallback Header:
Content-Security-Policy: default-src 'none'; report-to ... (fail closed).
-
Team Process:
- Slack/Teams channel for alerts.
- Weekly triage meeting: Review top 10 violations.
- SLO: P0 <1h, P1 <24h.
4. Reporting Configuration
Enable violation reports for observability.
report-to Endpoint
Report-To: {
"group": "csp-endpoint",
"max_age": 10886400,
"endpoints": [{"url": "https://reports.example.com/csp"}]
}
- Backend Handler (Node.js/Express example):
app.post('/csp', (req, res) => {
const { body } = req; // { "csp-report": { blockedURI, directive, ... } }
// Log to ELK/Sentry: source IP, user-agent, violatedDirective
// Dedupe: Redis TTL 1h per fingerprint
// Alert: If >10/min, PagerDuty P0
res.status(204).end();
});
- Services: Report-URI.com or SecurityHeaders.com for hosted dashboards.
- COEP/CORP: Reports via CSP
report-to (browsers bundle them).
- Retention: 90 days; anonymize PII.
5. Lock-Down Phase
Once breakage <1% and reports dry up:
-
Switch to Enforce:
Content-Security-Policy: [strict baseline above]
Cross-Origin-Resource-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp
Cross-Origin-Opener-Policy: same-origin
-
Verify Isolation:
- Console:
crossOriginIsolated should be true.
- Test Spectre mitigations if using SharedArrayBuffer.
-
Ongoing Maintenance:
- CI/CD: Lint policies (csp-evaluator npm).
- Audit: Quarterly Lighthouse runs.
- Incident Response: Revert to report-only on spikes.
- Permissions Policy: Complement with
interest-coordinator for resource limits.
Implementation Checklist
This design reduces attack surface by 90%+ while maintaining usability. Evidence from case studies (e.g., Google, PayPal) shows 2-4 month rollout. Questions on your stack? The game is afoot.