CSP Reporting and Violation Monitoring
What you'll learn: How to use CSP reporting directives to automatically collect and analyze security violations, turning your Content Security Policy into an active monitoring tool.
The Problem: Silent Failures
You've configured a Content Security Policy to block malicious scripts and styles. But what happens when CSP does block something? By default, it fails silently in the user's browser. You might never know if legitimate content is breaking or if attackers are attempting injections.
The Solution: CSP Reporting
CSP provides two directives to send violation reports back to your server:
report-uri (legacy, widely supported): Sends violation details to a specified endpoint as JSON.
report-to (modern standard): Uses the Reporting API for more flexible, grouped reporting.
How It Works
When a CSP violation occurs, the browser automatically sends a POST request containing:
- The blocked resource URL
- The violated directive
- The page where it happened
- The source of the violation
Think of it like a security camera that doesn't just record break-in attempts—it immediately sends you an alert with footage.
Basic Implementation
Content-Security-Policy:
default-src 'self';
script-src 'self' https://trusted.com;
report-uri /csp-violation-report;
When a script from an untrusted domain tries to load, your /csp-violation-report endpoint receives a JSON report you can log and analyze.
Report-Only Mode for Testing
Use Content-Security-Policy-Report-Only to monitor violations without blocking anything—perfect for testing new policies before enforcement.
Why This Matters
Violation reports help you:
- Detect XSS attempts in real-time
- Identify broken legitimate content before users complain
- Build a baseline of normal behavior
- Fine-tune your policy based on actual traffic
Key Takeaway: CSP reporting transforms your security policy from a passive filter into an active detection system, giving you visibility into both attacks and misconfigurations through automated violation reports.