yesod-csp: Add CSP headers to Yesod apps

[ library, mit, web ] [ Propose Tags ] [ Report a vulnerability ]

Add CSP headers to Yesod apps. This helps reduce exposure to XSS attacks and bad assets.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.1.0, 0.2.0.0, 0.2.1.0, 0.2.2.0, 0.2.3.0, 0.2.4.0, 0.2.5.0, 0.2.6.0, 0.2.7.0, 0.2.7.1, 0.2.8.0, 0.3.0.0
Change log CHANGELOG.md
Dependencies attoparsec (>=0.14.4 && <0.15), base (<5), base64-bytestring (>=1.2.1 && <1.3), case-insensitive (>=1.2.1 && <1.3), entropy (>=0.4.1.10 && <0.5), mime (>=0.4.0.0 && <0.5), mono-traversable (>=1.0.15 && <1.1), network-uri (>=2.6.4 && <2.7), semigroups (>=0.20 && <0.21), syb (>=0.7.2 && <0.8), template-haskell (>=2.19.0 && <2.24), text (>=2.0.2 && <2.2), wai (>=3.2.3 && <3.3), yesod (>=1.6.2 && <1.7), yesod-core (>=1.6.25 && <1.7) [details]
License MIT
Author Bob Long
Maintainer Pascal Wittmann <mail@pascal-wittmann.de>
Uploaded by PascalWittmann at 2026-11-24T12:01:04Z
Category Web
Source repo head: git clone https://github.com/pSub/yesod-csp.git
Distributions Stackage:0.3.0.0
Reverse Dependencies 1 direct, 0 indirect [details]
Downloads 5436 total (48 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for yesod-csp-0.3.0.0

[back to package description]

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.