Skip to content

http: enforce a maximum response size - #740

Merged
cubehouse merged 2 commits into
mainfrom
fix/http-max-response-size
Dec 5, 2026
Merged

cubehouse merged 2 commits into
mainfrom
fix/http-max-response-size

Conversation

@cubehouse

Copy link
Copy Markdown
Member

What was wrong

makeHttpRequest handed back a response whose body was read whole by whoever asked for it, whatever its size. With accept-encoding: gzip, deflate, br on every request, a few hundred kilobytes on the wire can decompress to hundreds of megabytes. The two destinations that download a ZIP archive inflated its entries to whatever size the archive stated. One oversized or highly compressed response could take all the memory of the process, and with it every other destination running in it.

What this changes

Response size. makeHttpRequest now reads the body itself, counting bytes as they arrive, after decompression. Past the limit it cancels the download and fails with an HttpResponseTooLargeError:

HTTP response larger than the limit of 67108864 bytes: GET https://…. Raise the limit with HTTP_MAX_RESPONSE_BYTES, or with maxResponseBytes on the request.
  • Default 64 MiB. HTTP_MAX_RESPONSE_BYTES sets another for the process, read like HTTP_TIMEOUT_MS (a positive whole number, anything else falls back to the default).
  • maxResponseBytes sets it for one request, in the options of makeHttpRequest or in the options of the request an @http method returns.
  • A body that states a Content-Length over the limit is refused without being read.
  • The @http queue does not retry such a failure, since the next attempt would download the same body, and the rejection keeps the error class and limitBytes.
  • The caller gets an equivalent Response over the bytes read: same status, status text, headers (repeated Set-Cookie included) and URL, still unread and clonable. Responses without a body (204, 304, HEAD) pass through.
  • The body is now read inside the request timeout, so a body that stalls fails with the usual "HTTP request timed out" message.

ZIP archives. New src/zipLimits.ts. Both ZIP destinations read entries through createZipEntryReader(), which fails with a ZipTooLargeError naming the entry, the limit and the setting that raises it:

  • ZIP_MAX_ENTRY_BYTES, default 128 MiB: one entry, uncompressed.
  • ZIP_MAX_TOTAL_BYTES, default 512 MiB: all entries read from one archive, uncompressed. Entries a destination never opens do not count.
  • ZIP_MAX_DOWNLOAD_BYTES, default 256 MiB: the archive itself, passed as the download's maxResponseBytes, because an archive is the one response that is legitimately larger than the general limit.

The check runs on the size the entry states, before anything is inflated. adm-zip stops inflating at the stated size, so an entry cannot state little and inflate to more. That holds for an entry stating a size of zero only from adm-zip 0.6.1, so the dependency floor moves from ^0.6.0 to ^0.6.1 (the lockfile was already on 0.6.1).

Asset pack redirect (second commit). The Attractions.io /data endpoint answers 303 with the Location of the pack, and the sync requested whatever that Location held. It now resolves the Location against the request and accepts a target on the API's own origin as it is. Any other target has to be https and a public host name: not an IP address, not localhost, not a name without a dot. assetPackHosts (ATTRACTIONSIO_ASSETPACKHOSTS, comma-separated host names) narrows that to named hosts; it is empty by default. A relative Location, which used to fail as an invalid URL, is now followed.

The defaults, and the headroom

Limit Default Largest legitimate size seen Headroom
Response body 64 MiB (67,108,864) 13,184,814 bytes: the largest JSON body any destination returns today. The next largest is 11,413,693. 5.1x
ZIP download 256 MiB 21,818,514 bytes: the Parc Asterix offline package, by its Content-Length 12.3x
ZIP entry 128 MiB Not measured per entry. Every entry read is part of an archive of the sizes above, and the Attractions.io records come to about 0.5 MB per park once stored.
ZIP total 512 MiB As above; four entries at the entry limit.

