-
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
13
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 6 commits
Commits
Show all changes
13 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 83dc17b
Beautify with spotless
RyanSkraba 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 |
|---|---|---|
|
|
@@ -21,21 +21,78 @@ | |
| import java.io.ByteArrayOutputStream; | ||
| import java.nio.ByteBuffer; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import org.apache.avro.SystemLimitException; | ||
|
|
||
| /** | ||
| * Utility to make data written to an {@link ByteArrayOutputStream} directly | ||
| * available as a {@link ByteBuffer}. | ||
| * available as a {@link ByteBuffer}. Optional limit to amount of data which may | ||
| * be written. | ||
| */ | ||
| public class NonCopyingByteArrayOutputStream extends ByteArrayOutputStream { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(NonCopyingByteArrayOutputStream.class); | ||
|
|
||
| /** | ||
| * System property declaring max size of any decompression stream: {@value}. | ||
| */ | ||
| private static final String MAX_DECOMPRESS_LENGTH_PROPERTY = "org.apache.avro.limits.decompress.maxLength"; | ||
|
|
||
| /** | ||
| * Default limit: {@value}. | ||
| */ | ||
| private static final long DEFAULT_MAX_DECOMPRESS_LENGTH = 200L * 1024 * 1024; | ||
|
|
||
| private static final long MAX_DECOMPRESS_LENGTH; | ||
|
|
||
| static { | ||
| String prop = System.getProperty(MAX_DECOMPRESS_LENGTH_PROPERTY); | ||
| long limit = DEFAULT_MAX_DECOMPRESS_LENGTH; | ||
| if (prop != null) { | ||
| try { | ||
| long parsed = Long.parseLong(prop); | ||
| if (parsed <= 0) { | ||
| LOG.warn("Invalid value '{}' for property '{}': must be positive. Using default: {}", prop, | ||
| MAX_DECOMPRESS_LENGTH_PROPERTY, DEFAULT_MAX_DECOMPRESS_LENGTH); | ||
| } else { | ||
| limit = parsed; | ||
| } | ||
| } catch (NumberFormatException e) { | ||
| LOG.warn("Could not parse property '{}' value '{}'. Using default: {}", MAX_DECOMPRESS_LENGTH_PROPERTY, prop, | ||
| DEFAULT_MAX_DECOMPRESS_LENGTH); | ||
| } | ||
| } | ||
| MAX_DECOMPRESS_LENGTH = limit; | ||
| } | ||
|
|
||
| /** | ||
| * Size limit, -1 for no limits. | ||
| */ | ||
| private final long limit; | ||
|
|
||
| /** | ||
| * Creates a new byte array output stream, with a buffer capacity of the | ||
| * specified size, in bytes. | ||
| * specified size, in bytes, size limit {@link #MAX_DECOMPRESS_LENGTH} | ||
| * | ||
| * @param size the initial size | ||
| * @throws IllegalArgumentException if size is negative | ||
| */ | ||
| public NonCopyingByteArrayOutputStream(int size) { | ||
| this(size, MAX_DECOMPRESS_LENGTH); | ||
|
Author
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. this does change the default operation. Apart from DataFileWriter, it is only ever used in decompressors. Options
|
||
| } | ||
|
|
||
| /** | ||
| * Creates a new byte array output stream, with a buffer capacity of the | ||
| * specified size, in bytes, size limit as specified. | ||
| * | ||
| * @param size buffer capacity | ||
| * @param limit size limit or -1 for no limit. | ||
| */ | ||
| public NonCopyingByteArrayOutputStream(final int size, final long limit) { | ||
|
RyanSkraba marked this conversation as resolved.
Outdated
|
||
| super(size); | ||
| this.limit = limit; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -48,4 +105,37 @@ public NonCopyingByteArrayOutputStream(int size) { | |
| public ByteBuffer asByteBuffer() { | ||
| return ByteBuffer.wrap(super.buf, 0, super.count); | ||
| } | ||
|
|
||
| /** | ||
| * Check there is capacity to write data. | ||
| * | ||
| * @param bytes bytes to add | ||
| * @throws SystemLimitException if the limit is exceeded. | ||
| */ | ||
| private void checkCapacity(int bytes) { | ||
| if (limit >= 0 && (size() + bytes) >= limit) { | ||
| throw new SystemLimitException( | ||
| String.format("Buffer size %,3d (bytes) exceeds maximum allowed size %,3d.", (size() + bytes), limit)); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void write(final int b) { | ||
| checkCapacity(1); | ||
| super.write(b); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void write(final byte[] b, final int off, final int len) { | ||
| checkCapacity(len); | ||
| super.write(b, off, len); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeBytes(final byte[] b) { | ||
| checkCapacity(b.length); | ||
| super.writeBytes(b); | ||
| } | ||
|
|
||
| } | ||
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.
could move to SystemLimitException, as that's where the int equivalent lives.
Uh oh!
There was an error while loading. Please reload this page.
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.
Yes,
MAX_DECOMPRESS_LENGTH_PROPERTYmakes more sense in theSystemLimitExceptionclass