Skip to content

python.fastapi.request_body_unbounded

Stability High

Detects FastAPI endpoints without request body size limits.

Unbounded request bodies:

  • Memory exhaustion — Large uploads crash server
  • DoS attacks — Attackers send huge payloads
  • Resource starvation — One request consumes all memory
# ❌ Before (unbounded)
@app.post("/upload")
async def upload(file: UploadFile):
content = await file.read() # No size limit!
return {"size": len(content)}
# ✅ After (with size limit)
from fastapi import HTTPException
MAX_SIZE = 10 * 1024 * 1024 # 10MB
@app.post("/upload")
async def upload(file: UploadFile):
content = await file.read(MAX_SIZE + 1)
if len(content) > MAX_SIZE:
raise HTTPException(413, "File too large")
return {"size": len(content)}
  • UploadFile.read() without size limits
  • Request.body() without limits
  • Missing content-length validation