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..6b88e1cd --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs @@ -0,0 +1,139 @@ +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, Continuation = 0x9; + 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: 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. + (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)]); + 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("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. + 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, byte[]? continuation = null) + { + 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(); + + 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); + + 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);