Skip to content

fix: prevent concatenated gzip response truncation when read via GZIPInputStream - #7331

Open
jencymaryjoseph wants to merge 1 commit into
masterfrom
jencyjos/gzip-concatenated-truncation
Open

fix: prevent concatenated gzip response truncation when read via GZIPInputStream#7331
jencymaryjoseph wants to merge 1 commit into
masterfrom
jencyjos/gzip-concatenated-truncation

Conversation

@jencymaryjoseph

@jencymaryjoseph jencymaryjoseph commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

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 to
only its first member
.

Root cause is in the JDK: GZIPInputStream.readTrailer() decides whether another gzip member follows partly
from the underlying stream's available(). A network-backed response body can transiently report
available() == 0 exactly at a member boundary (the socket momentarily has no buffered bytes even though more
data is still in flight). GZIPInputStream treats that transient 0 as end-of-stream, stops decoding, and
drops every remaining member — no error is raised, so the caller just receives partial data.

Modifications

  • New internal decorator GzipAvailabilityInputStream (@SdkInternalApi, core/sdk-core):
    • Passively classifies the stream from its first 3 bytes (gzip magic 1f 8b + DEFLATE method 08).
    • For detected gzip content only, reports available() as at least 1 while the stream is open and not at
      EOF, so GZIPInputStream keeps reading across member boundaries instead of stopping on a transient 0.
    • Non-gzip streams pass available() through unchanged, so text/line consumers (e.g. BufferedReader) are
      unaffected.
    • Tracks EOF so the coercion stops at the real end of the stream (terminates, does not hang), propagates
      release() to a Releasable delegate, and preserves read/skip/mark/reset/close semantics.
  • ResponseInputStream now wraps the body InputStream in GzipAvailabilityInputStream in both
    timeout-aware constructors. The abortable is still captured from the original stream, so abort()
    propagates to the underlying connection unchanged.
  • Wrapping is unconditional (applied to every response body) rather than gated on 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 0 from
available() for all streams, which broke incremental line reads (BufferedReader/InputStreamReader). By
detecting gzip passively, non-gzip streaming is left completely unchanged.

Testing

New tests, plus all existing ResponseInputStream tests continue to pass.

  • GzipAvailabilityInputStreamTest (unit): available() coercion for gzip vs. honest pass-through for
    non-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 real ResponseInputStream): end-to-end decode of
    concatenated and many-member gzip through a real GZIPInputStream over a trickle source that reports
    available() == 0; single-member decode without hanging; a genuinely blocking source that blocks then
    signals EOF; BufferedReader line delivery on non-gzip content; mark/reset transparency; and abort()
    propagation through the inserted wrapper.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@jencymaryjoseph
jencymaryjoseph requested a review from a team as a code owner August 28, 2026 19:56
@jencymaryjoseph
jencymaryjoseph requested a review from dagnir August 28, 2026 20:02
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