Repository navigation
Don't size ZstdDecoder output buffers from the frame content size - #17896
Merged
normanmaurer merged 1 commit intoOct 18, 2026
Merged
normanmaurer merged 1 commit into
normanmaurer merged 1 commit into
Conversation
Motivation: ZstdDecoder sized its first output buffer from the Frame_Content_Size in the zstd frame header and only clamped it when maximumAllocationSize was non-zero. HttpContentDecompressor() / HttpContentDecompressor(0) and the DelegatingDecompressorFrameListener constructors without a maxAllocation pass 0, so a 14 byte zstd body that declares a content size of 0x7FFFFFF0 makes the decoder allocate a ~2 GiB heap buffer before anything is decompressed. Modification: Cap the output buffer at maxForwardBytes, which is maximumAllocationSize or the default forward size when unlimited. Output is still forwarded in chunks, like the zlib and brotli decoders do. Result: The frame header can no longer drive the allocation size in ZstdDecoder.
✅ All tests passed ✅🏷️ Commit: a1d4599 Learn more about TestLens at testlens.app/docs. |
yawkat
approved these changes
Oct 18, 2026
yawkat
left a comment
Contributor
There was a problem hiding this comment.
new Decompressor API is unaffected
Contributor
|
Auto-port PR for 4.1: #17900 |
Contributor
|
Auto-port PR for 5.0: #17901 |
normanmaurer
added a commit
that referenced
this pull request
Oct 18, 2026
…ontent size (#17901) Auto-port of #17896 to 5.0 Cherry-picked commit: 4aa383f --- Motivation: `ZstdDecoder.decode` sizes its first output buffer from `Zstd.getFrameContentSize()`, which comes straight from the Frame_Content_Size field of the (untrusted) zstd frame header. The value is only clamped when `maximumAllocationSize > 0`. `HttpContentDecompressor()` / `HttpContentDecompressor(0)` and the `DelegatingDecompressorFrameListener` constructors without a `maxAllocation` pass 0 for `Content-Encoding: zstd`, so a peer can pick the allocation size. A 14 byte body is enough: ``` POST / HTTP/1.1 Content-Encoding: zstd Content-Length: 14 28 b5 2f fd 80 00 f0 ff ff 7f 09 00 00 41 magic FHD WD FCS=0x7FFFFFF0 raw last block, 1 byte ``` The decoder calls `heapBuffer(2147483632)` before decompressing anything. libzstd only notices the content size was a lie afterwards. With `-Xmx512m` this is an `OutOfMemoryError` (which is not caught by the `catch (Exception)`), with `-Xmx4g` the heap grows by ~2 GiB per request before the `DecompressionException`. With `maxAllocation=65536` only 64 KiB is requested. The zlib and brotli decoders and `ZstdDecompressor` already bound their buffers when unlimited, so zstd was the odd one out. Modification: - Cap the output buffer at `maxForwardBytes` (`maximumAllocationSize`, or `DEFAULT_MAX_FORWARD_BYTES` when it is 0). Output is still forwarded in chunks, so valid streams decode the same, just in up to 64 KiB pieces when unlimited. - Drop the now unused `maximumAllocationSize` field. - Add tests to `ZstdDecoderTest`: one feeds the frame above to `new ZstdDecoder(0)` with an allocator that records the requested capacity (fails before the fix with `requested 2147483632 bytes`), one checks a 1 MiB payload still round-trips with `maximumAllocationSize == 0`. Result: The zstd frame header can no longer drive the allocation size in `ZstdDecoder`. --------- Co-authored-by: Raghuvardhan-Reddy78 <[email protected]> Co-authored-by: Norman Maurer <[email protected]>
normanmaurer
added a commit
that referenced
this pull request
Oct 18, 2026
…ontent size (#17900) Auto-port of #17896 to 4.1 Cherry-picked commit: 4aa383f --- Motivation: `ZstdDecoder.decode` sizes its first output buffer from `Zstd.getFrameContentSize()`, which comes straight from the Frame_Content_Size field of the (untrusted) zstd frame header. The value is only clamped when `maximumAllocationSize > 0`. `HttpContentDecompressor()` / `HttpContentDecompressor(0)` and the `DelegatingDecompressorFrameListener` constructors without a `maxAllocation` pass 0 for `Content-Encoding: zstd`, so a peer can pick the allocation size. A 14 byte body is enough: ``` POST / HTTP/1.1 Content-Encoding: zstd Content-Length: 14 28 b5 2f fd 80 00 f0 ff ff 7f 09 00 00 41 magic FHD WD FCS=0x7FFFFFF0 raw last block, 1 byte ``` The decoder calls `heapBuffer(2147483632)` before decompressing anything. libzstd only notices the content size was a lie afterwards. With `-Xmx512m` this is an `OutOfMemoryError` (which is not caught by the `catch (Exception)`), with `-Xmx4g` the heap grows by ~2 GiB per request before the `DecompressionException`. With `maxAllocation=65536` only 64 KiB is requested. The zlib and brotli decoders and `ZstdDecompressor` already bound their buffers when unlimited, so zstd was the odd one out. Modification: - Cap the output buffer at `maxForwardBytes` (`maximumAllocationSize`, or `DEFAULT_MAX_FORWARD_BYTES` when it is 0). Output is still forwarded in chunks, so valid streams decode the same, just in up to 64 KiB pieces when unlimited. - Drop the now unused `maximumAllocationSize` field. - Add tests to `ZstdDecoderTest`: one feeds the frame above to `new ZstdDecoder(0)` with an allocator that records the requested capacity (fails before the fix with `requested 2147483632 bytes`), one checks a 1 MiB payload still round-trips with `maximumAllocationSize == 0`. Result: The zstd frame header can no longer drive the allocation size in `ZstdDecoder`. --------- Co-authored-by: Raghuvardhan-Reddy78 <[email protected]> Co-authored-by: Norman Maurer <[email protected]>
Contributor
Author
|
Thanks for merging this and for the ports. |
lltopk
pushed a commit
to lltopk/netty
that referenced
this pull request
Oct 24, 2026
…ontent size (netty#17900) Auto-port of netty#17896 to 4.1 Cherry-picked commit: 4aa383f --- Motivation: `ZstdDecoder.decode` sizes its first output buffer from `Zstd.getFrameContentSize()`, which comes straight from the Frame_Content_Size field of the (untrusted) zstd frame header. The value is only clamped when `maximumAllocationSize > 0`. `HttpContentDecompressor()` / `HttpContentDecompressor(0)` and the `DelegatingDecompressorFrameListener` constructors without a `maxAllocation` pass 0 for `Content-Encoding: zstd`, so a peer can pick the allocation size. A 14 byte body is enough: ``` POST / HTTP/1.1 Content-Encoding: zstd Content-Length: 14 28 b5 2f fd 80 00 f0 ff ff 7f 09 00 00 41 magic FHD WD FCS=0x7FFFFFF0 raw last block, 1 byte ``` The decoder calls `heapBuffer(2147483632)` before decompressing anything. libzstd only notices the content size was a lie afterwards. With `-Xmx512m` this is an `OutOfMemoryError` (which is not caught by the `catch (Exception)`), with `-Xmx4g` the heap grows by ~2 GiB per request before the `DecompressionException`. With `maxAllocation=65536` only 64 KiB is requested. The zlib and brotli decoders and `ZstdDecompressor` already bound their buffers when unlimited, so zstd was the odd one out. Modification: - Cap the output buffer at `maxForwardBytes` (`maximumAllocationSize`, or `DEFAULT_MAX_FORWARD_BYTES` when it is 0). Output is still forwarded in chunks, so valid streams decode the same, just in up to 64 KiB pieces when unlimited. - Drop the now unused `maximumAllocationSize` field. - Add tests to `ZstdDecoderTest`: one feeds the frame above to `new ZstdDecoder(0)` with an allocator that records the requested capacity (fails before the fix with `requested 2147483632 bytes`), one checks a 1 MiB payload still round-trips with `maximumAllocationSize == 0`. Result: The zstd frame header can no longer drive the allocation size in `ZstdDecoder`. --------- Co-authored-by: Raghuvardhan-Reddy78 <[email protected]> Co-authored-by: Norman Maurer <[email protected]>
This was referenced Nov 5, 2026
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
pushed a commit
that referenced
this pull request
Nov 13, 2026
…rwardBytes (#18289) 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.
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
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/`.
ikpil
added a commit
to ikpil/Netty.NET
that referenced
this pull request
Nov 29, 2026
…nt size (netty/netty#17896) Motivation: `ZstdDecoder.decode` sizes its first output buffer from `Zstd.getFrameContentSize()`, which comes straight from the Frame_Content_Size field of the (untrusted) zstd frame header. The value is only clamped when `maximumAllocationSize > 0`. `HttpContentDecompressor()` / `HttpContentDecompressor(0)` and the `DelegatingDecompressorFrameListener` constructors without a `maxAllocation` pass 0 for `Content-Encoding: zstd`, so a peer can pick the allocation size. A 14 byte body is enough: ``` POST / HTTP/1.1 Content-Encoding: zstd Content-Length: 14 28 b5 2f fd 80 00 f0 ff ff 7f 09 00 00 41 magic FHD WD FCS=0x7FFFFFF0 raw last block, 1 byte ``` The decoder calls `heapBuffer(2147483632)` before decompressing anything. libzstd only notices the content size was a lie afterwards. With `-Xmx512m` this is an `OutOfMemoryError` (which is not caught by the `catch (Exception)`), with `-Xmx4g` the heap grows by ~2 GiB per request before the `DecompressionException`. With `maxAllocation=65536` only 64 KiB is requested. The zlib and brotli decoders and `ZstdDecompressor` already bound their buffers when unlimited, so zstd was the odd one out. Modification: - Cap the output buffer at `maxForwardBytes` (`maximumAllocationSize`, or `DEFAULT_MAX_FORWARD_BYTES` when it is 0). Output is still forwarded in chunks, so valid streams decode the same, just in up to 64 KiB pieces when unlimited. - Drop the now unused `maximumAllocationSize` field. - Add tests to `ZstdDecoderTest`: one feeds the frame above to `new ZstdDecoder(0)` with an allocator that records the requested capacity (fails before the fix with `requested 2147483632 bytes`), one checks a 1 MiB payload still round-trips with `maximumAllocationSize == 0`. Result: The zstd frame header can no longer drive the allocation size in `ZstdDecoder`.
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
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Motivation:
ZstdDecoder.decodesizes its first output buffer fromZstd.getFrameContentSize(), which comes straight from the Frame_Content_Size field of the (untrusted) zstd frame header. The value is only clamped whenmaximumAllocationSize > 0.HttpContentDecompressor()/HttpContentDecompressor(0)and theDelegatingDecompressorFrameListenerconstructors without amaxAllocationpass 0 forContent-Encoding: zstd, so a peer can pick the allocation size.A 14 byte body is enough:
The decoder calls
heapBuffer(2147483632)before decompressing anything. libzstd only notices the content size was a lie afterwards. With-Xmx512mthis is anOutOfMemoryError(which is not caught by thecatch (Exception)), with-Xmx4gthe heap grows by ~2 GiB per request before theDecompressionException. WithmaxAllocation=65536only 64 KiB is requested. The zlib and brotli decoders andZstdDecompressoralready bound their buffers when unlimited, so zstd was the odd one out.Modification:
maxForwardBytes(maximumAllocationSize, orDEFAULT_MAX_FORWARD_BYTESwhen it is 0). Output is still forwarded in chunks, so valid streams decode the same, just in up to 64 KiB pieces when unlimited.maximumAllocationSizefield.ZstdDecoderTest: one feeds the frame above tonew ZstdDecoder(0)with an allocator that records the requested capacity (fails before the fix withrequested 2147483632 bytes), one checks a 1 MiB payload still round-trips withmaximumAllocationSize == 0.Result:
The zstd frame header can no longer drive the allocation size in
ZstdDecoder.