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 — SPDY: Limit the maximum number of bytes that can be decompressed per … · lltopk/netty@69562fa

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http/src
    • main/java/io/netty/handler/codec/spdy
      • SpdyHeaderBlockZlibDecoder.java
    • test/java/io/netty/handler/codec/spdy
      • SpdyHeaderBlockZlibDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockZlibDecoder.java‎

Copy file name to clipboardExpand all lines: codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHeaderBlockZlibDecoder.java
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,27 @@ final class SpdyHeaderBlockZlibDecoder extends SpdyHeaderBlockRawDecoder {
2929
private static final SpdyProtocolException INVALID_HEADER_BLOCK =
3030
new SpdyProtocolException("Invalid Header Block");
3131

32+
// Legitimate header blocks are bounded by maxHeaderSize, but framing overhead (the header
33+
// count and the per-header name/value length prefixes) means the decompressed size of a
34+
// wire-legitimate header block can exceed maxHeaderSize itself. A generous multiplier is used
35+
// instead of an equal cap so this never rejects well-formed header blocks, while still
36+
// preventing a maliciously compressible header block from forcing an effectively unbounded
37+
// number of Inflater#inflate() calls on the calling (event-loop) thread.
38+
private static final int MAX_DECOMPRESSED_SIZE_MULTIPLIER = 16;
39+
3240
private final Inflater decompressor = new Inflater();
41+
private final long maxDecompressedSize;
3342

3443
private ByteBuf decompressed;
44+
private long totalInflated;
3545

3646
SpdyHeaderBlockZlibDecoder(SpdyVersion spdyVersion, int maxHeaderSize) {
3747
super(spdyVersion, maxHeaderSize);
48+
maxDecompressedSize = calculateMaxDecompressedSizePerBlock(maxHeaderSize);
49+
}
50+
51+
static long calculateMaxDecompressedSizePerBlock(int maxHeaderSize) {
52+
return (long) maxHeaderSize * MAX_DECOMPRESSED_SIZE_MULTIPLIER;
3853
}
3954

4055
@Override
@@ -44,6 +59,11 @@ void decode(ByteBufAllocator alloc, ByteBuf headerBlock, SpdyHeadersFrame frame)
4459
int numBytes;
4560
do {
4661
numBytes = decompress(alloc, frame);
62+
totalInflated += numBytes;
63+
if (totalInflated > maxDecompressedSize) {
64+
throw new SpdyProtocolException(
65+
"Decompressed header block exceeds " + maxDecompressedSize + " bytes");
66+
}
4767
} while (numBytes > 0);
4868

4969
// z_stream has an internal 64-bit hold buffer
@@ -107,6 +127,7 @@ private void ensureBuffer(ByteBufAllocator alloc) {
107127
void endHeaderBlock(SpdyHeadersFrame frame) throws Exception {
108128
super.endHeaderBlock(frame);
109129
releaseBuffer();
130+
totalInflated = 0;
110131
}
111132

112133
@Override
Collapse file

‎codec-http/src/test/java/io/netty/handler/codec/spdy/SpdyHeaderBlockZlibDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http/src/test/java/io/netty/handler/codec/spdy/SpdyHeaderBlockZlibDecoderTest.java
+70Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
import org.junit.jupiter.api.Test;
2424
import org.junit.jupiter.api.function.Executable;
2525

26+
import java.io.ByteArrayOutputStream;
27+
import java.util.zip.Deflater;
28+
2629
import static org.junit.jupiter.api.Assertions.assertEquals;
2730
import static org.junit.jupiter.api.Assertions.assertFalse;
2831
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -36,6 +39,9 @@ public class SpdyHeaderBlockZlibDecoderTest {
3639

3740
private static final int maxHeaderSize = 8192;
3841

42+
private static final long MAX_DECOMPRESSED_SIZE =
43+
SpdyHeaderBlockZlibDecoder.calculateMaxDecompressedSizePerBlock(maxHeaderSize);
44+
3945
private static final String name = "name";
4046
private static final String value = "value";
4147
private static final byte[] nameBytes = name.getBytes();
@@ -242,4 +248,68 @@ public void execute() throws Throwable {
242248

243249
headerBlock.release();
244250
}
251+
252+
@Test
253+
public void testHeaderBlockAtDecompressionCapIsAccepted() throws Exception {
254+
long valueLength = MAX_DECOMPRESSED_SIZE - headerBlockOverhead();
255+
ByteBuf headerBlock = Unpooled.wrappedBuffer(deflate(buildHeaderBlock((int) valueLength)));
256+
257+
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
258+
decoder.endHeaderBlock(frame);
259+
260+
assertFalse(frame.isInvalid());
261+
assertTrue(frame.isTruncated());
262+
263+
headerBlock.release();
264+
}
265+
266+
@Test
267+
public void testHeaderBlockExceedingDecompressionCapThrows() throws Exception {
268+
long valueLength = MAX_DECOMPRESSED_SIZE - headerBlockOverhead() + 1;
269+
final ByteBuf headerBlock = Unpooled.wrappedBuffer(deflate(buildHeaderBlock((int) valueLength)));
270+
271+
assertThrows(SpdyProtocolException.class, new Executable() {
272+
@Override
273+
public void execute() throws Throwable {
274+
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
275+
}
276+
});
277+
278+
headerBlock.release();
279+
}
280+
281+
// 4 bytes for the header count, 4 bytes for the name length, the 1-byte name itself,
282+
// and 4 bytes for the value length: everything but the value payload.
283+
private static long headerBlockOverhead() {
284+
return 4 + 4 + 1 + 4;
285+
}
286+
287+
private static byte[] buildHeaderBlock(int valueLength) {
288+
ByteBuf buf = Unpooled.buffer((int) headerBlockOverhead() + valueLength);
289+
buf.writeInt(1); // number of Name/Value pairs
290+
buf.writeInt(1); // length of name
291+
buf.writeByte('n');
292+
buf.writeInt(valueLength);
293+
buf.writeZero(valueLength);
294+
295+
byte[] bytes = new byte[buf.readableBytes()];
296+
buf.readBytes(bytes);
297+
buf.release();
298+
return bytes;
299+
}
300+
301+
private static byte[] deflate(byte[] input) {
302+
Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION);
303+
deflater.setInput(input);
304+
deflater.finish();
305+
306+
ByteArrayOutputStream out = new ByteArrayOutputStream();
307+
byte[] chunk = new byte[8192];
308+
while (!deflater.finished()) {
309+
int n = deflater.deflate(chunk);
310+
out.write(chunk, 0, n);
311+
}
312+
deflater.end();
313+
return out.toByteArray();
314+
}
245315
}

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.