diff --git a/Sources/NIOHPACK/HPACKDecoder.swift b/Sources/NIOHPACK/HPACKDecoder.swift index f341bcf..80ed1c1 100644 --- a/Sources/NIOHPACK/HPACKDecoder.swift +++ b/Sources/NIOHPACK/HPACKDecoder.swift @@ -122,12 +122,18 @@ public struct HPACKDecoder: Sendable { headers.reserveCapacity(16) var listSize = 0 + var tableSizeUpdates = 0 while buffer.readableBytes > 0 { switch try self.decodeHeader(from: &buffer) { case .tableSizeChange: // RFC 7541 § 4.2 : // + // 1. "In the case that this size is changed more than once in [the interval between two + // header blocks], the smallest maximum table size that occurs in that interval MUST be + // signaled in a dynamic table size update. The final maximum size is always signaled, + // resulting in at most two dynamic table size updates." + // // 2. "This dynamic table size update MUST occur at the beginning of the first header block // following the change to the dynamic table size." guard headers.count == 0 else { @@ -135,6 +141,13 @@ public struct HPACKDecoder: Sendable { // Treat it as an invalid input throw NIOHPACKErrors.IllegalDynamicTableSizeChange() } + + // A conformant encoder emits at most two consecutive table size updates (see point 1 + // above). More than two is never valid, so reject the block as malformed. + tableSizeUpdates += 1 + guard tableSizeUpdates <= 2 else { + throw NIOHPACKErrors.IllegalDynamicTableSizeChange() + } case .header(let header): listSize += header.size guard listSize <= self.maxHeaderListSize else { diff --git a/Tests/NIOHPACKTests/HPACKCodingTests.swift b/Tests/NIOHPACKTests/HPACKCodingTests.swift index 95973b0..6a37345 100644 --- a/Tests/NIOHPACKTests/HPACKCodingTests.swift +++ b/Tests/NIOHPACKTests/HPACKCodingTests.swift @@ -551,6 +551,28 @@ class HPACKCodingTests: XCTestCase { } } + func testAcceptsTwoConsecutiveTableSizeUpdates() throws { + // Two dynamic-table-size updates (0x20 == update to 0) at the start of a header block, followed + // by an indexed header field (0x82 == static index 2, ":method: GET"). RFC 7541 § 4.2 permits up + // to two table size updates per header block, so this must decode successfully. + var request = buffer(wrapping: [0x20, 0x20, 0x82]) + var decoder = HPACKDecoder(allocator: ByteBufferAllocator()) + + let decoded = try decoder.decodeHeaders(from: &request) + XCTAssertEqual(decoded, HPACKHeaders([(":method", "GET")])) + } + + func testRejectsMoreThanTwoConsecutiveTableSizeUpdates() throws { + // Three dynamic-table-size updates (0x20) before the first header. RFC 7541 § 4.2 caps a header + // block at two table size updates, so the decoder must reject the block as malformed. + var request = buffer(wrapping: [0x20, 0x20, 0x20, 0x82]) + var decoder = HPACKDecoder(allocator: ByteBufferAllocator()) + + XCTAssertThrowsError(try decoder.decodeHeaders(from: &request)) { error in + XCTAssertTrue(error is NIOHPACKErrors.IllegalDynamicTableSizeChange) + } + } + func testHPACKHeadersDescription() throws { let headerList1: [(String, String)] = [ (":method", "GET"),