Using inline scripts and styles | 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)  Using inline scripts and styles

 Version   v1      

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

  Using inline scripts and styles    
- [ 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)

 Using inline scripts and styles
===============================

When using CSP you must specifically allow the use of inline scripts or styles. The recommended way of doing that with this package is to use a nonce. A nonce is a number that is unique per request. The nonce must be specified in the CSP headers and in an attribute on the HTML tag. This way an attacker has no way of injecting malicious scripts or styles without knowing the nonce.

First add the nonce to the right directives in your policy. `Basic` already does this:

```
func (MyPreset) Configure(p *csp.Policy, nonce string) error {
    return p.Add(csp.Script, csp.Self).
        Add(csp.Style, csp.Self).
        AddNonce(csp.Script, nonce).
        AddNonce(csp.Style, nonce).Err()
}
```

Next add the nonce to style and script tags in Laravel's Go HTML templates:

```
<style nonce="{{cspNonce}}">
    /* Your inline styles */
</style>
<script nonce="{{cspNonce}}">
    // Your inline script
</script>
```

To retrieve the nonce in a handler or pass it to another library, use `csp.Nonce(r)`. The middleware initializes it before the handler and view render. The nonce is generated once per request, shared between nested CSP middleware, and never stored on a global service.

There are other options for inline styles and scripts. Take a look at the [CSP docs on Mozilla's developer site](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src) to learn more.

### Integration with Vite

The middleware stores its nonce through Laravel Go's native `vite.WithCSPNonce`. The framework's `{{vite ...}}`, `{{viteReactRefresh}}`, and `{{cspNonce}}` helpers all read the same value.

If earlier middleware has already attached a native Vite nonce, CSP reuses it rather than generating another. Invalid nonce characters fail the request.

Replace the default generator when you need custom behavior:

```
csp.New(func(cfg *csp.Config) {
    cfg.NonceGenerator = func(ctx context.Context) (string, error) {
        return csp.RandomNonce(ctx)
    }
})
```

The generated nonce should be a **base64 value** derived from at least **16 bytes of secure random data**. Never use a fixed nonce outside tests. The default generator uses 32 random bytes. For more details, see the [W3C CSP specification](https://www.w3.org/TR/CSP3/#grammardef-base64-value).

Set `cfg.NonceEnabled = false` or `CSP_NONCE_ENABLED=false` to disable automatic nonce generation and attachment. This can be useful when you need `unsafe-inline` and cannot add nonces, but it makes your policy less secure. Disabling nonces does not add `unsafe-inline` automatically.

Do not cache rendered pages containing a nonce independently of their matching headers. Cached HTML with a newly generated header nonce will not run its inline scripts.

 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)
