wai-csrf-0.1: Cross-site request forgery protection for WAI
Safe HaskellNone
LanguageGHC2021

Wai.CSRF

Description

This module exports tool to prevent cross-site request forgeries in Network.Wai. Consider using it in combination with Wai.CryptoCookie.

Synopsis

Documentation

data Config Source #

Config common to middleware, tokenFromRequestHeader and tokenFromRequestCookie.

Consider using defaultConfig and updating desired fields only.

Constructors

Config 

Fields

defaultConfig :: Config Source #

Default CSRF settings.

  • Cookie name is CSRF-TOKEN.
  • Header name is X-CSRF-TOKEN.
  • Reject with forbidden403 all request who are neither GET, HEAD, OPTIONS nor TRACE, unless the Token is present in both cookie and header and they are equal.

tokenFromRequestHeader :: Config -> Request -> Maybe Token Source #

Obtain the Token from the Request headers.

You don't need to use this if you are using middleware.

tokenFromRequestCookie :: Config -> Request -> Maybe Token Source #

Obtain the Token from the Request cookies.

You don't need to use this if you are using middleware.

setCookie :: Config -> Token -> SetCookie Source #

Construct a SetCookie to set the CSRF Token.

The SetCookie has these settings, some of which could be overriden.

  • Cookie name is Config's cookieName.
  • HttpOnly: No, and you shouldn't change this.
  • Max-Age and Expires: This cookie never expires. We recommend relying on server-side expiration instead, as the lifetime of the cookie could easily be extended by a legitimate but malicious client. It is recommended that you rotate the Token each time a new user session is established.
  • Path: /
  • SameSite: Lax.
  • Secure: Yes.
  • Domain: Not set.

expireCookie :: Config -> SetCookie Source #

Construct a SetCookie expiring the cookie named Config's cookieName.

middleware :: Config -> (Maybe Token -> Application) -> Application Source #

Construct a Middleware (almost) that does the following:

  1. Try to find the CSRF Token among the incoming Request cookies (see Config's cookieName).
  2. Use Config's reject to decide if the incoming Request should be rejected.
  3. If the Request wasn't rejected, we pass the Token found in the cookie, if any, to the underlying Application.

Important: This doesn't set any cookie. You must explicitly add setCookie to a Response yourself.

Token

newtype Token Source #

CSRF token.

  • It is safe to send and receive the Token through HTTP cookies and headers.
  • If you need to send or receive the Token as part of the request or response body, use MaskedToken instead.

Constructors

Token (SizedByteArray 32 ByteString) 

Instances

Instances details
Show Token Source # 
Instance details

Defined in Wai.CSRF

Methods

showsPrec :: Int -> Token -> ShowS #

show :: Token -> String #

showList :: [Token] -> ShowS #

Eq Token Source # 
Instance details

Defined in Wai.CSRF

Methods

(==) :: Token -> Token -> Bool #

(/=) :: Token -> Token -> Bool #

randomToken :: MonadRandom m => m Token Source #

A CSRF token is just random 32 bytes. Its meaning and validity depends on how and whether you tie it to a user session.

Masked token

newtype MaskedToken Source #

If you embed a Token as is in a response body when HTTP body compression is enabled, it is possible for a malicious actor to recover the Token through a BREACH attack or similar. In order to prevent that, send a different MaskedToken (generated with randomMaskToken) each time instead.

Constructors

MaskedToken (SizedByteArray 64 Bytes) 

Instances

Instances details
Show MaskedToken Source # 
Instance details

Defined in Wai.CSRF

Eq MaskedToken Source # 
Instance details

Defined in Wai.CSRF

randomMaskToken :: MonadRandom m => Token -> m MaskedToken Source #

unmaskToken <$> randomMaskToken tok and pure tok produce the same output tok.

unmaskToken :: MaskedToken -> Token Source #

unmaskToken <$> randomMaskToken tok and pure tok produce the same output tok.