Skip to content

[AVRO-4247] enforce decompression size limits#3745

Open
steveloughran wants to merge 6 commits intoapache:mainfrom
steveloughran:pr/java-codec-decompression
Open

[AVRO-4247] enforce decompression size limits#3745
steveloughran wants to merge 6 commits intoapache:mainfrom
steveloughran:pr/java-codec-decompression

Conversation

@steveloughran
Copy link
Copy Markdown

What is the purpose of the change

#3625 with size limit checks moved into the NonCopyingByteArrayOutputStream

  • guarantees all decompressors get the coverage
  • will make writing a test trivial

There's a new constructor to NonCopyingByteArrayOutputStream to set a size limit, or no limit, and the default constructor now automatically picks up the size set by system property/fallback default.

Those choices could be discussed, with options being

  • static function to get "size limited output stream"
  • move fetch and pase of system property into org.apache.avro.SystemLimitException, where the int parser lives.

AI: No AI was used for this PR.

Verifying this change

Needs tests, if people are happy with the design I can put one in whichever module people would prefer...it's pretty straightforward

Documentation

  • Does this pull request introduce a new feature? (yes / no)
    yes

  • If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
    javadocs

OwenSanzas and others added 6 commits January 14, 2026 10:31
…b DoS

Add maximum decompression size limit in DeflateCodec to prevent
OutOfMemoryError when processing maliciously crafted Avro files
with high compression ratios (decompression bombs).

The limit defaults to 200MB and can be configured via system property:
org.apache.avro.limits.decompress.maxLength
….java


Thanks!

Co-authored-by: Martin Grigorov <martin-g@users.noreply.github.com>
….java

Co-authored-by: Martin Grigorov <martin-g@users.noreply.github.com>
- Move MAX_DECOMPRESS_LENGTH initialization to static block (read once at class load)
- Add WARNING log for invalid property values (NumberFormatException)
- Validate negative and zero values, reject with warning
- Add "(bytes)" to error message for clarity
- Add quotes around property name in error message

Test command:
java -Xmx64m -Dorg.apache.avro.limits.decompress.maxLength=1048576 \
  -jar avro-tools-1.13.0-SNAPSHOT.jar tojson poc.avro

Expected behavior:
Exception in thread "main" org.apache.avro.AvroRuntimeException:
Decompressed size 1056768 (bytes) exceeds maximum allowed size 1048576.
This can be configured by setting the system property 'org.apache.avro.limits.decompress.maxLength'
Change-Id: Ib24c52cdf3234a3805628041946b229b221383ad
* Automatically available to all codecs
* Does need an explicit constructor with no limit, used in DataFileWriter
* No tests, though that new constructor makes it trivial

Note: merged in main as DataFileWriter changes would otherwise stop merging
Change-Id: Ifc5b8921a00425df331a4889472b3e78c6677bde
@github-actions github-actions Bot added the Java Pull Requests for Java binding label Apr 28, 2026
@steveloughran steveloughran changed the title [AVRO-4247] decompresson size limits [AVRO-4247] enforce decompression size limits Apr 28, 2026
private static final long MAX_DECOMPRESS_LENGTH;

static {
String prop = System.getProperty(MAX_DECOMPRESS_LENGTH_PROPERTY);
Copy link
Copy Markdown
Author

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.

* @throws IllegalArgumentException if size is negative
*/
public NonCopyingByteArrayOutputStream(int size) {
this(size, MAX_DECOMPRESS_LENGTH);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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

  • change the default (here)
  • change the code uses to take a limit
  • private two arg ctor and a public static creator method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java Pull Requests for Java binding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants