yesod-csp
Add CSP headers to Yesod apps
| LTS Haskell 23.28: | 0.2.6.0 |
| Stackage Nightly 2026-11-25: | 0.3.0.0 |
| Latest on Hackage: | 0.3.0.0 |
yesod-csp-0.3.0.0@sha256:a02bdda651ead869990183432ec96559d8e79f5942265d99d2c65e7bd7279023,2382Module documentation for 0.3.0.0
yesod-csp
The aim of this library is to make it easy to add correct Content Security Policy headers to your responses. This reduces the risk of loading bad assets or scripts.
Using the data types
The following code:
getHomeR :: Handler Html
getHomeR = do
cspPolicy [ScriptSrc (Self :| []), StyleSrc (Https :| [Self])]
defaultLayout [whamlet|hello|]
will ensure that a Content-Security-Policy: script-src 'self'; style-src https: 'self' header is set. In this example we only want to load scripts from our own domain, and we only want styles that come from our domain or over https.
Use getCspPolicy to see the header a policy produces, and check that it matches what you need.
Adding a policy to every response
cspMiddleware is a WAI middleware that adds the same policy to every response:
main :: IO ()
main = toWaiApp App >>= run 3000 . cspMiddleware [DefaultSrc (Self :| [])]
A header set by a handler with cspPolicy is kept as well. The browser then enforces both policies, so a resource must be allowed by each.
Nonces
A nonce only protects you if it is unpredictable and changes with every response. getCspNonce returns a random nonce for the current request, and returns the same value on every call within that request:
getHomeR :: Handler Html
getHomeR = do
n <- getCspNonce
cspPolicy [ScriptSrc (nonce n :| [])]
defaultLayout [whamlet|<script nonce=#{n}>alert("hi");|]
Template Haskell support
The csp quasi-quoter from Yesod.Csp.TH lets you write policies in the familiar CSP syntax instead of building the data types yourself. The policy is parsed at compile time, so a malformed policy is a compile error:
getHomeR :: Handler Html
getHomeR = do
cspPolicy [csp|img-src 'self' https:; script-src https://foo.com|]
...
You can add URLs and nonces from the surrounding scope with $:
getHomeR :: Handler Html
getHomeR = do
let url = fromJust (escapeAndParseURI "https://cdn.example.com")
n <- getCspNonce
cspPolicy [csp|img-src 'self' $url; script-src $nonce-n|]
...
Examples
This module contains a host of runnable example Yesod handlers which set various CSP headers.
Changes
Changelog
Version 0.3.0.0
- Breaking:
BaseUrinow takes aSourceListinstead of anEscapedURI, sobase-uri 'none'andbase-uri 'self'can be expressed - Fix directive injection via
report-to: characters that are not valid in an HTTP token are now dropped from the group name - Restrict nonces to base64 and base64url characters; commas, line breaks and other characters are now dropped
- Escape commas in URIs so they can no longer split the header into several policies
- Fix directive injection via
plugin-types: onlytype/subtypeis rendered, restricted to valid MIME type characters - Add hash sources,
'unsafe-hashes'and'wasm-unsafe-eval' - Add
upgrade-insecure-requests,require-trusted-types-forandtrusted-types - Add the remaining sandbox flags, such as
allow-popupsandallow-modals - Add
cspPolicyReportOnlyandcspMiddlewareReportOnlyforContent-Security-Policy-Report-Only - Add
cspWarnings, which lists risky combinations such asdata:inscript-src escapeAndParseURIkeeps a leading*.in the host, so wildcard subdomains such ashttps://*.example.comcan be expressed- Add
getCspNonce, which returns a random nonce per request, and use it in the examples instead of a fixed nonce - Accept the standard quoted keywords
'unsafe-inline','unsafe-eval'and'strict-dynamic'in thecspquasi-quoter - Stop rendering a trailing space for
MetaSource - Document that
*and'none'drop other sources, thatplugin-typesandprefetch-srcare obsolete, and thatcspMiddlewarekeeps headers set by handlers
Version 0.2.8.0
- Add support for CSP Level 3 directives:
report-to,worker-src,manifest-srcandprefetch-src
Version 0.2.7.1
- Relax version boundaries on template-haskell to be able to build with ghc-9.10 on nixos-25.11
Version 0.2.7.0
- Add support for remaining CSP Level 2 directives (see https://github.com/pSub/yesod-csp/pull/5)
Version 0.2.6.0
- Fixes compilation failure with ghc-9.0.2 or newer (see https://github.com/bobjflong/yesod-csp/issues/8)