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 — Use safe decompressor in Lz4FrameDecoder (#17118) · netty/netty@e64a6b5

File tree

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

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

Copy file name to clipboardExpand all lines: codec-compression/src/main/java/io/netty/handler/codec/compression/Lz4FrameDecoder.java
+17-5Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
import io.netty.util.internal.ObjectUtil;
2222
import net.jpountz.lz4.LZ4Exception;
2323
import net.jpountz.lz4.LZ4Factory;
24-
import net.jpountz.lz4.LZ4FastDecompressor;
24+
import net.jpountz.lz4.LZ4SafeDecompressor;
2525

26+
import java.nio.ByteBuffer;
2627
import java.util.List;
2728
import java.util.zip.Checksum;
2829

@@ -67,7 +68,7 @@ private enum State {
6768
/**
6869
* Underlying decompressor in use.
6970
*/
70-
private LZ4FastDecompressor decompressor;
71+
private LZ4SafeDecompressor decompressor;
7172

7273
/**
7374
* Underlying checksum calculator in use.
@@ -174,7 +175,7 @@ public Lz4FrameDecoder(LZ4Factory factory, Checksum checksum) {
174175
* maximum length of the decompressed block. If {@code 0} is given it uses {@code 32MB} by default.
175176
*/
176177
public Lz4FrameDecoder(LZ4Factory factory, Checksum checksum, int maxDecompressedLength) {
177-
decompressor = ObjectUtil.checkNotNull(factory, "factory").fastDecompressor();
178+
decompressor = ObjectUtil.checkNotNull(factory, "factory").safeDecompressor();
178179
this.checksum = checksum == null ? null : ByteBufChecksum.wrapChecksum(checksum);
179180
this.maxDecompressedLength = maxDecompressedLength == 0 ? MAX_BLOCK_SIZE :
180181
ObjectUtil.checkInRange(maxDecompressedLength, 0, MAX_BLOCK_SIZE, "maxDecompressedLength");
@@ -266,8 +267,19 @@ protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) t
266267
case BLOCK_TYPE_COMPRESSED:
267268
uncompressed = ctx.alloc().buffer(decompressedLength, decompressedLength);
268269

269-
decompressor.decompress(CompressionUtil.safeReadableNioBuffer(in),
270-
uncompressed.internalNioBuffer(uncompressed.writerIndex(), decompressedLength));
270+
ByteBuffer source = CompressionUtil.safeNioBuffer(
271+
in, in.readerIndex(), compressedLength);
272+
ByteBuffer destination = uncompressed.internalNioBuffer(
273+
uncompressed.writerIndex(), decompressedLength);
274+
int actualDecompressedLength = decompressor.decompress(
275+
source, source.position(), compressedLength,
276+
destination, destination.position(), decompressedLength);
277+
if (actualDecompressedLength != decompressedLength) {
278+
throw new DecompressionException(String.format(
279+
"stream corrupted: decompressedLength(%d) and " +
280+
"actualDecompressedLength(%d) mismatch",
281+
decompressedLength, actualDecompressedLength));
282+
}
271283
// Update the writerIndex now to reflect what we decompressed.
272284
uncompressed.writerIndex(uncompressed.writerIndex() + decompressedLength);
273285
break;
Collapse file

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

Copy file name to clipboardExpand all lines: codec-compression/src/test/java/io/netty/handler/codec/compression/Lz4FrameDecoderTest.java
+38Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,44 @@ protected EmbeddedChannel createChannel() {
4949
return new EmbeddedChannel(new Lz4FrameDecoder(true, 31 * 1024 * 1024));
5050
}
5151

52+
@Test
53+
public void testRejectsCompressedDataBeyondDeclaredLength() {
54+
EmbeddedChannel decoder = new EmbeddedChannel(new Lz4FrameDecoder(false));
55+
ByteBuf input = decoder.alloc().buffer();
56+
input.writeLong(MAGIC_NUMBER);
57+
input.writeByte(BLOCK_TYPE_COMPRESSED);
58+
input.writeIntLE(1);
59+
input.writeIntLE(8);
60+
input.writeIntLE(0);
61+
input.writeByte(0x80);
62+
input.writeZero(8);
63+
64+
try {
65+
assertThrows(DecompressionException.class, () -> decoder.writeInbound(input));
66+
} finally {
67+
decoder.finishAndReleaseAll();
68+
}
69+
}
70+
71+
@Test
72+
public void testRejectsDecompressedLengthMismatch() {
73+
EmbeddedChannel decoder = new EmbeddedChannel(new Lz4FrameDecoder(false));
74+
ByteBuf input = decoder.alloc().buffer();
75+
input.writeLong(MAGIC_NUMBER);
76+
input.writeByte(BLOCK_TYPE_COMPRESSED);
77+
input.writeIntLE(2);
78+
input.writeIntLE(8);
79+
input.writeIntLE(0);
80+
input.writeByte(0x10);
81+
input.writeByte(0);
82+
83+
try {
84+
assertThrows(DecompressionException.class, () -> decoder.writeInbound(input));
85+
} finally {
86+
decoder.finishAndReleaseAll();
87+
}
88+
}
89+
5290
@Test
5391
public void testUnexpectedBlockIdentifier() {
5492
final byte[] data = Arrays.copyOf(DATA, DATA.length);

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.