A lightweight and efficient Content Security Policy (CSP) violation report collector with Prometheus metrics integration and Grafana dashboard visualization.
- Collects and processes CSP violation reports
- Exposes Prometheus metrics for monitoring
- Includes pre-configured Grafana dashboard
- JSON-formatted logging
- Support for multiple hosts/domains
- Tracks various CSP violation metrics:
- Total violations by directive and host
- Blocked URIs distribution
- Status codes distribution
- Top referrers
- Error counts
Launch the demo environment with Docker Compose:
docker-compose up -dAfter startup, the following services will be available:
- CSP Report Handler: http://localhost:8080/report
- Prometheus: http://localhost:9091
- Grafana: http://localhost:3000
Grafana credentials:
- Username: admin
- Password: admin
Send a test CSP report:
curl -X POST http://localhost:8080/report \
-H "Content-Type: application/csp-report" \
-d '{
"csp-report": {
"document-uri": "https://example.com",
"referrer": "https://example.com",
"violated-directive": "script-src",
"effective-directive": "script-src",
"original-policy": "script-src '\''self'\''",
"blocked-uri": "https://evil.com/script.js"
}
}'Open Grafana (http://localhost:3000) and navigate to the pre-configured "CSP Violations Dashboard"
Stop all services:
docker-compose downRemove all data including volumes:
docker-compose down -v- Go 1.24.2 or higher
- Prometheus
- Grafana (optional, for visualization)
git clone github.com/neverlless/csp-report-handler
cd csp-report-handler
go mod downloadThe application can be configured using environment variables:
PORT- Main server port (default: 8080)METRICS_PORT- Prometheus metrics port (default: 9090)ENABLE_METRICS- Enable/disable metrics endpoint (default: false)
- Start the server:
go run main.go- Configure your web application's CSP report-uri:
Content-Security-Policy: ...; report-uri http://your-server:8080/report;- Access Prometheus metrics:
http://your-server:9090/metricsThe following Prometheus metrics are exposed:
csp_reports_total- Total number of CSP violation reportscsp_reports_errors_total- Total number of processing errorscsp_reports_status_codes- Status codes distributioncsp_reports_referrers_total- Violations by referrercsp_reports_blocked_uris_total- Blocked URIs by directive
Build the image:
docker build -t csp-report-handler .Run the container:
docker run -p 8080:8080 -p 9090:9090 \
-e ENABLE_METRICS=true \
csp-report-handlerContributions are welcome! Please feel free to submit a Pull Request.
