From f3330a62793fa02a43e78376276ed1c0af7d7715 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Tue, 17 Nov 2026 00:20:27 +0000 Subject: [PATCH 1/3] http2, http3: a size update after a field, and Huffman padding that is not all ones, are decoding errors (#643) RFC 7541 4.2 allows a dynamic table size update only at the start of a header block, and 5.2 makes Huffman padding the most significant bits of EOS, which are all ones. The HPACK decoder applied a size update anywhere in a block, and both Huffman decoders (ioxide.http2's, and ioxide.http3's copy for QPACK, RFC 9204 4.1.2) checked only that the padding was shorter than a byte. Now a size update after a field throws HpackException, which Http2Connection already turns into GOAWAY COMPRESSION_ERROR, and a string whose trailing bits are not all ones decodes to -1 in both decoders. Http2HpackDecodeTests: a size update after :method, a Huffman literal padded with zeros, and the http3 decoder on the same octet fail without the change; their controls (a size update that opens the block, padding of ones) pass either way. h2spec hpack/4.2/1 and hpack/5.2/2. --- src/protocols/ioxide.http2/Hpack.cs | 7 ++ src/protocols/ioxide.http2/Huffman.cs | 7 +- src/protocols/ioxide.http3/Huffman.cs | 7 +- .../Http2HpackDecodeTests.cs | 96 +++++++++++++++++++ tests/Ioxide.Tests.Unit/Program.cs | 1 + 5 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs diff --git a/src/protocols/ioxide.http2/Hpack.cs b/src/protocols/ioxide.http2/Hpack.cs index 9012297b..3e90e952 100644 --- a/src/protocols/ioxide.http2/Hpack.cs +++ b/src/protocols/ioxide.http2/Hpack.cs @@ -27,6 +27,7 @@ internal void Decode(ReadOnlySpan block, Span scratch, TStat { int position = 0; int scratchUsed = 0; + bool fieldSeen = false; // a size update may only open the block (RFC 7541 4.2) while (position < block.Length) { @@ -37,6 +38,7 @@ internal void Decode(ReadOnlySpan block, Span scratch, TStat // 1xxxxxxx - fully indexed: both name and value come from a table. int index = (int)ReadInteger(block, ref position, 7); Lookup(index, out ReadOnlySpan name, out ReadOnlySpan value); + fieldSeen = true; onHeader(state, name, value); continue; } @@ -46,6 +48,10 @@ internal void Decode(ReadOnlySpan block, Span scratch, TStat if (sizeUpdate) { + if (fieldSeen) + { + throw new HpackException("dynamic table size update after a field of the block"); + } int newSize = (int)ReadInteger(block, ref position, 5); _table.Resize(newSize); continue; @@ -72,6 +78,7 @@ internal void Decode(ReadOnlySpan block, Span scratch, TStat _table.Add(headerName, headerValue); } + fieldSeen = true; onHeader(state, headerName, headerValue); } } diff --git a/src/protocols/ioxide.http2/Huffman.cs b/src/protocols/ioxide.http2/Huffman.cs index 6fe5831e..267980e7 100644 --- a/src/protocols/ioxide.http2/Huffman.cs +++ b/src/protocols/ioxide.http2/Huffman.cs @@ -125,6 +125,7 @@ public static int Decode(ReadOnlySpan input, Span output) int node = 0; int written = 0; int depth = 0; // bits consumed since the last emitted symbol (padding validity check) + bool ones = true; // and whether every one of them was a 1, as EOS's prefix is foreach (byte b in input) { @@ -133,6 +134,7 @@ public static int Decode(ReadOnlySpan input, Span output) int bit = (b >> i) & 1; int next = Tree[node * 2 + bit]; depth++; + ones &= bit == 1; if (next < 0) { if (written >= output.Length) @@ -142,6 +144,7 @@ public static int Decode(ReadOnlySpan input, Span output) output[written++] = (byte)(-next - 1); node = 0; depth = 0; + ones = true; } else { @@ -154,8 +157,8 @@ public static int Decode(ReadOnlySpan input, Span output) } } - // Trailing bits must be a prefix of EOS (all ones) and shorter than a byte. - return depth > 7 ? -1 : written; + // Trailing bits must be a prefix of EOS (all ones) and shorter than a byte (RFC 7541 5.2). + return depth > 7 || !ones ? -1 : written; } /// Worst-case decoded size (shortest code is 5 bits: 8/5 expansion). diff --git a/src/protocols/ioxide.http3/Huffman.cs b/src/protocols/ioxide.http3/Huffman.cs index f7a71101..1297758c 100644 --- a/src/protocols/ioxide.http3/Huffman.cs +++ b/src/protocols/ioxide.http3/Huffman.cs @@ -125,6 +125,7 @@ public static int Decode(ReadOnlySpan input, Span output) int node = 0; int written = 0; int depth = 0; // bits consumed since the last emitted symbol (padding validity check) + bool ones = true; // and whether every one of them was a 1, as EOS's prefix is foreach (byte b in input) { @@ -133,6 +134,7 @@ public static int Decode(ReadOnlySpan input, Span output) int bit = (b >> i) & 1; int next = Tree[node * 2 + bit]; depth++; + ones &= bit == 1; if (next < 0) { if (written >= output.Length) @@ -142,6 +144,7 @@ public static int Decode(ReadOnlySpan input, Span output) output[written++] = (byte)(-next - 1); node = 0; depth = 0; + ones = true; } else { @@ -154,8 +157,8 @@ public static int Decode(ReadOnlySpan input, Span output) } } - // Trailing bits must be a prefix of EOS (all ones) and shorter than a byte. - return depth > 7 ? -1 : written; + // Trailing bits must be a prefix of EOS (all ones) and shorter than a byte (RFC 7541 5.2). + return depth > 7 || !ones ? -1 : written; } /// Worst-case decoded size (shortest code is 5 bits: 8/5 expansion). diff --git a/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs b/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs new file mode 100644 index 00000000..00cc2c35 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs @@ -0,0 +1,96 @@ +using System.Text; +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// Header blocks that RFC 7541 calls decoding errors: a dynamic table size update after a field of +/// the block (4.2), and a Huffman string whose padding is not the most significant bits of EOS, that +/// is, not all ones (5.2). Either ends the connection with COMPRESSION_ERROR, since a peer that +/// encodes like this is out of step with the decoder for every later block. Each refusal has a +/// control that differs only in the octet under test. +/// +internal static class Http2HpackDecodeTests +{ + private const byte Headers = 0x1, GoAway = 0x7; + private const byte EndStream = 0x1, EndHeaders = 0x4; + private const uint CompressionError = 0x9; + + // :method GET, :scheme http, :path / - static table only. + private static readonly byte[] Get = [0x82, 0x86, 0x84]; + + // A literal without indexing, new name "x-a", its value Huffman-coded as one octet: 'a' is the + // 5-bit code 00011, so 0x1f pads it with ones, as EOS's prefix does, and 0x18 pads it with zeros. + private static byte[] Field(byte huffmanOctet) => [0x00, 0x03, (byte)'x', (byte)'-', (byte)'a', 0x81, huffmanOctet]; + + public static void Register(Runner runner) + { + runner.Test("h2 hpack: control: a dynamic table size update that opens the block is applied, and the request served", () => + { + (string goAway, List served) = Exchange([0x20, .. Get]); + Assert.Equal("", goAway); + Assert.True(served.SequenceEqual(["1"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 hpack: a dynamic table size update after a field of the block ends the connection with COMPRESSION_ERROR", () => + { + (string goAway, List served) = Exchange([Get[0], 0x20, Get[1], Get[2]]); + Assert.True(goAway == "COMPRESSION_ERROR", + $"a size update after :method was taken, and the server wrote [{goAway}] (RFC 7541 4.2 allows one only at the start of a block)"); + Assert.True(served.Count == 0, $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 hpack: control: a Huffman string padded with ones decodes", () => + { + (string goAway, List served) = Exchange([.. Get, .. Field(0x1f)]); + Assert.Equal("", goAway); + Assert.True(served.SequenceEqual(["1 x-a=a"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 hpack: a Huffman string padded with zeros ends the connection with COMPRESSION_ERROR", () => + { + (string goAway, List served) = Exchange([.. Get, .. Field(0x18)]); + Assert.True(goAway == "COMPRESSION_ERROR", + $"padding 000 after 'a' was taken as the end of the string, and the server wrote [{goAway}] (RFC 7541 5.2 wants the bits of EOS, all ones)"); + Assert.True(served.Count == 0, $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("http3 qpack: the Huffman decoder takes padding of ones and refuses padding of zeros", () => + { + // QPACK uses HPACK's Huffman code and its padding rule (RFC 9204 4.1.2); ioxide.http3 has its own copy. + Span output = stackalloc byte[8]; + Assert.Equal(1, ioxide.http3.Huffman.Decode([0x1f], output)); + Assert.True(output[0] == (byte)'a', $"0x1f decoded to 0x{output[0]:x2}, not 'a'"); + Assert.Equal(-1, ioxide.http3.Huffman.Decode([0x18], output)); + }); + } + + // One GET on stream 1 with the given block; what GOAWAY came back, and what was served ("stream name=value ..."). + private static (string GoAway, List Served) Exchange(byte[] block) + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + var line = new StringBuilder(request.StreamId.ToString()); + foreach (KeyValuePair, ReadOnlyMemory> field in request.Headers.AsSpan()) + { + line.Append(' ').Append(Encoding.ASCII.GetString(field.Key.Span)).Append('=').Append(Encoding.ASCII.GetString(field.Value.Span)); + } + served.Add(line.ToString()); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndHeaders | EndStream, 1, block)); + List frames = client.Drain(); + client.Close(run); + + string goAway = string.Join(", ", frames.Where(f => f.Type == GoAway).Select(f => + System.Buffers.Binary.BinaryPrimitives.ReadUInt32BigEndian(f.Payload.AsSpan(4, 4)) == CompressionError + ? "COMPRESSION_ERROR" + : $"0x{System.Buffers.Binary.BinaryPrimitives.ReadUInt32BigEndian(f.Payload.AsSpan(4, 4)):x}")); + return (goAway, served); + } +} diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index 95146068..6a0d9504 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -51,6 +51,7 @@ private static int Main() Http2PeerClosedFlushTests.Register(runner); Http2StreamResetTests.Register(runner); HpackEncodeTests.Register(runner); + Http2HpackDecodeTests.Register(runner); QpackStaticEncodeTests.Register(runner); AlpnSelectTests.Register(runner); KeylogSecretTests.Register(runner); From bc1c9bcaf5ebab3cf3ac1d5428653bad90798c52 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Tue, 17 Nov 2026 00:24:52 +0000 Subject: [PATCH 2/3] tests: two size updates opening a block, and one in a CONTINUATION after a field (#643) From review: the size-update rule had no test for a block split across HEADERS and CONTINUATION, where the update comes after :method in the HEADERS frame, nor a control for more than one update ahead of the first field (RFC 7541 4.2). --- .../Http2HpackDecodeTests.cs | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs b/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs index 00cc2c35..638fd8b8 100644 --- a/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs @@ -12,7 +12,7 @@ namespace Ioxide.Tests; /// internal static class Http2HpackDecodeTests { - private const byte Headers = 0x1, GoAway = 0x7; + private const byte Headers = 0x1, GoAway = 0x7, Continuation = 0x9; private const byte EndStream = 0x1, EndHeaders = 0x4; private const uint CompressionError = 0x9; @@ -40,6 +40,23 @@ public static void Register(Runner runner) Assert.True(served.Count == 0, $"the handler was handed: {string.Join(", ", served)}"); }); + runner.Test("h2 hpack: control: two size updates that open the block are both taken", () => + { + // RFC 7541 4.2: the smallest size the table went through, then the final one, both ahead of the first field. + (string goAway, List served) = Exchange([0x20, 0x3f, 0xe1, 0x1f, .. Get]); + Assert.Equal("", goAway); + Assert.True(served.SequenceEqual(["1"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 hpack: a size update in a CONTINUATION, after a field in its HEADERS, ends the connection with COMPRESSION_ERROR", () => + { + // One block in two frames is still one block: the update comes after :method whichever frame carries it. + (string goAway, List served) = Exchange([Get[0]], [0x20, Get[1], Get[2]]); + Assert.True(goAway == "COMPRESSION_ERROR", + $"a size update after :method, in the CONTINUATION of its block, was taken, and the server wrote [{goAway}]"); + Assert.True(served.Count == 0, $"the handler was handed: {string.Join(", ", served)}"); + }); + runner.Test("h2 hpack: control: a Huffman string padded with ones decodes", () => { (string goAway, List served) = Exchange([.. Get, .. Field(0x1f)]); @@ -66,7 +83,7 @@ public static void Register(Runner runner) } // One GET on stream 1 with the given block; what GOAWAY came back, and what was served ("stream name=value ..."). - private static (string GoAway, List Served) Exchange(byte[] block) + private static (string GoAway, List Served) Exchange(byte[] block, byte[]? continuation = null) { using var client = new StrictClient(); var served = new List(); @@ -83,7 +100,15 @@ private static (string GoAway, List Served) Exchange(byte[] block) client.SendRequests(); client.Drain(); - client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndHeaders | EndStream, 1, block)); + if (continuation is null) + { + client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndHeaders | EndStream, 1, block)); + } + else + { + client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndStream, 1, block)); + client.Feed(Http2FlowControlTests.FrameBytes(Continuation, EndHeaders, 1, continuation)); + } List frames = client.Drain(); client.Close(run); From 3acb8d96355defc1f178cab076994a1b76af4699 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Tue, 17 Nov 2026 00:27:57 +0000 Subject: [PATCH 3/3] tests: a size update after a literal field, and padding that ends in a one (#643) From review: the size-update refusals only had an indexed field before the update, and the only bad padding tested was all zeros, so a check of the last padding bit alone would have passed. --- .../Ioxide.Tests.Unit/Http2HpackDecodeTests.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs b/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs index 638fd8b8..6b88e1cd 100644 --- a/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs @@ -40,6 +40,15 @@ public static void Register(Runner runner) Assert.True(served.Count == 0, $"the handler was handed: {string.Join(", ", served)}"); }); + runner.Test("h2 hpack: a dynamic table size update after a literal field ends the connection with COMPRESSION_ERROR", () => + { + // The literal kinds count as fields too, not only fully indexed ones. + (string goAway, List served) = Exchange([.. Get, .. Field(0x1f), 0x20]); + Assert.True(goAway == "COMPRESSION_ERROR", + $"a size update after the literal x-a was taken, and the server wrote [{goAway}]"); + Assert.True(served.Count == 0, $"the handler was handed: {string.Join(", ", served)}"); + }); + runner.Test("h2 hpack: control: two size updates that open the block are both taken", () => { // RFC 7541 4.2: the smallest size the table went through, then the final one, both ahead of the first field. @@ -72,6 +81,15 @@ public static void Register(Runner runner) Assert.True(served.Count == 0, $"the handler was handed: {string.Join(", ", served)}"); }); + runner.Test("h2 hpack: Huffman padding that ends in a one but is not all ones ends the connection with COMPRESSION_ERROR", () => + { + // 0x1d is 'a' then 101: every padding bit has to be a 1, not only the last. + (string goAway, List served) = Exchange([.. Get, .. Field(0x1d)]); + Assert.True(goAway == "COMPRESSION_ERROR", + $"padding 101 after 'a' was taken as the end of the string, and the server wrote [{goAway}]"); + Assert.True(served.Count == 0, $"the handler was handed: {string.Join(", ", served)}"); + }); + runner.Test("http3 qpack: the Huffman decoder takes padding of ones and refuses padding of zeros", () => { // QPACK uses HPACK's Huffman code and its padding rule (RFC 9204 4.1.2); ioxide.http3 has its own copy.