Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/protocols/ioxide.http2/Hpack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal void Decode<TState>(ReadOnlySpan<byte> block, Span<byte> 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)
{
Expand All @@ -37,6 +38,7 @@ internal void Decode<TState>(ReadOnlySpan<byte> block, Span<byte> 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<byte> name, out ReadOnlySpan<byte> value);
fieldSeen = true;
onHeader(state, name, value);
continue;
}
Expand All @@ -46,6 +48,10 @@ internal void Decode<TState>(ReadOnlySpan<byte> block, Span<byte> 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;
Expand All @@ -72,6 +78,7 @@ internal void Decode<TState>(ReadOnlySpan<byte> block, Span<byte> scratch, TStat
_table.Add(headerName, headerValue);
}

fieldSeen = true;
onHeader(state, headerName, headerValue);
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/protocols/ioxide.http2/Huffman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static int Decode(ReadOnlySpan<byte> input, Span<byte> 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)
{
Expand All @@ -133,6 +134,7 @@ public static int Decode(ReadOnlySpan<byte> input, Span<byte> output)
int bit = (b >> i) & 1;
int next = Tree[node * 2 + bit];
depth++;
ones &= bit == 1;
if (next < 0)
{
if (written >= output.Length)
Expand All @@ -142,6 +144,7 @@ public static int Decode(ReadOnlySpan<byte> input, Span<byte> output)
output[written++] = (byte)(-next - 1);
node = 0;
depth = 0;
ones = true;
}
else
{
Expand All @@ -154,8 +157,8 @@ public static int Decode(ReadOnlySpan<byte> input, Span<byte> 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;
}

/// <summary>Worst-case decoded size (shortest code is 5 bits: 8/5 expansion).</summary>
Expand Down
7 changes: 5 additions & 2 deletions src/protocols/ioxide.http3/Huffman.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public static int Decode(ReadOnlySpan<byte> input, Span<byte> 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)
{
Expand All @@ -133,6 +134,7 @@ public static int Decode(ReadOnlySpan<byte> input, Span<byte> output)
int bit = (b >> i) & 1;
int next = Tree[node * 2 + bit];
depth++;
ones &= bit == 1;
if (next < 0)
{
if (written >= output.Length)
Expand All @@ -142,6 +144,7 @@ public static int Decode(ReadOnlySpan<byte> input, Span<byte> output)
output[written++] = (byte)(-next - 1);
node = 0;
depth = 0;
ones = true;
}
else
{
Expand All @@ -154,8 +157,8 @@ public static int Decode(ReadOnlySpan<byte> input, Span<byte> 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;
}

/// <summary>Worst-case decoded size (shortest code is 5 bits: 8/5 expansion).</summary>
Expand Down
139 changes: 139 additions & 0 deletions tests/Ioxide.Tests.Unit/Http2HpackDecodeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System.Text;
using ioxide.http2;

namespace Ioxide.Tests;

/// <summary>
/// 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.
/// </summary>
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<string> 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<string> 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<string> 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<string> 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<string> 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<string> 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<string> 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<string> 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<byte> 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<string> Served) Exchange(byte[] block, byte[]? continuation = null)
{
using var client = new StrictClient();
var served = new List<string>();
Task run = client.Connection.RunAsync(request =>
{
var line = new StringBuilder(request.StreamId.ToString());
foreach (KeyValuePair<ReadOnlyMemory<byte>, ReadOnlyMemory<byte>> 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<Frame> 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);
}
}
1 change: 1 addition & 0 deletions tests/Ioxide.Tests.Unit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading