MIT licensed by Bob Long
Maintained by Pascal Wittmann
This version can be pinned in stack with:yesod-csp-0.3.0.0@sha256:a02bdda651ead869990183432ec96559d8e79f5942265d99d2c65e7bd7279023,2382

Module 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: BaseUri now takes a SourceList instead of an EscapedURI, so base-uri 'none' and base-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: only type/subtype is rendered, restricted to valid MIME type characters
  • Add hash sources, 'unsafe-hashes' and 'wasm-unsafe-eval'
  • Add upgrade-insecure-requests, require-trusted-types-for and trusted-types
  • Add the remaining sandbox flags, such as allow-popups and allow-modals
  • Add cspPolicyReportOnly and cspMiddlewareReportOnly for Content-Security-Policy-Report-Only
  • Add cspWarnings, which lists risky combinations such as data: in script-src
  • escapeAndParseURI keeps a leading *. in the host, so wildcard subdomains such as https://*.example.com can 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 the csp quasi-quoter
  • Stop rendering a trailing space for MetaSource
  • Document that * and 'none' drop other sources, that plugin-types and prefetch-src are obsolete, and that cspMiddleware keeps headers set by handlers

Version 0.2.8.0

  • Add support for CSP Level 3 directives: report-to, worker-src, manifest-src and prefetch-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

Version 0.2.6.0