RepositoryGo
jacobbednarz

jacobbednarz/go-csp-collector

Self-hosted collector for browser security reports: CSP, NEL, COOP, COEP and other Reporting API reports.

132stars
31forks
0issues
2views

Featured achievements

The project's latest awards

1
TINIX TRENDING·Topic: csp-collector

#1 Repository of the month

1
TINIX TRENDING·Topic: reporting-api

#1 Repository of the month

1
TINIX TRENDING·Topic: coop

#1 Repository of the month

This is a content security policy (CSP) violation and network error logging (NEL) collector written in Golang.

It has been designed to listen on port 8080 and accept POST payloads containing the violation report. It captures the report and will write it to STDOUT via Go's logger.

A neat little feature of this tool is that it automatically ignores unactionable reports. Check out the default list if you're interested.

Installation

$ go get github.com/jacobbednarz/go-csp-collector

Alternatively, you can download the binaries from the release page.

Running

$ go build -o csp_collector main.go
$ ./csp_collector

Endpoints

CSP

  • POST /: accepts a CSP violation report (recommended to use /csp for future proofing though).
  • POST /csp: accepts a CSP violation report.
  • POST /csp/report-only: same as /csp but appends a report_only=true attribute to the log line. Helpful if you have enforced and report-only violations and wish to separate them.
  • OPTIONS /reporting-api/csp: CORS preflight handler for the Reporting API.
  • POST /reporting-api/csp: Implementation of the browser Reporting API (w3c / MDN) for CSP violations.

NEL (Network Error Logging)

[!IMPORTANT]
Network error logging is still experimental in most browsers. This functionality is best effort and may need to adjust as the specification does.

  • POST /nel: accepts NEL reports for an enforced NEL policy.
  • POST /nel/report-only: same as /nel but appends a report_only=true attribute to the log line. Useful when testing a NEL policy before enforcing it.

NEL reports are delivered by the browser via the Reporting API as a JSON array. Each entry with "type": "network-error" is logged; other types in a mixed batch are silently skipped.

To enable NEL reporting, send the NEL and Report-To (or Reporting-Endpoints) headers from your server:

Reporting-Endpoints: nel-endpoint="https://collector.example.com/nel"
NEL: {"report_to": "nel-endpoint", "max_age": 31536000}

Each logged NEL report includes the following fields:

FieldDescription
report_onlytrue when received on the /nel/report-only endpoint
urlThe URL of the request that failed
referrerThe referrer at the time of the request
typeError type (e.g. tcp.refused, dns.unreachable, ok)
phasePhase of the request: dns, connection, application, or abandoned
protocolNetwork protocol (e.g. h2, http/1.1)
methodHTTP method (e.g. GET, POST)
status_codeHTTP status code (0 if the connection never completed)
elapsed_timeDuration of the request in milliseconds
server_ipIP address of the server that handled the request
sampling_fractionFraction of matching requests that were reported
metadataValue of the metadata query parameter (if present)
pathPath of the collector endpoint that received the report

Building for Docker

You will either need to build within a docker container for the purpose, or use CGO_ENABLED=0 flag to make the build compatible with alpine linux in a docker container.

$ CGO_ENABLED=0 go build -o csp_collector main.go

Command Line Options

FlagDescription
versionShows the version string before exiting
debugRuns in debug mode producing more verbose output
portPort to run on, default 8080
metrics-portPort for the Prometheus metrics endpoint, default 9090
metrics-bind-addrBind address for the Prometheus metrics endpoint, default 127.0.0.1
filter-fileReads the blocked URI filter list from the specified file. Each line is matched as a prefix against the blocked URI. Note one filter per line.
filter-domains-fileReads a domain block list from the specified file. Each line is a bare domain (e.g. kaspersky-labs.com). A report is dropped when the blocked URI's hostname exactly matches the domain or is any subdomain of it (e.g. gc.kis.v2.scr.kaspersky-labs.com). Matching uses exact suffix comparison — no fuzzy or regex logic. Note one domain per line. Performance note: each check requires a url.Parse call to extract the hostname, which costs roughly 20–35× more than the prefix filter (~180 ns/op vs ~5–8 ns/op). For high-throughput deployments, prefer filter-file for simple prefix matches and only use filter-domains-file where subdomain wildcard matching is genuinely needed.
health-check-pathSets path for health checkers to use, default /_healthcheck
log-client-ipInclude a field in the log with the IP delivering the report, or the value of the X-Forwarded-For header, if present.
log-truncated-client-ipInclude a field in the log with the truncated IP (to /24 for IPv4, /64 for IPv6) delivering the report, or the value of the X-Forwarded-For header, if present. Conflicts with log-client-ip.
truncate-query-fragmentRemove all query strings and fragments (if set) from all URLs transmitted by the client
query-params-metadataLog all query parameters of the report URL as a map in the metadata field

See the sample.filterlist.txt file as an example of the URI prefix filter list, and sample.domainlist.txt as an example of the domain filter list.

Request metadata

Additional information can be attached to each report by adding a metadata url parameter to each report. That value will be copied verbatim into the logged report.

For example a report sent to https://collector.example.com/?metadata=foobar will include field metadata with value foobar.

If query-params-metadata is set, instead all query parameters are logged as a map, e.g. https://collector.example.com/?env=production&mode=enforce will result in "metadata": {"env": "production", "mode": "enforce"} in JSON format, and metadata="map[env:production mode:enforce]" in default format.

report-only mode

Both CSP and NEL have dedicated report-only endpoints (/csp/report-only and /nel/report-only). Reports received on these endpoints are logged identically to their enforced counterparts, but with report_only=true in the log output. This lets you distinguish enforced violations from report-only ones in your log aggregation tool without needing separate collector instances.

Metrics

Prometheus metrics are exposed on a dedicated endpoint:

  • Address: 127.0.0.1:9090 (default)
  • Path: /metrics
  • Full URL: http://127.0.0.1:9090/metrics

The collector exports:

MetricTypeLabelsDescription
csp_collector_reports_totalCounterhandler, modeSuccessfully processed CSP or Reporting API reports
csp_collector_nel_reports_totalCountermodeSuccessfully processed NEL reports
csp_collector_reports_filtered_totalCounterhandler, reasonReports dropped by URI/domain filters
csp_collector_reports_ignored_totalCounterhandler, reasonReports intentionally ignored (for example unsupported NEL types)
csp_collector_reports_errors_totalCounterhandler, typeRejected reports (decode or validation failures)
csp_collector_http_request_duration_secondsHistogramhandler, route, method, codeHTTP request duration for report-ingestion endpoints
csp_collector_http_requests_in_flightGaugehandler, routeActive in-flight report-ingestion requests
go_* / process_*Variousclient-go defaultsRuntime and process health metrics

Example Prometheus scrape config:

scrape_configs:
  - job_name: "go-csp-collector"
    static_configs:
      - targets: ["127.0.0.1:9090"]

If you expose metrics on a non-localhost interface using --metrics-bind-addr, protect access with network controls (firewall rules, private network, or service mesh policy).

Output formats

The output format can be controlled by passing --output-format <type> to the executable. Available formats are:

  • Text: A key/value output that quotes all the values. Example: blocked_uri="about:blank" ...
  • JSON: Single line, compressed JSON object. Example: {"blocked_uri":"about:blank"}

The default formatter is text.

Writing to a file instead of just STDOUT

If you'd rather have these violations end up in a file, I suggest just redirecting the output into a file like so:

$ ./csp_collector 2>> /path/to/violations.log

Visualisation

This project purposely doesn't try to solve the problem of visualing the violation data because there are already a bunch of great solutions out there. Once you have your violations being collected, be sure to slurp them into your favourite log aggregation tool.

Deployments

Currently supported deployment mechanisms:

Release

  • Checkout master.
  • Create a tag and push.
  • GitHub Actions will build, publish a new release, and publish the Helm chart.

Similar Projects

Discover other similar projects and AI models.

RepositoryGo
avelino/awesome-go

A curated list of awesome Go frameworks, libraries and software

197.4k
Dec 3, 2026
RepositoryGo
ollama/ollama

Get up and running with Inkling, Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

189.0k
Dec 3, 2026
RepositoryRuby
github/secure_headers

Manages application of security headers with many safe defaults

3.2k
Nov 25, 2026
RepositoryPHP
nico3333fr/CSP-useful

Collection of scripts, thoughts about CSP (Content Security Policy)

499
Jul 10, 2026
RepositoryPHP
aidantwoods/SecureHeaders

A PHP library aiming to make the use of browser security features more accessible.

429
Nov 17, 2023
RepositoryPython
moloch--/CSP-Bypass

A Burp Plugin for Detecting Weaknesses in Content Security Policies

166
May 19, 2023
RepositoryTypeScript
lisonge/Disable-CSP

A browser extension to disable http header Content-Security-Policy and html meta Content-Security-Policy

102
Jan 9, 2026
RepositoryPHP
wobqqq/oc-fortify-csp-plugin

🛡️ Content-Security-Policy header builder for October CMS

85
Nov 9, 2026
RepositoryJavaScript
dansmaculotte/nuxt-security

Module for Nuxt.js to configure security headers and more

55
Dec 13, 2024