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 — Snappy: Guard decoder against invalid chunk lengths (#17099) · netty/netty@b41f2a6

File tree

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

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

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/SnappyFrameDecoder.java
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ private enum ChunkType {
4747
private static final int SNAPPY_IDENTIFIER_LEN = 6;
4848
// See https://github.com/google/snappy/blob/1.1.9/framing_format.txt#L95
4949
private static final int MAX_UNCOMPRESSED_DATA_SIZE = 65536 + 4;
50+
// An uncompressed chunk contains a 4-byte masked checksum followed by the data.
51+
private static final int MIN_UNCOMPRESSED_DATA_SIZE = 4;
5052
// See https://github.com/google/snappy/blob/1.1.9/framing_format.txt#L82
5153
private static final int MAX_DECOMPRESSED_DATA_SIZE = 65536;
5254
// See https://github.com/google/snappy/blob/1.1.9/framing_format.txt#L82
5355
private static final int MAX_COMPRESSED_CHUNK_SIZE = 16777216 - 1;
56+
// A compressed chunk contains a 4-byte masked checksum followed by a Snappy stream.
57+
private static final int MIN_COMPRESSED_CHUNK_SIZE = 5;
5458

5559
private final Snappy snappy = new Snappy();
5660
private final boolean validateChecksums;
@@ -163,6 +167,10 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
163167
throw new DecompressionException("Received UNCOMPRESSED_DATA larger than " +
164168
MAX_UNCOMPRESSED_DATA_SIZE + " bytes");
165169
}
170+
if (chunkLength < MIN_UNCOMPRESSED_DATA_SIZE) {
171+
throw new DecompressionException("Received UNCOMPRESSED_DATA with invalid chunk length: " +
172+
chunkLength);
173+
}
166174

167175
if (inSize < 4 + chunkLength) {
168176
return;
@@ -186,6 +194,10 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
186194
throw new DecompressionException("Received COMPRESSED_DATA that contains" +
187195
" chunk that exceeds " + MAX_COMPRESSED_CHUNK_SIZE + " bytes");
188196
}
197+
if (chunkLength < MIN_COMPRESSED_CHUNK_SIZE) {
198+
throw new DecompressionException("Received COMPRESSED_DATA with invalid chunk length: " +
199+
chunkLength);
200+
}
189201

190202
if (inSize < 4 + chunkLength) {
191203
return;
Collapse file

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

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/SnappyFrameDecoderTest.java
+53Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
2424
import org.junit.jupiter.api.function.Executable;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.CsvSource;
2527

2628
import static org.junit.jupiter.api.Assertions.assertEquals;
2729
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -177,6 +179,26 @@ public void testCompressedDataDecodesAndAppendsToOut() {
177179
actual.release();
178180
}
179181

182+
@ParameterizedTest
183+
@CsvSource({
184+
"false, 0", "false, 1", "false, 2", "false, 3", "false, 4",
185+
"true, 0", "true, 1", "true, 2", "true, 3", "true, 4"
186+
})
187+
public void testCompressedDataWithTooShortChunkLengthThrowsException(
188+
boolean validateChecksums, int chunkLength) {
189+
assertInvalidChunkLength(validateChecksums, (byte) 0x00, chunkLength);
190+
}
191+
192+
@ParameterizedTest
193+
@CsvSource({
194+
"false, 0", "false, 1", "false, 2", "false, 3",
195+
"true, 0", "true, 1", "true, 2", "true, 3"
196+
})
197+
public void testUncompressedDataWithTooShortChunkLengthThrowsException(
198+
boolean validateChecksums, int chunkLength) {
199+
assertInvalidChunkLength(validateChecksums, (byte) 0x01, chunkLength);
200+
}
201+
180202
// The following two tests differ in only the checksum provided for the literal
181203
// uncompressed string "netty"
182204

@@ -222,4 +244,35 @@ public void testInvalidChecksumDoesNotThrowException() {
222244
channel.finishAndReleaseAll();
223245
}
224246
}
247+
248+
private static void assertInvalidChunkLength(boolean validateChecksums, byte chunkType, int chunkLength) {
249+
final EmbeddedChannel channel = new EmbeddedChannel(new SnappyFrameDecoder(validateChecksums));
250+
try {
251+
final ByteBuf in = channel.alloc().buffer(14 + chunkLength);
252+
253+
// Snappy stream identifier chunk: type 0xff, 3-byte little-endian length 6, payload "sNaPpY".
254+
in.writeByte(0xff);
255+
in.writeMediumLE(6);
256+
in.writeByte('s');
257+
in.writeByte('N');
258+
in.writeByte('a');
259+
in.writeByte('P');
260+
in.writeByte('p');
261+
in.writeByte('Y');
262+
263+
// Invalid data chunk header: caller-supplied type and too-short 3-byte little-endian length.
264+
in.writeByte(chunkType);
265+
in.writeMediumLE(chunkLength);
266+
in.writeZero(chunkLength);
267+
268+
assertThrows(DecompressionException.class, new Executable() {
269+
@Override
270+
public void execute() {
271+
channel.writeInbound(in);
272+
}
273+
});
274+
} finally {
275+
channel.finishAndReleaseAll();
276+
}
277+
}
225278
}

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.