-
Notifications
You must be signed in to change notification settings - Fork 1k
fix: prevent concatenated gzip response truncation when read via GZIPInputStream #7331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jencymaryjoseph
wants to merge
1
commit into
master
Choose a base branch
from
jencyjos/gzip-concatenated-truncation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "type": "bugfix", | ||
| "category": "AWS SDK for Java v2", | ||
| "contributor": "", | ||
| "description": "Fixed an issue where concatenated gzip response streams could be truncated to the first member when decoded with GZIPInputStream. A transient available()==0 at a gzip member boundary was treated as end of stream; ResponseInputStream now reports available() as at least 1 for gzip content while the stream is open." | ||
| } |
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
164 changes: 164 additions & 0 deletions
164
...re/src/main/java/software/amazon/awssdk/core/internal/io/GzipAvailabilityInputStream.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * A copy of the License is located at | ||
| * | ||
| * http://aws.amazon.com/apache2.0 | ||
| * | ||
| * or in the "license" file accompanying this file. This file is distributed | ||
| * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| * express or implied. See the License for the specific language governing | ||
| * permissions and limitations under the License. | ||
| */ | ||
|
|
||
| package software.amazon.awssdk.core.internal.io; | ||
|
|
||
| import java.io.FilterInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import software.amazon.awssdk.annotations.SdkInternalApi; | ||
| import software.amazon.awssdk.utils.IoUtils; | ||
|
|
||
| /** | ||
| * Wraps a response body so {@code available()} never returns {@code 0} for gzip content while the stream is open. | ||
| * {@link java.util.zip.GZIPInputStream} treats a transient {@code 0} from {@code available()} at a member boundary | ||
| * as end of stream and stops, truncating concatenated gzip. Gzip is detected passively from the leading bytes | ||
| * ({@code 1f 8b 08}); non-gzip streams keep honest {@code available()}. | ||
| */ | ||
| @SdkInternalApi | ||
| public final class GzipAvailabilityInputStream extends FilterInputStream implements Releasable { | ||
|
|
||
| private static final int GZIP_MAGIC_1 = 0x1f; | ||
| private static final int GZIP_MAGIC_2 = 0x8b; | ||
| private static final int GZIP_METHOD_DEFLATE = 0x08; | ||
| private static final int HEADER_LENGTH = 3; | ||
|
|
||
| private final byte[] header = new byte[HEADER_LENGTH]; | ||
| private volatile int headerLen; | ||
| private volatile boolean classified; | ||
| private volatile boolean gzipDetected; | ||
| private volatile boolean eof; | ||
| private volatile boolean closed; | ||
|
|
||
| private int markHeaderLen; | ||
| private boolean markClassified; | ||
| private boolean markGzipDetected; | ||
| private boolean markEof; | ||
| private boolean marked; | ||
|
|
||
| public GzipAvailabilityInputStream(InputStream in) { | ||
| super(in); | ||
| } | ||
|
|
||
| @Override | ||
| public int read() throws IOException { | ||
| int b = in.read(); | ||
| if (b == -1) { | ||
| eof = true; | ||
| } else { | ||
| if (eof) { | ||
| eof = false; | ||
| } | ||
| observe((byte) b); | ||
| } | ||
| return b; | ||
| } | ||
|
|
||
| @Override | ||
| public int read(byte[] b, int off, int len) throws IOException { | ||
| int n = in.read(b, off, len); | ||
| if (n == -1) { | ||
| eof = true; | ||
| } else if (n > 0) { | ||
| if (eof) { | ||
| eof = false; | ||
| } | ||
| observe(b, off, n); | ||
| } | ||
| return n; | ||
| } | ||
|
|
||
| @Override | ||
| public int available() throws IOException { | ||
| if (closed) { | ||
| return 0; | ||
| } | ||
| int available = in.available(); | ||
| return available == 0 && gzipDetected && !eof ? 1 : available; | ||
| } | ||
|
|
||
| @Override | ||
| public long skip(long n) throws IOException { | ||
| long skipped = in.skip(n); | ||
| if (skipped > 0) { | ||
| classified = true; | ||
| } | ||
| return skipped; | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void mark(int readlimit) { | ||
| markHeaderLen = headerLen; | ||
| markClassified = classified; | ||
| markGzipDetected = gzipDetected; | ||
| markEof = eof; | ||
| marked = true; | ||
| in.mark(readlimit); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void reset() throws IOException { | ||
| in.reset(); | ||
| if (marked) { | ||
| headerLen = markHeaderLen; | ||
| classified = markClassified; | ||
| gzipDetected = markGzipDetected; | ||
| eof = markEof; | ||
| } else { | ||
| headerLen = 0; | ||
| classified = false; | ||
| gzipDetected = false; | ||
| eof = false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void close() throws IOException { | ||
| closed = true; | ||
| in.close(); | ||
| } | ||
|
|
||
| @Override | ||
| public void release() { | ||
| IoUtils.closeQuietly(this, null); | ||
| if (in instanceof Releasable) { | ||
| ((Releasable) in).release(); | ||
| } | ||
| } | ||
|
|
||
| private void observe(byte[] b, int off, int len) { | ||
| if (classified) { | ||
| return; | ||
| } | ||
| for (int i = 0; i < len && !classified; i++) { | ||
| observe(b[off + i]); | ||
| } | ||
| } | ||
|
|
||
| private void observe(byte b) { | ||
| if (classified) { | ||
| return; | ||
| } | ||
| int len = headerLen; | ||
| header[len] = b; | ||
| headerLen = len + 1; | ||
| if (headerLen == HEADER_LENGTH) { | ||
| classified = true; | ||
| gzipDetected = (header[0] & 0xff) == GZIP_MAGIC_1 | ||
| && (header[1] & 0xff) == GZIP_MAGIC_2 | ||
| && (header[2] & 0xff) == GZIP_METHOD_DEFLATE; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I don't think this is the right approach; it would make more sense to push the decision of whether this is a GZIP inputstream further down the stack (i.e. closer to the transport/HTTP layer). For example, is there some way we can check the
content-typeheader, and if it's GZIP, wrap the stream there?Another reason this is problematic is what happens if somewhere down the line we need to make another "workaround" for some other type of content stream?