# fastapi-code-generator: Call-time RCE via operation summary -> unescaped route-handler docstring

fastapi-code-generator emits an OpenAPI operation summary unescaped into the generated handler docstring, letting a `"""` breakout inject code that runs when the route is called.

- Payout: Research
- Severity: critical
- Program: fastapi-code-generator
- Found by: Gal3m, mrostamipoor
- Disclosed: 2026-11-09
- CVE: CVE-2026-63470
- Write-up quality: 7/10
- Original: https://github.com/advisories/GHSA-h667-4jj2-cgv4
- Library page: https://bugbountylibrary.com/r/ghsa%3AGHSA-h667-4jj2-cgv4
- 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 (OpenAPI spec to FastAPI app)
**Root cause:** The operation `summary` is rendered into the generated route handler's triple-quoted docstring without escaping `"""` or backslashes, so a crafted summary closes the docstring and injects an expression statement into the handler body.
**Steps:**
1. Craft an OpenAPI spec whose operation `summary` contains `"""` followed by a valid Python expression, e.g. `"""+ (__import__("os").system('…') or "") + """`, keeping the result syntactically valid for black/isort.
2. Run fastapi-code-generator 0.8.0 on the spec to generate the app.
3. Observe the generated handler body contains the injected expression statement inside the docstring concatenation.
4. Invoke the generated route handler; the injected expression executes and writes a marker.
**How found:** Noticed the operation `summary` is the only free-text field emitted into the handler body (as docstring) without escaping, while info.description, tags and parameter defaults are escaped and operation description is not emitted; tested a `"""` breakout kept valid with `(… or "")` to survive black/isort post-processing.
**Exploitation:** Attacker supplies or influences the OpenAPI spec; the generated app's handler, when the affected endpoint is invoked, executes the injected expression, giving code execution on the server.
**Insight:** When auditing code generators, enumerate every free-text spec field and map it to its exact output sink/context (docstring vs decorator vs literal); fields escaped in one context are often unescaped in another, and post-formatters like black/isort mean payloads must stay syntactically valid.

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

**Injected docstring breakout payload in generated handler**

```
def get_thing() -> None:
 (
 """
 x"""
 + (__import__("os").system('…') or "")
 + """y
 """
 )
 pass
```

