Design a CSP violation report collector
Design the endpoint and pipeline behind Content-Security-Policy reporting: accept both the legacy report-uri format and the Reporting API, validate and scrub every field, drop extension noise and forged reports, group millions of reports into a few hundred bugs with bounded memory, and turn a report-only period into a real policy.
01Understand the problem
- Read both wire formats: `report-uri` (
application/csp-report) and the Reporting API (report-to,application/reports+json) - Handle ingestion at scale with sampling, size limits and grouping in bounded memory
- Filter browser extension noise and reports about pages you do not own
- Scrub PII from URLs and samples before anything is stored
- Turn a report-only period into a policy without reaching for
'unsafe-inline'
What they probe: whether you know the two formats and their field names, that the endpoint is public and unauthenticated so every field is attacker controlled, how you stop one bad deploy from flooding storage, and how reports become a policy. Where candidates slip: storing raw reports with query strings full of tokens, alerting on extension noise, assuming reports arrive immediately, allowlisting every origin that appears once.
Content Security Policy (CSP) is a response header that tells the browser which sources may load scripts, styles, frames and so on. When the browser blocks (or, in report-only mode, would block) something, it can send a violation report. Content-Security-Policy-Report-Only sends reports without blocking, which is how you roll a policy out safely.
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Reporting-Endpoints: csp="https://csp.shop.example/r"
Content-Security-Policy-Report-Only: default-src 'self'; script-src 'self' 'nonce-r4nd0mPerResp0nse' 'strict-dynamic' 'report-sample'; object-src 'none'; base-uri 'none'; report-uri https://csp.shop.example/r; report-to csp
POST /r HTTP/1.1
Host: csp.shop.example
Content-Type: application/reports+json
[{"type":"csp-violation","age":53531,"url":"https://shop.example/orders/48213","user_agent":"Mozilla/5.0 ...",
"body":{"documentURL":"https://shop.example/orders/48213","blockedURL":"inline","effectiveDirective":"script-src-elem",
"disposition":"report","sourceFile":"https://shop.example/orders/48213","lineNumber":121,"columnNumber":39,
"sample":"console.log(\"lo\")","statusCode":200,"referrer":"","originalPolicy":"default-src 'self'; ..."}}]| Legacy report-uri | Reporting API (report-to) | |
|---|---|---|
| Content-Type | application/csp-report | application/reports+json |
| Body | One object under csp-report | An array of reports, each with type: "csp-violation" and a body |
| Field style | Hyphenated: document-uri, blocked-uri, effective-directive | camelCase: documentURL, blockedURL, effectiveDirective |
| Delivery | Sent as the violation happens | Batched and delayed by the browser, typically up to a minute |
| Endpoint setup | URL in the directive | Named group in the Reporting-Endpoints header; HTTPS only |
This is an advanced module
Problems: detection & enforcement components is part of the Pro pass. 3 more sections — the deep dives, the final design, follow-ups and the test — unlock with it.
Already a cyart.io or studio.cyart.io member? Your pass code is in your Studio account.