fix(fetcher): stream-cap fetchXml body instead of buffering the whole ZIP (#231)#245
Merged
Merged
Conversation
… ZIP (#231) fetchXml read the entire response with `Buffer.from(await arrayBuffer())` and only checked the size afterward. The Content-Length pre-check is the primary guard, but a chunked or untruthful-Content-Length response slips past it, so an oversized/unbounded body was fully materialized into memory before the length check — a runner-OOM vector. Extract the streaming size-guard from readBodyCapped into readBytesCapped (Response -> Buffer | null), which aborts and cancels the stream the moment the running total exceeds the cap. readBodyCapped becomes a thin UTF-8 decoder over it. fetchXml now uses readBytesCapped, so the body is never fully buffered when oversized. Adds readBytesCapped tests (under-cap buffer, over-cap abort+cancel on a no-Content-Length stream, no-body fallback). Closes #231 Co-Authored-By: Claude Opus 4.8 <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.
Summary
fetchXmlread the entire response withBuffer.from(await result.value.arrayBuffer())and only checkedbuffer.length > MAX_DOWNLOAD_BYTESafterward. TheContent-Lengthpre-check is the primary guard, but a chunked / untruthful-Content-Lengthresponse slips past it — so an oversized or unbounded body was fully materialized into memory before the length check, a runner-OOM vector.Fix
readBodyCappedintoreadBytesCapped(response, maxBytes): Buffer | null— it streams the body and aborts (reader.cancel()) the instant the running total exceeds the cap, so an oversized body is never fully buffered.readBodyCappedbecomes a thin UTF-8 decoder overreadBytesCapped.fetchXmlnow reads viareadBytesCappedand returns the same'Download exceeds size limit'error on overflow — memory-bounded for the no-/false-Content-Length case the pre-check can't catch.Tests
New
readBytesCappedtests: returns full buffer under cap; returnsnulland cancels the stream once a no-Content-Length body exceeds the cap (proving it doesn't buffer everything); no-bodyarrayBufferfallback. ExistingfetchXmltests (valid ZIP, unchanged-hash, Content-Length reject, non-ZIP) still pass.Full fetcher suite green (162 tests); lint + typecheck clean.
Closes #231