GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix GZIP FEXTRA extra-field handling in JdkZlibDecoder (#16951) · netty/netty@a655e64

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-compression/src
    • main/java/io/netty/handler/codec/compression
      • JdkZlibDecoder.java
    • test/java/io/netty/handler/codec/compression
      • JdkZlibTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-compression/src/main/java/io/netty/handler/codec/compression/JdkZlibDecoder.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/JdkZlibDecoder.java
+6-1Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ private boolean handleGzipFooter(ByteBuf in) {
324324
if (!finished) {
325325
inflater.reset();
326326
crc.reset();
327+
xlen = -1;
327328
gzipState = GzipState.HEADER_START;
328329
return true;
329330
}
@@ -394,7 +395,11 @@ private boolean readGZIPHeader(ByteBuf in) {
394395
crc.update(xlen1);
395396
crc.update(xlen2);
396397

397-
xlen |= xlen1 << 8 | xlen2;
398+
// XLEN is a little-endian unsigned 16-bit value (RFC 1952), so xlen1 is the
399+
// low byte and xlen2 the high byte. This must be an assignment, not |=: xlen
400+
// starts at the -1 "no extra field" sentinel, and OR-ing into -1 (0xFFFFFFFF)
401+
// would leave it -1, so the extra field was never skipped.
402+
xlen = xlen2 << 8 | xlen1;
398403
}
399404
gzipState = GzipState.XLEN_READ;
400405
// fall through
Collapse file

‎codec-compression/src/test/java/io/netty/handler/codec/compression/JdkZlibTest.java‎

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/JdkZlibTest.java
+109Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.zip.Deflater;
3737
import java.util.zip.GZIPOutputStream;
3838

39+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3940
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
4041
import static org.junit.jupiter.api.Assertions.assertEquals;
4142
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -140,6 +141,114 @@ public void testConcatenatedStreamsReadFullyWhenFragmented() throws IOException
140141
}
141142
}
142143

144+
@Test
145+
public void testGZIPDecodeWithExtraField() throws Exception {
146+
byte[] data = "Hello, gzip FEXTRA world!".getBytes(CharsetUtil.UTF_8);
147+
byte[] extra = { 0x42, 0x43, 0x02, 0x00, (byte) 0x99, 0x00 }; // 6 arbitrary bytes
148+
byte[] gzipWithExtra = gzipWithExtraField(data, extra);
149+
150+
// Sanity-check the crafted stream is a valid gzip by decoding it with the JDK itself.
151+
assertArrayEquals(data, jdkGunzip(gzipWithExtra));
152+
153+
// netty must decode it identically; before the FEXTRA fix the extra bytes were never
154+
// skipped, corrupting the deflate stream and throwing DecompressionException.
155+
EmbeddedChannel ch = new EmbeddedChannel(createDecoder(ZlibWrapper.GZIP));
156+
try {
157+
assertTrue(ch.writeInbound(Unpooled.copiedBuffer(gzipWithExtra)));
158+
ByteBuf out = ch.readInbound();
159+
assertEquals(new String(data, CharsetUtil.UTF_8), out.toString(CharsetUtil.UTF_8));
160+
out.release();
161+
} finally {
162+
assertFalse(ch.finish());
163+
ch.close();
164+
}
165+
}
166+
167+
@Test
168+
public void testConcatenatedGzipFirstStreamHasExtraField() throws Exception {
169+
// Regression guard: with concatenated streams, an FEXTRA field on the first stream must not
170+
// leak its XLEN into the second stream's header parsing. The xlen state has to be reset
171+
// between streams; otherwise the second stream (which has no extra field) would skip
172+
// xlen bytes that are actually deflate data and fail to decode.
173+
String firstText = "first stream";
174+
String secondText = "second stream";
175+
byte[] first = firstText.getBytes(CharsetUtil.UTF_8);
176+
byte[] second = secondText.getBytes(CharsetUtil.UTF_8);
177+
byte[] extra = { 0x42, 0x43, 0x02, 0x00, (byte) 0x99, 0x00 };
178+
179+
byte[] firstGz = gzipWithExtraField(first, extra); // first stream HAS an extra field
180+
byte[] secondGz = gzip(second); // second stream has none
181+
byte[] both = new byte[firstGz.length + secondGz.length];
182+
System.arraycopy(firstGz, 0, both, 0, firstGz.length);
183+
System.arraycopy(secondGz, 0, both, firstGz.length, secondGz.length);
184+
185+
EmbeddedChannel ch = new EmbeddedChannel(new JdkZlibDecoder(true, 0));
186+
try {
187+
assertTrue(ch.writeInbound(Unpooled.copiedBuffer(both)));
188+
ByteArrayOutputStream decoded = new ByteArrayOutputStream();
189+
ByteBuf msg;
190+
while ((msg = ch.readInbound()) != null) {
191+
msg.readBytes(decoded, msg.readableBytes());
192+
msg.release();
193+
}
194+
assertArrayEquals((firstText + secondText).getBytes(CharsetUtil.UTF_8),
195+
decoded.toByteArray());
196+
decoded.close();
197+
} finally {
198+
assertFalse(ch.finish());
199+
}
200+
}
201+
202+
private static byte[] gzip(byte[] data) throws IOException {
203+
ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
204+
GZIPOutputStream gzipOut = new GZIPOutputStream(bytesOut);
205+
gzipOut.write(data);
206+
gzipOut.close();
207+
return bytesOut.toByteArray();
208+
}
209+
210+
private static byte[] gzipWithExtraField(byte[] data, byte[] extra) throws IOException {
211+
// GZIPOutputStream never emits an FEXTRA field, so build a standard gzip stream first ...
212+
byte[] standard = gzip(data);
213+
214+
// ... then splice in an FEXTRA field by hand: set the FEXTRA flag in FLG and insert
215+
// XLEN (2 bytes, little-endian per RFC 1952) followed by the extra subfield, right after
216+
// the fixed 10-byte gzip header.
217+
ByteArrayOutputStream withExtra = new ByteArrayOutputStream();
218+
try {
219+
byte[] header = Arrays.copyOfRange(standard, 0, 10);
220+
header[3] |= 0x04; // FLG.FEXTRA
221+
withExtra.write(header);
222+
withExtra.write(extra.length & 0xff); // XLEN low byte (little-endian)
223+
withExtra.write((extra.length >>> 8) & 0xff); // XLEN high byte
224+
withExtra.write(extra);
225+
withExtra.write(standard, 10, standard.length - 10);
226+
return withExtra.toByteArray();
227+
} finally {
228+
withExtra.close();
229+
}
230+
}
231+
232+
private static byte[] jdkGunzip(byte[] gz) throws IOException {
233+
ByteArrayOutputStream out = new ByteArrayOutputStream();
234+
try {
235+
java.util.zip.GZIPInputStream in =
236+
new java.util.zip.GZIPInputStream(new java.io.ByteArrayInputStream(gz));
237+
try {
238+
byte[] buf = new byte[256];
239+
int n;
240+
while ((n = in.read(buf)) != -1) {
241+
out.write(buf, 0, n);
242+
}
243+
} finally {
244+
in.close();
245+
}
246+
return out.toByteArray();
247+
} finally {
248+
out.close();
249+
}
250+
}
251+
143252
@Test
144253
public void testDecodeWithHeaderFollowingFooter() throws Exception {
145254
byte[] bytes = new byte[1024];

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.