The largest fixture in the repo is 64,351 bytes (chessington-2026-10-01.json), about a thousandth of the response limit.

Not measured: the size of a full Attractions.io asset pack. Its manifest lists media, so it may be much larger than its records. It falls under the 256 MiB download limit, and only manifest.json and records.json are inflated.

What could break

  • A destination whose response grows past 64 MiB starts failing with an error that names the limit and the setting. Nothing today is within a factor of five.
  • A full Attractions.io pack over 256 MiB would fail to download until ZIP_MAX_DOWNLOAD_BYTES is raised.
  • An Attractions.io pack served over plain http from another origin, or from an IP address, is no longer downloaded.
  • A transport that returns its own Response (a replay) is not subject to the limit. Only what send reads from the network is.
  • Code that relied on response.redirected on the result of makeHttpRequest would see false; nothing in the repo reads it, and redirects are never followed automatically anyway.

Tests

All against servers on 127.0.0.1.

  • src/__tests__/httpMaxResponseSize.test.ts: a 64 MiB stream under a 1 MiB limit is cut off, measured on the server (connection closed early, a fraction of the body sent); a gzip body of a few kilobytes that inflates to 8 MiB is refused under a 1 MiB limit and accepted under an 8 MiB one; a body under the limit comes back unchanged with status, headers and URL; exactly the limit passes, one byte less fails; 204, 304, HEAD and a 303 pass through; the environment variable and the per-request option, directly and through @http; no retry.
  • src/__tests__/zipLimits.test.ts: a few kilobytes of archive whose entry inflates to 4 MiB is refused under a 1 MiB entry limit without being inflated; the total across entries; entries never read do not count; an entry that states less than it holds, zero included, is stopped by the inflater.
  • src/parks/attractionsio/__tests__/assetPackSize.test.ts and assetPackRedirect.test.ts: the same through a real sync, and each refused redirect target, with the request log showing only /data was requested.

Before the change, on main: accepts a body of exactly the limit fails with "promise resolved Response { status: 200 … } instead of rejecting", and the redirect cases fail because the pack is downloaded from the other origin. After: 202 files, 4397 tests pass, typecheck clean.

🤖 Generated with Claude Code

cubehouse and others added 2 commits December 5, 2026 00:15
A response body was read whole, whatever its size, and a ZIP entry was
inflated to whatever size the archive stated. One oversized or highly
compressed response could take all the memory of the process.

makeHttpRequest now reads the body while counting its bytes, after
decompression, and abandons the download past the limit with an
HttpResponseTooLargeError that names the limit and how to raise it. The
default is 64 MiB; HTTP_MAX_RESPONSE_BYTES sets another for the process and
maxResponseBytes for one request, in the options of makeHttpRequest or of
the request an @http method returns. Such a failure is not retried.

The two destinations that download a ZIP read its entries through
createZipEntryReader(), which refuses an entry over ZIP_MAX_ENTRY_BYTES
(128 MiB) and stops at ZIP_MAX_TOTAL_BYTES (512 MiB) per archive, checked
on the stated size before anything is inflated. Their downloads carry
ZIP_MAX_DOWNLOAD_BYTES (256 MiB) as their own response limit.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The /data endpoint answers 303 with the Location of the asset pack, and
the sync requested whatever that Location held. It now resolves the
Location against the request and accepts a target on the API's own origin
as it is. Any other target has to be https and a public host name: not an
IP address, not localhost, and not a name without a dot. A relative
Location, which used to fail as an invalid URL, is followed.

assetPackHosts (ATTRACTIONSIO_ASSETPACKHOSTS), a comma-separated list of
host names, narrows the accepted targets to those hosts. It is empty by
default, which accepts any public https host.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@cubehouse
cubehouse merged commit bd1eae1 into main Dec 5, 2026
5 checks passed
@cubehouse
cubehouse deleted the fix/http-max-response-size branch December 5, 2026 12:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant