# fastapi-code-generator: Import-time RCE via route path -> unescaped @app.get('...') decorator

fastapi-code-generator emits OpenAPI path into a single-quoted @app.get decorator unescaped, so a quote in the path injects code that runs at import.

- Payout: Research
- Severity: critical
- Program: fastapi-code-generator
- Found by: Gal3m, mrostamipoor
- Disclosed: 2026-11-09
- CVE: CVE-2026-63471
- Write-up quality: 7/10
- Original: https://github.com/advisories/GHSA-p2mm-64f6-gmpp
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-p2mm-64f6-gmpp
- Tags: area:oss domain:oss impact:rce kind:research platform:ghsa sev:critical tech:python technique:chained technique:source-review vuln:rce

**Target:** Python code generator emitting FastAPI route decorators from OpenAPI specs
**Root cause:** The OpenAPI path is written into the generated `@app.get('...')` decorator as a single-quoted string literal without escaping `'`, so a quote in the path closes the string and allows expression injection. Other sinks (description, tags, parameter defaults) are escaped; the path is not.
**Steps:**
1. Craft an OpenAPI spec whose path contains a `'` and a concatenated expression, e.g. `/u/'+(__import__("os").system('…') or "")+'/x`.
2. Run fastapi-code-generator 0.8.0 on the spec to produce main.py.
3. Generated code: `@app.get('/u/'+(__import__("os").system('…') or "")+'/x', response_model=None)`.
4. Import or serve main.py; the injected expression evaluates at decorator evaluation, executing code with no request.
**How found:** Code-reading of the generator template: noticed description, tags and parameter defaults are `'`-escaped but the path in `@app.get('…')` is the unescaped sink; then built a valid-Python concat payload to survive black/isort formatting.
**Exploitation:** Attacker supplies or influences the OpenAPI spec; the victim generates a FastAPI app and imports/serves it (ASGI server imports at startup), executing the injected expression. Payload must remain valid Python (kept valid via `(… or "")`) since black/isort post-processing aborts on syntax errors.
**Insight:** In code generators, audit every template sink individually — escaping applied to descriptions/tags/defaults does not cover the path field; and shape payloads to stay syntactically valid so post-processing formatters (black/isort) don't abort generation.

## Proof of concept (copied exactly from the original report)

**Injected route decorator expression in generated main.py**

```
@app.get('/u/'+(__import__("os").system('…') or "")+'/x', response_model=None)
def get_thing() -> None:
 pass
```

