Skip to content

Only fail in ZlibDecoder when the decompression buffer is full - #17692

Merged
normanmaurer merged 1 commit into
netty:4.2from
allthingssecurity:fix-zlib-decoder-small-max-allocation
Oct 1, 2026
Merged

normanmaurer merged 1 commit into
netty:4.2from
allthingssecurity:fix-zlib-decoder-small-max-allocation

Conversation

@allthingssecurity

Copy link
Copy Markdown
Contributor

Motivation:

Since #17370 (and #17392 for JZlibDecoder), the zlib decoders ask ZlibDecoder.prepareDecompressBuffer(...) for at least 512 bytes (MIN_OUTPUT_BUFFER_SIZE) whenever an inflate call did not finish the stream. prepareDecompressBuffer throws as soon as ensureWritable(preferredSize, true) returns 1, that is, when the buffer is already at maxAllocation and has fewer writable bytes than requested. It does not check whether the buffer still has room.

So with a maxAllocation of 512 or less, the decoder throws DecompressionException: Decompression buffer has reached maximum size for every stream that arrives in more than one read, even when the buffer is empty. For example, a ZLIB stream of 36 bytes of output with maxAllocation = 375, split into two reads, is rejected. With a larger maxAllocation the same happens once the buffer holds more than maxAllocation - 512 bytes and the stream continues, although the remaining space would be used by the next inflate call.

Measured with 2,000 random streams per row and wrapper (ZLIB, GZIP, NONE), split into two reads, whose whole output fits into maxAllocation:

maxAllocation JdkZlibDecoder 4.2.18 before #17370 with this change
1-512 73-100% fail 0 0 (1)
513-1100 about 8% fail 0 0
4096-70000 2 of 6,000 fail 0 0

(1) except when one read produces exactly maxAllocation bytes and the stream continues; see below.

JZlibDecoder also requests max(2 * remaining input, 512) bytes before its first inflate. When 2 * input > maxAllocation, an input whose output fits is rejected before any byte is decompressed: a single read that holds a ZLIB stream of 600 random bytes fails with maxAllocation = 1024. That part predates #17392.

#17364 was merged into 4.1 as well, so 4.1.138.Final has the same behaviour for JdkZlibDecoder.

The javadoc of prepareDecompressBuffer already says that it calls decompressionBufferExhausted(...) "if the buffer is full and cannot be expanded further".

Modification:

  • In ZlibDecoder.prepareDecompressBuffer(...), only give up when the buffer could not be expanded and has no writable byte left. Until then the decoder keeps inflating into the space that is left, and a stream whose output exceeds maxAllocation still fails as before.
  • New tests in ZlibTest, so they run for both JdkZlibDecoder and JZlibDecoder:
    • @ParameterizedTest over ZLIB, GZIP and NONE with maxAllocation 2, 64 and 512: an output smaller than maxAllocation, decoded in one read, in two reads and byte by byte;
    • @ParameterizedTest over 600, 1000 and 1024 incompressible bytes with maxAllocation = 1024 in one read.
    • a guard, @ParameterizedTest over ZLIB, GZIP and NONE with maxAllocation 64, and ZLIB with 512: 64 KiB of zeros (about 100 bytes compressed) in one read is still rejected with "Decompression buffer has reached maximum size", so the limit keeps protecting against small inputs with a large output.

Result:

maxAllocation limits the output of a read again, without rejecting streams whose output fits.

  • JdkZlibTest (44) and JZlibTest (26) pass. Without the change to ZlibDecoder, 7 of the new tests fail for JdkZlibDecoder and 10 for JZlibDecoder; the incompressible cases pass for JdkZlibDecoder both ways, and the guard and the existing testMaxAllocation (a 1 MiB output in one read must fail) pass both ways.
  • codec-compression tests: 548 tests, 0 failures. Checkstyle and revapi pass.
  • The table above was produced by the same random check with the change.

Not changed: a read whose output is exactly maxAllocation bytes, while the stream continues, still fails as on 4.2.18. The decoder cannot tell whether more output is pending without a larger buffer.

