|
7 | 7 | read, a page that streams past the cap is refused once it does, and a |
8 | 8 | robots.txt is parsed from its first ``MAX_RESPONSE_BYTES``. Redirects are |
9 | 9 | followed without reading the redirect's body, which httpx would otherwise read |
10 | | -whole. These tests drive the real server through an MCP client |
| 10 | +whole. A gzip or deflate body is decoded by the server itself with each |
| 11 | +step's output bounded by the room left under the cap, since httpx inflates a |
| 12 | +whole network chunk at once (a compression bomb); any other content coding is |
| 13 | +refused. These tests drive the real server through an MCP client |
11 | 14 | (``harness.connect``) with HTTP served by ``harness.FakeWeb``, under a small |
12 | 15 | cap so a body over it stays cheap; endless bodies count the chunks they hand |
13 | 16 | out, to show the read stopped. |
|
16 | 19 | from __future__ import annotations |
17 | 20 |
|
18 | 21 | import gzip |
| 22 | +import zlib |
19 | 23 | from collections.abc import AsyncIterator |
20 | 24 | from typing import Any |
21 | 25 |
|
@@ -242,3 +246,104 @@ async def test_the_prompt_reports_an_oversized_page(web: FakeWeb) -> None: |
242 | 246 | result = await session.get_prompt("fetch", {"url": PAGE}) |
243 | 247 | message = result.model_dump(mode="json")["messages"][0]["content"]["text"] |
244 | 248 | assert message == too_large().removeprefix(TOOL_ERROR) |
| 249 | + |
| 250 | + |
| 251 | +# --- content codings ---------------------------------------------------------- |
| 252 | + |
| 253 | + |
| 254 | +def encoded(body: bytes, coding: str) -> httpx.Response: |
| 255 | + # Over a stream, not `content=`, which httpx would decode while building it. |
| 256 | + return httpx.Response( |
| 257 | + 200, |
| 258 | + stream=httpx.ByteStream(body), |
| 259 | + headers={"content-type": "text/plain", "content-encoding": coding}, |
| 260 | + ) |
| 261 | + |
| 262 | + |
| 263 | +def raw_deflate(data: bytes) -> bytes: |
| 264 | + compressor = zlib.compressobj(wbits=-zlib.MAX_WBITS) |
| 265 | + return compressor.compress(data) + compressor.flush() |
| 266 | + |
| 267 | + |
| 268 | +async def test_a_compression_bomb_never_decodes_past_the_cap( |
| 269 | + web: FakeWeb, monkeypatch: pytest.MonkeyPatch |
| 270 | +) -> None: |
| 271 | + # One small network chunk that inflates to 10,000 times the cap. httpx |
| 272 | + # would inflate it whole before any size check; the server's own decoder |
| 273 | + # must never produce more than the room left plus one byte in a step. |
| 274 | + outputs: list[int] = [] |
| 275 | + real = zlib.decompressobj |
| 276 | + |
| 277 | + class Spy: |
| 278 | + def __init__(self, *args: Any) -> None: |
| 279 | + self._z = real(*args) |
| 280 | + |
| 281 | + @property |
| 282 | + def unconsumed_tail(self) -> bytes: |
| 283 | + return self._z.unconsumed_tail |
| 284 | + |
| 285 | + def decompress(self, data: bytes, max_length: int = 0) -> bytes: |
| 286 | + out = self._z.decompress(data, max_length) |
| 287 | + outputs.append(len(out)) |
| 288 | + return out |
| 289 | + |
| 290 | + monkeypatch.setattr(zlib, "decompressobj", Spy) |
| 291 | + bomb = gzip.compress(b"a" * (CAP * 10_000)) |
| 292 | + assert len(bomb) < CAP * 50 |
| 293 | + web.add(PAGE, encoded(bomb, "gzip")) |
| 294 | + result = await call() |
| 295 | + assert text_of(result) == too_large() |
| 296 | + assert outputs and max(outputs) <= CAP + 1 |
| 297 | + assert sum(outputs) <= CAP + 1 |
| 298 | + |
| 299 | + |
| 300 | +@pytest.mark.parametrize( |
| 301 | + ("coding", "encode"), |
| 302 | + [ |
| 303 | + ("gzip", gzip.compress), |
| 304 | + ("x-gzip", gzip.compress), |
| 305 | + ("GZIP", gzip.compress), |
| 306 | + ("identity, gzip", gzip.compress), |
| 307 | + ("deflate", zlib.compress), |
| 308 | + ("deflate", raw_deflate), # sent without zlib's wrapper |
| 309 | + ], |
| 310 | +) |
| 311 | +async def test_a_gzip_or_deflate_body_is_decoded( |
| 312 | + web: FakeWeb, coding: str, encode: Any |
| 313 | +) -> None: |
| 314 | + web.add(PAGE, encoded(encode(b"d" * CAP), coding)) |
| 315 | + result = await call() |
| 316 | + assert wire(result)["isError"] is False |
| 317 | + assert text_of(result).endswith("d" * CAP) |
| 318 | + |
| 319 | + |
| 320 | +async def test_an_identity_coding_is_read_as_is(web: FakeWeb) -> None: |
| 321 | + web.add(PAGE, encoded(b"plain", "identity")) |
| 322 | + result = await call() |
| 323 | + assert text_of(result).endswith("plain") |
| 324 | + |
| 325 | + |
| 326 | +@pytest.mark.parametrize("coding", ["br", "zstd", "gzip, gzip", "compress"]) |
| 327 | +async def test_a_coding_the_server_cannot_bound_is_refused( |
| 328 | + web: FakeWeb, coding: str |
| 329 | +) -> None: |
| 330 | + web.add(PAGE, encoded(b"\x00\x01\x02", coding)) |
| 331 | + result = await call() |
| 332 | + assert wire(result)["isError"] is True |
| 333 | + assert "Unsupported content encoding" in text_of(result) |
| 334 | + assert "this server decodes only gzip, deflate" in text_of(result) |
| 335 | + |
| 336 | + |
| 337 | +@pytest.mark.parametrize("coding", ["gzip", "deflate"]) |
| 338 | +async def test_a_corrupt_body_is_a_decoding_error(web: FakeWeb, coding: str) -> None: |
| 339 | + # Deflate is retried as raw deflate once, then fails like gzip. |
| 340 | + web.add(PAGE, encoded(b"\xff" * 64, coding)) |
| 341 | + result = await call() |
| 342 | + assert wire(result)["isError"] is True |
| 343 | + assert "Error decoding the response body" in text_of(result) |
| 344 | + |
| 345 | + |
| 346 | +async def test_only_the_codings_it_decodes_are_asked_for(web: FakeWeb) -> None: |
| 347 | + web.add(PAGE, httpx.Response(200, content=b"ok")) |
| 348 | + await call() |
| 349 | + assert web.requests[0].headers["accept-encoding"] == "gzip, deflate" |
0 commit comments