Using inline scripts and styles | laravel-csp-rust | Spatie       

 SPATIE  

laravel-csp-rust
================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-csp-rust](https://spatie.be/docs/laravel-csp-rust/v1)  Using inline scripts and styles

 Version   v1      

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

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

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

###  On this page 

1. [ Integration with Vite ](#content-integration-with-vite)
2. [ Custom nonce generators ](#content-custom-nonce-generators)

When using CSP, you must specifically allow inline scripts or styles. The recommended way is to use a nonce. A nonce is unique per response and appears in both the CSP header and the HTML tag.

The `Basic` preset adds nonces for scripts and styles. In a custom preset, use `policy.add_nonce(Directive::Script, nonce)?` and `policy.add_nonce(Directive::Style, nonce)?` with the nonce passed to `configure`.

Add the nonce to your template's script and style tags using Laravel's built-in helper:

```
<style nonce="{{ csp_nonce() }}">
    /* ... */
</style>
<script nonce="{{ csp_nonce() }}">
    // ...
</script>
```

In Rust handlers, `laravel::view::csp_nonce()` returns the same nonce as an `Option<String>`. The middleware initializes it before rendering the response. Nested CSP middleware reuse it, and concurrent requests do not share it. Do not generate a second nonce in a template or handler. Do not cache nonce-bearing HTML separately from its matching response headers.

Integration with Vite
-----------------------------------------------------------------------------------------------------------------------

Laravel already adds the registered nonce to tags generated by `vite(...)` and `vite_react_refresh()`. This package calls `laravel::view::use_csp_nonce` and reads `laravel::view::csp_nonce`; there is no separate CSP nonce store or Vite adapter to configure.

```
{{ vite("resources/js/app.tsx") }}
```

If earlier middleware has already called `view::use_csp_nonce`, its nonce is reused. Register nonce-producing middleware before `AddCspHeaders`; changing the nonce after tags have rendered can make the header and HTML disagree.

Custom nonce generators
-----------------------------------------------------------------------------------------------------------------------------

Implement `NonceGenerator::generate(&self) -> Result<String, Error>` and set `config.nonce_generator = Arc::new(MyGenerator)`. Return a base64 value derived from at least 16 bytes of cryptographically secure random data. The default `RandomNonce` uses 32 bytes from the operating system random source. A generator failure fails the request instead of silently serving an unprotected page.

The generator only returns the nonce. The middleware registers it through Laravel's native API. Custom generators must not return a fixed nonce in production. The package checks the base64 character set, but cannot verify a custom generator's entropy.

Set `CSP_NONCE_ENABLED=false` to disable automatic generation and nonce expressions. This does not erase a nonce owned by earlier application middleware. Your policy will need another way to permit any required inline content. Allowing `unsafe-inline` weakens protection.

See Laravel's [Content Security Policy nonce guide](https://laravel.com/rust/docs/templates#content-security-policy-nonces).

 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)
