koxudaxi/fastapi-code-generator · Security
fastapi-code-generator: Import-time RCE via route path -> unescaped @app.get('...') decorator
- GHSA ID
- GHSA-p2mm-64f6-gmpp
- CVE ID
- CVE-2026-63471
- Severity
- critical
- Published
- Updated
- CVSS v4
- 9.3 · CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N
Description
Summary
fastapi-code-generator (koxudaxi) emits the OpenAPI path into the generated main.py route decorator as a single-quoted string literal (@app.get('/u/<path>/x')) without escaping the '. A ' in the path closes the decorator string and, via concatenation, injects a live expression that the decorator argument evaluates when main.py is imported (the ASGI server imports it at startup) -- code execution, no request needed. The path is a pure data field. Verified on fastapi-code-generator 0.8.0 / Python 3 / FastAPI.
Details
@app.get('/u/'+(__import__("os").system('…') or "")+'/x', response_model=None)
def get_thing() -> None:
pass
The generator post-processes output with black/isort, so a syntax-breaking payload aborts generation (DoS); a valid-Python concat ('+expr+', kept valid with (… or "")) is written and runs at import. info.description (description='…'), tags, and parameter defaults are '-escaped; the path in @app.get('…') is the unescaped sink. Models come from datamodel-code-generator (repr-escaped, no RCE).
Not a duplicate: distinct product from OpenAPI Generator's python-fastapi backend (different codebase/template/decorator); and distinct from the operation-summary docstring sink in the same product.
PoC
reproduce.sh (+ make_spec.py) attached: generates the app and merely imports main.py; the injected expression runs at decorator evaluation and writes a marker. Verified on 0.8.0.
Impact
Code execution at import/startup in any application/CI that generates a FastAPI app with fastapi-code-generator from an attacker-controlled or attacker-influenced spec and imports/serves it. Estimated Critical, e.g. CVSS:4.0/AV:N/AC:L/AT:N/PR:N/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N.
Suggested fix
Escape the path for the Python string literal (escape ', , newlines via repr) before emitting @app.get('…'), and validate path characters.
CWEs
- CWE-94 · Improper Control of Generation of Code
- CWE-116 · Improper Encoding or Escaping of Output
References
- https://github.com/koxudaxi/fastapi-code-generator/security/advisories/GHSA-p2mm-64f6-gmpp ↗
- https://nvd.nist.gov/vuln/detail/CVE-2026-63471 ↗
- https://github.com/koxudaxi/fastapi-code-generator/commit/49301dbdb82170d7f50877419d994b05cb1fa2bc ↗
- https://github.com/koxudaxi/fastapi-code-generator/commit/b3b9bd66e0b0c4d8c686fda700258104615a1332 ↗
- https://github.com/koxudaxi/fastapi-code-generator/releases/tag/0.8.1 ↗
- https://github.com/advisories/GHSA-p2mm-64f6-gmpp ↗