Course contentsShow
← Web Development
Lesson 2872 of 2,98251. Content Security PolicyFree lesson

Collecting and Monitoring Reports

Set up endpoints and services to collect, aggregate, and monitor CSP violation data.

Collecting and Monitoring Reports

You'll learn how to set up infrastructure to collect, aggregate, and monitor CSP violation reports in production.

The Challenge of CSP Reports

Once you've configured CSP with report-uri or report-to directives, browsers will send violation reports to your specified endpoint. But receiving these reports is just the beginning—you need a system to collect, store, analyze, and alert on violations to make CSP actionable.

Setting Up Collection Endpoints

Your endpoint must accept POST requests containing JSON payloads. You can:

Build a custom endpoint that receives reports and stores them in a database:

// Express.js example
app.post('/csp-report', express.json({ type: 'application/csp-report' }), (req, res) => {
  const report = req.body['csp-report'];
  
  // Store in database
  db.violations.insert({
    documentUri: report['document-uri'],
    blockedUri: report['blocked-uri'],
    violatedDirective: report['violated-directive'],
    timestamp: new Date(),
    userAgent: req.headers['user-agent']
  });
  
  res.status(204).send();
});

Use specialized services like report-uri.com, Sentry, or Datadog that provide built-in CSP violation monitoring.

Aggregation and Alerting

Don't treat every report individually—aggregate similar violations to identify patterns. Group by violated directive, blocked URI, and source page. Set up alerts for new violation types or sudden spikes that indicate breaking changes.

Filtering Noise

Browser extensions and injected scripts generate false positives. Filter reports from known extensions and focus on violations from legitimate users to avoid alert fatigue.

Key Takeaway: Effective CSP reporting requires more than just an endpoint—build or adopt a complete monitoring solution that aggregates violations, filters noise, and alerts your team to security issues or policy misconfigurations.