Repository navigation
fix(backend): enforce multipart limits per route - #8251
Merged
krushnarout merged 23 commits intoJul 15, 2026
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #7130
Summary
This PR replaces the backend-wide
MultiPartParser.max_part_size = 200 * 1024 * 1024monkey-patch with explicit, route-appropriate multipart limits.The previous startup-time mutation made every multipart endpoint inherit the voice-message endpoint's 200 MB allowance. That preserved uploads after Starlette introduced a default multipart-part limit, but it also allowed endpoints handling small images or form payloads to accept parts far larger than their use case requires.
This change:
backend/main.py;UploadFile = File(...);request.form();application/x-www-form-urlencodedbehavior for Twilio form posts;Motivation
The old global cap used the voice-message PCM ceiling as the default for all multipart endpoints:
That meant an authenticated request could send a part of up to 200 MB to routes that normally process only a thumbnail, profile sample, chat attachment, import archive, sync chunk, or small phone-call form.
Per-route caps reduce the maximum allocation available through each endpoint to a value appropriate for that endpoint's actual workload. This narrows the memory-exhaustion surface without reducing the existing voice-message allowance.
Dependency state
The issue originally suggested upgrading from Starlette 0.40.x to 0.46.x because
Request.form(max_part_size=...)became available in newer Starlette releases.The repository has moved forward since the issue was opened and currently pins:
fastapi==0.121.0starlette==0.49.1Therefore, this PR does not include an additional dependency bump.
There is also an important Starlette 0.49.1 detail:
max_part_sizelimits in-memory non-file form fields, but uploaded file bytes are streamed intoUploadFilewithout applying that same size check. CallingRequest.form(max_part_size=...)alone would not fully satisfy the security goal for file uploads.To cover both cases, this PR adds
FileSizeLimitedMultiPartParser, which:MultiPartExceptioncleanup behavior.Implementation
Shared multipart utility
backend/utils/multipart.pycontains:@max_part_size(...), which attaches the selected cap to a FastAPI endpoint;MultipartMaxPartSizeRoute, a customAPIRoutethat parses and caches multipart data with the endpoint's declared cap before FastAPI resolvesFile(...)parameters;FileSizeLimitedMultiPartParser, which extends Starlette's parser to enforce the cap on file parts as well as regular fields;parse_multipart_form(...), for endpoints that manually parse the request body.The parsed
FormDatais cached on the request, matching Starlette's normal request-form behavior. FastAPI can then resolveUploadFileparameters without reparsing or rereading the request stream.Manual form parsing
Two routes parse their request bodies manually:
/v2/voice-message/transcribe/v1/phone/twimlThey call
parse_multipart_form(...)directly so their existing control flow and dependency order remain intact.For non-multipart content types, the helper delegates to the normal
request.form()implementation. This is necessary for Twilio, which normally sendsapplication/x-www-form-urlencodedpayloads.Route limits
POST /v2/voice-messagesPOST /v2/voice-message/transcribe_MAX_PCM_BODY_BYTESPOST /v2/filesPOST /v1/filesPOST /v1/sync-local-filesPOST /v2/sync-local-filesPOST /v3/upload-audioPOST /v1/appsPATCH /v1/apps/{app_id}POST /v1/personasPATCH /v1/personas/{persona_id}POST /v1/app/thumbnailsPOST /v1/import/limitlessPOST /v1/phone/twimlRequest behavior
Within the limit
UploadFileendpoints receive the same objects they received before.Over the limit
The limit is per multipart part, matching Starlette's
max_part_sizemodel and the scope of #7130. It is not a total request-body-size limit.Security impact
Before this PR, all multipart routes effectively inherited a 200 MB per-part allowance.
After this PR:
This reduces the worst-case per-part allocation available through smaller upload surfaces while retaining current functionality for large voice payloads.
Tests
Added
backend/tests/unit/test_multipart_limits.pywith coverage for:application/x-www-form-urlencodedform parsing;MultiPartParser.max_part_sizevalue is not mutated;Focused test command:
cd backend pytest tests/unit/test_multipart_limits.py -vResult:
Additional validation:
Both completed successfully.
Product invariants affected
Compatibility and risk
@max_part_size(...)continue using FastAPI's normal request handling.Out of scope
Reviewer guide
The main review points are:
backend/utils/multipart.py: parser behavior, file-byte accounting, request form caching, and URL-encoded fallback.backend/main.py: confirmation that the global monkey-patch is fully removed.backend/tests/unit/test_multipart_limits.py: boundary, split-chunk, compatibility, and route-audit coverage.