Cross-Site Request Forgery (CSRF) in FastAPI
Impact
FastAPI versions lower than 0.65.2 that used cookies for authentication in path operations that received JSON payloads sent by browsers were vulnerable to a Cross-Site Request Forgery (CSRF) attack.
In versions lower than 0.65.2, FastAPI would try to read the request payload as JSON even if the content-type header sent was not set to application/json or a compatible JSON media type (e.g. application/geo+json).
So, a request with a content type of text/plain containing JSON data would be accepted and the JSON data would be extracted.
But requests with content type text/plain are exempt from CORS preflights, for being considered Simple requests. So, the browser would execute them right away including cookies, and the text content could be a JSON string that would be parsed and accepted by the FastAPI application.
Patches
This is fixed in FastAPI 0.65.2.
The request data is now parsed as JSON only if the content-type header is application/json or another JSON compatible media type like application/geo+json.
Workarounds
It's best to upgrade to the latest FastAPI.
But still, it would be possible to add a middleware or a dependency that checks the content-type header and aborts the request if it is not application/json or another JSON compatible content type.
References
For more information
If you have any questions or comments, write to security@tiangolo.com
Software afectado
Haz clic en una tecnología para ver todas sus vulnerabilidades.
| Plataforma | Paquete | Versiones vulnerables | Corregida en |
|---|---|---|---|
| Python (PyPI) | fastapi | < 0.65.2 | 0.65.2 |
Referencias
- https://github.com/tiangolo/fastapi/commit/fa7e3c996edf2d5482fff8f9d890ac2390dede4d
- https://github.com/tiangolo/fastapi/security/advisories/GHSA-8h2j-cgx8-6xv7
- https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/MATAWX25TYKNEKLDMKWNLYDB34UWTROA/
- https://nvd.nist.gov/vuln/detail/CVE-2021-32677
- https://github.com/pypa/advisory-database/tree/main/vulns/fastapi/PYSEC-2021-100.yaml
- https://github.com/tiangolo/fastapi
- https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/MATAWX25TYKNEKLDMKWNLYDB34UWTROA
- Vector CVSS
CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N- Debilidad (CWE)
CWE-352- Plataforma
- Python (PyPI)
- Paquete
fastapi- Publicada
- 09.06.2021
- Actualizado
- 11.10.2026 13:19
Fuentes y licencias
OSV / GitHub Advisory Database - PYSEC-2021-100 - PyPA Advisory Database; CC BY 4.0
OSV / GitHub Advisory Database - GHSA-8h2j-cgx8-6xv7 - CC BY 4.0
Historial de vulnerabilidades: fastapi
Todas las vulnerabilidades publicadas de este paquete (Python (PyPI)), de la más reciente a la más antigua. Muestra si es un fallo aislado o un paquete que hay que vigilar.
- Entradas en total
- 2
- Primera → última
- 06.2021 02.2024
- Sin corrección
- 0
- Explotada en ataques
- 0
Distribución por gravedad
- Alta 2
Divulgaciones por año
- 2021: 1
- 2022: 0
- 2023: 0
- 2024: 1
- 2025: 0
- 2026: 0
-
Cross-Site Request Forgery (CSRF) in FastAPI esta entrada
Identifica las vulnerabilidades de tu aplicación
Con la integración de DockRay y el inventario de software activado, DockRay compara las versiones notificadas de los componentes con las vulnerabilidades conocidas y avisa de las coincidencias confirmadas. La cobertura depende de la tecnología compatible y de los avisos disponibles.