Creating a preset | 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)  Creating a preset

 Version   v1      

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

  Creating a preset    
- [ 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)

 Creating a preset
=================

An example of a CSP directive is `script-src`. If this has the value `'self' www.google.com` then your site can only load scripts from its own domain or `www.google.com`. You'll find [a list with all CSP directives](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy) at Mozilla's developer site.

According to the spec certain directive values need quotes. Examples are `'self'`, `'none'`, and `'unsafe-inline'`. Use the keyword constants to add the quotes. Script and style hashes are detected and quoted automatically.

```
type MyPreset struct{}

func (MyPreset) Configure(p *csp.Policy, nonce string) error {
    return p.Add(csp.Script, csp.Self, "www.google.com").
        Add(csp.Style, "sha256-hash").
        AddNonce(csp.Script, nonce).Err()
}
```

Register your preset alongside the built-in ones:

```
csp.New(func(cfg *csp.Config) {
    cfg.Policy.Presets = []csp.Preset{csp.Basic{}, MyPreset{}}
})
```

For a short preset, use `csp.PresetFunc`:

```
custom := csp.PresetFunc(func(p *csp.Policy, nonce string) error {
    return p.Add(csp.Img, "images.example.com").Err()
})
```

You may also use the same sources for multiple directives:

```
p.AddTo([]csp.Directive{csp.Script, csp.Style}, "www.google.com", "analytics.google.com")
p.Add(csp.Script, csp.UnsafeEval, csp.UnsafeInline)
```

There are a few cases where you don't need to specify a value, such as `upgrade-insecure-requests`. Leave off the values:

```
p.Add(csp.UpgradeInsecureRequests)
```

`csp.None` must be the only value in an add operation. It replaces earlier sources on that directive. Adding later sources replaces `None`. Duplicate sources are removed while preserving their first position.

`Add` returns the builder for chaining. Invalid directives or values set its first error. Always check `Err()` when returning from a preset or `Contents()` before using a standalone policy. A failed builder never serializes partial output. `SetReportURI` and `SetReportTo` replace existing reporting directives.

### Trusted Types

Use `csp.TrustedTypes` to allow specific Trusted Types policy names. Combine it with `csp.RequireTrustedTypesFor` to require Trusted Types for script injection sinks:

```
p.Add(csp.TrustedTypes, "default", "my-policy", csp.AllowDuplicates)
p.Add(csp.RequireTrustedTypesFor, csp.ScriptKeyword)
```

Policy names are not quoted, even if they look like a hash. Use `csp.None` on its own to disallow policy creation, or `"*"` to allow any policy name.

### Standalone policies

Policy construction does not need a running Laravel application:

```
policy := new(csp.Policy).Add(csp.Default, csp.Self)
contents, err := policy.Contents()
```

Or compose presets and directives using `csp.PolicyConfig.Build(nonce)`. Pass an empty nonce to omit nonce handling. The policy builder is mutable and must not be shared across concurrent requests.

 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)
