fix: prevent concatenated gzip response truncation when read via GZIPInputStream - #7331
Open
jencymaryjoseph wants to merge 1 commit into
Open
fix: prevent concatenated gzip response truncation when read via GZIPInputStream#7331jencymaryjoseph wants to merge 1 commit into
jencymaryjoseph wants to merge 1 commit into
Conversation
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.
Motivation and Context
When an SDK streaming response body is gzip-compressed and the caller decodes it with
java.util.zip.GZIPInputStream, a concatenated (multi-member) gzip stream can be silently truncated toonly its first member.
Root cause is in the JDK:
GZIPInputStream.readTrailer()decides whether another gzip member follows partlyfrom the underlying stream's
available(). A network-backed response body can transiently reportavailable() == 0exactly at a member boundary (the socket momentarily has no buffered bytes even though moredata is still in flight).
GZIPInputStreamtreats that transient0as end-of-stream, stops decoding, anddrops every remaining member — no error is raised, so the caller just receives partial data.
Modifications
GzipAvailabilityInputStream(@SdkInternalApi,core/sdk-core):1f 8b+ DEFLATE method08).available()as at least1while the stream is open and not atEOF, so
GZIPInputStreamkeeps reading across member boundaries instead of stopping on a transient0.available()through unchanged, so text/line consumers (e.g.BufferedReader) areunaffected.
release()to aReleasabledelegate, and preservesread/skip/mark/reset/closesemantics.ResponseInputStreamnow wraps the bodyInputStreaminGzipAvailabilityInputStreamin bothtimeout-aware constructors. The
abortableis still captured from the original stream, soabort()propagates to the underlying connection unchanged.
Content-Encoding,because that header is not reliably present for gzip payloads and is not available at this layer. Passive
byte detection keeps the behavior change scoped to actual gzip content.
This is intentionally a narrower fix than the previously reverted approach of never returning
0fromavailable()for all streams, which broke incremental line reads (BufferedReader/InputStreamReader). Bydetecting gzip passively, non-gzip streaming is left completely unchanged.
Testing
New tests, plus all existing
ResponseInputStreamtests continue to pass.GzipAvailabilityInputStreamTest(unit):available()coercion for gzip vs. honest pass-through fornon-gzip and near-miss headers (parameterized); bulk-read classification; EOF / closed guards; and regression
guards for zero-length reads, skip-before-classification, mark/reset, and
release()propagation.ResponseInputStreamTest(integration, through the realResponseInputStream): end-to-end decode ofconcatenated and many-member gzip through a real
GZIPInputStreamover a trickle source that reportsavailable() == 0; single-member decode without hanging; a genuinely blocking source that blocks thensignals EOF;
BufferedReaderline delivery on non-gzip content; mark/reset transparency; andabort()propagation through the inserted wrapper.
Screenshots (if appropriate)
Types of changes
Checklist
mvn installsucceedsscripts/new-changescript and following the instructions. Commit the new file created by the script in.changes/next-releasewith your changes.License