Skip to content

backend: cap multipart upload size per-route instead of globally #7130

Closed

Description

@mdmohsin7

Context

PR #7127 bumped starlette to 0.40.0, which introduced a default 1 MB cap per multipart form part (CVE-2024-47874 fix). Several backend routes legitimately accept uploads larger than 1 MB, so the bump shipped with a startup-time monkey-patch in `backend/main.py`:

```python
from starlette.formparsers import MultiPartParser
MultiPartParser.max_part_size = 200 * 1024 * 1024 # 200 MB
```

The 200 MB number was chosen to match the existing `_MAX_PCM_BODY_BYTES` ceiling on the voice-message route. That keeps every upload route working but applies the same generous cap globally — including routes that should never receive anything close to 200 MB.

Why a per-route cap is better

A per-route cap shrinks the worst-case allocation an authed-but-malicious user can trigger to whatever each endpoint actually serves:

Route Realistic part size Today's cap
voice message transcribe up to 200 MB (PCM ceiling) 200 MB
sync audio batch up to ~50 MB 200 MB
speech profile sample typically 5-20 MB 200 MB
chat file attachment depends on file type 200 MB
app/persona thumbnail typically 1-5 MB 200 MB
imports payload varies 200 MB
phone-call form data small 200 MB

Existing mitigations limit the practical risk (auth on every route, per-user rate limiting, explicit `_MAX_PCM_BODY_BYTES` check on the PCM path), but the global 200 MB cap is still a vulnerability multiplier on routes that have no reason to allocate that much.

Suggested approach

  1. Bump `starlette` to 0.46.x. fastapi 0.118.0 (current) allows starlette `>=0.40,<0.49`, so 0.46 is in range. starlette 0.45+ adds `Request.form(max_part_size=…)` as a per-call parameter.
  2. Build a small `MaxPartSize(bytes: int)` dependency that wraps `Request.form()` and `UploadFile` parsing. The fastapi-style `UploadFile = File(...)` annotation does not expose `max_part_size` directly, so a custom dependency is needed to cover routes using either pattern.
  3. Apply the dependency on every multipart route with a route-appropriate value:
    • `routers/chat.py /v2/voice-message/transcribe`: 200 MB
    • `routers/chat.py /v2/files`: 50 MB (or whatever the chat-file ceiling actually is)
    • `routers/sync.py /v2/sync`: 50 MB
    • `routers/speech_profile.py /v1/users/profile/upload`: 25 MB
    • `routers/apps.py` thumbnails / personas: 10 MB
    • `routers/imports.py /v1/imports`: 100 MB (or whatever the import-size guard expects)
    • `routers/phone_calls.py`: 5 MB
  4. Remove the global `MultiPartParser.max_part_size = 200 * 1024 * 1024` patch from `main.py` once every route has its own cap.
  5. Add a unit test per route that POSTs a multipart payload one byte over its declared limit and expects 400/413.

Acceptance criteria

  • No global `MultiPartParser.max_part_size` mutation in `main.py`.
  • Every multipart route declares an explicit cap.
  • Tests verify each route rejects oversize uploads.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Securitybackendp2pythonrefactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions