csrf#

class litestar.middleware.csrf.CSRFMiddleware#

Bases: MiddlewareProtocol

CSRF Middleware class.

This Middleware protects against attacks by setting a CSRF cookie with a token and verifying it in request headers.

__init__(app: ASGIApp, config: CSRFConfig) None#

Initialize CSRFMiddleware.

Parameters:
  • app – The next ASGI app to call.

  • config – The CSRFConfig instance.

async __call__(scope: Scope, receive: Receive, send: Send) None#

ASGI callable.

Parameters:
  • scope – The ASGI connection scope.

  • receive – The ASGI receive function.

  • send – The ASGI send function.

Returns:

None

create_send_wrapper(send: Send, token: str, csrf_cookie: str | None) Send#

Wrap send to handle CSRF validation.

Parameters:
  • token – The CSRF token.

  • send – The ASGI send function.

  • csrf_cookie – CSRF cookie.

Returns:

An ASGI send function.