CVE-2021-32677: Fastapi Cross-Site Request Forgery (CSRF) Vulnerability

HIGH
Advisory Identifier: GHSA-8h2j-cgx8-6xv7
CVSS 8.2
CWE-352
Published
Advisory Summary

CVE-2021-32677 is a HIGH (CVSS 8.2) cross-site request forgery (csrf) vulnerability (CWE-352: Cross-Site Request Forgery (CSRF)) affecting Fastapi libraries in the PyPI ecosystem. The issue involves cross-site request forgery (csrf) in fastapi. Remediation is available by upgrading affected packages to version 0.65.2.

CVE-2021-32677 at a Glance

CVE Identifier CVE-2021-32677
GHSA Identifier GHSA-8h2j-cgx8-6xv7
Severity HIGH
CVSS v3.1 Base Score 8.2
CWE Classification CWE-352: Cross-Site Request Forgery (CSRF)
Affected Product Fastapi
Package Ecosystem PyPI
Published Date
Last Updated

Security Impact

CVSS: 8.2

The security impact metrics below are parsed directly from the CVSS v3.1 vector string (CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N).

Attack Vector Network (AV:N)
Attack Complexity Low (AC:L)
Privileges Required None (PR:N)
User Interaction None (UI:N)
Scope Unchanged (S:U)
Confidentiality Low (C:L)
Integrity High (I:H)
Availability None (A:N)

How to Fix CVE-2021-32677

To remediate this vulnerability, upgrade all affected Fastapi dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

fastapi fixed: 0.65.2
pip install fastapi>=0.65.2
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

pip show fastapi

How to Check if You Are Affected

To determine whether your Python environment is affected by CVE-2021-32677, inspect your installed packages using pip:

pip show fastapi

Analyze the terminal output to locate any version of Fastapi in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 0.65.2), your application is affected.

Note: The pip show fastapi command checks currently installed packages in your active Python environment.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-352: Cross-Site Request Forgery (CSRF), where the web application does not, or cannot, sufficiently verify whether a request was intentionally provided by the user who sent the request, which could have originated from an unauthorized actor. In Fastapi, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### 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](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) preflights, for being considered [Simple requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#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 * [CORS on Mozilla web docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) * [This answer on StackExchange](https://security.stackexchange.com/questions/157528/ways-to-bypass-browsers-cors-policy/157531#157531) * [OWASP CSRF](https://owasp.org/www-community/attacks/csrf) * Fixed in PR [#2118](https://github.com/tiangolo/fastapi/pull/2118) ### For more information If you have any questions or comments, write to [security@tiangolo.com](mailto:security@tiangolo.com)

CVE-2021-32677 Affected Fastapi Versions

1 component(s)
Fixed Versions:
0.65.2
Product / Package Ecosystem Affected Versions Fixed Version
fastapi
PyPI
< 0.65.2
0.65.2

References

Frequently Asked Questions

What is CVE-2021-32677?
CVE-2021-32677 is a security vulnerability in Fastapi involving Cross-Site Request Forgery (CSRF) in FastAPI under CWE-352: Cross-Site Request Forgery (CSRF).
Which Fastapi versions are affected?
Affected versions include releases prior to 0.65.2 across affected packages: fastapi.
What version fixes CVE-2021-32677?
Upgrading to 0.65.2 (or the latest patched release for your respective branch) resolves the vulnerability.
Is fastapi affected?
Yes, fastapi is affected in releases prior to 0.65.2. Upgrade your dependency to 0.65.2 or later.
What is the CVSS score of CVE-2021-32677?
CVE-2021-32677 has a CVSS v3.1 base score of 8.2 (HIGH) with vector string CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N.
What CWE is associated with CVE-2021-32677?
CVE-2021-32677 is classified under CWE-352: Cross-Site Request Forgery (CSRF) (The web application does not, or cannot, sufficiently verify whether a request was intentionally provided by the user who sent the request, which could have originated from an unauthorized actor.).
How can I check PyPI dependencies for CVE-2021-32677?
Run the dependency tree command: 'pip show fastapi'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-8h2j-cgx8-6xv7",
  "modified" : "2024-09-20T17:18:26Z",
  "published" : "2021-06-10T15:43:54Z",
  "aliases" : [ "CVE-2021-32677" ],
  "summary" : "Cross-Site Request Forgery (CSRF) in FastAPI",
  "details" : "### Impact\n\nFastAPI 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.\n\nIn 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`).\n\nSo, a request with a content type of `text/plain` containing JSON data would be accepted and the JSON data would be extracted.\n\nBut requests with content type `text/plain` are exempt from [CORS](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS) preflights, for being considered [Simple requests](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#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.\n\n### Patches\n\nThis is fixed in FastAPI `0.65.2`.\n\nThe 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`.\n\n### Workarounds\n\nIt's best to upgrade to the latest FastAPI.\n\nBut 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.\n\n### References\n\n* [CORS on Mozilla web docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS)\n* [This answer on StackExchange](https://security.stackexchange.com/questions/157528/ways-to-bypass-browsers-cors-policy/157531#157531)\n* [OWASP CSRF](https://owasp.org/www-community/attacks/csrf)\n* Fixed in PR [#2118](https://github.com/tiangolo/fastapi/pull/2118)\n\n### For more information\n\nIf you have any questions or comments, write to [security@tiangolo.com](mailto:security@tiangolo.com)",
  "severity" : [ {
    "type" : "CVSS_V3",
    "score" : "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:H/A:N"
  }, {
    "type" : "CVSS_V4",
    "score" : "CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:L/VI:H/VA:N/SC:N/SI:N/SA:N"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "PyPI",
      "name" : "fastapi"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "0"
      }, {
        "fixed" : "0.65.2"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/tiangolo/fastapi/security/advisories/GHSA-8h2j-cgx8-6xv7"
  }, {
    "type" : "ADVISORY",
    "url" : "https://nvd.nist.gov/vuln/detail/CVE-2021-32677"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/tiangolo/fastapi/commit/fa7e3c996edf2d5482fff8f9d890ac2390dede4d"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/pypa/advisory-database/tree/main/vulns/fastapi/PYSEC-2021-100.yaml"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/tiangolo/fastapi"
  }, {
    "type" : "WEB",
    "url" : "https://lists.fedoraproject.org/archives/list/package-announce@lists.fedoraproject.org/message/MATAWX25TYKNEKLDMKWNLYDB34UWTROA"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-352" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2021-06-09T13:34:54Z",
    "nvd_published_at" : "2021-06-09T18:15:00Z"
  }
}