GeezerVison is showing github.com — Check the second gzip magic byte in JdkZlibDecoder (#17972) · netty/netty@f69f14a
File tree
Expand file treeCollapse file tree
Open diff view settings
main/java/io/netty/handler/codec/compression test/java/io/netty/handler/codec/compression
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/JdkZlibDecoder.java +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -381,10 +381,10 @@ private boolean readGZIPHeader(ByteBuf in) { |
381 | 381 | return false; |
382 | 382 | } |
383 | 383 | // read magic numbers |
384 | | - int magic0 = in.readByte(); |
385 | | - int magic1 = in.readByte(); |
| 384 | + int magic0 = in.readUnsignedByte(); |
| 385 | + int magic1 = in.readUnsignedByte(); |
386 | 386 |
|
387 | | - if (magic0 != 31) { |
| 387 | + if (magic0 != 31 || magic1 != 139) { |
388 | 388 | throw new DecompressionException("Input is not in the GZIP format"); |
389 | 389 | } |
390 | 390 | crc.update(magic0); |
|
Collapse file
Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/JdkZlibTest.java +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -462,6 +462,26 @@ public void testGzipFooterCrcMismatchThrows() throws Exception { |
462 | 462 | ch.finishAndReleaseAll(); |
463 | 463 | } |
464 | 464 |
|
| 465 | + @Test |
| 466 | + public void testGzipInvalidSecondMagicByteThrows() throws Exception { |
| 467 | + byte[] data = "hello gzip".getBytes(CharsetUtil.UTF_8); |
| 468 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 469 | + GZIPOutputStream gzipOut = new GZIPOutputStream(baos); |
| 470 | + gzipOut.write(data); |
| 471 | + gzipOut.close(); |
| 472 | + |
| 473 | + byte[] compressed = baos.toByteArray(); |
| 474 | + // Replace ID2 (0x8b) of the header. |
| 475 | + compressed[1] = 0; |
| 476 | + |
| 477 | + EmbeddedChannel ch = new EmbeddedChannel(new JdkZlibDecoder(ZlibWrapper.GZIP, Integer.MAX_VALUE)); |
| 478 | + assertThrows(DecompressionException.class, () -> { |
| 479 | + ch.writeInbound(Unpooled.wrappedBuffer(compressed)); |
| 480 | + }); |
| 481 | + // the remaining input is decoded again when the channel is closed |
| 482 | + assertThrows(DecompressionException.class, ch::finishAndReleaseAll); |
| 483 | + } |
| 484 | + |
465 | 485 | @Test |
466 | 486 | public void testGzipFooterISizeMismatchThrows() throws Exception { |
467 | 487 | byte[] data = "wrong size".getBytes(CharsetUtil.UTF_8); |
|