Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions tests/test_upload_body_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions web/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down