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 — Auto-port 5.0: Fix GZIP FEXTRA extra-field handling in JdkZlibDecoder… · netty/netty@63d7a29

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
@@ -34,6 +34,7 @@
3434
import java.util.zip.Deflater;
3535
import java.util.zip.GZIPOutputStream;
3636

37+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3738
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3839
import static org.junit.jupiter.api.Assertions.assertEquals;
3940
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -133,6 +134,114 @@ public void testConcatenatedStreamsReadFullyWhenFragmented() throws Exception {
133134
}
134135
}
135136

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