CVE-2021-32677
JSON

8.2high

Cross-Site Request Forgery (CSRF) in FastAPI

Severity
8.2high CVSS:3.1 by GitHub_M
Exploit likelihood
0.72% (higher than 55% of CVEs; EPSS, by FIRST)
Published
Last changed
Assigned by
GitHub_M
Also known as
GHSA-8h2j-cgx8-6xv7PYSEC-2021-100

Description

FastAPI is a web framework for building APIs with Python 3.6+ based on standard Python type hints. 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). A request with a content type of text/plain containing JSON data would be accepted and the JSON data would be extracted. Requests with content type text/plain are exempt from CORS preflights, for being considered Simple requests. The browser will 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. 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. It's best to upgrade to the latest FastAPI, but if updating is not possible then 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 can act as a mitigating workaround.

Affected

Product or packageVersionsReported by
tiangolo fastapi< 0.65.2, before 0.65.2CVE List, NVD
fastapi (PyPI)before 0.65.2, before fa7e3c996edf2d5482fff8f9d890ac2390dede4dGitHub Advisory Database, PyPA Advisory Database
fedoraproject fedora34NVD

All scores

GitHub_M8.2high CVSS:3.1CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N
GitHub8.2high CVSS:3.1CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N
GitHubhigh CVSS:4.0CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:L/VI:H/VA:N/SC:N/SI:N/SA:N
NVD8.1high CVSS:3.1CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:N
NVD5.8medium CVSS:2.0AV:N/AC:M/Au:N/C:P/I:P/A:N

Classification

What each source says

CVE List

FastAPI is a web framework for building APIs with Python 3.6+ based on standard Python type hints. 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). A request with a content type of text/plain containing JSON data would be accepted and the JSON data would be extracted. Requests with content type text/plain are exempt from CORS preflights, for being considered Simple requests. The browser will 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. 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. It's best to upgrade to the latest FastAPI, but if updating is not possible then 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 can act as a mitigating workaround.

GitHub Advisory Database
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

NVD (es)

FastAPI es un framework web para construir APIs con Python versión 3.6+ basado en sugerencias de tipo estándar de Python. Las versiones de FastAPI inferiores a la 0.65.2 que usaban cookies para la autenticación en las operaciones de ruta que recibían cargas útiles JSON enviadas por los navegadores eran vulnerables a un ataque de tipo Cross-Site Request Forgery (CSRF). En versiones inferiores a la 0.65.2, FastAPI intentaba leer la carga útil de la petición como JSON incluso si la encabezado content-type enviada no estaba configurada como application/json o un tipo de medio JSON compatible (por ejemplo, application/geo+json). Una petición con un tipo de contenido text/plain que contenga datos JSON será aceptada y los datos JSON serán extraídos. Las peticiones con tipo de contenido text/plain están exentas de preflights CORS, por ser consideradas peticiones simples. El navegador las ejecutará de inmediato, incluyendo las cookies, y el contenido de texto podría ser una cadena JSON que sería analizada y aceptada por la aplicación FastAPI. Esto se ha corregido en FastAPI 0.65.2. Ahora los datos de la petición se analizan como JSON sólo si la encabezado content-type es application/json u otro tipo de medio compatible con JSON como application/geo+json. Es mejor actualizar a la última FastAPI, pero si la actualización no es posible, entonces un middleware o una dependencia que compruebe la encabezado de tipo de contenido y aborte la petición si no es application/json u otro tipo de contenido compatible con JSON puede actuar como una solución de mitigación

References

https://github.com/tiangolo/fastapi/security/advisories/GHSA-8h2j-cgx8-6xv7advisory, third-party-advisory, web
https://github.com/tiangolo/fastapi/commit/fa7e3c996edf2d5482fff8f9d890ac2390dede4dfix, patch, third-party-advisory, web
https://lists.fedoraproject.org/archives/list/package-announce%40lists.fedoraproject.org/message/MATAWX25TYKNEKLDMKWNLYDB34UWTROA/vendor-advisory
https://nvd.nist.gov/vuln/detail/CVE-2021-32677advisory
https://github.com/pypa/advisory-database/tree/main/vulns/fastapi/PYSEC-2021-100.yamlweb
https://github.com/tiangolo/fastapipackage
https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/MATAWX25TYKNEKLDMKWNLYDB34UWTROAweb
https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/MATAWX25TYKNEKLDMKWNLYDB34UWTROA/web

Sources

SourceRecordLicence
CVE ListCVE-2021-32677 changed 2024-08-03CVE Terms of Use
GitHub Advisory DatabaseGHSA-8h2j-cgx8-6xv7 changed 2026-07-08CC-BY-4.0
NVDCVE-2021-32677 changed 2026-06-17Public domain
PyPA Advisory DatabasePYSEC-2021-100 changed 2023-11-08CC-BY-4.0