-
Notifications
You must be signed in to change notification settings - Fork 1.8k
AVRO-4247: Enforce decompression size limits #3745
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
steveloughran
wants to merge
12
commits into
apache:main
Choose a base branch
from
steveloughran:pr/java-codec-decompression
base: main
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 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
50a6bfc
[AVRO-4081] Add decompression size limit to prevent decompression bom…
OwenSanzas 13edbf8
Update lang/java/avro/src/main/java/org/apache/avro/file/DeflateCodec…
OwenSanzas 406bf50
Update lang/java/avro/src/main/java/org/apache/avro/file/DeflateCodec…
OwenSanzas fd85f2e
Address code review feedback for decompression bomb fix
OwenSanzas a406f72
Merge branch 'main' into pr/java-codec-decompression
steveloughran 87b7126
[AVRO-4] Move limit checks into NonCopyingByteArrayOutputStream
steveloughran a0bc310
[AVRO-4247] review feedback
steveloughran c1b35a7
[AVRO-4247] Rework invocation
steveloughran 5f0b517
spotless
steveloughran f481b5a
getLongLimitFromProperty() to use parameters over inline constants
steveloughran adaf226
change static factory method to "capacityLimitedOutputStream()"
steveloughran 82b04ec
Apply compressor changes from iemejia
steveloughran 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
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 |
|---|---|---|
|
|
@@ -25,6 +25,8 @@ | |
| import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; | ||
| import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream; | ||
|
|
||
| import static org.apache.avro.util.NonCopyingByteArrayOutputStream.restrictedCapacityOutputStream; | ||
|
|
||
| /** * Implements bzip2 compression and decompression. */ | ||
| public class BZip2Codec extends Codec { | ||
|
|
||
|
|
@@ -45,7 +47,7 @@ public String getName() { | |
|
|
||
| @Override | ||
| public ByteBuffer compress(ByteBuffer uncompressedData) throws IOException { | ||
| NonCopyingByteArrayOutputStream baos = new NonCopyingByteArrayOutputStream(DEFAULT_BUFFER_SIZE); | ||
| NonCopyingByteArrayOutputStream baos = restrictedCapacityOutputStream(DEFAULT_BUFFER_SIZE); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just want to confirm: did you actually want this to be restricted? I thought the last round of review landed on not setting a limit on compression (despite Claude's enthusiasm!) |
||
|
|
||
| try (BZip2CompressorOutputStream outputStream = new BZip2CompressorOutputStream(baos)) { | ||
| outputStream.write(uncompressedData.array(), computeOffset(uncompressedData), uncompressedData.remaining()); | ||
|
|
@@ -60,7 +62,7 @@ public ByteBuffer decompress(ByteBuffer compressedData) throws IOException { | |
| compressedData.remaining()); | ||
|
|
||
| @SuppressWarnings("resource") | ||
| NonCopyingByteArrayOutputStream baos = new NonCopyingByteArrayOutputStream(DEFAULT_BUFFER_SIZE); | ||
| NonCopyingByteArrayOutputStream baos = restrictedCapacityOutputStream(DEFAULT_BUFFER_SIZE); | ||
|
|
||
| try (BZip2CompressorInputStream inputStream = new BZip2CompressorInputStream(bais)) { | ||
|
|
||
|
|
||
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
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
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
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
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
79 changes: 79 additions & 0 deletions
79
lang/java/avro/src/test/java/org/apache/avro/util/NonCopyingByteArrayOutputStreamTest.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,79 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License 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 org.apache.avro.util; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.apache.avro.SystemLimitException; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
|
||
| public class NonCopyingByteArrayOutputStreamTest { | ||
|
|
||
| /** | ||
| * Basic test: write then read. | ||
| */ | ||
| @Test | ||
| public void testDefaultWriteWorks() throws IOException { | ||
| NonCopyingByteArrayOutputStream out = new NonCopyingByteArrayOutputStream(1); | ||
| out.write('a'); | ||
| final byte[] b = "string".getBytes(); | ||
| out.write(b, 0, b.length); | ||
| out.close(); | ||
| final ByteBuffer buffer = out.asByteBuffer(); | ||
| assertEquals('a', buffer.get()); | ||
| for (byte value : b) { | ||
| assertEquals(value, buffer.get()); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test write limiting. | ||
| */ | ||
| @Test | ||
| public void testLimitedWrite() throws IOException { | ||
| NonCopyingByteArrayOutputStream out = NonCopyingByteArrayOutputStream.restrictedCapacityOutputStream(1, 4); | ||
| out.write('a'); | ||
| // it's impossible to go over the limit in a write(bytes) call. | ||
| final byte[] b = "longstring".getBytes(); | ||
| assertThrows(SystemLimitException.class, () -> out.write(b), | ||
| "Buffer size 11 (bytes) exceeds maximum allowed size 4."); | ||
| // we can still write up to the limit...the buffer has not been written to yet. | ||
| out.write(b, 0, 2); | ||
| out.write('z'); | ||
| // now at end of file, so another write shall fail. | ||
| assertThrows(SystemLimitException.class, () -> out.write('x')); | ||
| out.close(); | ||
| // validate everything successfully written is there | ||
| final ByteBuffer buffer = out.asByteBuffer(); | ||
| for (byte value : "aloz".getBytes()) { | ||
| assertEquals(value, buffer.get()); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testInnerLimitCheck() throws Throwable { | ||
| assertThrows(SystemLimitException.class, () -> SystemLimitException.checkMaxDecompressCapacity(256L, 0, 100_000)); | ||
| SystemLimitException.checkMaxDecompressCapacity(256L, 0, 256); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.