Skip to content

Fix ZstdDecoder and ZstdDecompressor with a non-positive defaultMaxForwardBytes - #18289

Merged
normanmaurer merged 2 commits into
netty:4.2from
mkurz:fix/zstd-decoder-non-positive-forward-bytes
Nov 13, 2026
Merged

normanmaurer merged 2 commits into
netty:4.2from
mkurz:fix/zstd-decoder-non-positive-forward-bytes

Conversation

@mkurz

@mkurz mkurz commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

Motivation:

Since #17896, ZstdDecoder sizes its output buffers as the smaller of maxForwardBytes and the frame content size. With a maximumAllocationSize of 0, which HttpContentDecompressor and DelegatingDecompressorFrameListener pass by default, maxForwardBytes is io.netty.compression.defaultMaxForwardBytes. When that property is set to 0, every output buffer has a capacity of 0, so nothing is decompressed and only empty buffers are forwarded: a zstd body comes out empty, without an error. Below 0, the allocation fails with initialCapacity : -1. Before #17896, both decoded the body.

Modification:

  • Size the output buffers from maxForwardBytes only when it is positive, and use 64 KiB, the property's default, otherwise. The forwarding threshold itself is unchanged.
  • Don't forward empty buffers, which a threshold of 0 or less also did before Don't size ZstdDecoder output buffers from the frame content size #17896, once the output was drained.
  • Add a package-private constructor that takes the default threshold, and a test that decodes a 1 MiB body with thresholds of 0 and -1.

Result:

ZstdDecoder decodes zstd bodies again when io.netty.compression.defaultMaxForwardBytes is 0 or negative, and the frame header still cannot drive the allocation size.

Motivation:

Since netty#17896, ZstdDecoder sizes its output buffers as the smaller of
maxForwardBytes and the frame content size. With a
maximumAllocationSize of 0, which HttpContentDecompressor and
DelegatingDecompressorFrameListener pass by default,
maxForwardBytes is io.netty.compression.defaultMaxForwardBytes. When
that property is set to 0, every output buffer has a capacity of 0, so
nothing is decompressed and only empty buffers are forwarded: a zstd
body comes out empty, without an error. Below 0, the allocation fails
with "initialCapacity : -1". Before netty#17896, both decoded the body.

Modification:

- Size the output buffers from maxForwardBytes only when it is
  positive, and use 64 KiB, the property's default, otherwise. The
  forwarding threshold itself is unchanged.
- Don't forward empty buffers, which a threshold of 0 or less also did
  before netty#17896, once the output was drained.
- Add a package-private constructor that takes the default threshold,
  and a test that decodes a 1 MiB body with thresholds of 0 and -1.

Result:

ZstdDecoder decodes zstd bodies again when
io.netty.compression.defaultMaxForwardBytes is 0 or negative, and the
frame header still cannot drive the allocation size.
hyperxpro pushed a commit to AsyncHttpClient/async-http-client that referenced this pull request Nov 6, 2026
## Summary

