Outputting a CSP policy as a meta tag | 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)  Outputting a CSP policy as a meta tag

 Version   v1      

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

  Outputting a CSP policy as a meta tag    
- [ 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)

 Outputting a CSP policy as a meta tag
=====================================

In rare circumstances, a large site may have so many external connections that the CSP header exceeds the maximum header size. Or you might be generating a static page and don't control the headers when the response is sent. The [CSP specification](https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP) allows for outputting policy information as a meta tag in the head of a webpage.

This package provides a `cspMetaTag` template function:

```
<head>
    {{cspMetaTag}}
</head>
```

For meta-only use, initialize the nonce without emitting CSP headers:

```
Middleware: func(m *laravel.Middleware, _ *config.Config) {
    m.Global.Append(csp.AddCspHeaders{MetaOnly: true})
},
```

The helper renders the configured enforcing policy. To use a specific preset from a handler:

```
tag, err := csp.From(r).MetaTag(r, MyPreset{})
if err != nil {
    return web.Fail(err)
}
return web.View("page", map[string]any{"CSP": tag})
```

Place `{{.CSP}}` in the view's `<head>`. The result is escaped `template.HTML`, so Go templates do not double-escape the generated tag.

For a standalone policy, call `policy.MetaTag()`. An empty policy produces no tag.

Unlike PHP, this port does not output report-only meta tags. Browsers ignore report-only policies in meta tags. The `report-uri`, `report-to`, `sandbox`, and `frame-ancestors` directives are also omitted because they do not work there. Use HTTP headers when you need these features. The meta helper never emits the configured report-only policy.

 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)
