Skip to content

Check the second gzip magic byte in JdkZlibDecoder - #17972

Merged
normanmaurer merged 2 commits into
netty:4.2from
Marti2203:jdkzlib-gzip-id2
Oct 22, 2026
Merged

normanmaurer merged 2 commits into
netty:4.2from
Marti2203:jdkzlib-gzip-id2

Conversation

@Marti2203

Copy link
Copy Markdown
Contributor

Motivation:

JdkZlibDecoder reads both gzip magic bytes (ID1 = 0x1f, ID2 = 0x8b, RFC 1952 section 2.3.1) but only checks ID1, so a stream with a wrong ID2 is decoded as gzip. java.util.zip.GZIPInputStream and netty's own JdkZlibDecompressor (magic0 != 31 || magic1 != 139) reject such a stream.

Modification:

  • Read both bytes unsigned and check ID2, as JdkZlibDecompressor does.
  • Add JdkZlibTest.testGzipInvalidSecondMagicByteThrows.

Result:

JdkZlibDecoder rejects input whose gzip header has a wrong ID2.

Reproduction on 4.2 (5776ebc): compress "hello gzip" with GZIPOutputStream and set byte 1 to 0x00:

JDK GZIPInputStream: rejected (Not in GZIP format)
netty JdkZlibDecoder(GZIP): accepted, output = [hello gzip]

With this change JdkZlibDecoder throws DecompressionException: Input is not in the GZIP format. The new test fails without the change (no exception is thrown). The *Zlib*Test tests in codec-compression pass.

This issue was found by an LLM-Enhanced Static Analysis Engine and verified manually.

🤖 Generated with Claude Code

Motivation:

JdkZlibDecoder reads both gzip magic bytes (ID1 = 0x1f, ID2 = 0x8b, RFC
1952 section 2.3.1) but only checks the first one, so a stream with a
wrong ID2 is decoded as gzip. java.util.zip.GZIPInputStream and
JdkZlibDecompressor reject such a stream.

Modifications:

- Read both bytes unsigned and check ID2 as JdkZlibDecompressor does.
- Add a test.

Result:

JdkZlibDecoder rejects input whose gzip header has a wrong ID2.
@normanmaurer normanmaurer added this to the 4.2.20.Final milestone Oct 21, 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 21, 2026
@testlens-app

testlens-app Bot commented Oct 21, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: fd8f851
▶️ Tests: 44389 executed
⚪️ Checks: 26/26 completed


Learn more about TestLens at testlens.app/docs.

@normanmaurer
normanmaurer merged commit f69f14a into netty:4.2 Oct 22, 2026
27 checks passed
@netty-project-bot

Copy link
Copy Markdown
Contributor

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

@netty-project-bot

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #18006

@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 22, 2026
@normanmaurer normanmaurer removed the needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. label Oct 22, 2026
@normanmaurer

Copy link
Copy Markdown
Member

Port for 4.1. #18007

normanmaurer added a commit that referenced this pull request Oct 22, 2026
…8006)

Auto-port of #17972 to 5.0
Cherry-picked commit: f69f14a

---
Motivation:

`JdkZlibDecoder` reads both gzip magic bytes (ID1 = 0x1f, ID2 = 0x8b,
RFC 1952 section 2.3.1) but only checks ID1, so a stream with a wrong
ID2 is decoded as gzip. `java.util.zip.GZIPInputStream` and netty's own
`JdkZlibDecompressor` (`magic0 != 31 || magic1 != 139`) reject such a
stream.

Modification:

- Read both bytes unsigned and check ID2, as `JdkZlibDecompressor` does.
- Add `JdkZlibTest.testGzipInvalidSecondMagicByteThrows`.

Result:

`JdkZlibDecoder` rejects input whose gzip header has a wrong ID2.

Co-authored-by: Martin Mirchev <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
normanmaurer added a commit that referenced this pull request Oct 22, 2026
…#18007)

Auto-port of #17972 to 4.1
Cherry-picked commit: f69f14a

Motivation:

`JdkZlibDecoder` reads both gzip magic bytes (ID1 = 0x1f, ID2 = 0x8b,
RFC 1952 section 2.3.1) but only checks ID1, so a stream with a wrong
ID2 is decoded as gzip. `java.util.zip.GZIPInputStream` and netty's own
`JdkZlibDecompressor` (`magic0 != 31 || magic1 != 139`) reject such a
stream.

Modification:

- Read both bytes unsigned and check ID2, as `JdkZlibDecompressor` does.
- Add `JdkZlibTest.testGzipInvalidSecondMagicByteThrows`.

Result:

A gzip stream with an invalid ID2 byte is rejected with a
`DecompressionException`.
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
…7972) (netty#18007)

Auto-port of netty#17972 to 4.1
Cherry-picked commit: f69f14a

Motivation:

`JdkZlibDecoder` reads both gzip magic bytes (ID1 = 0x1f, ID2 = 0x8b,
RFC 1952 section 2.3.1) but only checks ID1, so a stream with a wrong
ID2 is decoded as gzip. `java.util.zip.GZIPInputStream` and netty's own
`JdkZlibDecompressor` (`magic0 != 31 || magic1 != 139`) reject such a
stream.

Modification:

- Read both bytes unsigned and check ID2, as `JdkZlibDecompressor` does.
- Add `JdkZlibTest.testGzipInvalidSecondMagicByteThrows`.

Result:

A gzip stream with an invalid ID2 byte is rejected with a
`DecompressionException`.
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]>
ikpil added a commit to ikpil/Netty.NET that referenced this pull request Nov 29, 2026
…netty#17972)

Motivation:

`JdkZlibDecoder` reads both gzip magic bytes (ID1 = 0x1f, ID2 = 0x8b,
RFC 1952 section 2.3.1) but only checks ID1, so a stream with a wrong
ID2 is decoded as gzip. `java.util.zip.GZIPInputStream` and netty's own
`JdkZlibDecompressor` (`magic0 != 31 || magic1 != 139`) reject such a
stream.

Modification:

- Read both bytes unsigned and check ID2, as `JdkZlibDecompressor` does.
- Add `JdkZlibTest.testGzipInvalidSecondMagicByteThrows`.

Result:

`JdkZlibDecoder` rejects input whose gzip header has a wrong ID2.

Co-authored-by: Norman Maurer <[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.

3 participants