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

 Version   v1      

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

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

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

###  On this page 

1. [ Configuration ](#content-configuration)

This package requires Rust 1.99 or higher and Laravel Rust 0.1.4 or higher within 0.1. Laravel's CSP nonce APIs are required; version 0.1.0 does not provide them.

The first registry release is not yet published. Use the local checkout while developing:

```
[dependencies]
spatie-laravel-csp = { path = "../laravel-csp-rust" }
```

The development checkout currently pins the package-tools 0.1.3 update through a Git dependency override. That prerequisite must be released before installing this package from crates.io. Registry publication remains disabled until the first release is approved. Cargo overrides only apply at a workspace root, so a consuming application must temporarily copy this checkout's `[patch.crates-io]` entry into its own manifest when testing locally.

Register the provider and middleware in `src/app.rs`:

```
use laravel::Application;
use spatie_csp::{AddCspHeaders, Config, service_provider};

let app = Application::configure()
    .with_providers(|providers| {
        providers.add(service_provider());
    })
    .with_config(|config, _| {
        let csp = config.section_mut::<Config>();
        csp.policy.preset(spatie_csp::presets::GoogleFonts);
    })
    .with_middleware(|middleware, _| {
        middleware.append(AddCspHeaders::new());
    })
    .create();
```

The provider uses `spatie-laravel-package-tools` and registers typed settings before the application's configuration. It does not silently register global middleware. No database driver, Inertia feature or template engine is enabled by this package.

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

Use `config.section_mut::<Config>()` inside `with_config` to customize settings:

- `enabled` defaults to true (`CSP_ENABLED`).
- `nonce_enabled` defaults to true (`CSP_NONCE_ENABLED`).
- `enabled_while_hot_reloading` defaults to false (`CSP_ENABLED_WHILE_HOT_RELOADING`).
- `policy.presets` starts with `Basic`; `policy.directives` starts empty.
- `report_only.presets` and `report_only.directives` start empty.
- `report_uri`, `report_to`, `report_only_uri` and `report_only_to` default to empty strings. Their variables are `CSP_REPORT_URI`, `CSP_REPORT_TO`, `CSP_REPORT_ONLY_URI` and `CSP_REPORT_ONLY_TO`.
- `reporting_endpoints` is an ordered list of endpoint name and URL pairs.
- `nonce_generator` defaults to `RandomNonce`. Set an `Arc<dyn NonceGenerator>` to replace it.
- `custom_policy` and `custom_report_only_policy` default to `None`. Set an `Arc<dyn PolicyBuilder>` to replace construction of either policy.

Clear `policy.presets` to start without `Basic`. Environment variables supply defaults; application configuration overrides them. Malformed boolean variables stop startup rather than silently changing security settings.

 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)
