starlette-csrf
Starlette middleware implementing Double Submit Cookie technique to mitigate CSRF
What it is and what it does
Starlette CSRF is a middleware package that guards Starlette and FastAPI applications against cross-site request forgery attacks. It implements the Double Submit Cookie pattern: on safe HTTP methods (GET, HEAD, OPTIONS, TRACE by default), it sets a cookie containing a secret token; on unsafe methods (POST, PUT, DELETE, PATCH), it requires the same token to be sent in a request header, comparing the two to verify the request originated from your application.
The middleware is configurable for different authentication schemes, URL patterns, and cookie settings. You can exempt specific URLs from CSRF checks, enforce checks on particular URLs regardless of method, or restrict CSRF validation to requests that carry certain cookies. It depends only on itsdangerous (for token signing) and starlette itself, making it lightweight to integrate into existing Starlette or FastAPI applications.
Use it for:
- Protect form submissions and API endpoints in browser-based Starlette applications from CSRF attacks.
- Customize CSRF error responses (e.g., return JSON instead of plain text) by subclassing the middleware.
- Exempt specific API routes that don't require CSRF protection while enforcing it elsewhere.
- Configure different cookie domains to allow frontend and API subdomains to share CSRF tokens.
- Restrict CSRF checks to requests carrying session cookies while allowing stateless API calls.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Starlette middleware that protects web applications from CSRF attacks using the Double Submit Cookie technique, validating that unsafe requests include a matching token in both a cookie and a request header.
Yes, if you are building a Starlette or FastAPI application with cookie-based authentication and need CSRF protection. The package is stable, has no known vulnerabilities, and integrates cleanly with minimal dependencies. However, note that maintenance is aging (last release June 2023); verify compatibility with your current Starlette version and monitor the repository for any future security updates.
Install
starlette-csrf on PyPI
pip
pip install starlette-csrfuv
uv add starlette-csrfpoetry
poetry add starlette-csrfInstalling starlette-csrf
Before you install
Low friction installation with only two runtime dependencies (itsdangerous and starlette). Maintenance status is aging—last release was 2023-06-27 and no commits since 2025-03-15—but the package is marked Production/Stable and has seen no security vulnerabilities.
License in practice
MIT license (permissive) means you can use, modify, and distribute this package freely in both open-source and commercial projects with minimal restrictions.
Quickstart
pip install starlette-csrf
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette_csrf import CSRFMiddleware
middleware = [Middleware(CSRFMiddleware, secret="__CHANGE_ME__")]
app = Starlette(routes=routes, middleware=middleware)
Requires a strong secret passphrase to be set; using the default placeholder will not provide security.
Verify before relying
- Whether the aging maintenance status (last release June 2023) affects compatibility with current Starlette versions.
- Performance impact of CSRF token validation on high-traffic applications.
- Whether cookie_secure=False default is appropriate for production deployments.
Package facts
| License | not declared (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — itsdangerous, starlette |
| Maintenance | aging — 1,144 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 267,044/month — #8,298 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: starlette_csrf-3.0.0-py3-none-any.whl
Tags
More Session packages
Flask-Session adds server-side session storage…
permissive · top 5,000 on PyPI
httpx-wsAdds WebSocket support to HTTPX, enabling sync…
permissive · top 5,000 on PyPI
fastapi-usersProvides ready-to-use user registration, login,…
permissive · top 5,000 on PyPI
fastapi-users-db-sqlalchemyProvides a SQLAlchemy ORM adapter for FastAPI…
permissive · top 5,000 on PyPI
django-rest-knoxProvides token-based authentication for Django…
permissive · top 5,000 on PyPI
supertokens-pythonInterfaces a Python API with the SuperTokens…
permissive · top 15,000 on PyPI
asgi-csrfASGI middleware that protects web applications…
permissive · top 15,000 on PyPI
starlette-contextMiddleware for Starlette that stores and…
permissive · top 5,000 on PyPI
fastapi-csrf-protectAdds stateless CSRF protection to FastAPI…
permissive · top 15,000 on PyPI
SecwebSecweb applies security headers (CSP, HSTS,…
copyleft · top 15,000 on PyPI
starsessionsProvides session management middleware for…
permissive · top 15,000 on PyPI
starlette-compressMiddleware for Starlette and FastAPI that…
permissive · top 5,000 on PyPI
piccolo-apiExposes Piccolo ORM tables as REST endpoints in…
permissive · top 15,000 on PyPI
starletteStarlette is a lightweight ASGI framework for…
permissive · top 100 on PyPI
asgi-correlation-idASGI middleware that reads or generates…
permissive · top 5,000 on PyPI
secureApplies HTTP security headers to Python web…
permissive · top 5,000 on PyPI