asgi-csrf
ASGI middleware for protecting against CSRF attacks
What it is and what it does
asgi-csrf is ASGI middleware that defends web applications against Cross-Site Request Forgery attacks by implementing the Double Submit Cookie pattern. It automatically sets a CSRF token cookie on incoming requests and validates that token in subsequent POST requests—either as a hidden form field or as an x-csrftoken HTTP header. The middleware depends on itsdangerous for token signing and python-multipart for parsing multipart form data.
The middleware is configured by wrapping your ASGI application and providing a signing secret. It offers fine-grained control over cookie behavior (name, path, domain, secure flag, SameSite policy), can skip protection for API routes or Bearer-token requests, and supports custom error handlers. Requests without cookies or with Bearer authentication are allowed through by default, though specific paths can be marked for always-protection to defend against login CSRF.
Use it for:
- Protect traditional server-rendered web forms from CSRF attacks by validating tokens on POST requests.
- Defend login endpoints against login CSRF by marking them with always_protect to require tokens even from unauthenticated users.
- Skip CSRF checks for REST API endpoints while protecting form-based routes using skip_if_scope callbacks.
- Customize CSRF error responses for different content types or to match your application's error handling style.
- Configure cookie security settings (HTTPS-only, SameSite) to align with your deployment environment and security policy.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
ASGI middleware that protects web applications against CSRF attacks by implementing the Double Submit Cookie pattern, validating tokens in form fields or HTTP headers.
Yes, with conditions. asgi-csrf is a straightforward, low-friction implementation of a standard CSRF defense pattern with no known vulnerabilities. Install it if you are building traditional server-rendered ASGI applications that handle form submissions. However, note that maintenance is dormant (no releases in 637 days)—verify compatibility with your specific ASGI framework and Python version before committing to production, and be prepared to fork or switch if critical issues arise.
Install
asgi-csrf on PyPI
pip
pip install asgi-csrfuv
uv add asgi-csrfpoetry
poetry add asgi-csrfInstalling asgi-csrf
Before you install
Low install friction with just two runtime dependencies. Maintenance is dormant—last commit was 2024-11-15 and no releases in the past 637 days—but the repository is not archived and the package remains functional for current Python versions.
License in practice
Apache-2.0 is permissive; you can use, modify, and distribute this package freely in commercial and private projects with minimal restrictions.
Quickstart
pip install asgi-csrf
from asgi_csrf import asgi_csrf
app = asgi_csrf(app, signing_secret="your-secret-key")
# In templates, include the token:
# <input type="hidden" name="csrftoken" value="{{ request.scope['csrftoken']() }}" />
Requires Python 3.9 or later; signing_secret should be configured explicitly or via ASGI_CSRF_SECRET environment variable to persist across server restarts.
Verify before relying
- Whether the dormant maintenance status (no releases in 637 days) affects compatibility with recent ASGI framework versions or Python 3.12+.
- Real-world performance impact when handling multipart form data with the python-multipart dependency.
Package facts
| License | Apache-2.0 (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — itsdangerous, python-multipart |
| Maintenance | dormant — 637 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 166,658/month — #10,484 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: asgi_csrf-0.11-py3-none-any.whl
Tags
More Security packages
Provides Python bindings to the FreeDesktop.org…
permissive · top 1,000 on PyPI
msalMSAL for Python handles OAuth2 and OpenID…
permissive · top 1,000 on PyPI
joserfcjoserfc implements JOSE standards (JWS, JWE,…
permissive · top 1,000 on PyPI
AuthlibAuthlib provides a complete implementation of…
permissive · top 1,000 on PyPI
argon2-cffi-bindingsProvides low-level CFFI bindings to the…
permissive · top 1,000 on PyPI
adalADAL for Python authenticates applications with…
permissive · top 1,000 on PyPI
fastapi-csrf-protectAdds stateless CSRF protection to FastAPI…
permissive · top 15,000 on PyPI
starlette-csrfStarlette middleware that protects web…
permissive · top 15,000 on PyPI
piccolo-apiExposes Piccolo ORM tables as REST endpoints in…
permissive · top 15,000 on PyPI
django-cookie-consentA Django application that manages cookie…
permissive · top 15,000 on PyPI
flask-talismanFlask extension that automatically sets HTTP…
permissive · top 5,000 on PyPI
Flask-ParanoidFlask-Paranoid detects and blocks session…
permissive · top 15,000 on PyPI
safehttpxWraps httpx.AsyncClient.get() with DNS…
permissive · top 5,000 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
tufTUF is a Python reference implementation of The…
permissive · top 5,000 on PyPI