Usage | 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)  Usage

 Version   v1      

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

  Usage    
- [ 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)

 Usage
=====

###  On this page 

1. [ Service presets ](#content-service-presets)

After registering the provider, add the middleware globally to set the headers on every response:

```
use spatie_csp::{AddCspHeaders, service_provider};

let app = Application::configure()
    .with_providers(|providers| {
        providers.add(service_provider());
    })
    .with_middleware(|middleware, _| {
        middleware.append(AddCspHeaders::new());
    })
    .create();
```

The default `Basic` preset allows images, scripts, styles, connections and form actions from your own site. It blocks objects, and permits inline scripts and styles carrying the request nonce. It also restricts `base-uri`, fonts, frames and media to your own site.

You can add presets and directives in `with_config`. Application configuration runs after package defaults:

```
config.policy.preset(presets::GoogleFonts);
config.policy.directives.add(Directive::Script, "www.google.com")?;
config.report_only.preset(presets::Basic);
config.report_only.directives.add(Directive::Script, Keyword::StrictDynamic)?;
```

To use a middleware only on certain routes, append it to those routes instead of the global stack:

```
route.get("/widget", async || "widget")
    .middleware(AddCspHeaders::using(GoogleFonts));
```

`using` replaces the configured policies for that route. It does not append `Basic`, global directives, reporting endpoints or report-only policies. Compose a custom preset when you need several sets of sources together. A response that already has either CSP header keeps it unchanged, so route middleware takes precedence over global middleware.

Set `CSP_ENABLED=false` to disable the middleware. It skips debug server errors and debug requests while `public/hot` exists, unless `CSP_ENABLED_WHILE_HOT_RELOADING=true`. Production error responses retain their policy.

Service presets
-----------------------------------------------------------------------------------------------------

The package includes `Basic`, `AdobeFonts`, `Alchemer`, `Algolia`, `Bootstrap`, `BunnyFonts`, `Chargebee`, `Clarity`, `CloudflareCdn`, `CloudflareTurnstile`, `CloudflareWebAnalytics`, `Cookiebot`, `Fathom`, `Featurebase`, `Firebase`, `GoogleAds`, `GoogleAnalytics`, `GoogleFonts`, `GoogleLookerStudio`, `GoogleMaps`, `GoogleRecaptcha`, `GoogleTagManager`, `GoogleTlds`, `Hcaptcha`, `HeapAnalytics`, `Hireroad`, `HotJar`, `HubSpot`, `Intercom`, `JQuery`, `JsDelivr`, `LaravelPulse`, `Maze`, `MetaPixel`, `Plain`, `PlausibleAnalytics`, `Posthog`, `Rollbar`, `Sentry`, `Stripe`, `SurveyMonkey`, `ThereThere`, `TicketTailor`, `Tolt`, `TrackJs`, `Vimeo`, `VisualWebsiteOptimizer` and `Whereby` in `spatie_csp::presets`.

Choose only services you actually use. Service presets add sources, not a complete baseline. Some permit `unsafe-inline` or `unsafe-eval` because the service requires it. Review the resulting policy before enabling it. `LaravelPulse` describes the sources required by an existing Pulse installation; it does not install Pulse or Livewire in a Rust application.

Continue with [creating a preset](creating-a-preset), [inline scripts and styles](using-inline-scripts-and-styles), [meta tags](outputting-a-csp-policy-as-a-meta-tag), or [reporting](reporting-csp-errors).

 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)
