Skip to content

Auto-port 5.0: Don't size ZstdDecoder output buffers from the frame content size - #17901

Merged
normanmaurer merged 2 commits into
5.0from
auto-port-pr-17896-to-5.0
Oct 18, 2026
Merged

normanmaurer merged 2 commits into
5.0from
auto-port-pr-17896-to-5.0

Conversation

@netty-project-bot

Copy link
Copy Markdown
Contributor

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.

…7896)

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`.

(cherry picked from commit 4aa383f)
finishAndReleaseAll() and readInbound() throw checked exceptions in 5.0.
@testlens-app

testlens-app Bot commented Oct 18, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 87aad09
▶️ Tests: 40876 executed
⚪️ Checks: 13/13 completed


Learn more about TestLens at testlens.app/docs.

@normanmaurer
normanmaurer merged commit e7cf0c3 into 5.0 Oct 18, 2026
14 checks passed
@normanmaurer normanmaurer added this to the 5.0.0.Final milestone Oct 18, 2026
@normanmaurer
normanmaurer deleted the auto-port-pr-17896-to-5.0 branch October 18, 2026 18:06
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.

3 participants