Repository navigation
http: enforce a maximum response size - #740
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
makeHttpRequesthanded back a response whose body was read whole by whoever asked for it, whatever its size. Withaccept-encoding: gzip, deflate, bron 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.
makeHttpRequestnow reads the body itself, counting bytes as they arrive, after decompression. Past the limit it cancels the download and fails with anHttpResponseTooLargeError:HTTP_MAX_RESPONSE_BYTESsets another for the process, read likeHTTP_TIMEOUT_MS(a positive whole number, anything else falls back to the default).maxResponseBytessets it for one request, in the options ofmakeHttpRequestor in theoptionsof the request an@httpmethod returns.Content-Lengthover the limit is refused without being read.@httpqueue does not retry such a failure, since the next attempt would download the same body, and the rejection keeps the error class andlimitBytes.Responseover the bytes read: same status, status text, headers (repeatedSet-Cookieincluded) and URL, still unread and clonable. Responses without a body (204, 304, HEAD) pass through.ZIP archives. New
src/zipLimits.ts. Both ZIP destinations read entries throughcreateZipEntryReader(), which fails with aZipTooLargeErrornaming 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'smaxResponseBytes, 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.0to^0.6.1(the lockfile was already on 0.6.1).Asset pack redirect (second commit). The Attractions.io
/dataendpoint 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, notlocalhost, 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
Content-LengthThe 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.jsonandrecords.jsonare inflated.What could break
ZIP_MAX_DOWNLOAD_BYTESis raised.Response(a replay) is not subject to the limit. Only whatsendreads from the network is.response.redirectedon the result ofmakeHttpRequestwould seefalse; 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.tsandassetPackRedirect.test.ts: the same through a real sync, and each refused redirect target, with the request log showing only/datawas requested.Before the change, on
main:accepts a body of exactly the limitfails 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