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

 Version   v1      

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

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

  Laravel-csp-go 
================

 Set Content Security Policy headers in Laravel Go apps 

### Useful links

- [Repository](https://github.com/spatie/laravel-csp-go)
- [Discussions](https://github.com/spatie/laravel-csp-go/discussions)

Introduction
------------

###  On this page 

1. [ About this port ](#content-about-this-port)
2. [ Differences from PHP ](#content-differences-from-php)

By default, all scripts on a webpage are allowed to send and fetch data to any site they want. This can be a security problem. Imagine one of your JavaScript dependencies sends all keystrokes, including passwords, to a third party website.

It's very easy for someone to hide this malicious behaviour, making it nearly impossible for you to detect it unless you manually read all the JavaScript code on your site.

Setting Content Security Policy headers helps solve this problem. These headers dictate which sites your site is allowed to contact. This package makes it easy for you to set the right headers.

This documentation does not aim to fully explain all the possible usages of CSP and its directives. We highly recommend that you read [Mozilla's documentation on Content Security Policy](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) before using this package.

Start with [installation and setup](installation-and-setup), then read [usage](usage).

About this port
-----------------------------------------------------------------------------------------------------

This is the Go port of [spatie/laravel-csp](https://github.com/spatie/laravel-csp), based on PHP commit `b878b5f33160822a39129022f28e274d1d3f0808`. The [Rust port](https://github.com/spatie/laravel-csp-rust) at `533820f5890177b3b329dde47114c454e1d0ff9c` informed nonce handling, input validation, and meta tag restrictions.

The PHP package has no separate `docs/` tree at that revision. These pages follow its README sections and wording, using the same page hierarchy as the Rust documentation.

All 48 service presets and 29 directives are included. Presets are replaceable Go interfaces and the nonce generator is a replaceable function. There are no model or job extension points in the PHP package. The port has no prerequisite Spatie Go module.

Differences from PHP
--------------------------------------------------------------------------------------------------------------------

- Configure the package in Go through `New` callbacks instead of publishing a PHP config file. Registration does not add middleware automatically.
- Keywords are quoted constants, such as `csp.Self`. Raw strings remain raw. Use variadic sources, `AddTo` for several directives, and an empty argument list for valueless directives.
- Builder errors are sticky. `Err()` or `Contents()` returns the first error and serialization never emits an incomplete policy.
- Header values reject non-ASCII text, control characters, and semicolons. Use ASCII URLs and punycode domain names. Nonces reject characters outside the CSP base64 alphabet. Custom generators must supply at least 16 bytes of cryptographic randomness.
- Nonces use 32 random bytes encoded as base64 and are stored in Laravel's native request context, shared with Vite and views. They are never cached across requests.
- Report-only policies, reporting directives, `sandbox`, and `frame-ancestors` are not emitted in meta tags because browsers do not support them there. Meta tag contents are escaped.
- `LaravelPulse` preserves the PHP preset's source rules for serving compatible dashboard assets. It does not install PHP's Pulse or Livewire in Go. Most applications should not use that preset.
- Register CSP middleware before rendering views. For meta-only pages, use `MetaOnly: true`. Existing enforcing or report-only headers take precedence over global policies. Debug server errors and hot reload bypass headers as in PHP.

 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)
