From 0dfeb37c2c3914ba124befb48bcebecf3cbc66d4 Mon Sep 17 00:00:00 2001 From: Samika Perera Date: Fri, 20 Nov 2026 00:48:14 +1300 Subject: [PATCH 1/2] Pin that a cut before the file part says the request was over the limit (red) With a decoy part first, the 2 MB cut falls inside the decoy and the `file` part never arrives. FastAPI then answers 422 "Field required", which tells the user their file is missing when the request was too big. Shubham noted this in his review of #470. Fails on main: 6 failed. --- tests/test_upload_body_limit.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/test_upload_body_limit.py b/tests/test_upload_body_limit.py index 768570c..d54b11e 100644 --- a/tests/test_upload_body_limit.py +++ b/tests/test_upload_body_limit.py @@ -343,6 +343,27 @@ def body() -> Iterator[bytes]: assert r.status in (400, 422), r.body[:300] +@pytest.mark.parametrize("path", ["/api/upload", "/api/policy", "/api/business-context"]) +@pytest.mark.parametrize("decoy_size", [LIMIT + 100_000, 6 * MB]) +def test_a_cut_before_the_file_part_says_the_request_was_over_the_limit(path, decoy_size): + """#470 review (@shubhamkataria2005): with a decoy part first, the cut + falls before `file`, and FastAPI's own 422 said the file was missing -- + "Field required" -- when it was the request that was too big.""" + def body() -> Iterator[bytes]: + yield part_head("decoy.bin", name="other") + yield from file_bytes(decoy_size) + yield CRLF + part_head("small.json") + yield b"{}" + yield CLOSING + + r = post(path, body(), content_length=sum(len(p) for p in body())) + + assert r.status == 400, (r.status, r.body[:300]) + assert r.detail == ("The upload is larger than the 2 MB limit, so it was not read. " + "Upload the file on its own."), r.detail + assert any(c.startswith(b"netwise_session=") for c in r.header(b"set-cookie")) + + # --- The middleware on its own, without the parser in front of it ----------- # # Two of its rules cannot be seen through main.app, and mutation testing From fd8a3da9cdbd764792940253566f9c5f7b4a2e22 Mon Sep 17 00:00:00 2001 From: Samika Perera Date: Fri, 20 Nov 2026 00:48:14 +1300 Subject: [PATCH 2/2] Answer a cut that leaves no whole file part with the limit, not "Field required" After UploadBodyLimit cuts a body, a 422 means the endpoint never ran: FastAPI found no whole `file` part in the shortened body. That answer is replaced with the same 400 the endpoints give for an oversized file, "The upload is larger than the 2 MB limit, so it was not read. Upload the file on its own." The endpoints' own refusals are left alone, and the response keeps its headers, so the session cookie still arrives. --- web/main.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/web/main.py b/web/main.py index 1581ab2..e10e4dc 100644 --- a/web/main.py +++ b/web/main.py @@ -614,10 +614,40 @@ async def limited_receive(): body += b"\r\n" + delimiter + b"--\r\n" return {"type": "http.request", "body": body, "more_body": False} + # After a cut, a 422 means the endpoint never ran: FastAPI found no + # whole `file` part in what it was shown -- the cut fell in a part + # before it, or in the middle of it. Its "Field required" is true of + # the shortened body, not of what was sent, so it is replaced with + # the limit. The endpoints' own refusals (400) are kept as they are. + limit_mb = MAX_UPLOAD_BYTES / (1024 * 1024) + over_limit = json.dumps({"detail": ( + f"The upload is larger than the {limit_mb:.0f} MB limit, so it " + "was not read. Upload the file on its own." + )}).encode() + replacing = False + async def watched_send(message): - if message["type"] == "http.response.body" and not message.get( + nonlocal replacing + last = message["type"] == "http.response.body" and not message.get( "more_body", False - ): + ) + if message["type"] == "http.response.start" and cut \ + and message["status"] == 422: + # Same 400 the endpoints give for an oversized file. The + # headers are kept (attach_session's cookie is one of them), + # except the two that describe the body being replaced. + replacing = True + headers = [(k, v) for k, v in message.get("headers", []) + if k.lower() not in (b"content-length", b"content-type")] + headers += [(b"content-type", b"application/json"), + (b"content-length", str(len(over_limit)).encode())] + message = {"type": "http.response.start", "status": 400, + "headers": headers} + elif replacing and message["type"] == "http.response.body": + if not last: + return # the endpoint's own words, dropped + message = {"type": "http.response.body", "body": over_limit} + if last: response_done.set() await send(message)