Reporting CSP errors | laravel-csp-go | Spatie       

 SPATIE  

laravel-csp-go
==============

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-csp-go](https://spatie.be/docs/laravel-csp-go/v1)  Reporting CSP errors

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-csp-go/v1) 

  Reporting CSP errors    
- [ Introduction ](https://spatie.be/docs/laravel-csp-go/v1/introduction)
- [ Installation and setup ](https://spatie.be/docs/laravel-csp-go/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-csp-go/v1/usage)
- [ Creating a preset ](https://spatie.be/docs/laravel-csp-go/v1/creating-a-preset)
- [ Using inline scripts and styles ](https://spatie.be/docs/laravel-csp-go/v1/using-inline-scripts-and-styles)
- [ Outputting a CSP policy as a meta tag ](https://spatie.be/docs/laravel-csp-go/v1/outputting-a-csp-policy-as-a-meta-tag)
- [ Reporting CSP errors ](https://spatie.be/docs/laravel-csp-go/v1/reporting-csp-errors)
- [ Testing ](https://spatie.be/docs/laravel-csp-go/v1/testing)
- [ Releasing ](https://spatie.be/docs/laravel-csp-go/v1/releasing)
- [ Changelog ](https://spatie.be/docs/laravel-csp-go/v1/changelog)
- [ Support us ](https://spatie.be/docs/laravel-csp-go/v1/support-us)

 Reporting CSP errors
====================

### In the browser

Instead of outright blocking all violations, you can configure a CSP policy in report-only mode by registering presets in `ReportOnly.Presets`. In this case all requests will be made, but violations will display in your browser's console.

```
csp.New(func(cfg *csp.Config) {
    cfg.ReportOnly.Presets = []csp.Preset{MyPreset{}}
})
```

### To an external URL

Any violations against the policy can be reported to a given URL. There are two CSP directives for this:

- `report-uri` is the original directive. It is deprecated, but still supported by most browsers. Set the URL in `ReportURI` or `CSP_REPORT_URI`.
- `report-to` is the modern replacement. It points to a named endpoint defined in the `Reporting-Endpoints` HTTP header. Set the name in `ReportTo` and the URL in `ReportingEndpoints`.

While the new directive is rolling out across browsers, you can configure both at the same time. Older browsers use `report-uri`, newer ones use `report-to`.

```
csp.New(func(cfg *csp.Config) {
    cfg.ReportURI = "https://example.com/csp-reports"
    cfg.ReportTo = "default"
    cfg.ReportingEndpoints = []csp.ReportingEndpoint{
        {Name: "default", URL: "https://example.com/csp-reports"},
    }
})
```

A service specifically built for handling these violation reports is [report-uri.com](https://report-uri.com).

If enforcing and report-only policies need to report to different endpoints, configure `ReportOnlyURI` and `ReportOnlyTo`:

```
csp.New(func(cfg *csp.Config) {
    cfg.ReportURI = "https://example.report-uri.com/r/d/csp/enforce"
    cfg.ReportOnlyURI = "https://example.report-uri.com/r/d/csp/reportOnly"
    cfg.ReportTo = "enforce"
    cfg.ReportOnlyTo = "preview"
    cfg.ReportingEndpoints = []csp.ReportingEndpoint{
        {Name: "enforce", URL: "https://example.com/enforce"},
        {Name: "preview", URL: "https://example.com/preview"},
    }
})
```

When a report-only endpoint is empty, it reuses the enforcing endpoint. Reporting settings do not activate an otherwise empty policy. Route-specific preset overrides do not inherit global reporting configuration.

This package configures response headers. It does not add a report ingestion route or store reports. Point reporting at your own endpoint or a reporting service. Reporting through meta tags is not supported by browsers.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
