Bug: Content Length Check on the Request Body Incompatible with Decompression Middleware #4125
Open
1 of 4 tasks
Labels
Bug 🐛
This is something that is not working as expected
Description
The
connection.request.Request
class has astream
method that is used to populate the request's body. It will raise aClientException("Malformed request")
when it detectstotal_bytes_streamed > announced_content_length
.This does not account for the situation where the ASGI receive function includes decompression middleware in the asgi app chain. When decompressed by middleware the body would naturally be larger than the content length of the network message.
The comments in request.py suggest that the check is somewhat redundant because the ASGI server, such as uvicorn, should check this. As
total_bytes_streamed > announced_content_length
could be true before the body has finished streaming in and decompressing, would it be better to just remove the check if there isContent-Encoding
header present that is set to anything other thanidentity
? Alternatively, if the decompression middleware mutates the content-length header as it goes we could reload theannounced_content_length
for another check before raising theClientException
.A version of decompression middleware without gzip support
At this point I'm unclear if this is really a bug or if there's some other approach to avoid the issue.
URL to code causing the issue
No response
MCVE
Steps to reproduce
Send a network request to a Litestar app with a compressed body and a content-encoding header that decompression middleware can use to choose how to decompress that body.
Without the decompression middleware the types won't match the API, it'll just be a byte stream and not e.g json.
Screenshots
No response
Logs
Litestar Version
2.15.2
Platform
The text was updated successfully, but these errors were encountered: