Installation and setup | 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)  Installation and setup

 Version   v1      

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

  Installation and setup    
- [ 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)

 Installation and setup
======================

###  On this page 

1. [ Configuration ](#content-configuration)

Requires Go 1.27 or newer and Laravel Go `go.laravel.com v0.1.0`.

```
go get github.com/spatie/laravel-csp-go@v0.1.0
```

Install the package explicitly in `bootstrap/app.go`. You can add CSP headers to all responses of your app by registering the middleware globally:

```
import (
    csp "github.com/spatie/laravel-csp-go"
    laravel "go.laravel.com"
    "go.laravel.com/config"
)

func Options() laravel.Options {
    return laravel.Options{
        Packages: []laravel.Package{csp.New()},
        Middleware: func(m *laravel.Middleware, _ *config.Config) {
            m.Global.Append(csp.AddCspHeaders{})
        },
    }
}
```

Installing the Go module alone does not activate the package. Registering `csp.New()` installs configuration and the `cspMetaTag` template helper, but middleware remains explicit. No package-tools dependency or config publishing command is needed.

Alternatively you can apply the middleware on the route or route group level:

```
route.Get("/my-page", showPage).Middleware(csp.AddCspHeaders{})
```

You can also pass a preset to the middleware:

```
route.Get("/my-page", showPage).Middleware(csp.AddCspHeaders{Preset: MyPreset{}})
```

The given preset overrides both configured policies for that route. It does not inherit global reporting endpoints. If a nested route already sets either CSP header, outer middleware preserves it.

Configuration
-----------------------------------------------------------------------------------------------

Pass configuration callbacks to `New`. They run after environment defaults. `DefaultConfig()` exposes the same defaults outside an application:

```
csp.New(func(cfg *csp.Config) {
    cfg.Policy.Presets = []csp.Preset{csp.Basic{}}
    cfg.ReportOnly.Presets = nil
    cfg.Enabled = true
    cfg.NonceEnabled = true
    cfg.EnabledWhileHotReloading = false
    cfg.NonceGenerator = csp.RandomNonce
})
```

Environment variableDefaultConfig field`CSP_ENABLED``true``Enabled``CSP_NONCE_ENABLED``true``NonceEnabled``CSP_ENABLED_WHILE_HOT_RELOADING``false``EnabledWhileHotReloading``CSP_REPORT_URI`empty`ReportURI``CSP_REPORT_TO`empty`ReportTo``CSP_REPORT_ONLY_URI`empty`ReportOnlyURI``CSP_REPORT_ONLY_TO`empty`ReportOnlyTo`Invalid typed environment values stop startup. Empty report-only endpoints fall back to their enforcing counterparts. Set preset lists, additional directives and reporting endpoint lists in code.

Debug server error responses do not receive headers. In debug mode, CSP is also skipped while Vite is hot reloading unless `EnabledWhileHotReloading` is true.

Presets and nonce generators may be shared by concurrent requests and must be concurrency-safe. Policy builders belong to one request. Reusing a `Package` across isolated applications creates fresh configuration for each application.

 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)
