Fix early proxy request size enforcement - #27311
Conversation
Co-authored-by: ishaan-berri <ishaan-berri@users.noreply.github.com>
|
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Greptile SummaryThis PR adds an outer ASGI middleware (
Confidence Score: 5/5The change is safe to merge; it adds an opt-in, premium-gated middleware that is inactive unless The middleware correctly sits inside No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/proxy/middleware/request_size_limit_middleware.py | New ASGI middleware that correctly rejects oversized requests by Content-Length header before body buffering, and counts streamed bytes via a wrapped receive callable; _mb_to_bytes properly treats <= 0 as disabled (no-limit). |
| litellm/proxy/proxy_server.py | Registers RequestSizeLimitMiddleware at the end of the setup block; lambdas capture general_settings and premium_user by reference so runtime changes are respected. |
| tests/proxy_unit_tests/test_request_size_limit_middleware.py | Adds three unit tests covering Content-Length rejection (no body read, downstream not called), zero-limit disabling the guard, and streamed body byte-counting; all tests are mock-only with no real network calls. |
Reviews (2): Last reviewed commit: "Address request size review feedback" | Re-trigger Greptile
Co-authored-by: ishaan-berri <ishaan-berri@users.noreply.github.com>
|
@greptile review this again |
c8e47dc
into
BerriAI:litellm_internal_staging
* Add early proxy request size guard Co-authored-by: ishaan-berri <ishaan-berri@users.noreply.github.com> * Address request size review feedback Co-authored-by: ishaan-berri <ishaan-berri@users.noreply.github.com> --------- Co-authored-by: oss-agent-shin <279349115+oss-agent-shin@users.noreply.github.com> Co-authored-by: ishaan-berri <ishaan-berri@users.noreply.github.com>
Summary
general_settings.max_request_size_mbwas only enforced after auth had already read and parsed the request body, so oversized inline base64 payloads could be buffered before rejection. This adds an outer ASGI middleware that rejects oversizedContent-Lengthrequests before downstream code runs and counts streamed request bytes whenContent-Lengthis absent, while preserving the existing auth-level guard as a second check.Repro
On the starting ref, the focused repro test failed during collection because there was no early ASGI request-size guard to reject before body parsing:
The new test covers the issue by asserting an oversized
Content-Lengthrequest returns 413 without calling the downstream app, and a streamed request withoutContent-Lengthis cut off when the configured byte limit is exceeded.Evidence
Terminal transcript fallback (no UI involved):
Tests
tests/proxy_unit_tests/test_request_size_limit_middleware.pyfor earlyContent-Lengthrejection and streamed-body byte counting withoutContent-Length.python3 -m pytest tests/proxy_unit_tests/test_request_size_limit_middleware.py tests/test_litellm/proxy/common_utils/test_http_parsing_utils.py -q(32 passed).python3 -m black --check litellm/proxy/middleware/request_size_limit_middleware.py tests/proxy_unit_tests/test_request_size_limit_middleware.py litellm/proxy/proxy_server.py.python3 -m ruff check litellm/proxy/middleware/request_size_limit_middleware.py tests/proxy_unit_tests/test_request_size_limit_middleware.py litellm/proxy/proxy_server.py.