@@ -60,6 +60,9 @@ final class QpackDecoder { |
60 | 60 |
|
61 | 61 | private static final QpackException UNKNOWN_TYPE = |
62 | 62 | 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"); |
63 | 66 |
|
64 | 67 | private final QpackHuffmanDecoder huffmanDecoder; |
65 | 68 | private final QpackDecoderDynamicTable dynamicTable; |
@@ -324,6 +327,27 @@ private static boolean isLiteralWithPostBaseNameRef(byte b) { |
324 | 327 | return (b & 0b1111_0000) == 0b0000_0000; |
325 | 328 | } |
326 | 329 |
|
| 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 | + |
327 | 351 | private void decodeIndexed(ByteBuf in, BiConsumer<CharSequence, CharSequence> sink, int base) |
328 | 352 | throws QpackException { |
329 | 353 | // 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 |
335 | 359 | // T == 1 implies static table |
336 | 360 | final QpackHeaderField field; |
337 | 361 | if (firstByteEquals(in, (byte) 0b1100_0000)) { |
338 | | - final int idx = decodePrefixedIntegerAsInt(in, 6); |
339 | | - assert idx >= 0; |
| 362 | + final int idx = decodePrefixedIntOrThrow(in, 6); |
340 | 363 | if (idx >= QpackStaticTable.length) { |
341 | 364 | throw HEADER_ILLEGAL_INDEX_VALUE; |
342 | 365 | } |
343 | 366 | field = QpackStaticTable.getField(idx); |
344 | 367 | } else { |
345 | | - final int idx = decodePrefixedIntegerAsInt(in, 6); |
346 | | - assert idx >= 0; |
| 368 | + final int idx = decodePrefixedIntOrThrow(in, 6); |
347 | 369 | field = dynamicTable.getEntryRelativeEncodedField(base - idx - 1); |
348 | 370 | } |
349 | 371 | sink.accept(field.name, field.value); |
@@ -356,8 +378,7 @@ private void decodeIndexedWithPostBase(ByteBuf in, BiConsumer<CharSequence, Char |
356 | 378 | // +---+---+---+---+---+---+---+---+ |
357 | 379 | // | 0 | 0 | 0 | 1 | Index (4+) | |
358 | 380 | // +---+---+---+---+---------------+ |
359 | | - final int idx = decodePrefixedIntegerAsInt(in, 4); |
360 | | - assert idx >= 0; |
| 381 | + final int idx = decodePrefixedIntOrThrow(in, 4); |
361 | 382 | QpackHeaderField field = dynamicTable.getEntryRelativeEncodedField(base + idx); |
362 | 383 | sink.accept(field.name, field.value); |
363 | 384 | } |
@@ -377,15 +398,13 @@ private void decodeLiteralWithNameRef(ByteBuf in, BiConsumer<CharSequence, CharS |
377 | 398 | // |
378 | 399 | // T == 1 implies static table |
379 | 400 | if (firstByteEquals(in, (byte) 0b0001_0000)) { |
380 | | - final int idx = decodePrefixedIntegerAsInt(in, 4); |
381 | | - assert idx >= 0; |
| 401 | + final int idx = decodePrefixedIntOrThrow(in, 4); |
382 | 402 | if (idx >= QpackStaticTable.length) { |
383 | 403 | throw NAME_ILLEGAL_INDEX_VALUE; |
384 | 404 | } |
385 | 405 | name = QpackStaticTable.getField(idx).name; |
386 | 406 | } else { |
387 | | - final int idx = decodePrefixedIntegerAsInt(in, 4); |
388 | | - assert idx >= 0; |
| 407 | + final int idx = decodePrefixedIntOrThrow(in, 4); |
389 | 408 | name = dynamicTable.getEntryRelativeEncodedField(base - idx - 1).name; |
390 | 409 | } |
391 | 410 | final CharSequence value = decodeHuffmanEncodedLiteral(in, 7); |
@@ -403,8 +422,7 @@ private void decodeLiteralWithPostBaseNameRef(ByteBuf in, BiConsumer<CharSequenc |
403 | 422 | // +---+---------------------------+ |
404 | 423 | // | Value String (Length bytes) | |
405 | 424 | // +-------------------------------+ |
406 | | - final int idx = decodePrefixedIntegerAsInt(in, 3); |
407 | | - assert idx >= 0; |
| 425 | + final int idx = decodePrefixedIntOrThrow(in, 3); |
408 | 426 | CharSequence name = dynamicTable.getEntryRelativeEncodedField(base + idx).name; |
409 | 427 | final CharSequence value = decodeHuffmanEncodedLiteral(in, 7); |
410 | 428 | sink.accept(name, value); |
@@ -430,8 +448,7 @@ private void decodeLiteral(ByteBuf in, BiConsumer<CharSequence, CharSequence> si |
430 | 448 | private CharSequence decodeHuffmanEncodedLiteral(ByteBuf in, int prefix) throws QpackException { |
431 | 449 | assert prefix < 8; |
432 | 450 | 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); |
435 | 452 | if (huffmanEncoded) { |
436 | 453 | return huffmanDecoder.decode(in, length); |
437 | 454 | } |
@@ -445,8 +462,7 @@ private CharSequence decodeHuffmanEncodedLiteral(ByteBuf in, int prefix) throws |
445 | 462 |
|
446 | 463 | // Visible for testing |
447 | 464 | 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); |
450 | 466 | // https://www.rfc-editor.org/rfc/rfc9204.html#name-required-insert-count |
451 | 467 | // FullRange = 2 * MaxEntries |
452 | 468 | // if EncodedInsertCount == 0: |
@@ -503,8 +519,7 @@ int decodeBase(ByteBuf buf, int requiredInsertCount) throws QpackException { |
503 | 519 | // | S | Delta Base (7+) | |
504 | 520 | // +---+---------------------------+ |
505 | 521 | 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); |
508 | 523 | // https://www.rfc-editor.org/rfc/rfc9204.html#name-base |
509 | 524 | // if S == 0: |
510 | 525 | // Base = ReqInsertCount + DeltaBase |
|