From b1d4cf1e99a1a4f3dbeb6443d9bb7b20d5719d89 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Tue, 17 Nov 2026 00:32:22 +0000 Subject: [PATCH 1/4] http2: malformed PRIORITY, PUSH_PROMISE, RST_STREAM, SETTINGS and PING frames are refused with the error RFC 9113 names, and a GOAWAY carries the last stream (#641) PRIORITY and PUSH_PROMISE were ignored, an RST_STREAM's payload was never looked at, a SETTINGS ACK returned before its length check, SETTINGS on a stream and SETTINGS_ENABLE_PUSH past 1 passed, and a PING was checked only for its length. Each is now refused as RFC 9113 says: - PRIORITY (6.3): on stream 0, GOAWAY(PROTOCOL_ERROR). Not five octets, a stream error of FRAME_SIZE_ERROR. A stream that depends on itself, a stream error of PROTOCOL_ERROR: RFC 9113 dropped that rule with the priority scheme, but RFC 7540 5.3.1 had it, and h2spec and nghttp2 still hold a server to it. A PRIORITY may name an idle stream, which no RST_STREAM may (6.4), so for one the connection answers with GOAWAY of the same code. A stream that is open is reset as RefuseBody resets one: its body reader ends and its response window goes, so nothing more goes out on it. RefuseBody now shares that helper, ResetOpenStream. - HEADERS: the priority fields are read, and a self-dependency is refused the same way. The block is still decoded aside, so HPACK stays in step; a new stream is reset once its block is in, with PROTOCOL_ERROR rather than the REFUSED_STREAM of a stream past the concurrency limit, which DiscardHeaderBlock now takes from a field. - PUSH_PROMISE from a client: GOAWAY(PROTOCOL_ERROR) (8.4). - RST_STREAM: on stream 0, GOAWAY(PROTOCOL_ERROR); not four octets, GOAWAY(FRAME_SIZE_ERROR) (6.4). - SETTINGS: on a stream, GOAWAY(PROTOCOL_ERROR); an ACK with a payload, GOAWAY(FRAME_SIZE_ERROR) (6.5); SETTINGS_ENABLE_PUSH other than 0 or 1, GOAWAY(PROTOCOL_ERROR) (6.5.2). - PING on a stream: GOAWAY(PROTOCOL_ERROR) (6.7). - GOAWAY's last-stream-id was always 0. It is now the highest stream the client opened, refused ones included (6.8): the client learns which requests it may retry, and REFUSED_STREAM already told it that a refused one may be. Unknown frame types are still ignored (4.1). h2spec 2.6.0 against a reactor serving h2c: the 12 cases of this issue fail on origin/main (5.3.1/1, 5.3.1/2, 5.4.1/1, 6.3/1, 6.3/2, 6.4/1, 6.4/3, 6.5/1, 6.5/2, 6.5.2/1, 6.7/3, 8.2/1) and pass with this; 35 failures go to 23. Tests: Http2ControlFrameTests, in Unit. Each malformed frame after a served stream 1 ends the connection with the expected GOAWAY naming stream 1, and stream 3 behind it is not served; PRIORITY of four octets or depending on itself resets a request still arriving and the next is served; HEADERS depending on their own stream are reset with no handler run and HPACK in step. A control sends a well-formed PRIORITY on a closed and on an idle stream, ENABLE_PUSH 0 and 1, an empty ACK and a PING, and both streams are served with the PING echoed. Run against origin/main's source every refusal test fails, "expected [GOAWAY 1 0x1], got []" and the like, and a DATA on an idle stream, refused before as well, "expected [GOAWAY 1 0x1], got [GOAWAY 0 0x1]"; the control passes either way. Suites: Unit 321, Chaos 105, Http 113, none failing. --- .../ioxide.http2/Http2Connection.Frames.cs | 131 ++++++++++++-- .../ioxide.http2/Http2Connection.Write.cs | 4 +- src/protocols/ioxide.http2/Http2Connection.cs | 4 + .../Http2ControlFrameTests.cs | 166 ++++++++++++++++++ tests/Ioxide.Tests.Unit/Program.cs | 1 + 5 files changed, 295 insertions(+), 11 deletions(-) create mode 100644 tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs diff --git a/src/protocols/ioxide.http2/Http2Connection.Frames.cs b/src/protocols/ioxide.http2/Http2Connection.Frames.cs index 18d9c3ea..ab683076 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Frames.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Frames.cs @@ -101,18 +101,74 @@ private void Handle(in FrameHeader header, ReadOnlySpan payload) case FrameType.Settings: HandleSettings(header, payload); break; case FrameType.WindowUpdate: HandleWindowUpdate(header, payload); break; case FrameType.Ping: HandlePing(header, payload); break; - case FrameType.RstStream: HandleRstStream(header); break; + case FrameType.RstStream: HandleRstStream(header, payload); break; + case FrameType.Priority: HandlePriority(header, payload); break; case FrameType.GoAway: _failed = true; break; - // PRIORITY carries no obligation, and PUSH_PROMISE cannot arrive at a server. Both are - // ignored rather than treated as errors, which is what the RFC asks of a receiver. - case FrameType.Priority: - case FrameType.PushPromise: + // RFC 9113 8.4: a server never receives a push, so a client that sends one is broken. + case FrameType.PushPromise: GoAway(Http2Error.ProtocolError); break; + + // RFC 9113 4.1: a frame of a type nobody knows is ignored. default: break; } } + /// + /// PRIORITY carries no obligation (RFC 9113 5.3.2), so its signal is ignored, but not its framing: + /// it always names a stream, and it is five octets long (6.3). A stream that depends on itself is + /// refused too: RFC 9113 dropped the rule with the priority scheme, but RFC 7540 5.3.1 made it a + /// stream error, and nghttp2 still refuses one. + /// + private void HandlePriority(in FrameHeader header, ReadOnlySpan payload) + { + if (header.StreamId == 0) + { + GoAway(Http2Error.ProtocolError); + return; + } + + if (payload.Length != 5) + { + RefusePriority(header.StreamId, Http2Error.FrameSizeError); + return; + } + + if (DependsOn(payload) == header.StreamId) + { + RefusePriority(header.StreamId, Http2Error.ProtocolError); + } + } + + // A stream error, but PRIORITY may name an idle stream, which no RST_STREAM may (RFC 9113 6.4): the + // connection answers for it then. + private void RefusePriority(int streamId, uint errorCode) + { + if (streamId > _highestStream) + { + GoAway(errorCode); + } + else + { + ResetOpenStream(streamId, errorCode); + } + } + + // The stream a priority's five octets name, its exclusive bit aside. + private static int DependsOn(ReadOnlySpan priority) + => (int)(BinaryPrimitives.ReadUInt32BigEndian(priority) & 0x7FFFFFFF); + + /// + /// A stream error on a stream that may be serving already: its body reader ends, and its response + /// finds the stream gone, as after the peer's own reset - nothing may follow a reset (RFC 9113 5.1). + /// + private void ResetOpenStream(int streamId, uint errorCode) + { + _responseWindows.Remove(streamId); + ResetStream(streamId, errorCode); + ReleaseCreditWaiters(streamId); // a writer parked on credit wakes to find the stream gone + } + private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) { if (header.StreamId == 0) @@ -141,6 +197,7 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) block = block[..^padding]; } + bool selfDependent = false; if ((header.Flags & FrameFlags.Priority) != 0) { if (block.Length < 5) @@ -148,9 +205,24 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) GoAway(Http2Error.FrameSizeError); return; } + selfDependent = DependsOn(block) == header.StreamId; block = block[5..]; } + if (selfDependent) + { + // Refused like a malformed request (see HandlePriority): decoded aside to keep HPACK in + // step, and whatever the stream was doing ends. A new one is reset once its block is in. + if (_streams.ContainsKey(header.StreamId)) + { + ResetOpenStream(header.StreamId, Http2Error.ProtocolError); + } + _discardBlock.StreamId = header.StreamId; + _discardReset = Http2Error.ProtocolError; + DiscardHeaderBlock(header, block); + return; + } + if (!_streams.TryGetValue(header.StreamId, out PendingRequest? pending)) { // An id at or below one already opened opens nothing (RFC 9113 5.1.1) - it is trailers @@ -251,12 +323,14 @@ private void DiscardHeaderBlock(in FrameHeader header, ReadOnlySpan block) int streamId = _discardBlock.StreamId; _discardBlock.StreamId = 0; + uint reset = _discardReset; + _discardReset = Http2Error.RefusedStream; // Only a new id was refused; nothing of another stream can arrive mid-block to move the // highest. Trailers are not reset: REFUSED_STREAM says the request went unprocessed. if (DecodeHeaderBlock(_discardBlock, discard: true) && streamId > _highestStream) { - ResetStream(streamId, Http2Error.RefusedStream); + ResetStream(streamId, reset); _highestStream = streamId; // refused, but used } } @@ -470,9 +544,7 @@ private void RefuseBody(PendingRequest pending) ? "the request body ran past its content-length" : $"the request body ended {pending.ContentLength - pending.BodyReceived} bytes short of its content-length"); - _responseWindows.Remove(streamId); - ResetStream(streamId, Http2Error.ProtocolError); - ReleaseCreditWaiters(streamId); // a writer parked on credit wakes to find the stream gone + ResetOpenStream(streamId, Http2Error.ProtocolError); } private void TryComplete(PendingRequest pending) @@ -497,8 +569,21 @@ private void TryComplete(PendingRequest pending) private void HandleSettings(in FrameHeader header, ReadOnlySpan payload) { + // RFC 9113 6.5: SETTINGS are the connection's, and an ACK says only that ours were applied. + if (header.StreamId != 0) + { + GoAway(Http2Error.ProtocolError); + return; + } + if ((header.Flags & FrameFlags.Ack) != 0) { + if (payload.Length != 0) + { + GoAway(Http2Error.FrameSizeError); + return; + } + // Our SETTINGS acknowledged: the peer has resized its open streams by them, and sizes new ones by them (RFC 9113 6.9.2). int delta = _options.InitialWindowSize - _initialStreamWindow; if (delta != 0) @@ -527,6 +612,15 @@ private void HandleSettings(in FrameHeader header, ReadOnlySpan payload) switch (id) { + case Http2Setting.EnablePush: + // Nothing here pushes, so the value says nothing to us, but it is a flag (6.5.2). + if (value > 1) + { + GoAway(Http2Error.ProtocolError); + return; + } + break; + case Http2Setting.InitialWindowSize: if (value > int.MaxValue) { @@ -627,6 +721,11 @@ private void HandleWindowUpdate(in FrameHeader header, ReadOnlySpan payloa private void HandlePing(in FrameHeader header, ReadOnlySpan payload) { + if (header.StreamId != 0) + { + GoAway(Http2Error.ProtocolError); // RFC 9113 6.7: a PING is the connection's + return; + } if (payload.Length != 8) { GoAway(Http2Error.FrameSizeError); @@ -644,8 +743,20 @@ private void HandlePing(in FrameHeader header, ReadOnlySpan payload) Stage(frame); } - private void HandleRstStream(in FrameHeader header) + private void HandleRstStream(in FrameHeader header, ReadOnlySpan payload) { + // RFC 9113 6.4: it always names a stream, and carries one error code. + if (header.StreamId == 0) + { + GoAway(Http2Error.ProtocolError); + return; + } + if (payload.Length != 4) + { + GoAway(Http2Error.FrameSizeError); + return; + } + if (_streams.Remove(header.StreamId, out PendingRequest? pending)) { Discard(pending); // the peer gave up; there is nobody to answer diff --git a/src/protocols/ioxide.http2/Http2Connection.Write.cs b/src/protocols/ioxide.http2/Http2Connection.Write.cs index 9ca2d941..acc57af2 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Write.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Write.cs @@ -338,7 +338,9 @@ private void GoAway(uint errorCode) { Span frame = stackalloc byte[FrameHeader.Size + 8]; new FrameHeader(8, FrameType.GoAway, FrameFlags.None, 0).Write(frame); - BinaryPrimitives.WriteUInt32BigEndian(frame[FrameHeader.Size..], 0); // last stream id + // RFC 9113 6.8: the highest stream that was or might be processed, so the client knows which of its + // requests it may retry elsewhere. Refused ones are among them, and REFUSED_STREAM already said so. + BinaryPrimitives.WriteUInt32BigEndian(frame[FrameHeader.Size..], (uint)_highestStream); BinaryPrimitives.WriteUInt32BigEndian(frame[(FrameHeader.Size + 4)..], errorCode); Stage(frame); _failed = true; diff --git a/src/protocols/ioxide.http2/Http2Connection.cs b/src/protocols/ioxide.http2/Http2Connection.cs index c269c524..e9d43840 100644 --- a/src/protocols/ioxide.http2/Http2Connection.cs +++ b/src/protocols/ioxide.http2/Http2Connection.cs @@ -57,6 +57,10 @@ public sealed partial class Http2Connection : IDisposable // with another stream's frames, so one of these is enough. private readonly PendingRequest _discardBlock = new(); + // What a new stream whose block is discarded is reset with once that block is decoded: + // REFUSED_STREAM past the concurrency limit, PROTOCOL_ERROR for one refused as malformed. + private uint _discardReset = Http2Error.RefusedStream; + // The stream whose header block is still open, or 0. private int _headerBlockStream; diff --git a/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs b/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs new file mode 100644 index 00000000..9dfa89d7 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs @@ -0,0 +1,166 @@ +using System.Buffers.Binary; +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// The framing of PRIORITY, PUSH_PROMISE, RST_STREAM, SETTINGS and PING (RFC 9113 6.3-6.7), and the +/// GOAWAY a connection error is answered with (6.8). A malformed control frame is the peer's error, of +/// the stream or of the connection as the RFC names it, and never something taken as it came. +/// +internal static class Http2ControlFrameTests +{ + private const byte Headers = 0x1, Priority = 0x2, RstStream = 0x3, Settings = 0x4, PushPromise = 0x5, Ping = 0x6, GoAway = 0x7; + private const byte EndStream = 0x1, Ack = 0x1, EndHeaders = 0x4, PriorityFlag = 0x20; + + public static void Register(Runner runner) + { + // Each one follows stream 1, served, so the GOAWAY has a stream to name; stream 3 comes after it. + foreach ((string name, byte[] frame, string expected) in new[] + { + ("a PRIORITY on stream 0", Frame(Priority, 0, 0, Dependency(1)), "GOAWAY 1 0x1"), + ("a PRIORITY of four octets on an idle stream", Frame(Priority, 0, 5, [0, 0, 0, 1]), "GOAWAY 1 0x6"), + ("a PRIORITY on an idle stream that depends on itself", Frame(Priority, 0, 5, Dependency(5)), "GOAWAY 1 0x1"), + ("a PUSH_PROMISE", Frame(PushPromise, EndHeaders, 1, [0, 0, 0, 2, 0x82, 0x86, 0x84]), "GOAWAY 1 0x1"), + ("an RST_STREAM on stream 0", Frame(RstStream, 0, 0, [0, 0, 0, 8]), "GOAWAY 1 0x1"), + ("an RST_STREAM of three octets", Frame(RstStream, 0, 1, [0, 0, 8]), "GOAWAY 1 0x6"), + ("a SETTINGS ACK with a payload", Frame(Settings, Ack, 0, [0, 4, 0, 0, 0xff, 0xff]), "GOAWAY 1 0x6"), + ("a SETTINGS on stream 1", Frame(Settings, 0, 1, []), "GOAWAY 1 0x1"), + ("SETTINGS_ENABLE_PUSH 2", Http2FlowControlTests.Settings((0x2, 2)), "GOAWAY 1 0x1"), + ("a PING on stream 1", Frame(Ping, 0, 1, new byte[8]), "GOAWAY 1 0x1"), + ("a DATA on an idle stream", Frame(0x0, 0, 9, [1]), "GOAWAY 1 0x1"), + }) + { + string shape = name; + byte[] bad = frame; + string answer = expected; + runner.Test($"h2 control frames: {shape} ends the connection, and its GOAWAY names the last stream served", () => + { + (List frames, List served) = Exchange(bad); + + Assert.Equal(answer, Errors(frames)); + Assert.True(served.SequenceEqual([1]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + runner.Test("h2 control frames: control: a well-formed PRIORITY, SETTINGS_ENABLE_PUSH 0 and 1, an empty SETTINGS ACK and a PING take nothing down", () => + { + (List frames, List served) = Exchange( + [ + .. Frame(Priority, 0, 1, Dependency(0)), // a closed stream + .. Frame(Priority, 0, 7, Dependency(1)), // an idle one, which it leaves idle + .. Http2FlowControlTests.Settings((0x2, 0)), + .. Http2FlowControlTests.Settings((0x2, 1)), + .. Frame(Settings, Ack, 0, []), + .. Frame(Ping, 0, 0, [1, 2, 3, 4, 5, 6, 7, 8]), + ]); + + Assert.Equal("", Errors(frames)); + Assert.True(served.SequenceEqual([1, 3]), $"the handler was handed: {string.Join(", ", served)}"); + Assert.Equal(1, frames.Count(f => f.Type == Ping && f.Flags == Ack && f.Payload.SequenceEqual(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }))); + }); + + // A request still arriving, so the stream is open when the frame names it. + foreach ((string name, byte[] frame, string expected) in new[] + { + ("a PRIORITY of four octets", Frame(Priority, 0, 1, [0, 0, 0, 3]), "RST_STREAM 1 0x6"), + ("a PRIORITY that depends on its own stream", Frame(Priority, 0, 1, Dependency(1)), "RST_STREAM 1 0x1"), + }) + { + string shape = name; + byte[] bad = frame; + string answer = expected; + runner.Test($"h2 control frames: {shape} resets a request still arriving, and the next is served", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add(request.StreamId); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Frame(Headers, EndHeaders, 1, [0x83, 0x86, 0x84])); // a POST, its body still to come + client.Feed(bad); + client.Feed(Http2FlowControlTests.Get(3)); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal(answer, Errors(frames)); + Assert.True(served.SequenceEqual([3]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + // RFC 7540 5.3.1, which h2spec and nghttp2 still hold a server to. + runner.Test("h2 control frames: HEADERS whose priority depends on their own stream are reset before any handler runs, and HPACK stays in step", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add($"{request.StreamId} {Fields(request)}"); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + byte[] insert = Http2TrailerTests.Field("x-b", "w", index: true); // only counts if the block was decoded + client.Feed(Frame(Headers, EndHeaders | EndStream | PriorityFlag, 1, [.. Dependency(1), 0x82, 0x86, 0x84, .. insert])); + client.Feed(Frame(Headers, EndHeaders | EndStream | PriorityFlag, 3, [.. Dependency(1), 0x82, 0x86, 0x84, 0xBE])); // control: on another stream + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x1", Errors(frames)); + Assert.True(served.SequenceEqual(["3 x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + // Stream 1 served, the frame, then stream 3. + private static (List Frames, List Served) Exchange(byte[] frame) + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add(request.StreamId); + return Http2Response.Text("ok"); + }); + client.SendRequests(1); + List frames = client.Drain(); + client.Feed(frame); + client.Feed(Http2FlowControlTests.Get(3)); + frames.AddRange(client.Drain()); + client.Close(run); + return (frames, served); + } + + private static byte[] Dependency(int streamId) + { + byte[] priority = new byte[5]; + BinaryPrimitives.WriteInt32BigEndian(priority, streamId); + priority[4] = 15; + return priority; + } + + private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) + => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); + + private static string Fields(Http2Request request) + { + var fields = new List(); + foreach (KeyValuePair, ReadOnlyMemory> field in request.Headers.AsSpan()) + { + fields.Add($"{System.Text.Encoding.ASCII.GetString(field.Key.Span)}={System.Text.Encoding.ASCII.GetString(field.Value.Span)}"); + } + return string.Join(' ', fields); + } + + // Every RST_STREAM and GOAWAY, in wire order: a GOAWAY by its last stream id and code. + internal static string Errors(List frames) => string.Join(", ", frames + .Where(f => f.Type is RstStream or GoAway) + .Select(f => f.Type == RstStream + ? $"RST_STREAM {f.StreamId} 0x{BinaryPrimitives.ReadUInt32BigEndian(f.Payload):x}" + : $"GOAWAY {BinaryPrimitives.ReadUInt32BigEndian(f.Payload)} 0x{BinaryPrimitives.ReadUInt32BigEndian(f.Payload.AsSpan(4)):x}")); +} diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index 95146068..a96ff29e 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -50,6 +50,7 @@ private static int Main() Nghttp2EarlyAnswerTests.Register(runner); Http2PeerClosedFlushTests.Register(runner); Http2StreamResetTests.Register(runner); + Http2ControlFrameTests.Register(runner); HpackEncodeTests.Register(runner); QpackStaticEncodeTests.Register(runner); AlpnSelectTests.Register(runner); From 215f19a7ac8dcbd9cff240a39d36607f7317f299 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Tue, 17 Nov 2026 00:45:10 +0000 Subject: [PATCH 2/4] http2: frames on idle, half-closed (remote) and closed streams are refused as RFC 9113 5.1 says, so DATA after END_STREAM no longer reaches a body reader (#640) The connection kept no state for a stream outside _streams, so a frame on a stream that was not open was dropped or served. Now: - Idle (5.1): RST_STREAM and WINDOW_UPDATE on a stream the client has not opened are GOAWAY (PROTOCOL_ERROR), as DATA already was. An even stream counts as idle everywhere, since only a server opens one and this one never does. - Stream ids (5.1.1): HEADERS on an even stream, or on one below the highest opened that was never opened itself, are GOAWAY(PROTOCOL_ERROR). Before, they were decoded and dropped as trailers. - Half-closed (remote) (5.1): DATA or HEADERS after the request's END_STREAM, with the response still going, reset the stream with STREAM_CLOSED, through ResetOpenStream, before anything reaches the body reader. A request with a body reader stayed in _streams after its END_STREAM and its DATA was pushed into a reader that had already ended; ReadAsync hands out queued chunks before it checks the end, so a handler still reading got bytes the client sent after it ended the body. The reset now aborts the reader as the peer's own reset does, so a handler still reading is told the stream is gone. - Closed (5.1): the connection keeps a ring of the 128 streams closed last and how each closed. - The peer closed it, by its END_STREAM (recorded when a buffered request is ready, or when a streamed one is retired with its request ended) or by its RST_STREAM: DATA or HEADERS on it are GOAWAY(STREAM_CLOSED). RFC 9113 makes that a MAY; RFC 7540 made it a MUST, and h2spec holds a server to it. - This side reset it (every ResetStream: REFUSED_STREAM, a malformed request, flow control, the NO_ERROR reset after an early answer) or stopped reading it (a streamed request retired with the upload behind a parked answer): its frames are still decoded where HPACK needs it and dropped, as RFC 9113 5.1 requires. - Not remembered, because it is older than the ring: DATA is dropped as before, and HEADERS are an unexpected stream id. RFC 9113 5.1 lets a receiver bound how long it remembers a stream it reset. WINDOW_UPDATE, RST_STREAM and PRIORITY on a closed stream are still ignored (5.1 allows them for a while). The ring is an int[128] per connection, written once per stream and searched only for a frame on a stream that is not open; an id this side closed is stored negated. h2spec 2.6.0 against a reactor serving h2c with a body-reader handler: the 12 cases of this issue fail on the fix-641 base (5.1/2, 5.1/3, 5.1/5, 5.1/6, 5.1/8, 5.1/9, 5.1/11, 5.1/12, 5.1.1/1, 5.1.1/2, 6.1/2, 6.4/2) and pass with this; 23 failures go to 11, those of #642 and #643. Tests: Http2StreamStateTests, in Unit. RST_STREAM and WINDOW_UPDATE on an idle stream, WINDOW_UPDATE and HEADERS on an even one, HEADERS below the highest opened, and DATA and HEADERS on a stream closed both ways or after the peer's RST_STREAM, each end the connection with the expected GOAWAY; DATA after END_STREAM with the handler still reading resets the stream and the reader hands out none of it; HEADERS after END_STREAM with the answer still to come reset the stream, no answer goes out and HPACK stays in step. Controls: WINDOW_UPDATE, RST_STREAM and PRIORITY on a stream closed both ways, and DATA and trailers on a stream this side reset after an early answer, take nothing down. Run against the fix-641 source every refusal test fails, "expected [GOAWAY 1 0x1], got []" and the like, the reader test with "expected [|reset], got [bodylate]"; the controls pass either way. Changed: Chaos's "h2c: DATA on an idle stream is a PROTOCOL_ERROR" had, as a control, DATA after a stream answered after the client's END_STREAM, served on. That is the closed-by-peer case above, now GOAWAY(STREAM_CLOSED), which the control asserts instead (H2cClient names 0x5). Its other control, DATA on a stream closed by opening a higher one, is unchanged. Suites: Unit 333, Chaos 105, Http 113, none failing. --- .../ioxide.http2/Http2Connection.Frames.cs | 102 ++++++++- .../ioxide.http2/Http2Connection.Write.cs | 1 + src/protocols/ioxide.http2/Http2Connection.cs | 32 +++ tests/Ioxide.Tests.Chaos/H2ChaosTests.cs | 14 +- tests/Ioxide.Tests.Chaos/H2cClient.cs | 1 + .../Http2ControlFrameTests.cs | 2 +- .../Http2StreamStateTests.cs | 194 ++++++++++++++++++ tests/Ioxide.Tests.Unit/Program.cs | 1 + 8 files changed, 334 insertions(+), 13 deletions(-) create mode 100644 tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs diff --git a/src/protocols/ioxide.http2/Http2Connection.Frames.cs b/src/protocols/ioxide.http2/Http2Connection.Frames.cs index ab683076..284e7ab6 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Frames.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Frames.cs @@ -144,7 +144,7 @@ private void HandlePriority(in FrameHeader header, ReadOnlySpan payload) // connection answers for it then. private void RefusePriority(int streamId, uint errorCode) { - if (streamId > _highestStream) + if (IsIdle(streamId)) { GoAway(errorCode); } @@ -171,7 +171,8 @@ private void ResetOpenStream(int streamId, uint errorCode) private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) { - if (header.StreamId == 0) + // RFC 9113 5.1.1: a client opens odd streams only, and a server that pushes nothing has no even ones. + if (header.StreamId == 0 || (header.StreamId & 1) == 0) { GoAway(Http2Error.ProtocolError); return; @@ -209,11 +210,18 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) block = block[5..]; } + bool open = _streams.TryGetValue(header.StreamId, out PendingRequest? pending); + if (!open && header.StreamId <= _highestStream) + { + HandleHeadersOnClosed(header, block); + return; + } + if (selfDependent) { // Refused like a malformed request (see HandlePriority): decoded aside to keep HPACK in // step, and whatever the stream was doing ends. A new one is reset once its block is in. - if (_streams.ContainsKey(header.StreamId)) + if (open) { ResetOpenStream(header.StreamId, Http2Error.ProtocolError); } @@ -223,11 +231,9 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) return; } - if (!_streams.TryGetValue(header.StreamId, out PendingRequest? pending)) + if (!open) { - // An id at or below one already opened opens nothing (RFC 9113 5.1.1) - it is trailers - // for a request already answered - so it is decoded like a refused block, but not reset. - if (header.StreamId <= _highestStream || _streams.Count >= _options.MaxConcurrentStreams) + if (_streams.Count >= _options.MaxConcurrentStreams) { // Past the limit we advertised. The block still has to be DECODED - HPACK is one // stream across the whole connection, so skipping it would desynchronise every @@ -246,6 +252,16 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) // one ends trailers, which come after a body, empty or not. pending.HasBody = (header.Flags & FrameFlags.EndStream) == 0; } + else if (pending!.RequestEnded) + { + // Half-closed (remote): the peer has ended the request, and may send nothing more on it + // but WINDOW_UPDATE, PRIORITY and RST_STREAM (RFC 9113 5.1). Decoded aside, as HPACK + // must be, and nothing of it reaches a handler. + ResetOpenStream(header.StreamId, Http2Error.StreamClosed); + _discardBlock.StreamId = header.StreamId; + DiscardHeaderBlock(header, block); + return; + } else if (pending.BodyReader is { } reader) { // Trailers for a request already being served: its arena backs what the handler holds, @@ -301,6 +317,39 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) } } + /// + /// HEADERS on a stream at or below the highest the client opened, and no longer open (RFC 9113 5.1). + /// One this side reset, or stopped reading, is still on its way, which the peer could not have + /// known: its block is decoded and dropped, as HPACK must be. One the peer itself closed, by its + /// END_STREAM or its RST_STREAM, is its error: of the stream while the response is still going, + /// and of the connection once it has closed both ways. One never opened, or closed too long ago + /// to be remembered, is an unexpected stream id (5.1.1). + /// + private void HandleHeadersOnClosed(in FrameHeader header, ReadOnlySpan block) + { + switch (ClosedHow(header.StreamId)) + { + case ClosedByUs: + _discardBlock.StreamId = header.StreamId; + DiscardHeaderBlock(header, block); + return; + + case ClosedByPeer when _responseWindows.ContainsKey(header.StreamId): + ResetOpenStream(header.StreamId, Http2Error.StreamClosed); + _discardBlock.StreamId = header.StreamId; + DiscardHeaderBlock(header, block); + return; + + case ClosedByPeer: + GoAway(Http2Error.StreamClosed); + return; + + default: + GoAway(Http2Error.ProtocolError); + return; + } + } + /// /// A header block that opens no request - a refused stream's, or trailers: decoded to keep the /// HPACK table in step with the peer, and thrown away. Bounded like any other block, so refusing @@ -435,7 +484,7 @@ private bool DecodeHeaderBlock(PendingRequest pending, bool endsStream = false, private void HandleData(in FrameHeader header, ReadOnlySpan payload) { - if (header.StreamId == 0 || header.StreamId > _highestStream) + if (header.StreamId == 0 || IsIdle(header.StreamId)) { GoAway(Http2Error.ProtocolError); // RFC 9113 6.1: DATA always belongs to a stream, and 5.1: never an idle one return; @@ -478,7 +527,29 @@ private void HandleData(in FrameHeader header, ReadOnlySpan payload) if (!_streams.TryGetValue(header.StreamId, out PendingRequest? pending)) { - return; // done with its request, so owed no credit, behind a buffered answer still parked on credit too + // Past the peer's END_STREAM or its RST_STREAM it is the peer's error (RFC 9113 5.1): of the + // stream while the response is still going, and of the connection once the stream has + // closed both ways. Otherwise this side is done with the request, so the frame is owed no + // credit, behind a buffered answer still parked on credit too. + if (ClosedHow(header.StreamId) == ClosedByPeer) + { + if (_responseWindows.ContainsKey(header.StreamId)) + { + ResetOpenStream(header.StreamId, Http2Error.StreamClosed); + } + else + { + GoAway(Http2Error.StreamClosed); + } + } + return; + } + + // Half-closed (remote): after its END_STREAM, not a byte more reaches the body reader. + if (pending.RequestEnded) + { + ResetOpenStream(header.StreamId, Http2Error.StreamClosed); + return; } // Past its window is a stream error (6.9), and its response stops with it; an empty frame may still end it (6.9.1). @@ -563,6 +634,7 @@ private void TryComplete(PendingRequest pending) // Its window moves with it: a WINDOW_UPDATE finds a stream by _streams only until here. _streams.Remove(pending.StreamId); + RememberClosed(pending.StreamId, ClosedByPeer); // half-closed (remote), and closed once answered _responseWindows[pending.StreamId] = pending.SendWindow; _ready.Enqueue(pending); } @@ -674,6 +746,12 @@ private void HandleWindowUpdate(in FrameHeader header, ReadOnlySpan payloa return; } + if (header.StreamId != 0 && IsIdle(header.StreamId)) + { + GoAway(Http2Error.ProtocolError); // RFC 9113 5.1: nothing but HEADERS and PRIORITY on an idle stream + return; + } + int increment = (int)(BinaryPrimitives.ReadUInt32BigEndian(payload) & 0x7FFFFFFF); if (increment == 0) { @@ -756,9 +834,15 @@ private void HandleRstStream(in FrameHeader header, ReadOnlySpan payload) GoAway(Http2Error.FrameSizeError); return; } + if (IsIdle(header.StreamId)) + { + GoAway(Http2Error.ProtocolError); // RFC 9113 5.1: nothing but HEADERS and PRIORITY on an idle stream + return; + } if (_streams.Remove(header.StreamId, out PendingRequest? pending)) { + RememberClosed(header.StreamId, ClosedByPeer); Discard(pending); // the peer gave up; there is nobody to answer } diff --git a/src/protocols/ioxide.http2/Http2Connection.Write.cs b/src/protocols/ioxide.http2/Http2Connection.Write.cs index acc57af2..5215eecb 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Write.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Write.cs @@ -327,6 +327,7 @@ private void ResetStream(int streamId, uint errorCode) { Discard(pending); } + RememberClosed(streamId, ClosedByUs); // what the peer sends before it hears of this is dropped (RFC 9113 5.1) Span frame = stackalloc byte[FrameHeader.Size + 4]; new FrameHeader(4, FrameType.RstStream, FrameFlags.None, streamId).Write(frame); diff --git a/src/protocols/ioxide.http2/Http2Connection.cs b/src/protocols/ioxide.http2/Http2Connection.cs index e9d43840..0e24a59b 100644 --- a/src/protocols/ioxide.http2/Http2Connection.cs +++ b/src/protocols/ioxide.http2/Http2Connection.cs @@ -68,6 +68,17 @@ public sealed partial class Http2Connection : IDisposable // opens nothing (RFC 9113 5.1.1). private int _highestStream; + // Streams closed lately, and how, for the frames that still arrive on them (RFC 9113 5.1): an id + // the peer closed, by its END_STREAM or its RST_STREAM, and an id this side reset or stopped + // reading, negated. A ring, the newest over the oldest, so it is bounded whatever the peer does, + // and looked up only for a frame on a stream that is not open. RFC 9113 5.1 lets a receiver + // limit how long it remembers a stream it reset; past that, DATA is dropped and HEADERS are an + // unexpected stream id. + private readonly int[] _closedStreams = new int[ClosedStreamMemory]; + private int _closedNext; + private const int ClosedStreamMemory = 128; + private const int ClosedByPeer = 1, ClosedByUs = 2; + // The handler takes a body reader: a request is dispatched at END_HEADERS, its body to follow. private bool _streamBodies; @@ -446,6 +457,11 @@ private void RetireStream(PendingRequest pending) { ResetStream(pending.StreamId, Http2Error.NoError); } + else if (live) + { + // Its END_STREAM is in, or, behind a buffered rest parked on credit, the upload is dropped until the rest's own reset. + RememberClosed(pending.StreamId, pending.RequestEnded ? ClosedByPeer : ClosedByUs); + } Recycle(pending); @@ -456,6 +472,22 @@ private void RetireStream(PendingRequest pending) } } + private void RememberClosed(int streamId, int how) + { + _closedStreams[_closedNext] = how == ClosedByUs ? -streamId : streamId; + _closedNext = (_closedNext + 1) % ClosedStreamMemory; + } + + // ClosedByUs wins: once this side has reset a stream, whatever follows is dropped, whoever ended it first. + private int ClosedHow(int streamId) + { + ReadOnlySpan closed = _closedStreams; + return closed.Contains(-streamId) ? ClosedByUs : closed.Contains(streamId) ? ClosedByPeer : 0; + } + + // RFC 9113 5.1.1: a stream the client has not opened yet, or one only a server could, which this one never does. + private bool IsIdle(int streamId) => streamId > _highestStream || (streamId & 1) == 0; + // Kept unless the pool is full or a peer's header lines grew its lists; only the reactor rents. private void Recycle(PendingRequest pending) { diff --git a/tests/Ioxide.Tests.Chaos/H2ChaosTests.cs b/tests/Ioxide.Tests.Chaos/H2ChaosTests.cs index 2b0c5d8f..46b99bab 100644 --- a/tests/Ioxide.Tests.Chaos/H2ChaosTests.cs +++ b/tests/Ioxide.Tests.Chaos/H2ChaosTests.cs @@ -124,21 +124,29 @@ public static void Register(Runner runner) })); // Control: at or below the highest id opened a stream is closed, not idle - stream 1 by - // opening 3 (RFC 9113 5.1.1), stream 3 once answered - and its DATA is still dropped. + // opening 3 (RFC 9113 5.1.1) - and its DATA is still dropped. Assert.Equal("HEADERS 3", H2cClient.Verdict(port, c => { c.RequestHeadersOnly(streamId: 3, endHeaders: true, endStream: false); c.WriteFrame(0x0, flags: 0, streamId: 1, "x"u8); c.WriteFrame(0x0, flags: 0x1, streamId: 3, []); })); + + // A stream answered after the client's END_STREAM is closed too, but by the client, whose + // DATA after it is its error (RFC 9113 5.1, a MUST in RFC 7540): STREAM_CLOSED, not PROTOCOL_ERROR. using (var client = new H2cClient(port)) { client.Open(); client.Request(streamId: 3); Assert.True(client.AwaitResponse(streamId: 3), "server did not answer stream 3"); client.WriteFrame(0x0, flags: 0x1, streamId: 3, "x"u8); - client.Request(streamId: 5); - Assert.True(client.AwaitResponse(streamId: 5), "DATA on a stream already answered ended the connection"); + string verdict; + do + { + verdict = client.AwaitVerdict(); // past the rest of the answer + } + while (verdict == "DATA 3"); + Assert.Equal("GOAWAY STREAM_CLOSED", verdict); } AssertServes(port); diff --git a/tests/Ioxide.Tests.Chaos/H2cClient.cs b/tests/Ioxide.Tests.Chaos/H2cClient.cs index 82f46f51..8954035e 100644 --- a/tests/Ioxide.Tests.Chaos/H2cClient.cs +++ b/tests/Ioxide.Tests.Chaos/H2cClient.cs @@ -544,6 +544,7 @@ private static string ErrorName(byte[] payload, int at) { 0x1 => "PROTOCOL_ERROR", 0x3 => "FLOW_CONTROL_ERROR", + 0x5 => "STREAM_CLOSED", 0x7 => "REFUSED_STREAM", 0x9 => "COMPRESSION_ERROR", _ => $"0x{code:x}", diff --git a/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs b/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs index 9dfa89d7..7d6c7a87 100644 --- a/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs @@ -147,7 +147,7 @@ private static byte[] Dependency(int streamId) private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); - private static string Fields(Http2Request request) + internal static string Fields(Http2Request request) { var fields = new List(); foreach (KeyValuePair, ReadOnlyMemory> field in request.Headers.AsSpan()) diff --git a/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs b/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs new file mode 100644 index 00000000..559b72d0 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs @@ -0,0 +1,194 @@ +using System.Text; +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// Frames on streams that are not open (RFC 9113 5.1, 5.1.1). On an idle stream, or one a client +/// could never have opened, they are a connection error. After the peer's END_STREAM they are its +/// error, of the stream while the response is still going and of the connection once it is closed, +/// and nothing of them reaches a body reader. After this side's own reset they are dropped: the peer +/// sent them before it could know. +/// +internal static class Http2StreamStateTests +{ + private const byte Data = 0x0, Headers = 0x1, RstStream = 0x3, WindowUpdate = 0x8; + private const byte EndStream = 0x1, EndHeaders = 0x4; + + public static void Register(Runner runner) + { + // After stream 1, served and closed both ways. + foreach ((string name, byte[] frame, string expected) in new[] + { + ("an RST_STREAM on an idle stream", Frame(RstStream, 0, 3, [0, 0, 0, 8]), "GOAWAY 1 0x1"), + ("a WINDOW_UPDATE on an idle stream", Frame(WindowUpdate, 0, 3, [0, 0, 0, 1]), "GOAWAY 1 0x1"), + ("a WINDOW_UPDATE on an even stream, which only a server could open,", Frame(WindowUpdate, 0, 2, [0, 0, 0, 1]), "GOAWAY 1 0x1"), + ("HEADERS on an even stream", Frame(Headers, EndHeaders | EndStream, 2, [0x82, 0x86, 0x84]), "GOAWAY 1 0x1"), + ("a DATA after the stream closed both ways", Frame(Data, EndStream, 1, [1]), "GOAWAY 1 0x5"), + ("HEADERS after the stream closed both ways", Frame(Headers, EndHeaders | EndStream, 1, [0x82, 0x86, 0x84]), "GOAWAY 1 0x5"), + }) + { + string shape = name; + byte[] bad = frame; + string answer = expected; + runner.Test($"h2 stream states: {shape} ends the connection", () => + { + (List frames, List served) = Exchange([.. Http2FlowControlTests.Get(1)], bad); + + Assert.Equal(answer, Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([1]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + runner.Test("h2 stream states: control: a WINDOW_UPDATE, an RST_STREAM and a PRIORITY on a stream closed both ways take nothing down", () => + { + (List frames, List served) = Exchange([.. Http2FlowControlTests.Get(1)], + [ + .. Frame(WindowUpdate, 0, 1, [0, 0, 0, 1]), // sent before the client saw the END_STREAM, as RFC 9113 5.1 allows + .. Frame(RstStream, 0, 1, [0, 0, 0, 8]), + .. Frame(0x2, 0, 1, [0, 0, 0, 0, 15]), + ]); + + Assert.Equal("", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([1, 7]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 stream states: HEADERS on a stream below one already opened, and never opened itself, end the connection", () => + { + (List frames, List served) = Exchange([.. Http2FlowControlTests.Get(5)], Http2FlowControlTests.Get(1)); + + Assert.Equal("GOAWAY 5 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([5]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 stream states: a DATA after the peer's RST_STREAM ends the connection", () => + { + (List frames, List served) = Exchange([], [.. Post(1), .. Frame(RstStream, 0, 1, [0, 0, 0, 8]), .. Frame(Data, EndStream, 1, [1])]); + + Assert.Equal("GOAWAY 1 0x5", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(0, served.Count); + }); + + // The issue's own case: no content-length, so nothing else would tell the reader the bytes are not the body's. + runner.Test("h2 stream states: a DATA after END_STREAM, with the handler still reading, resets the stream, and its body reader hands out none of it", () => + { + using var client = new StrictClient(); + var gate = new TaskCompletionSource(); + var read = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + Task run = client.Connection.RunAsync(async (request, body) => + { + if (request.StreamId != 1) + { + return Http2Response.Text("ok"); + } + await gate.Task; // reads only once every frame below is in + var bytes = new List(); + try + { + for (ReadOnlyMemory chunk = await body.ReadAsync(); !chunk.IsEmpty; chunk = await body.ReadAsync()) + { + bytes.AddRange(chunk.ToArray()); + } + } + catch (IOException) + { + bytes.AddRange("|reset"u8.ToArray()); // as after the peer's own reset: the stream is gone under it + } + read.TrySetResult(Encoding.ASCII.GetString([.. bytes])); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed([.. Post(1), .. Frame(Data, EndStream, 1, "body"u8), .. Frame(Data, 0, 1, "late"u8)]); + client.Feed(Http2FlowControlTests.Get(3)); // the connection goes on + gate.SetResult(); + List frames = client.Drain(); + + Assert.True(read.Task.Wait(2_000), "the handler never finished reading"); + Assert.Equal("|reset", read.Task.Result); + Assert.Equal("RST_STREAM 1 0x5", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 3)); + client.Close(run); + }); + + runner.Test("h2 stream states: HEADERS after END_STREAM, with the answer still to come, reset the stream, and HPACK stays in step", () => + { + using var client = new StrictClient(); + var gate = new TaskCompletionSource(); + var served = new List(); + Task run = client.Connection.RunAsync(async request => + { + served.Add($"{request.StreamId} {Http2ControlFrameTests.Fields(request)}"); + if (request.StreamId == 1) + { + await gate.Task; + } + return Http2Response.Text("ok"); + }); + client.SendRequests(1); // its handler parks: half-closed (remote), the answer still to come + client.Drain(); + + client.Feed(Frame(Headers, EndHeaders | EndStream, 1, Http2TrailerTests.Field("x-b", "w", index: true))); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + gate.SetResult(); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x5", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(0, frames.Count(f => f.Type == Headers && f.StreamId == 1)); // the reset ends the answer too + Assert.True(served.SequenceEqual(["1 ", "3 x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + // RFC 9113 5.1: what follows this side's own reset was sent before the peer knew of it. + runner.Test("h2 stream states: control: DATA and trailers on a stream this side reset are dropped, and the connection goes on", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync((request, body) => + { + served.Add(request.StreamId); + return ValueTask.FromResult(Http2Response.Text("early")); // answered unread: the upload is reset with NO_ERROR + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Post(1)); + client.Feed(Frame(Data, 0, 1, new byte[10])); + client.Feed(Frame(Headers, EndHeaders | EndStream, 1, Http2TrailerTests.Field("x-b", "w", index: true))); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x0", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([1, 3]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + // The opening frames, then the one under test, then stream 7's GET. + private static (List Frames, List Served) Exchange(byte[] before, byte[] frame) + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add(request.StreamId); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + List frames = client.Drain(); + client.Feed(before); + frames.AddRange(client.Drain()); + client.Feed(frame); + client.Feed(Http2FlowControlTests.Get(7)); + frames.AddRange(client.Drain()); + client.Close(run); + return (frames, served); + } + + // A POST, its body still to come. + private static byte[] Post(int streamId) => Frame(Headers, EndHeaders, streamId, [0x83, 0x86, 0x84]); + + private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) + => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); +} diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index a96ff29e..efc71fe0 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); Http2ControlFrameTests.Register(runner); + Http2StreamStateTests.Register(runner); HpackEncodeTests.Register(runner); QpackStaticEncodeTests.Register(runner); AlpnSelectTests.Register(runner); From 407af89a731f86d07af64c892d572e9988582001 Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Tue, 17 Nov 2026 18:58:22 +0000 Subject: [PATCH 3/4] http2: a malformed PRIORITY on a stream that is not live ends the connection instead of resetting a stream that may never have been opened (#641) Review of #647. RefusePriority reset any stream at or below the highest the client opened. That includes an even stream, which only a server opens, and an odd one the client skipped, and RFC 9113 6.4 forbids an RST_STREAM on a stream never opened. Nothing on this branch can tell a skipped stream from one closed long ago, so only a stream that is still live (still arriving, or still being answered) is reset now. A PRIORITY on any other stream is answered with GOAWAY of the same code. Tests, Http2PriorityResetTests in Unit: - A PRIORITY of four octets on an even stream, and on an odd one skipped below the highest, ends the connection. Against the previous commit's source they fail with "expected [GOAWAY 5 0x6], got [RST_STREAM 2 0x6]" (and 3). - Coverage the review asked for, of what the previous commit already did, so these fail against origin/main's source and pass against the previous commit's: - A PRIORITY of four octets, and trailers whose priority depends on their own stream, on a stream whose handler has a body read parked and a flush parked on credit: the read raises, the flush wakes and reports the peer gone, only the RST_STREAM goes out on the stream, and a self-dependent trailer block still keeps HPACK in step. With ResetOpenStream's ReleaseCreditWaiters removed, both fail with "the flush parked on credit never woke". - A self-dependent HEADERS whose block continues in CONTINUATION is reset once the block is in, and HPACK stays in step. - Self-dependent trailers reset a buffered request that is still arriving. Http2ControlFrameTests.Fields is internal now, so these tests can use it. Suites: Unit 327, Chaos 105, Http 113, none failing. --- .../ioxide.http2/Http2Connection.Frames.cs | 12 +- .../Http2ControlFrameTests.cs | 2 +- .../Http2PriorityResetTests.cs | 162 ++++++++++++++++++ tests/Ioxide.Tests.Unit/Program.cs | 1 + 4 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 tests/Ioxide.Tests.Unit/Http2PriorityResetTests.cs diff --git a/src/protocols/ioxide.http2/Http2Connection.Frames.cs b/src/protocols/ioxide.http2/Http2Connection.Frames.cs index ab683076..3d4488ea 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Frames.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Frames.cs @@ -140,17 +140,19 @@ private void HandlePriority(in FrameHeader header, ReadOnlySpan payload) } } - // A stream error, but PRIORITY may name an idle stream, which no RST_STREAM may (RFC 9113 6.4): the - // connection answers for it then. + // A stream error, but PRIORITY may name a stream never opened - an idle one, an even one, or one + // skipped below the highest - which no RST_STREAM may name (RFC 9113 6.4), and which nothing here + // tells from one closed long ago. Only a stream still live is reset; for any other the connection + // answers. private void RefusePriority(int streamId, uint errorCode) { - if (streamId > _highestStream) + if (_streams.ContainsKey(streamId) || _responseWindows.ContainsKey(streamId)) { - GoAway(errorCode); + ResetOpenStream(streamId, errorCode); } else { - ResetOpenStream(streamId, errorCode); + GoAway(errorCode); } } diff --git a/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs b/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs index 9dfa89d7..7d6c7a87 100644 --- a/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs @@ -147,7 +147,7 @@ private static byte[] Dependency(int streamId) private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); - private static string Fields(Http2Request request) + internal static string Fields(Http2Request request) { var fields = new List(); foreach (KeyValuePair, ReadOnlyMemory> field in request.Headers.AsSpan()) diff --git a/tests/Ioxide.Tests.Unit/Http2PriorityResetTests.cs b/tests/Ioxide.Tests.Unit/Http2PriorityResetTests.cs new file mode 100644 index 00000000..025e582f --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2PriorityResetTests.cs @@ -0,0 +1,162 @@ +using System.Buffers.Binary; +using System.Text; +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// The stream errors a PRIORITY, or the priority fields of a HEADERS, can raise (RFC 9113 6.3, RFC 7540 +/// 5.3.1), on a stream in each state it can be in: never opened, being decoded, or being served by a +/// handler that is reading its body and parked on credit for its response. +/// +internal static class Http2PriorityResetTests +{ + private const byte Headers = 0x1, Priority = 0x2, Continuation = 0x9; + private const byte EndStream = 0x1, EndHeaders = 0x4, PriorityFlag = 0x20; + + public static void Register(Runner runner) + { + // RFC 9113 6.4: no RST_STREAM may name a stream that was never opened. + foreach ((string name, int streamId) in new[] { ("an even stream", 2), ("an odd stream skipped below the highest", 3) }) + { + string shape = name; + int stream = streamId; + runner.Test($"h2 priority: a PRIORITY of four octets on {shape} ends the connection rather than reset a stream never opened", () => + { + using var client = new StrictClient(); + Task run = client.Connection.RunAsync(_ => Http2Response.Text("ok")); + client.SendRequests(1, 5); + List frames = client.Drain(); + client.Feed(Frame(Priority, 0, stream, [0, 0, 0, 1])); + frames.AddRange(client.Drain()); + client.Close(run); + + Assert.Equal("GOAWAY 5 0x6", Http2ControlFrameTests.Errors(frames)); + }); + } + + // Each frame lands while the handler has a read parked on the body and a flush parked on credit. + byte[] insert = Http2TrailerTests.Field("x-b", "w", index: true); + foreach ((string name, byte[] frame, string expected) in new[] + { + ("a PRIORITY of four octets", Frame(Priority, 0, 1, [0, 0, 0, 3]), "RST_STREAM 1 0x6"), + ("trailers whose priority depends on their own stream", Frame(Headers, EndHeaders | EndStream | PriorityFlag, 1, [.. Dependency(1), .. insert]), "RST_STREAM 1 0x1"), + }) + { + string shape = name; + byte[] bad = frame; + string answer = expected; + runner.Test($"h2 priority: {shape} on a stream being served ends its read, wakes its flush parked on credit to the peer gone, and nothing more goes out on it", () => + { + using var client = new StrictClient(); + var read = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var flushed = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var served = new List(); + Task run = client.Connection.RunAsync(async (request, body, writer) => + { + served.Add($"{request.StreamId} {Http2ControlFrameTests.Fields(request)}"); + writer.WriteHeaders(new Http2Response { Status = 200 }); + if (request.StreamId != 1) + { + return; + } + _ = ReadAsync(body, read); + writer.GetSpan(70_000)[..70_000].Fill((byte)'x'); // past the peer's 65535-byte window + writer.Advance(70_000); + flushed.TrySetResult((await writer.FlushAsync()).IsCompleted ? "peer gone" : "flushed"); + }); + + client.ReleaseFlush(); // server SETTINGS + client.Feed([.. Http2FlowControlTests.Open(), .. Frame(Headers, EndHeaders, 1, [0x83, 0x86, 0x84])]); + client.Drain(); + Assert.True(!read.Task.IsCompleted && !flushed.Task.IsCompleted, "precondition: the handler was meant to be parked on its read and its flush"); + + client.Feed(bad); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, .. (bad[3] == Headers ? (byte[])[0xBE] : [])])); + List frames = client.Drain(); + + Assert.True(read.Task.Wait(2_000), "the read parked on the body never woke"); + Assert.True(read.Task.Result.StartsWith("IOException"), $"the read ended with: {read.Task.Result}"); + Assert.True(flushed.Task.Wait(2_000), "the flush parked on credit never woke"); + Assert.Equal("peer gone", flushed.Task.Result); + Assert.Equal(answer, Http2ControlFrameTests.Errors(frames)); + Assert.Equal(0, frames.Count(f => f.StreamId == 1 && f.Type != 0x3)); + Assert.Equal(bad[3] == Headers ? "3 x-b=w" : "3 ", served[^1]); + client.Close(run); + }); + } + + runner.Test("h2 priority: HEADERS depending on their own stream, the block continued in CONTINUATION, are reset once it is in, and HPACK stays in step", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add($"{request.StreamId} {Http2ControlFrameTests.Fields(request)}"); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Frame(Headers, EndStream | PriorityFlag, 1, [.. Dependency(1), 0x82, 0x86])); + client.Feed(Frame(Continuation, EndHeaders, 1, [0x84, .. Http2TrailerTests.Field("x-b", "w", index: true)])); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual(["3 x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 priority: trailers depending on their own stream reset a buffered request still arriving, and it is never served", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync(request => + { + served.Add(request.StreamId); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Frame(Headers, EndHeaders, 1, [0x83, 0x86, 0x84])); + client.Feed(Http2FlowControlTests.FrameBytes(0x0, 0, 1, new byte[4])); + client.Feed(Frame(Headers, EndHeaders | EndStream | PriorityFlag, 1, [.. Dependency(1), .. Http2TrailerTests.Field("x-checksum", "abc")])); + client.Feed(Http2FlowControlTests.Get(3)); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([3]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + private static async Task ReadAsync(Http2BodyReader body, TaskCompletionSource outcome) + { + try + { + var bytes = new List(); + for (ReadOnlyMemory chunk = await body.ReadAsync(); !chunk.IsEmpty; chunk = await body.ReadAsync()) + { + bytes.AddRange(chunk.ToArray()); + } + outcome.TrySetResult($"ended after {Encoding.ASCII.GetString([.. bytes])}"); + } + catch (Exception e) + { + outcome.TrySetResult(e.GetType().Name); + } + } + + private static byte[] Dependency(int streamId) + { + byte[] priority = new byte[5]; + BinaryPrimitives.WriteInt32BigEndian(priority, streamId); + priority[4] = 15; + return priority; + } + + private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) + => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); +} diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index a96ff29e..9e789a96 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); Http2ControlFrameTests.Register(runner); + Http2PriorityResetTests.Register(runner); HpackEncodeTests.Register(runner); QpackStaticEncodeTests.Register(runner); AlpnSelectTests.Register(runner); From 301902b43c3abdc8aead1055eea719915e5ed42a Mon Sep 17 00:00:00 2001 From: Diogo Martins Date: Tue, 17 Nov 2026 19:06:57 +0000 Subject: [PATCH 4/4] http2: a stream past the closed-stream ring takes the peer's late frames instead of ending the connection, and DATA after the peer's RST_STREAM resets the stream (#640) Review of #648. The ring of 128 closed streams decided too much. Suppose this side reset stream A (the NO_ERROR reset after an early answer, REFUSED_STREAM, a content-length mismatch, flow control) or stopped reading it behind a parked answer. With up to MaxConcurrentStreams (default 100, up to 1000) streams in flight, more than 128 later closes evict A. Its trailers, already in flight, then fell to the never-opened branch and got GOAWAY(PROTOCOL_ERROR), which killed a connection that had done nothing wrong. Before #640 they were decoded and dropped. A miss in the ring is no longer read as "never opened". The connection now records the ids a client skips: when a stream opens above the next id expected, the range in between goes into a second ring of 16 ranges (a long each, low id over high id). Only an id in one of those ranges is an unexpected stream id (RFC 9113 5.1.1). Any other stream at or below the highest that is neither open nor remembered is treated as one this side reset: its DATA and HEADERS are dropped and its blocks decoded, so a client is never refused for frames that were only late. A client that skips ids more than 16 times keeps only the newest ranges, and HEADERS on an older skipped id is dropped rather than refused. Neither ring needed sizing from MaxConcurrentStreams for that guarantee. DATA or HEADERS after the peer's own RST_STREAM now reset the stream with STREAM_CLOSED rather than end the connection, as RFC 9113 5.1 says for a stream closed by RST_STREAM (h2spec accepts either). After that reset the stream is this side's, so a second frame is dropped. The ring therefore records three kinds: an id with the kind above it in a long (ClosedByPeer, ClosedByPeerReset, ClosedByUs), and ClosedByUs still wins over the others. DATA and HEADERS on a stream the peer ended are refused through one helper, RefuseOnPeerClosed. Tests: - Changed: "a DATA after the peer's RST_STREAM" now expects RST_STREAM 1 STREAM_CLOSED, once for two DATA frames, with the connection going on. Against the previous source: "expected [RST_STREAM 1 0x5], got [GOAWAY 1 0x5]". - New, Http2ClosedStreamTests in Unit: - Trailers on a stream answered early, after 131 other closes, are dropped and the next stream is served. Against the previous source: "got [RST_STREAM 1 0x0, GOAWAY 263 0x1]". - Guards for behaviour the previous commit had, each confirmed red by a temporary mutation of this worktree's source and then restored: - DATA on a stream the peer ended and this side then reset is dropped (ClosedByUs wins). With the precedence reversed: "got [RST_STREAM 1 0x6, GOAWAY 1 0x5]". - DATA and trailers behind an answer parked on credit are dropped and the answer still ends whole. With RetireStream recording that stream as ClosedByPeer: a GOAWAY appears. - HEADERS continued in CONTINUATION on a stream this side reset, and on one the peer ended with its answer still to come, keep HPACK in step. Without the discard block's stream id: "GOAWAY 1 0x1" on both. - Not tested: DATA that is dropped is still charged to the connection window. That code is unchanged, it runs before the stream lookup, and the connection window opens to 2^31-1, so observing a top-up would take about 1.5 GiB of DATA. Suites: Unit 344, Chaos 105, Http 113, none failing. --- .../ioxide.http2/Http2Connection.Frames.cs | 81 +++++----- src/protocols/ioxide.http2/Http2Connection.cs | 63 ++++++-- .../Http2ClosedStreamTests.cs | 140 ++++++++++++++++++ .../Http2StreamStateTests.cs | 9 +- tests/Ioxide.Tests.Unit/Program.cs | 1 + 5 files changed, 240 insertions(+), 54 deletions(-) create mode 100644 tests/Ioxide.Tests.Unit/Http2ClosedStreamTests.cs diff --git a/src/protocols/ioxide.http2/Http2Connection.Frames.cs b/src/protocols/ioxide.http2/Http2Connection.Frames.cs index 6badc4d6..dbe56bf5 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Frames.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Frames.cs @@ -248,7 +248,7 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) pending = RentPending(header.StreamId); _streams[header.StreamId] = pending; - _highestStream = header.StreamId; + OpenStreamId(header.StreamId); // Only the block that opens a stream can say there is no body. An END_STREAM on a later // one ends trailers, which come after a body, empty or not. @@ -321,34 +321,46 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) /// /// HEADERS on a stream at or below the highest the client opened, and no longer open (RFC 9113 5.1). - /// One this side reset, or stopped reading, is still on its way, which the peer could not have - /// known: its block is decoded and dropped, as HPACK must be. One the peer itself closed, by its - /// END_STREAM or its RST_STREAM, is its error: of the stream while the response is still going, - /// and of the connection once it has closed both ways. One never opened, or closed too long ago - /// to be remembered, is an unexpected stream id (5.1.1). + /// One the peer itself closed is its error (). One the client + /// skipped, and so never opened, is an unexpected stream id (5.1.1). Any other - one this side + /// reset or stopped reading, or one closed too long ago to be remembered - is still on its way, + /// which the peer could not have known. A block not refused with the connection is decoded and + /// dropped, as HPACK must be. /// private void HandleHeadersOnClosed(in FrameHeader header, ReadOnlySpan block) { - switch (ClosedHow(header.StreamId)) + int how = ClosedHow(header.StreamId); + if (how is ClosedByPeer or ClosedByPeerReset) { - case ClosedByUs: - _discardBlock.StreamId = header.StreamId; - DiscardHeaderBlock(header, block); - return; - - case ClosedByPeer when _responseWindows.ContainsKey(header.StreamId): - ResetOpenStream(header.StreamId, Http2Error.StreamClosed); - _discardBlock.StreamId = header.StreamId; - DiscardHeaderBlock(header, block); - return; + RefuseOnPeerClosed(header.StreamId, how); + } + else if (how == 0 && NeverOpened(header.StreamId)) + { + GoAway(Http2Error.ProtocolError); + } - case ClosedByPeer: - GoAway(Http2Error.StreamClosed); - return; + if (!_failed) + { + _discardBlock.StreamId = header.StreamId; + DiscardHeaderBlock(header, block); + } + } - default: - GoAway(Http2Error.ProtocolError); - return; + /// + /// DATA or HEADERS on a stream the peer closed are its error (RFC 9113 5.1), of the stream after its + /// RST_STREAM or while the response is still going, and of the connection once its END_STREAM and + /// ours have closed the stream both ways - a MAY in RFC 9113, a MUST in RFC 7540. After the reset + /// the stream is this side's, so what follows is dropped. + /// + private void RefuseOnPeerClosed(int streamId, int how) + { + if (how == ClosedByPeerReset || _responseWindows.ContainsKey(streamId)) + { + ResetOpenStream(streamId, Http2Error.StreamClosed); + } + else + { + GoAway(Http2Error.StreamClosed); } } @@ -382,7 +394,7 @@ private void DiscardHeaderBlock(in FrameHeader header, ReadOnlySpan block) if (DecodeHeaderBlock(_discardBlock, discard: true) && streamId > _highestStream) { ResetStream(streamId, reset); - _highestStream = streamId; // refused, but used + OpenStreamId(streamId); // refused, but used } } @@ -529,20 +541,13 @@ private void HandleData(in FrameHeader header, ReadOnlySpan payload) if (!_streams.TryGetValue(header.StreamId, out PendingRequest? pending)) { - // Past the peer's END_STREAM or its RST_STREAM it is the peer's error (RFC 9113 5.1): of the - // stream while the response is still going, and of the connection once the stream has - // closed both ways. Otherwise this side is done with the request, so the frame is owed no - // credit, behind a buffered answer still parked on credit too. - if (ClosedHow(header.StreamId) == ClosedByPeer) + // Past the peer's END_STREAM or its RST_STREAM it is the peer's error. Otherwise this side is + // done with the request, so the frame is owed no credit, behind a buffered answer still + // parked on credit too. + int how = ClosedHow(header.StreamId); + if (how is ClosedByPeer or ClosedByPeerReset) { - if (_responseWindows.ContainsKey(header.StreamId)) - { - ResetOpenStream(header.StreamId, Http2Error.StreamClosed); - } - else - { - GoAway(Http2Error.StreamClosed); - } + RefuseOnPeerClosed(header.StreamId, how); } return; } @@ -844,7 +849,7 @@ private void HandleRstStream(in FrameHeader header, ReadOnlySpan payload) if (_streams.Remove(header.StreamId, out PendingRequest? pending)) { - RememberClosed(header.StreamId, ClosedByPeer); + RememberClosed(header.StreamId, ClosedByPeerReset); Discard(pending); // the peer gave up; there is nobody to answer } diff --git a/src/protocols/ioxide.http2/Http2Connection.cs b/src/protocols/ioxide.http2/Http2Connection.cs index 0e24a59b..48ac94b4 100644 --- a/src/protocols/ioxide.http2/Http2Connection.cs +++ b/src/protocols/ioxide.http2/Http2Connection.cs @@ -68,16 +68,23 @@ public sealed partial class Http2Connection : IDisposable // opens nothing (RFC 9113 5.1.1). private int _highestStream; - // Streams closed lately, and how, for the frames that still arrive on them (RFC 9113 5.1): an id - // the peer closed, by its END_STREAM or its RST_STREAM, and an id this side reset or stopped - // reading, negated. A ring, the newest over the oldest, so it is bounded whatever the peer does, - // and looked up only for a frame on a stream that is not open. RFC 9113 5.1 lets a receiver - // limit how long it remembers a stream it reset; past that, DATA is dropped and HEADERS are an - // unexpected stream id. - private readonly int[] _closedStreams = new int[ClosedStreamMemory]; + // Streams closed lately, and how, for the frames that still arrive on them (RFC 9113 5.1): each an + // id with its ClosedBy kind above it. A ring, the newest over the oldest, so it is bounded whatever + // the peer does, and looked up only for a frame on a stream that is not open. RFC 9113 5.1 lets a + // receiver limit how long it remembers a stream it reset, and a stream past the ring is treated + // as one this side reset: its DATA and HEADERS are dropped, so a client whose frames were simply + // late is never refused for them. + private readonly long[] _closedStreams = new long[ClosedStreamMemory]; private int _closedNext; private const int ClosedStreamMemory = 128; - private const int ClosedByPeer = 1, ClosedByUs = 2; + private const int ClosedByPeer = 1, ClosedByPeerReset = 2, ClosedByUs = 3; + + // Ranges of ids the client skipped as it opened higher ones, so never opened: HEADERS on one is an + // unexpected stream id (RFC 9113 5.1.1). A ring of the latest, packed low id over high id; a + // client that skips more often than it holds is let off rather than refused. + private readonly long[] _skippedStreams = new long[SkippedRanges]; + private int _skippedNext; + private const int SkippedRanges = 16; // The handler takes a body reader: a request is dispatched at END_HEADERS, its body to follow. private bool _streamBodies; @@ -474,15 +481,47 @@ private void RetireStream(PendingRequest pending) private void RememberClosed(int streamId, int how) { - _closedStreams[_closedNext] = how == ClosedByUs ? -streamId : streamId; + _closedStreams[_closedNext] = ((long)how << 32) | (uint)streamId; _closedNext = (_closedNext + 1) % ClosedStreamMemory; } - // ClosedByUs wins: once this side has reset a stream, whatever follows is dropped, whoever ended it first. + // ClosedByUs wins: once this side has reset a stream, whatever follows is dropped, whoever ended it + // first. Then the peer's reset over its END_STREAM. Zero for a stream past the ring, or never opened. private int ClosedHow(int streamId) { - ReadOnlySpan closed = _closedStreams; - return closed.Contains(-streamId) ? ClosedByUs : closed.Contains(streamId) ? ClosedByPeer : 0; + ReadOnlySpan closed = _closedStreams; + for (int how = ClosedByUs; how >= ClosedByPeer; how--) + { + if (closed.Contains(((long)how << 32) | (uint)streamId)) + { + return how; + } + } + return 0; + } + + // A new stream: every id the client skipped to reach it is one it can never open (RFC 9113 5.1.1). + private void OpenStreamId(int streamId) + { + int next = _highestStream == 0 ? 1 : _highestStream + 2; + if (streamId > next) + { + _skippedStreams[_skippedNext] = ((long)next << 32) | (uint)(streamId - 2); + _skippedNext = (_skippedNext + 1) % SkippedRanges; + } + _highestStream = streamId; + } + + private bool NeverOpened(int streamId) + { + foreach (long range in _skippedStreams) + { + if (range != 0 && streamId >= (int)(range >> 32) && streamId <= (int)range) + { + return true; + } + } + return false; } // RFC 9113 5.1.1: a stream the client has not opened yet, or one only a server could, which this one never does. diff --git a/tests/Ioxide.Tests.Unit/Http2ClosedStreamTests.cs b/tests/Ioxide.Tests.Unit/Http2ClosedStreamTests.cs new file mode 100644 index 00000000..3d9f9c4b --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2ClosedStreamTests.cs @@ -0,0 +1,140 @@ +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// What the connection remembers of closed streams, and what it does when it does not. A stream this +/// side reset or stopped reading takes the peer's late frames without complaint (RFC 9113 5.1), and so +/// does one closed too long ago to be remembered: a client whose frames were only late is never +/// refused for them. Each block is still decoded, so HPACK stays in step. +/// +internal static class Http2ClosedStreamTests +{ + private const byte Data = 0x0, Headers = 0x1, Priority = 0x2, Continuation = 0x9; + private const byte EndStream = 0x1, EndHeaders = 0x4; + + public static void Register(Runner runner) + { + runner.Test("h2 closed streams: trailers on a stream answered early, after more closes than the connection remembers, are dropped and the connection goes on", () => + { + using var client = new StrictClient(); + var served = new List(); + Task run = client.Connection.RunAsync((request, _) => + { + served.Add(request.StreamId); + return ValueTask.FromResult(Http2Response.Text("ok")); // answered unread: stream 1's upload is reset with NO_ERROR + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Post(1)); + for (int stream = 3; stream <= 263; stream += 2) + { + client.Feed(Http2FlowControlTests.Get(stream)); // 131 closes, past what the connection remembers + } + client.Feed(Frame(Headers, EndHeaders | EndStream, 1, Insert)); // stream 1's trailers, sent before the client saw the reset + client.Feed(Frame(Headers, EndHeaders | EndStream, 265, [0x82, 0x86, 0x84, 0xBE])); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x0", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(133, served.Count); + Assert.Equal(265, served[^1]); + }); + + // A buffered request ready, so closed by the peer's END_STREAM, then reset by this side: the later wins. + runner.Test("h2 closed streams: DATA on a stream the peer ended and this side then reset is dropped, not refused with the connection", () => + { + using var client = new StrictClient(); + var gate = new TaskCompletionSource(); + Task run = client.Connection.RunAsync(async request => + { + if (request.StreamId == 1) + { + await gate.Task; + } + return Http2Response.Text("ok"); + }); + client.SendRequests(1); + client.Drain(); + + client.Feed(Frame(Priority, 0, 1, [0, 0, 0, 3])); // a stream error: this side resets it + client.Feed(Frame(Data, EndStream, 1, [1])); + client.Feed(Http2FlowControlTests.Get(3)); + gate.SetResult(); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal("RST_STREAM 1 0x6", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 3)); + }); + + // RetireStream with the answer parked on the client's credit: no reset yet, but the upload is dropped. + runner.Test("h2 closed streams: DATA and trailers behind an answer parked on credit are dropped, and the answer still ends whole", () => + { + using var client = new StrictClient(); + Task run = client.Connection.RunAsync((request, _) => ValueTask.FromResult(request.StreamId == 1 + ? new Http2Response { Status = 200, Body = new byte[70_000] } // past the client's 65535-byte window, unread + : Http2Response.Text("ok"))); + client.SendRequests(); + List frames = client.Drain(); + + client.Feed(Post(1)); + frames.AddRange(client.Drain()); + client.Feed(Frame(Data, 0, 1, new byte[10])); + client.Feed(Frame(Headers, EndHeaders | EndStream, 1, Insert)); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + frames.AddRange(client.Drain()); + client.SendWindowUpdate(1, 70_000); + client.SendWindowUpdate(0, 70_000); + frames.AddRange(client.Drain()); + client.Close(run); + + Assert.Equal(0, frames.Count(f => f.Type == 0x7)); + Assert.Equal(70_000, Ioxide.Tests.Frame.Body(frames, 1).Length); + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 3)); + }); + + foreach (bool halfClosed in new[] { false, true }) + { + bool answering = halfClosed; + runner.Test($"h2 closed streams: HEADERS continued in CONTINUATION on a stream {(answering ? "the peer ended, its answer still to come, reset the stream" : "this side reset are dropped")}, and HPACK stays in step", () => + { + using var client = new StrictClient(); + var gate = new TaskCompletionSource(); + var served = new List(); + Task run = client.Connection.RunAsync(async (request, _) => + { + served.Add($"{request.StreamId} {Http2ControlFrameTests.Fields(request)}"); + if (request.StreamId == 1 && answering) + { + await gate.Task; + } + return Http2Response.Text("ok"); // unread: an upload still arriving is reset with NO_ERROR + }); + client.SendRequests(); + client.Drain(); + + client.Feed(answering ? Http2FlowControlTests.Get(1) : Post(1)); + client.Feed(Frame(Headers, EndStream, 1, Insert[..3])); + client.Feed(Frame(Continuation, EndHeaders, 1, Insert[3..])); + client.Feed(Frame(Headers, EndHeaders | EndStream, 3, [0x82, 0x86, 0x84, 0xBE])); + gate.SetResult(); + List frames = client.Drain(); + client.Close(run); + + Assert.Equal(answering ? "RST_STREAM 1 0x5" : "RST_STREAM 1 0x0", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual(["1 ", "3 x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + } + + // Inserts x-b: w into the dynamic table, which a later GET names by index. + private static byte[] Insert => Http2TrailerTests.Field("x-b", "w", index: true); + + // A POST, its body still to come. + private static byte[] Post(int streamId) => Frame(Headers, EndHeaders, streamId, [0x83, 0x86, 0x84]); + + private static byte[] Frame(byte type, int flags, int streamId, ReadOnlySpan payload) + => Http2FlowControlTests.FrameBytes(type, (byte)flags, streamId, payload); +} diff --git a/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs b/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs index 559b72d0..0c08e549 100644 --- a/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs +++ b/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs @@ -61,12 +61,13 @@ .. Frame(0x2, 0, 1, [0, 0, 0, 0, 15]), Assert.True(served.SequenceEqual([5]), $"the handler was handed: {string.Join(", ", served)}"); }); - runner.Test("h2 stream states: a DATA after the peer's RST_STREAM ends the connection", () => + // RFC 9113 5.1: after the peer's RST_STREAM, a stream error; the stream is then this side's, so a second frame is dropped. + runner.Test("h2 stream states: a DATA after the peer's RST_STREAM resets the stream with STREAM_CLOSED, once, and the connection goes on", () => { - (List frames, List served) = Exchange([], [.. Post(1), .. Frame(RstStream, 0, 1, [0, 0, 0, 8]), .. Frame(Data, EndStream, 1, [1])]); + (List frames, List served) = Exchange([], [.. Post(1), .. Frame(RstStream, 0, 1, [0, 0, 0, 8]), .. Frame(Data, 0, 1, [1]), .. Frame(Data, EndStream, 1, [1])]); - Assert.Equal("GOAWAY 1 0x5", Http2ControlFrameTests.Errors(frames)); - Assert.Equal(0, served.Count); + Assert.Equal("RST_STREAM 1 0x5", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual([7]), $"the handler was handed: {string.Join(", ", served)}"); }); // The issue's own case: no content-length, so nothing else would tell the reader the bytes are not the body's. diff --git a/tests/Ioxide.Tests.Unit/Program.cs b/tests/Ioxide.Tests.Unit/Program.cs index 7fe0bbf1..7a3d144d 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -53,6 +53,7 @@ private static int Main() Http2ControlFrameTests.Register(runner); Http2PriorityResetTests.Register(runner); Http2StreamStateTests.Register(runner); + Http2ClosedStreamTests.Register(runner); HpackEncodeTests.Register(runner); QpackStaticEncodeTests.Register(runner); AlpnSelectTests.Register(runner);