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 — Reject truncated QPACK prefixed integers in field section decode (#17… · netty/netty@e526345

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • codec-http3/src
    • main/java/io/netty/handler/codec/http3
      • QpackDecoder.java
    • test/java/io/netty/handler/codec/http3
      • QpackDecoderTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎codec-http3/src/main/java/io/netty/handler/codec/http3/QpackDecoder.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/main/java/io/netty/handler/codec/http3/QpackDecoder.java
+33-18Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ final class QpackDecoder {
6060

6161
private static final QpackException UNKNOWN_TYPE =
6262
QpackException.newStatic(QpackDecoder.class, "decode(...)", "QPACK - unknown type");
63+
private static final QpackException TRUNCATED_PREFIXED_INTEGER =
64+
QpackException.newStatic(QpackDecoder.class, "decode(...)",
65+
"QPACK - truncated prefixed integer in field section");
6366

6467
private final QpackHuffmanDecoder huffmanDecoder;
6568
private final QpackDecoderDynamicTable dynamicTable;
@@ -324,6 +327,27 @@ private static boolean isLiteralWithPostBaseNameRef(byte b) {
324327
return (b & 0b1111_0000) == 0b0000_0000;
325328
}
326329

330+
// decode() and its helpers always operate on a slice that holds the complete field section, so a prefixed
331+
// integer can never be legitimately truncated here. decodePrefixedInteger returns -1 when it would need more
332+
// bytes, which can only happen for a malformed encoding in this context. Turn that into a decompression error
333+
// instead of relying on an assert (disabled in production) and then advancing with a negative value, which
334+
// would index the dynamic table out of range or spin in the decode loop without consuming any bytes.
335+
private int decodePrefixedIntOrThrow(ByteBuf in, int prefixLength) throws QpackException {
336+
final int i = decodePrefixedIntegerAsInt(in, prefixLength);
337+
if (i < 0) {
338+
throw TRUNCATED_PREFIXED_INTEGER;
339+
}
340+
return i;
341+
}
342+
343+
private static long decodePrefixedLongOrThrow(ByteBuf in, int prefixLength) throws QpackException {
344+
final long i = QpackUtil.decodePrefixedInteger(in, prefixLength);
345+
if (i < 0) {
346+
throw TRUNCATED_PREFIXED_INTEGER;
347+
}
348+
return i;
349+
}
350+
327351
private void decodeIndexed(ByteBuf in, BiConsumer<CharSequence, CharSequence> sink, int base)
328352
throws QpackException {
329353
// https://www.rfc-editor.org/rfc/rfc9204.html#name-indexed-field-line
@@ -335,15 +359,13 @@ private void decodeIndexed(ByteBuf in, BiConsumer<CharSequence, CharSequence> si
335359
// T == 1 implies static table
336360
final QpackHeaderField field;
337361
if (firstByteEquals(in, (byte) 0b1100_0000)) {
338-
final int idx = decodePrefixedIntegerAsInt(in, 6);
339-
assert idx >= 0;
362+
final int idx = decodePrefixedIntOrThrow(in, 6);
340363
if (idx >= QpackStaticTable.length) {
341364
throw HEADER_ILLEGAL_INDEX_VALUE;
342365
}
343366
field = QpackStaticTable.getField(idx);
344367
} else {
345-
final int idx = decodePrefixedIntegerAsInt(in, 6);
346-
assert idx >= 0;
368+
final int idx = decodePrefixedIntOrThrow(in, 6);
347369
field = dynamicTable.getEntryRelativeEncodedField(base - idx - 1);
348370
}
349371
sink.accept(field.name, field.value);
@@ -356,8 +378,7 @@ private void decodeIndexedWithPostBase(ByteBuf in, BiConsumer<CharSequence, Char
356378
// +---+---+---+---+---+---+---+---+
357379
// | 0 | 0 | 0 | 1 | Index (4+) |
358380
// +---+---+---+---+---------------+
359-
final int idx = decodePrefixedIntegerAsInt(in, 4);
360-
assert idx >= 0;
381+
final int idx = decodePrefixedIntOrThrow(in, 4);
361382
QpackHeaderField field = dynamicTable.getEntryRelativeEncodedField(base + idx);
362383
sink.accept(field.name, field.value);
363384
}
@@ -377,15 +398,13 @@ private void decodeLiteralWithNameRef(ByteBuf in, BiConsumer<CharSequence, CharS
377398
//
378399
// T == 1 implies static table
379400
if (firstByteEquals(in, (byte) 0b0001_0000)) {
380-
final int idx = decodePrefixedIntegerAsInt(in, 4);
381-
assert idx >= 0;
401+
final int idx = decodePrefixedIntOrThrow(in, 4);
382402
if (idx >= QpackStaticTable.length) {
383403
throw NAME_ILLEGAL_INDEX_VALUE;
384404
}
385405
name = QpackStaticTable.getField(idx).name;
386406
} else {
387-
final int idx = decodePrefixedIntegerAsInt(in, 4);
388-
assert idx >= 0;
407+
final int idx = decodePrefixedIntOrThrow(in, 4);
389408
name = dynamicTable.getEntryRelativeEncodedField(base - idx - 1).name;
390409
}
391410
final CharSequence value = decodeHuffmanEncodedLiteral(in, 7);
@@ -403,8 +422,7 @@ private void decodeLiteralWithPostBaseNameRef(ByteBuf in, BiConsumer<CharSequenc
403422
// +---+---------------------------+
404423
// | Value String (Length bytes) |
405424
// +-------------------------------+
406-
final int idx = decodePrefixedIntegerAsInt(in, 3);
407-
assert idx >= 0;
425+
final int idx = decodePrefixedIntOrThrow(in, 3);
408426
CharSequence name = dynamicTable.getEntryRelativeEncodedField(base + idx).name;
409427
final CharSequence value = decodeHuffmanEncodedLiteral(in, 7);
410428
sink.accept(name, value);
@@ -430,8 +448,7 @@ private void decodeLiteral(ByteBuf in, BiConsumer<CharSequence, CharSequence> si
430448
private CharSequence decodeHuffmanEncodedLiteral(ByteBuf in, int prefix) throws QpackException {
431449
assert prefix < 8;
432450
final boolean huffmanEncoded = firstByteEquals(in, (byte) (1 << prefix));
433-
final int length = decodePrefixedIntegerAsInt(in, prefix);
434-
assert length >= 0;
451+
final int length = decodePrefixedIntOrThrow(in, prefix);
435452
if (huffmanEncoded) {
436453
return huffmanDecoder.decode(in, length);
437454
}
@@ -445,8 +462,7 @@ private CharSequence decodeHuffmanEncodedLiteral(ByteBuf in, int prefix) throws
445462

446463
// Visible for testing
447464
int decodeRequiredInsertCount(QpackAttributes qpackAttributes, ByteBuf buf) throws QpackException {
448-
final long encodedInsertCount = QpackUtil.decodePrefixedInteger(buf, 8);
449-
assert encodedInsertCount >= 0;
465+
final long encodedInsertCount = decodePrefixedLongOrThrow(buf, 8);
450466
// https://www.rfc-editor.org/rfc/rfc9204.html#name-required-insert-count
451467
// FullRange = 2 * MaxEntries
452468
// if EncodedInsertCount == 0:
@@ -503,8 +519,7 @@ int decodeBase(ByteBuf buf, int requiredInsertCount) throws QpackException {
503519
// | S | Delta Base (7+) |
504520
// +---+---------------------------+
505521
final boolean s = (buf.getByte(buf.readerIndex()) & 0b1000_0000) == 0b1000_0000;
506-
final int deltaBase = decodePrefixedIntegerAsInt(buf, 7);
507-
assert deltaBase >= 0;
522+
final int deltaBase = decodePrefixedIntOrThrow(buf, 7);
508523
// https://www.rfc-editor.org/rfc/rfc9204.html#name-base
509524
// if S == 0:
510525
// Base = ReqInsertCount + DeltaBase
Collapse file

‎codec-http3/src/test/java/io/netty/handler/codec/http3/QpackDecoderTest.java‎

Copy file name to clipboardExpand all lines: codec-http3/src/test/java/io/netty/handler/codec/http3/QpackDecoderTest.java
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,26 @@ public void getWithPostBaseRelativeIndex(int capacity, int insertionCount) throw
123123
verifyField(entry, insertionCount - 1);
124124
}
125125

126+
@Test
127+
public void truncatedPostBaseIndexIsRejected() throws Exception {
128+
setup(128);
129+
insertLiterals(3);
130+
131+
// A field section whose last line is an "Indexed Field Line With Post-Base Index" whose prefixed
132+
// integer is cut off (the continuation byte never arrives). decodePrefixedInteger returns -1, which used
133+
// to flow into the dynamic table lookup with assertions disabled and spin in the decode loop forever.
134+
ByteBuf in = Unpooled.buffer();
135+
QpackUtil.encodePrefixedInteger(in, (byte) 0b0, 8, 3 % (2L * maxEntries) + 1); // Required Insert Count
136+
QpackUtil.encodePrefixedInteger(in, (byte) 0b0, 7, 0); // Base (post-base, delta 0)
137+
in.writeByte(0x1f); // post-base index, 4-bit prefix all ones, continuation byte missing
138+
try {
139+
assertThrows(QpackException.class, () ->
140+
decoder.decode(attributes, 0L, in, in.readableBytes(), (n, v) -> { }, () -> { }));
141+
} finally {
142+
in.release();
143+
}
144+
}
145+
126146
@Test
127147
public void zeroMaxBlockedStreamsThrowsOnBlockedStream() throws Exception {
128148
setup(128);

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.