fastapi-csrf-protect
Stateless implementation of Cross-Site Request Forgery (XSRF) Protection by using Double Submit Cookie mitigation pattern
Decision gist · record as of 2026-08-14
Yes, with conditions. Install if you need lightweight, stateless CSRF protection in FastAPI and can accept that the package is aging (332 days since last release). The implementation is sound, dependencies are minimal, and there are no known vulnerabilities. However, verify compatibility with your FastAPI/Starlette versions and be prepared to maintain or fork if upstream updates lag behind framework changes.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Requires Python 3.9 or above; token validation is async and must be awaited.
- Low install friction with a pure-Python wheel and only four runtime dependencies (itsdangerous, pydantic, pydantic-settings, starlette).
- Maintenance status is aging—last release was 332 days ago—so expect slower response to issues or compatibility updates.
License · maintenance · safety
MIT (permissive) — MIT license (permissive) allows use in commercial and private projects with minimal restrictions; attribution required but no copyleft obligations.
last release 2025-09-16 (332 days)
0 known vulnerabilities (OSV.dev, 2026-08-14) · 154,965 downloads/mo, #10,835 on PyPI
Alternatives
Verify before relying
pip install fastapi-csrf-protect
from fastapi import FastAPI, Depends, Request
from fastapi_csrf_protect import CsrfProtect
from pydantic_settings import BaseSettings
app = FastAPI()
class CsrfSettings(BaseSettings):
secret_key: str = "your-secret"
@CsrfProtect.load_config
def get_csrf_config():
return CsrfSettings()
@app.post("/submit")
async def submit(request: Request, csrf_protect: CsrfProtect = Depends()):
await csrf_protect.validate_csrf(request)
return {"status": "ok"}- Whether the package is actively maintained or in maintenance-only mode given the 332-day gap since last release.
- Real-world performance and security audit status of the Double Submit Cookie implementation.
- Compatibility with the latest FastAPI and Starlette versions beyond what classifiers declare.
What it is and what it does
fastapi-csrf-protect is a FastAPI extension that implements stateless Cross-Site Request Forgery (CSRF) protection using the Double Submit Cookie mitigation pattern. It generates CSRF tokens, stores them in cookies, and validates incoming requests by comparing the token in the cookie against a token in the request header or form body. The package is designed as a lightweight, easy-to-use alternative to heavier CSRF solutions, inspired by flask-wtf.
The library works by generating a pair of tokens (a plain token and a signed token), setting the signed token as a cookie, and requiring the client to send the plain token back in either an HTTP header (X-CSRFToken) or form field. A flexible mode allows accepting tokens from either location, useful for applications mixing server-side rendering with API endpoints. Configuration is done via a Pydantic settings class, and validation failures raise CsrfProtectError exceptions that can be caught with a custom exception handler.
Use it for
- Protect FastAPI form submissions from CSRF attacks by validating tokens in POST/PUT/DELETE requests.
- Add CSRF defense to hybrid applications serving both HTML forms and JSON APIs from the same FastAPI instance.
- Migrate from flask-wtf to FastAPI while keeping a familiar CSRF protection pattern.
- Validate CSRF tokens in AJAX requests by reading them from cookies and sending them in request headers.
- Prevent token reuse by unsetting the CSRF cookie after successful form submission.
Worth the install?
AI-flagged interpretation of the facts on this page. Verify before relying on it.
Yes, with conditions.
Install if you need lightweight, stateless CSRF protection in FastAPI and can accept that the package is aging (332 days since last release). The implementation is sound, dependencies are minimal, and there are no known vulnerabilities. However, verify compatibility with your FastAPI/Starlette versions and be prepared to maintain or fork if upstream updates lag behind framework changes.
Install
fastapi-csrf-protect on PyPI
Before you install
Low install friction with a pure-Python wheel and only four runtime dependencies (itsdangerous, pydantic, pydantic-settings, starlette). Maintenance status is aging—last release was 332 days ago—so expect slower response to issues or compatibility updates.
Requires Python 3.9 or above; token validation is async and must be awaited.
License in practice
MIT license (permissive) allows use in commercial and private projects with minimal restrictions; attribution required but no copyleft obligations.
Quickstart
pip install fastapi-csrf-protect
from fastapi import FastAPI, Depends, Request
from fastapi_csrf_protect import CsrfProtect
from pydantic_settings import BaseSettings
app = FastAPI()
class CsrfSettings(BaseSettings):
secret_key: str = "your-secret"
@CsrfProtect.load_config
def get_csrf_config():
return CsrfSettings()
@app.post("/submit")
async def submit(request: Request, csrf_protect: CsrfProtect = Depends()):
await csrf_protect.validate_csrf(request)
return {"status": "ok"}
Verify before relying
- Whether the package is actively maintained or in maintenance-only mode given the 332-day gap since last release.
- Real-world performance and security audit status of the Double Submit Cookie implementation.
- Compatibility with the latest FastAPI and Starlette versions beyond what classifiers declare.
Package facts
| License | MIT permissive |
| Python support | Supports the current Python release >=3.9 |
| Install friction | Low. Pure-Python wheel |
| Runtime dependencies | 4 packagesitsdangerouspydantic-settingspydanticstarlette |
| Maintenance | Aging 332 days since the last release |
| First released | |
| Downloads | 154,965 / month, #10,835 on PyPI 30-day window, as of 2026-08-14 |
| Known vulnerabilities | None known OSV.dev, checked 2026-08-14 |
| Classifiers | Environment :: Web EnvironmentFramework :: AsyncIOIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.9Topic :: Internet :: WWW/HTTP :: Dynamic ContentTopic :: Software Development :: Libraries :: Python Modules |
Evidence: fastapi_csrf_protect-1.0.7-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “fastapi csrf protection”
- fastapi-csrf-protectAdds stateless CSRF protection to FastAPI applications using the…
- starlette-csrfStarlette middleware that protects web applications from CSRF attacks…
- piccolo-apiExposes Piccolo ORM tables as REST endpoints in ASGI applications and…
Give your agent the search over MCP, or paste the wish link into any chat.
More Python Modules packages
Converts domain names between Unicode and ASCII-compatible encoding (Punycode) according to IDNA 2008 and Unicode Technical Standard 46, with security validation and broader script coverage than the standard library.
Install it if you work with internationalized domain names, need to validate domains, or use HTTP clients that depend on it transitively.
Setuptools is a Python build backend and package management tool that handles building, distributing, and installing Python packages, including support for C/C++ extension modules.
PyYAML parses and emits YAML 1.1 data format, enabling serialization and deserialization of configuration files and Python objects to and from human-readable YAML text.
Pydantic validates Python data structures against type hints, coercing and checking input at runtime to ensure it matches a declared schema.
Provides reusable metadata objects for use with PEP-593 `typing.Annotated` to express common constraints like bounds, collection sizes, and predicates on types.
Install it if you use or build libraries that need to express type constraints in a standardized, inspectable way—or if you want to annotate your own types with…
Provides runtime tools to inspect and introspect Python type annotations, enabling programmatic examination of type hints at execution time.
See also asgi-csrf · starlette-csrf · fastapi-sessions · flask-talisman · edx-drf-extensions · cross-web · safehttpx · fastapi-login · Secweb · piccolo-api