Repository navigation
fix(proxy): decode zstd and gzip request bodies before auth reads them - #47379
mateo-berri wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Merging this PR will not alter performance
|
|
@litellm-stampbot |
litellm-stampbot: risk highNot approved at Rules (
Cursor Bugbot: Medium Risk at Pushes do not re-run this check. Comment |
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit c62305d. Configure here.
|
Superseded by #47418, which carries these same changes plus the middleware registration, so closing this one in favor of it |
TLDR
Problem this solves:
Content-Encoding: zstdorgzipfail with 400 Invalid JSONHow it solves it:
MAX_DECOMPRESSED_REQUEST_BODY_SIZE_MB(default 50) caps decoded size with a 413User Flow
Before: a developer using the TypeSafe SDK through the proxy gets a 400 on every call whose body is 1 KB or more
TypeSafeClient(base_url="https://litellm-domain", api_key=<virtual key for jev>, model="jev")system_one(...)with about 2 KB of state, so the SDK sends POST https://litellm-domain/v1/systemone withContent-Encoding: zstdInvalid JSON: expected value at line 1 column 1withx-litellm-response-cost: 0After: the same call comes back with answers and a cost
TypeSafeClient(base_url="https://litellm-domain", api_key=<virtual key for jev>, model="jev")system_one(...)with about 2 KB of state, so the SDK sends POST https://litellm-domain/v1/systemone withContent-Encoding: zstdx-litellm-response-costDecisions
Pass-through routes are decoded too. The plan was to skip them so a compressed body would be forwarded as sent, but on main that never happens. A compressed JSON body to a pass-through route gets a 500 before it reaches the target, and with a non-JSON content type the target gets
{}with the originalContent-Encodingheader still set. Decoding fixes those routes too, and the target gets the plain bodyThis supersedes #41567 by @enternal111, which decoded gzip only, and adds zstd and the size cap on top of the same idea
Other decisions and their alternatives
The middleware runs outside every body reader, so auth, the route handlers, and pass-through routes all see the decoded body. Decoding in the handler or in the shared body reader would leave auth checking the compressed bytes. It goes right after
RequestSizeLimitMiddlewareinproxy_server.py, so that limit measures the decoded body and admission control still runs firstOnly a single
gziporzstdcoding is decoded, matched without regard to case or surrounding spaces. No header,identity,br,deflate, a list such asgzip, zstd, and websocket requests pass through untouched, so anyone sending those today sees no change. The alternatives were a 415 for codings the proxy can't decode, as RFC 9110 suggests, or decoding stacked codings.x-gzipis not acceptedThe cap is a new env var instead of
max_request_size_mb. That setting is enterprise-only and unset by default, so it would leave open-source proxies with no bomb protection. The cap bounds both the compressed read and the decoded output, and the decoder stops at cap + 1 bytes, so a 200 MB bomb never inflates past the cap. Over the cap gets the 413 bodyRequestSizeLimitMiddlewarealready sends, and the cap set to 0 rejects every non-empty compressed body. A compression ratio limit was the other optionA body must be exactly one gzip member or one zstd frame. Concatenated members or frames, a truncated stream, or bytes after the end get a 400 with
type: invalid_request_error. The alternative was to accept several members like Python'sgzip.decompressdoesThe zstd decoder refuses windows over 8 MB, the limit RFC 9659 sets for the
zstdcontent coding, so a crafted frame can't make the decoder allocate a huge window. Such a frame gets the same 400Decoding runs in a worker thread, like the response gzip middleware, so a 50 MB decode doesn't block the event loop. The middleware drops
content-encoding,content-length, andtransfer-encodingand setscontent-lengthto the decoded size on a copy of the request scope. A client that disconnects mid-body gets no response, and an empty body with a supported coding reaches the route emptyzstd comes from the standard library's
compression.zstdon Python 3.14 and frombackports-zstdbelow that. It is a proxy extra dependency, and theproxy-devgroup carries it too so the type checker resolves it. It is PSF-2.0 licensed, so it is added to the license allowlist. Thezstandardpackage was the alternative, but its API differs from the standard library'sKnown gap
This commit adds the middleware but does not register it yet, so the proxy behaves as before. The next commit on this branch registers it and adds app-level tests, one for the 403 on a compressed body naming another model and one for a zstd
/v1/systemonebody getting past body parsingThe env var row for the docs is in BerriAI/litellm-docs#2458, which the docs check in CI needs merged
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreDelays in PR merge?
If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).
Screenshots / Proof of Fix
Type
Bug Fix
Caveats (if any)
Final Attestation
Note
Medium Risk
New request-path middleware affects all compressed bodies once registered; size caps and bomb tests mitigate memory/DoS risk, but incorrect wiring order could change auth or size-limit semantics.
Overview
Adds
RequestBodyDecompressionMiddlewareso the proxy can transparently inflate HTTP request bodies when clients send a singleContent-Encoding: gziporzstdheader. Downstream handlers (auth, JSON parsing, pass-through) see the decoded bytes withcontent-encoding/transfer-encodingstripped and an updatedcontent-length.MAX_DECOMPRESSED_REQUEST_BODY_SIZE_MB(default 50) caps both the compressed read and decompressed output; oversize bodies return the same 413 asRequestSizeLimitMiddlewarevia the newly publicsend_request_too_large. Invalid or truncated compressed data returns an OpenAI-style 400. Decompression runs in a worker thread; zstd usesbackports-zstdon Python < 3.14.This commit ships the middleware, dependency/license updates, and unit tests only—the PR notes registration in
proxy_server.pyis a follow-up, so behavior is unchanged until that lands.Reviewed by Cursor Bugbot for commit c62305d. Bugbot is set up for automated code reviews on this repo. Configure here.