- Upgrades Netty from 4.2.19.Final to 4.2.20.Final. Its [security
fixes](https://github.com/netty/netty/releases/tag/netty-4.2.20.Final)
include several in modules AHC uses: three out-of-bounds reads in
`netty-buffer`, unbounded memory usage in `netty-codec-http2`,
insufficient control character neutralization in `netty-codec-http`,
insufficient entropy in `netty-resolver-dns`, and an mTLS bypass via
session resumption in `netty-handler`.
- On 4.2.20, two of AHC's tests fail, both because of Netty changes.
This PR adapts AHC to them.

## Change

- **Upgrade Netty to 4.2.20.Final, and check the second gzip magic
byte:** `JdkZlibDecoder` now rejects a gzip header with a wrong second
magic byte (netty/netty#17972). The gzip inflate loop from #2357 follows
`JdkZlibDecoder`, and `Http1ContentDecompressorTest` compares the two,
so it now checks that byte too. Both bytes are read unsigned, since
`0x8b` is negative as a signed byte.
- **Keep zstd working at a non-positive threshold:** since
netty/netty#17896, the `ZstdDecoder` that `HttpContentDecompressor`
creates sizes its output buffers by
`io.netty.compression.defaultMaxForwardBytes`. At 0 it hands on an empty
body without an error; below 0 it fails with `IllegalArgumentException:
initialCapacity : -1`. AHC leaves zstd to that decoder while the
property is not positive (#2362), so it now replaces it with a
`ZstdDecoder` whose buffers hold `MAX_PART_SIZE`. The size limit still
applies, and the body is still decompressed eagerly there, as before. At
the default value of the property nothing changes. netty/netty#18289
fixes the decoder in Netty; this workaround is harmless with that fix
and can go once AHC requires a Netty version that has it.

## Notes

- Netty 4.2.0 to 4.2.4 have no `ZstdDecoder(int)` constructor.
Overriding AHC's Netty with one of those and also setting the property
to 0 or below would fail zstd responses with a `NoSuchMethodError`.

## AI disclosure

Claude Code on behalf of Matthias Kurz. Both commits include
`Co-Authored-By` per `AGENTS.md`.

## Test plan

- [x] `main` at `5542c9fc4` with `-Dnetty.version=4.2.20.Final`: 1,857
tests, 2 failures:
- `Http1ContentDecompressorTest.decodesLikeNettyForEveryBodyAndSplit`,
case "gzip second magic byte ignored": Netty now fails that body, AHC
did not.
- `Http1ContentDecompressorForwardThresholdTest`: "the zstd body
differs".
- [x] `Http1ContentDecompressorForwardThresholdTest` now runs at
thresholds 0 and -1, each in a fresh JVM, and also checks the size limit
for zstd and that every exchange releases its input and buffers. On
4.2.20 without the zstd commit it fails at both ("the zstd body
differs", "the zstd body did not end"). With it, it passes on 4.2.19 and
4.2.20.
- [x] `./mvnw -B -ntp -Dgpg.skip=true clean verify` on JDK 11, on `main`
`3e95ec8c2`: 1,905 tests, 0 failures, 0 errors, 11 skipped; Revapi
passed. No test-skipping flags were used.
- [x] `Http1ContentDecompressorForwardThresholdTest` and
`Http1ContentDecompressorTest` compiled on JDK 11 and run on JDK 17, 21
and 25 (Maven Surefire's `-Djvm`): 36 tests each, 0 failures.
- [x] This branch merged with #2369: `clean verify` on JDK 11, 1,914
tests, 0 failures, 0 errors, 11 skipped; Revapi passed (run before
#2359, #2360 and #2361 were merged, with those three merged in as well).

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@normanmaurer normanmaurer added this to the 4.2.21.Final milestone Nov 12, 2026
LI123456mo

This comment was marked as duplicate.

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ZstdDecompressor.takeOutput() looks like it has the same problem for a non-positive io.netty.compression.defaultMaxForwardBytes:

ByteBuf buf = allocator.buffer(DEFAULT_MAX_FORWARD_BYTES, DEFAULT_MAX_FORWARD_BYTES);
buf.writeBytes(output, DEFAULT_MAX_FORWARD_BYTES);
With the property at 0, the buffer has capacity 0 and nothing is written, so takeOutput() returns EMPTY_BUFFER on every call. Below 0, allocator.buffer(-1, -1) throws IllegalArgumentException.

Could we apply the same fallback here, and share the 64 KiB constant with ZstdDecoder.DEFAULT_OUTPUT_BUFFER_SIZE so the two can't drift apart? A test for the Decompressor path would also be good.

Motivation:

ZstdDecompressor.takeOutput() allocates its buffer with
io.netty.compression.defaultMaxForwardBytes as both capacity and
maximum. At 0, nothing is written, so takeOutput() returns an empty
buffer on every call while status() stays NEED_OUTPUT. Below 0, the
allocation fails with "initialCapacity : -1".

Modification:

- Size the output buffer from the property only when it is positive,
  and use ZstdDecoder.DEFAULT_OUTPUT_BUFFER_SIZE (64 KiB) otherwise, so
  that both zstd paths share the fallback.
- Add a package-private constructor that takes the default, and a test
  that decompresses a 1 MiB body with 0 and -1.

Result:

ZstdDecompressor decompresses zstd input when
io.netty.compression.defaultMaxForwardBytes is 0 or negative.
@mkurz mkurz changed the title Fix ZstdDecoder with a non-positive defaultMaxForwardBytes Fix ZstdDecoder and ZstdDecompressor with a non-positive defaultMaxForwardBytes Nov 13, 2026
@mkurz

mkurz commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, done in 5f85d55: ZstdDecompressor now uses the same 64 KiB fallback, shared through ZstdDecoder.DEFAULT_OUTPUT_BUFFER_SIZE, and ZstdDecompressorTest covers 0 and -1.

@testlens-app

testlens-app Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 5f85d55
▶️ Tests: 44445 executed
🟡 Checks: 30/33 completed


Learn more about TestLens at testlens.app/docs.

@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 Nov 13, 2026
@normanmaurer
normanmaurer merged commit 5baf6f3 into netty:4.2 Nov 13, 2026
31 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #18383

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

Copy link
Copy Markdown
Contributor

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

chrisvest added a commit that referenced this pull request Nov 13, 2026
…ve defaultMaxForwardBytes (#18383)

Auto-port of #18289 to 5.0
Cherry-picked commit: 5baf6f3

---
Motivation:

Since #17896, `ZstdDecoder` sizes its output buffers as the smaller of
`maxForwardBytes` and the frame content size. With a
`maximumAllocationSize` of 0, which `HttpContentDecompressor` and
`DelegatingDecompressorFrameListener` pass by default, `maxForwardBytes`
is `io.netty.compression.defaultMaxForwardBytes`. When that property is
set to 0, every output buffer has a capacity of 0, so nothing is
decompressed and only empty buffers are forwarded: a zstd body comes out
empty, without an error. Below 0, the allocation fails with
`initialCapacity : -1`. Before #17896, both decoded the body.

Modification:

- Size the output buffers from `maxForwardBytes` only when it is
positive, and use 64 KiB, the property's default, otherwise. The
forwarding threshold itself is unchanged.
- Don't forward empty buffers, which a threshold of 0 or less also did
before #17896, once the output was drained.
- Add a package-private constructor that takes the default threshold,
and a test that decodes a 1 MiB body with thresholds of 0 and -1.

Result:

`ZstdDecoder` decodes zstd bodies again when
`io.netty.compression.defaultMaxForwardBytes` is 0 or negative, and the
frame header still cannot drive the allocation size.

---------

Co-authored-by: Matthias Kurz <[email protected]>
Co-authored-by: Chris Vest <[email protected]>
@chrisvest

Copy link
Copy Markdown
Member

4.1 port #18401

@chrisvest chrisvest removed the needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. label Nov 13, 2026
chrisvest added a commit that referenced this pull request Nov 13, 2026
…18289) (#18401)

Motivation:

Since #17896, `ZstdDecoder` sizes its output buffers as the smaller of
`maxForwardBytes` and the frame content size. With a
`maximumAllocationSize` of 0, which `HttpContentDecompressor` and
`DelegatingDecompressorFrameListener` pass by default, `maxForwardBytes`
is `io.netty.compression.defaultMaxForwardBytes`. When that property is
set to 0, every output buffer has a capacity of 0, so nothing is
decompressed and only empty buffers are forwarded: a zstd body comes out
empty, without an error. Below 0, the allocation fails with
`initialCapacity : -1`. Before #17896, both decoded the body.

Modification:

- Size the output buffers from `maxForwardBytes` only when it is
positive, and use 64 KiB, the property's default, otherwise. The
forwarding threshold itself is unchanged.
- Don't forward empty buffers, which a threshold of 0 or less also did
before #17896, once the output was drained.
- Add a package-private constructor that takes the default threshold,
and a test that decodes a 1 MiB body with thresholds of 0 and -1.

Result:

`ZstdDecoder` decodes zstd bodies again when
`io.netty.compression.defaultMaxForwardBytes` is 0 or negative, and the
frame header still cannot drive the allocation size.

(cherry picked from commit 5baf6f3)

4.1 port: `ZstdDecompressor` does not exist on 4.1, so only the
`ZstdDecoder` part is ported; files live in `codec/` instead of
`codec-compression/`.
@mkurz
mkurz deleted the fix/zstd-decoder-non-positive-forward-bytes branch November 14, 2026 18:18
hyperxpro pushed a commit to AsyncHttpClient/async-http-client that referenced this pull request Nov 21, 2026
## Summary

- Upgrades Netty from 4.2.20.Final to
[4.2.21.Final](https://github.com/netty/netty/releases/tag/netty-4.2.21.Final).
It mainly reverts a build mistake in 4.2.20: Netty's native libraries
for x86_64 and aarch64 required glibc 2.33. AHC declares the native
epoll and io_uring jars at its Netty version, and when they do not load,
it falls back to NIO, silently unless `useNativeTransport` is set. On
4.2.20 that would happen on systems with an older glibc, such as RHEL 8
or Ubuntu 20.04. AHC 3.0.14 used Netty 4.2.18, so no AHC release has
4.2.20.
- 4.2.21 also fixes `ZstdDecoder` and `ZstdDecompressor` at a
non-positive `io.netty.compression.defaultMaxForwardBytes`
(netty/netty#18289). This PR drops AHC's workarounds for that bug, so
zstd now waits for demand at any value of the property, like br and
snappy. A zero or negative value of the property now needs Netty 4.2.21
or later.

## Change

- **Netty 4.2.21.Final.**
- **Drop the `ZstdDecoder` replacement from #2375.** At a non-positive
value of the property, `Http1ContentDecompressor` replaced the
`ZstdDecoder` that `HttpContentDecompressor` creates with one whose
buffers hold `MAX_PART_SIZE`, because Netty's decoder handed on an empty
body at 0 and failed below it.
- **Drop the zstd guard from #2362.** At such a value, zstd went to
`HttpContentDecompressor` instead of `PullDecompression`, because
Netty's `ZstdDecompressor` handed out nothing at 0 while reporting more
output. It now takes the pull path there too:
  - it waits for demand;
  - parts are at most 64 KiB;
  - the size limit applies;
- a truncated stream fails with `Truncated zstd stream` instead of
ending quietly.
- **Docs:** the README and the Javadoc of
`ResponseBodyControl.suspend()` and of `Http1ContentDecompressor` no
longer make an exception for zstd. The README and `suspend()` say
instead that a zero or negative value of the property needs Netty 4.2.21
or later. With Netty 4.2.17 to 4.2.20, zstd responses then fail at a
negative value, and at 0 they never end and keep the event loop busy, as
Netty's `ZstdDecompressor` hands out empty buffers while it reports more
output. `MAX_FORWARD_BYTES` is private, since only
`Http1ContentDecompressor` uses it now.
- **Test:** `Http1ContentDecompressorForwardThresholdTest` runs with the
property at 0 and -1 in a fresh JVM. It now also checks zstd while
suspending after every part (one part per resume, none larger than
`MAX_PART_SIZE`) and a truncated zstd body (it fails, and nothing is
left allocated).

## Notes

- With the property at its default (64 KiB), nothing changes for zstd.
- Other changes in 4.2.21 that touch AHC's paths:
- Netty now also rejects control characters such as NUL and ESC, and
DEL, in a response's reason phrase (netty/netty#18331). CR and LF were
already rejected; HTAB is still allowed.
  - `SslHandler` changes: netty/netty#18253, #18323, #18363 and #18406.
  
  AHC's tests pass without changes.

## AI disclosure

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

## Test plan

- [x] `./mvnw -B -ntp -Dgpg.skip=true clean verify` on JDK 11: 2,098
tests, 0 failures, 0 errors, 11 skipped; Revapi passed. No test-skipping
flags were used.
- [x] With the README and Javadoc change from review (`5fd35bad8`), the
same `clean verify`: 2,098 tests, 0 failures, 0 errors, 22 skipped;
Revapi passed. Docker was not running, so the 11 Testcontainers proxy
tests skipped themselves; they ran and passed in the runs above, on the
same code.
- [x] The version change alone, without the other changes, passes the
same `clean verify`: 2,098 tests, 0 failures.
- [x] The extended test fails where it should:
- with the guard put back, at 0 and -1: "the suspended zstd body did not
wait for demand after its first part";
- without the workarounds on Netty 4.2.20: at 0, the check hangs until
its 60 s timeout; at -1, it fails with `IllegalArgumentException:
initialCapacity : -1`.
- [x] The `Http1ContentDecompressor*Test`,
`Http2ContentDecompressorTest`, `Http1Decompression*Test` and
`ResponseBodyControlTest` classes: 142 tests, 0 failures.
- [x] Merged with #2394 (`ef18b0b68`): no conflicts; the same classes,
203 tests, 0 failures; `clean verify` on JDK 11, 2,159 tests, 0
failures, 0 errors, 11 skipped; Revapi passed.

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
ikpil added a commit to ikpil/Netty.NET that referenced this pull request Nov 29, 2026
…efaultMaxForwardBytes (netty/netty#18289)

Motivation:

Since netty/netty#17896, `ZstdDecoder` sizes its output buffers as the smaller of
`maxForwardBytes` and the frame content size. With a
`maximumAllocationSize` of 0, which `HttpContentDecompressor` and
`DelegatingDecompressorFrameListener` pass by default, `maxForwardBytes`
is `io.netty.compression.defaultMaxForwardBytes`. When that property is
set to 0, every output buffer has a capacity of 0, so nothing is
decompressed and only empty buffers are forwarded: a zstd body comes out
empty, without an error. Below 0, the allocation fails with
`initialCapacity : -1`. Before netty/netty#17896, both decoded the body.

Modification:

- Size the output buffers from `maxForwardBytes` only when it is
positive, and use 64 KiB, the property's default, otherwise. The
forwarding threshold itself is unchanged.
- Don't forward empty buffers, which a threshold of 0 or less also did
before netty/netty#17896, once the output was drained.
- Add a package-private constructor that takes the default threshold,
and a test that decodes a 1 MiB body with thresholds of 0 and -1.

Result:

`ZstdDecoder` decodes zstd bodies again when
`io.netty.compression.defaultMaxForwardBytes` is 0 or negative, and the
frame header still cannot drive the allocation size.

Upstream-Commit: netty/netty@5baf6f3
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.

5 participants