The problem was found by a randomized split-input check of all decoders, and the buffer logic was modelled in Lean 4 (the changed method throws only when the buffer is full, and never where it did not throw before). The change was prepared with Claude Code (Claude Opus 5.5), and the commit has a Co-Authored-By trailer.

Prepared with Claude Code on behalf of allthingssecurity.

🤖 Generated with Claude Code

Motivation:

Since the zlib decoders ask for at least 512 bytes of output space after
every inflate call that did not finish the stream (netty#17370, netty#17392),
ZlibDecoder.prepareDecompressBuffer(...) throws as soon as the buffer is
at maxAllocation and has fewer than 512 writable bytes, even when it is
empty. With a maxAllocation of 512 or less every stream that is split
across reads is rejected, and with larger values streams whose output
fits are rejected once the buffer holds more than maxAllocation - 512
bytes. JZlibDecoder also rejects a single read whose input is more than
half of maxAllocation before decompressing anything.

Modification:

- Only throw when the buffer cannot be expanded and has no writable byte
  left, as the javadoc describes.
- Add tests to ZlibTest, so they run for JdkZlibDecoder and JZlibDecoder.

Result:

maxAllocation limits the output of a read without rejecting streams whose
output fits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@testlens-app

testlens-app Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 4fca64c
▶️ Tests: 411125 executed
🟡 Checks: 29/32 completed


Learn more about TestLens at testlens.app/docs.

@franz1981 franz1981 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@normanmaurer normanmaurer added this to the 4.2.19.Final milestone Oct 1, 2026
@normanmaurer normanmaurer added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Oct 1, 2026
@normanmaurer
normanmaurer merged commit e6fc471 into netty:4.2 Oct 1, 2026
30 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #17698

@github-actions github-actions Bot removed the needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. label Oct 1, 2026
@netty-project-bot

Copy link
Copy Markdown
Contributor

Could not create auto-port PR.
Got conflicts when cherry-picking onto 5.0.

@allthingssecurity
allthingssecurity deleted the fix-zlib-decoder-small-max-allocation branch October 1, 2026 10:15
normanmaurer added a commit that referenced this pull request Oct 1, 2026
… is full (#17698)

Auto-port of #17692 to 4.1
Cherry-picked commit: e6fc471

---
Motivation:

Since #17370 (and #17392 for `JZlibDecoder`), the zlib decoders ask
`ZlibDecoder.prepareDecompressBuffer(...)` for at least 512 bytes
(`MIN_OUTPUT_BUFFER_SIZE`) whenever an `inflate` call did not finish the
stream. `prepareDecompressBuffer` throws as soon as
`ensureWritable(preferredSize, true)` returns 1, that is, when the
buffer is already at `maxAllocation` and has fewer writable bytes than
requested. It does not check whether the buffer still has room.

So with a `maxAllocation` of 512 or less, the decoder throws
`DecompressionException: Decompression buffer has reached maximum size`
for every stream that arrives in more than one read, even when the
buffer is empty. For example, a ZLIB stream of 36 bytes of output with
`maxAllocation = 375`, split into two reads, is rejected. With a larger
`maxAllocation` the same happens once the buffer holds more than
`maxAllocation - 512` bytes and the stream continues, although the
remaining space would be used by the next `inflate` call.

Measured with 2,000 random streams per row and wrapper (ZLIB, GZIP,
NONE), split into two reads, whose whole output fits into
`maxAllocation`:

| `maxAllocation` | `JdkZlibDecoder` 4.2.18 | before #17370 | with this
change |
|---|---|---|---|
| 1-512 | 73-100% fail | 0 | 0 (1) |
| 513-1100 | about 8% fail | 0 | 0 |
| 4096-70000 | 2 of 6,000 fail | 0 | 0 |

(1) except when one read produces exactly `maxAllocation` bytes and the
stream continues; see below.

`JZlibDecoder` also requests `max(2 * remaining input, 512)` bytes
before its first `inflate`. When `2 * input > maxAllocation`, an input
whose output fits is rejected before any byte is decompressed: a single
read that holds a ZLIB stream of 600 random bytes fails with
`maxAllocation = 1024`. That part predates #17392.

#17364 was merged into 4.1 as well, so 4.1.138.Final has the same
behaviour for `JdkZlibDecoder`.

The javadoc of `prepareDecompressBuffer` already says that it calls
`decompressionBufferExhausted(...)` "if the buffer is full and cannot be
expanded further".

Modification:

- In `ZlibDecoder.prepareDecompressBuffer(...)`, only give up when the
buffer could not be expanded and has no writable byte left. Until then
the decoder keeps inflating into the space that is left, and a stream
whose output exceeds `maxAllocation` still fails as before.
- New tests in `ZlibTest`, so they run for both `JdkZlibDecoder` and
`JZlibDecoder`:
- `@ParameterizedTest` over ZLIB, GZIP and NONE with `maxAllocation` 2,
64 and 512: an output smaller than `maxAllocation`, decoded in one read,
in two reads and byte by byte;
- `@ParameterizedTest` over 600, 1000 and 1024 incompressible bytes with
`maxAllocation = 1024` in one read.
- a guard, `@ParameterizedTest` over ZLIB, GZIP and NONE with
`maxAllocation` 64, and ZLIB with 512: 64 KiB of zeros (about 100 bytes
compressed) in one read is still rejected with "Decompression buffer has
reached maximum size", so the limit keeps protecting against small
inputs with a large output.

Result:

`maxAllocation` limits the output of a read again, without rejecting
streams whose output fits.

---------

Co-authored-by: allthingssecurity <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
@normanmaurer

Copy link
Copy Markdown
Member

Port for 5.0: #17704

@normanmaurer normanmaurer removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 2, 2026
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
… is full (netty#17698)

Auto-port of netty#17692 to 4.1
Cherry-picked commit: e6fc471

---
Motivation:

Since netty#17370 (and netty#17392 for `JZlibDecoder`), the zlib decoders ask
`ZlibDecoder.prepareDecompressBuffer(...)` for at least 512 bytes
(`MIN_OUTPUT_BUFFER_SIZE`) whenever an `inflate` call did not finish the
stream. `prepareDecompressBuffer` throws as soon as
`ensureWritable(preferredSize, true)` returns 1, that is, when the
buffer is already at `maxAllocation` and has fewer writable bytes than
requested. It does not check whether the buffer still has room.

So with a `maxAllocation` of 512 or less, the decoder throws
`DecompressionException: Decompression buffer has reached maximum size`
for every stream that arrives in more than one read, even when the
buffer is empty. For example, a ZLIB stream of 36 bytes of output with
`maxAllocation = 375`, split into two reads, is rejected. With a larger
`maxAllocation` the same happens once the buffer holds more than
`maxAllocation - 512` bytes and the stream continues, although the
remaining space would be used by the next `inflate` call.

Measured with 2,000 random streams per row and wrapper (ZLIB, GZIP,
NONE), split into two reads, whose whole output fits into
`maxAllocation`:

| `maxAllocation` | `JdkZlibDecoder` 4.2.18 | before netty#17370 | with this
change |
|---|---|---|---|
| 1-512 | 73-100% fail | 0 | 0 (1) |
| 513-1100 | about 8% fail | 0 | 0 |
| 4096-70000 | 2 of 6,000 fail | 0 | 0 |

(1) except when one read produces exactly `maxAllocation` bytes and the
stream continues; see below.

`JZlibDecoder` also requests `max(2 * remaining input, 512)` bytes
before its first `inflate`. When `2 * input > maxAllocation`, an input
whose output fits is rejected before any byte is decompressed: a single
read that holds a ZLIB stream of 600 random bytes fails with
`maxAllocation = 1024`. That part predates netty#17392.

netty#17364 was merged into 4.1 as well, so 4.1.138.Final has the same
behaviour for `JdkZlibDecoder`.

The javadoc of `prepareDecompressBuffer` already says that it calls
`decompressionBufferExhausted(...)` "if the buffer is full and cannot be
expanded further".

Modification:

- In `ZlibDecoder.prepareDecompressBuffer(...)`, only give up when the
buffer could not be expanded and has no writable byte left. Until then
the decoder keeps inflating into the space that is left, and a stream
whose output exceeds `maxAllocation` still fails as before.
- New tests in `ZlibTest`, so they run for both `JdkZlibDecoder` and
`JZlibDecoder`:
- `@ParameterizedTest` over ZLIB, GZIP and NONE with `maxAllocation` 2,
64 and 512: an output smaller than `maxAllocation`, decoded in one read,
in two reads and byte by byte;
- `@ParameterizedTest` over 600, 1000 and 1024 incompressible bytes with
`maxAllocation = 1024` in one read.
- a guard, `@ParameterizedTest` over ZLIB, GZIP and NONE with
`maxAllocation` 64, and ZLIB with 512: 64 KiB of zeros (about 100 bytes
compressed) in one read is still rejected with "Decompression buffer has
reached maximum size", so the limit keeps protecting against small
inputs with a large output.

Result:

`maxAllocation` limits the output of a read again, without rejecting
streams whose output fits.

---------

Co-authored-by: allthingssecurity <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
hyperxpro pushed a commit to AsyncHttpClient/async-http-client that referenced this pull request Nov 4, 2026
…ty 4.2.19 does (#2366)

## Summary

- `main` fails
`Http1ContentDecompressorTest.aBufferThatCannotGrowFailsLikeNetty` on
every platform and JDK since #2357 and #2364 (Netty 4.2.19) were both
merged.
- Netty 4.2.19 includes netty/netty#17692: `ZlibDecoder` fails with
"Decompression buffer has reached maximum size" only once the buffer
cannot grow and is also full. The HTTP/1.1 inflate loop from #2357
copies the check from before that change.
- Add the same condition to the loop, and let the test compare both
cases with Netty.

## Problem

The inflate loop grows its output buffer after every unfinished step, as
`ZlibDecoder.prepareDecompressBuffer` does, so that a buffer that cannot
grow fails at the same point as with Netty. Up to 4.2.18, Netty failed
as soon as `ensureWritable(preferredSize, true)` returned 1, even if the
buffer still had room. Since 4.2.19 it keeps inflating into the space
that is left and fails only once nothing is writable.

`aBufferThatCannotGrowFailsLikeNetty` feeds a flushed raw deflate stream
of 900 random bytes into a buffer capped at 1024 bytes. Netty 4.2.19
decodes it; the loop still failed it, so the test's comparison fails.
The "Build PR" run on `b9dbb212a` failed on all 12 test jobs with this
test alone.

## Change

- `Http1ContentDecompressor`: fail only if `ensureWritable(...) == 1 &&
!decompressed.isWritable()`, as `ZlibDecoder` does now.
- `aBufferThatCannotGrowFailsLikeNetty` compares two cases with Netty:
output that fits into the capped buffer is decoded, and output that
overflows it (64 KiB of zeros) fails with Netty's message. Both still
check that the inputs are released.

The test from #2357 caught this; thanks @pavel-ptashyts.

## AI disclosure

Claude Code on behalf of Matthias Kurz. The commit includes
`Co-Authored-By` per `AGENTS.md`.

## Test plan

- [x] On `main` (`b9dbb212a`), JDK 11:
`aBufferThatCannotGrowFailsLikeNetty` fails in 3 of 3 runs, as in CI.
- [x] With the change, `Http1ContentDecompressorTest` passes: 28 tests,
including its comparisons with Netty over more than 300 scenarios.
- [x] Without the code change, the new test fails in the "output fits"
case, with the error `main` shows; the "output overflows" case fails the
same way in both decoders.
- [x] `./mvnw -B -ntp -Dgpg.skip=true clean verify` on JDK 11: 1,819
tests, 0 failures, 0 errors, 11 skipped; Revapi passed. No test-skipping
flags were used.

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

6 participants