TSecCSRF

tsec.csrf.TSecCSRF
See theTSecCSRF companion object
final class TSecCSRF[F[_], A]

Middleware to avoid Cross-site request forgery attacks. More info on CSRF at: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)

This middleware is modeled after the double submit cookie pattern: https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet#Double_Submit_Cookie

When a user authenticates, embedNew is used to send a random CSRF value as a cookie. (Alternatively, an authenticating service can be wrapped in withNewToken).

For requests that are unsafe (PUT, POST, DELETE, PATCH), services protected by the validated method in the middleware will check that the csrf token is present in both the header headerName and the cookie cookieName. Due to the Same-Origin policy, an attacker will be unable to reproduce this value in a custom header, resulting in a 401 Unauthorized response.

Requests with safe methods (such as GET, OPTIONS, HEAD) will have a new token embedded in them if there isn't one, or will receive a refreshed token based off of the previous token to mitigate the BREACH vulnerability. If a request contains an invalid token, regardless of whether it is a safe method, this middleware will fail it with 401 Unauthorized. In this situation, your user(s) should clear their cookies for your page, to receive a new token.

We'd like to emphasize that you please follow proper design principles in creating endpoints, as to not mutate in what should otherwise be idempotent methods (i.e no dropping your DB in a GET method, or altering user data). If you choose to not to, this middleware cannot protect you.

Value parameters

clock

clock used as a nonce

cookieName

the CSRF cookie name

headerName

your CSRF header name

key

the CSRF signing key

Attributes

Companion
object
Source
TSecCSRF.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

def checkEqual(token1: CSRFToken, token2: CSRFToken): OptionT[F, Boolean]

Attributes

Source
TSecCSRF.scala
def embedNew(response: Response[F]): F[Response[F]]

Attributes

Source
TSecCSRF.scala
def extractRaw(token: CSRFToken): OptionT[F, String]

Extract a signed token

Extract a signed token

Attributes

Source
TSecCSRF.scala
def filter(predicate: Request[F] => Boolean, request: Request[F], service: HttpRoutes[F]): OptionT[F, Response[F]]

Attributes

Source
TSecCSRF.scala

Attributes

Source
TSecCSRF.scala
def isEqual(s1: String, s2: String): Boolean

Attributes

Source
TSecCSRF.scala
def signToken(string: String): F[CSRFToken]

Attributes

Source
TSecCSRF.scala
def validate(predicate: Request[F] => Boolean): () => F

Attributes

Source
TSecCSRF.scala
def withNewToken: () => F

Attributes

Source
TSecCSRF.scala

Concrete fields

Attributes

Source
TSecCSRF.scala

Attributes

Source
TSecCSRF.scala

Attributes

Source
TSecCSRF.scala