frankie567 / starlette-csrf

Starlette middleware implementing Double Submit Cookie technique to mitigate CSRF
MIT License
63 stars 5 forks source link
asgi csrf fastapi fastapi-middleware starlette starlette-middleware

Starlette CSRF Middleware

Starlette middleware implementing Double Submit Cookie technique to mitigate CSRF.

build codecov PyPI version Downloads

How it works?

  1. The user makes a first request with a method considered safe (by default GET, HEAD, OPTIONS, TRACE).
  2. It receives in response a cookie (named by default csrftoken) which contains a secret value.
  3. When the user wants to make an unsafe request, the server expects them to send the same secret value in a header (named by default x-csrftoken).
  4. The middleware will then compare the secret value provided in the cookie and the header.
    • If they match, the request is processed.
    • Otherwise, a 403 Forbidden error response is given.

This mechanism is necessary if you rely on cookie authentication in a browser. You can have more information about CSRF and Double Submit Cookie in the OWASP Cheat Sheet Series.

Installation

pip install starlette-csrf

Usage with Starlette

from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_csrf import CSRFMiddleware

routes = ...

middleware = [
    Middleware(CSRFMiddleware, secret="__CHANGE_ME__")
]

app = Starlette(routes=routes, middleware=middleware)

Usage with FastAPI

from fastapi import FastAPI
from starlette_csrf import CSRFMiddleware

app = FastAPI()

app.add_middleware(CSRFMiddleware, secret="__CHANGE_ME__")

Arguments

Customize error response

By default, a plain text response with the status code 403 is returned when the CSRF verification is failing. You can customize it by overloading the middleware class and implementing the _get_error_response method. It accepts in argument the original Request object and expects a Response. For example:

from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from starlette_csrf import CSRFMiddleware

class CustomResponseCSRFMiddleware(CSRFMiddleware):
    def _get_error_response(self, request: Request) -> Response:
        return JSONResponse(
            content={"code": "CSRF_ERROR"}, status_code=403
        )

Development

Setup environment

We use Hatch to manage the development environment and production build. Ensure it's installed on your system.

Run unit tests

You can run all the tests with:

hatch run test

Format the code

Execute the following command to apply linting and check typing:

hatch run lint

License

This project is licensed under the terms of the MIT license.