diff --git a/src/protocols/ioxide.http2/Http2Connection.Frames.cs b/src/protocols/ioxide.http2/Http2Connection.Frames.cs index 18d9c3ea..7ab063e3 100644 --- a/src/protocols/ioxide.http2/Http2Connection.Frames.cs +++ b/src/protocols/ioxide.http2/Http2Connection.Frames.cs @@ -101,19 +101,26 @@ 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; } } - private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) + /// + /// 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) { @@ -121,6 +128,58 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) 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 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 (_streams.ContainsKey(streamId) || _responseWindows.ContainsKey(streamId)) + { + ResetOpenStream(streamId, errorCode); + } + else + { + GoAway(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) + { + // 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; + } + ReadOnlySpan block = payload; // Padding and priority sit in front of the header block and are not part of it. @@ -141,6 +200,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,14 +208,34 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan payload) GoAway(Http2Error.FrameSizeError); return; } + selfDependent = DependsOn(block) == header.StreamId; block = block[5..]; } - if (!_streams.TryGetValue(header.StreamId, out PendingRequest? pending)) + bool open = _streams.TryGetValue(header.StreamId, out PendingRequest? pending); + if (!open && header.StreamId <= _highestStream) + { + HandleHeadersOnClosed(header, block); + return; + } + + if (selfDependent) { - // 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) + // 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 (open) + { + ResetOpenStream(header.StreamId, Http2Error.ProtocolError); + } + _discardBlock.StreamId = header.StreamId; + _discardReset = Http2Error.ProtocolError; + DiscardHeaderBlock(header, block); + return; + } + + if (!open) + { + 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 @@ -168,28 +248,30 @@ 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. pending.HasBody = (header.Flags & FrameFlags.EndStream) == 0; } - else if (pending.BodyReader is { } reader) + 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 not null) { // Trailers for a request already being served: its arena backs what the handler holds, - // so the block is decoded aside and dropped, and only its END_STREAM is kept. + // so the block is decoded aside, checked and dropped, and only its END_STREAM is kept (EndTrailers). _discardBlock.StreamId = header.StreamId; + _discardTrailers = true; + _discardEndsStream = (header.Flags & FrameFlags.EndStream) != 0; DiscardHeaderBlock(header, block); - if ((header.Flags & FrameFlags.EndStream) != 0) - { - if (!pending.BodyFits(ended: true)) - { - RefuseBody(pending); - return; - } - pending.RequestEnded = true; - reader.End(); - } return; } else if ((header.Flags & FrameFlags.EndStream) == 0) @@ -229,6 +311,51 @@ 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 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) + { + int how = ClosedHow(header.StreamId); + if (how is ClosedByPeer or ClosedByPeerReset) + { + RefuseOnPeerClosed(header.StreamId, how); + } + else if (how == 0 && NeverOpened(header.StreamId)) + { + GoAway(Http2Error.ProtocolError); + } + + if (!_failed) + { + _discardBlock.StreamId = header.StreamId; + DiscardHeaderBlock(header, block); + } + } + + /// + /// 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); + } + } + /// /// 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 @@ -251,14 +378,64 @@ private void DiscardHeaderBlock(in FrameHeader header, ReadOnlySpan block) int streamId = _discardBlock.StreamId; _discardBlock.StreamId = 0; + uint reset = _discardReset; + _discardReset = Http2Error.RefusedStream; + bool trailers = _discardTrailers; + _discardTrailers = false; + + bool decoded = DecodeHeaderBlock(_discardBlock, discard: true); + bool malformed = _discardBlock.Malformed; + _discardBlock.Malformed = false; + + if (!decoded) + { + return; + } + + if (trailers) + { + EndTrailers(streamId, malformed); + return; + } // 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) + if (streamId > _highestStream) + { + ResetStream(streamId, reset); + OpenStreamId(streamId); // refused, but used + } + } + + /// + /// The trailers of a request with a body reader, decoded. RFC 9113 8.1 makes them malformed when + /// they carry a pseudo-header or a field no request may (), + /// and when they do not end the stream, since nothing may follow them. Either is refused as a body + /// that misses its content-length is (): the handler hears of it at its + /// next read, and nothing more goes out on the stream. Otherwise their END_STREAM ends the body. + /// + private void EndTrailers(int streamId, bool malformed) + { + // A handler done with the request while its trailers arrived has retired the stream already. + if (!_streams.TryGetValue(streamId, out PendingRequest? pending) || pending.BodyReader is not { } reader) + { + return; + } + + if (malformed || !_discardEndsStream) { - ResetStream(streamId, Http2Error.RefusedStream); - _highestStream = streamId; // refused, but used + reader.Abort(malformed ? "the request's trailers were malformed" : "the request's trailers did not end its stream"); + ResetOpenStream(streamId, Http2Error.ProtocolError); + return; } + + if (!pending.BodyFits(ended: true)) + { + RefuseBody(pending); + return; + } + pending.RequestEnded = true; + reader.End(); } /// @@ -329,7 +506,8 @@ private bool DecodeHeaderBlock(PendingRequest pending, bool endsStream = false, // The request rides as state, so the sink is a static lambda and no closure is made per block. int limit = _options.MaxHeaderListSize; _decoder.Decode(block, _headerScratch, (Pending: pending, Limit: limit), - discard ? static (_, _, _) => { } : static (state, name, value) => state.Pending.AddHeader(name, value, state.Limit)); + discard ? static (state, name, value) => state.Pending.CheckDiscarded(name, value) + : static (state, name, value) => state.Pending.AddHeader(name, value, state.Limit)); if (pending.HeaderListSize > limit) { @@ -339,7 +517,12 @@ private bool DecodeHeaderBlock(PendingRequest pending, bool endsStream = false, pending.ClearHeaderBlock(); pending.HeadersDone = true; - pending.ClosePseudoHeaders(); + + if (discard) + { + return true; // nothing kept, so nothing to refuse here: whoever discarded it reads Malformed + } + pending.EndHeaderBlock(); // A block that ends the stream ends the body with it, so the DATA before it - none, for // the block that opens the request - has to be all the content-length declared. @@ -361,7 +544,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; @@ -404,7 +587,22 @@ 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. 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) + { + RefuseOnPeerClosed(header.StreamId, how); + } + 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). @@ -470,9 +668,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) @@ -491,14 +687,28 @@ 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); } 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 +737,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) { @@ -580,6 +799,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) { @@ -627,6 +852,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,10 +874,28 @@ 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 (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, ClosedByPeerReset); Discard(pending); // the peer gave up; there is nobody to answer } @@ -701,8 +949,9 @@ private sealed class PendingRequest : IDisposable public (int Offset, int Length) Scheme; public (int Offset, int Length) Authority; - // A pseudo-header out of place, or a content-length that is no number or comes twice: the - // stream is reset once the block is decoded, never dispatched. + // A pseudo-header out of place or missing, a field no HTTP/2 request may carry, or a + // content-length that is no number or comes twice: the stream is reset once the block is + // decoded, never dispatched. public bool Malformed; // What the content-length says the DATA comes to, -1 without one, and what it has come to so @@ -768,6 +1017,7 @@ public void AddHeader(ReadOnlySpan name, ReadOnlySpan value, int lim } ClosePseudoHeaders(); + Malformed |= IsMalformedField(name, value); (int Offset, int Length) nameRange = Append(name); (int Offset, int Length) valueRange = Append(value); _fields.Add((nameRange.Offset, nameRange.Length, valueRange.Offset, valueRange.Length)); @@ -793,13 +1043,72 @@ public void AddHeader(ReadOnlySpan name, ReadOnlySpan value, int lim // RFC 9113 8.3: each at most once and ahead of every regular field; 8.1: none in trailers. private (int Offset, int Length) Pseudo(int seen, ReadOnlySpan value) { - Malformed |= (_pseudoSeen & (seen | PseudoClosed)) != 0; + Malformed |= (_pseudoSeen & (seen | PseudoClosed)) != 0 || IsMalformedValue(value); _pseudoSeen |= seen; return Append(value); } public void ClosePseudoHeaders() => _pseudoSeen |= PseudoClosed; + /// + /// A block is decoded. The one that opens the request has to have carried what RFC 9113 8.3.1 + /// requires of a request: a :method, a :scheme and a :path, none of them empty - or, for a + /// CONNECT, an :authority that is not empty and neither :scheme nor :path (8.5). Trailers + /// carry none (8.1). + /// + public void EndHeaderBlock() + { + if (!_requestDecoded) + { + bool connect = _arena.AsSpan(Method.Offset, Method.Length).SequenceEqual("CONNECT"u8); + Malformed |= connect + ? (_pseudoSeen & (2 | 4 | 8)) != 8 || Authority.Length == 0 + : (_pseudoSeen & (1 | 2 | 4)) != (1 | 2 | 4) || Method.Length == 0 || Scheme.Length == 0 || Path.Length == 0; + } + _requestDecoded = true; + ClosePseudoHeaders(); + } + + // The block that opens the request has been decoded: later ones are trailers. + private bool _requestDecoded; + + /// A field of a block decoded aside, trailers among them: checked as trailers, and not kept. + public void CheckDiscarded(ReadOnlySpan name, ReadOnlySpan value) + => Malformed |= (name.Length > 0 && name[0] == (byte)':') || IsMalformedField(name, value); + + /// + /// RFC 9113 8.2.1: a field name is visible ASCII and lowercase, and its value is no field + /// value of its own (). 8.2.2: HTTP/2 carries no + /// connection-specific field, which the framing replaced, and a te only of "trailers". Each of + /// these reaching a handler, or a proxy behind it, is what a request smuggled past the framing + /// looks like: an HTTP/1.1 hop would read "x-a: b\r\ntransfer-encoding: chunked" as two lines. + /// + private static bool IsMalformedField(ReadOnlySpan name, ReadOnlySpan value) + { + if (name.IsEmpty + || name.IndexOfAnyExceptInRange((byte)0x21, (byte)0x7E) >= 0 + || name.IndexOfAnyInRange((byte)'A', (byte)'Z') >= 0 + || IsMalformedValue(value)) + { + return true; + } + + return name.Length switch + { + 2 => name.SequenceEqual("te"u8) && !System.Text.Ascii.EqualsIgnoreCase(value, "trailers"u8), // as nghttp2 compares it + 7 => name.SequenceEqual("upgrade"u8), + 10 => name.SequenceEqual("connection"u8) || name.SequenceEqual("keep-alive"u8), + 16 => name.SequenceEqual("proxy-connection"u8), + 17 => name.SequenceEqual("transfer-encoding"u8), + _ => false, + }; + } + + // RFC 9113 8.2.1: no NUL, CR or LF anywhere in a value, and no space or tab at either end of it. + private static bool IsMalformedValue(ReadOnlySpan value) + => value.IndexOfAny((byte)0, (byte)'\r', (byte)'\n') >= 0 + || (value.Length > 0 && (value[0] is (byte)' ' or (byte)'\t' || value[^1] is (byte)' ' or (byte)'\t')); + public void AppendBody(ReadOnlySpan data) { (int Offset, int Length) range = Append(data); @@ -886,6 +1195,7 @@ public void Dispose() Malformed = false; _pseudoSeen = 0; + _requestDecoded = false; ContentLength = -1; BodyReceived = 0; ClearHeaderBlock(); diff --git a/src/protocols/ioxide.http2/Http2Connection.Write.cs b/src/protocols/ioxide.http2/Http2Connection.Write.cs index 9ca2d941..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); @@ -338,7 +339,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..6efe5549 100644 --- a/src/protocols/ioxide.http2/Http2Connection.cs +++ b/src/protocols/ioxide.http2/Http2Connection.cs @@ -57,6 +57,14 @@ 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 block discarded is trailers of a request with a body reader, and whether its HEADERS ended the stream. + private bool _discardTrailers; + private bool _discardEndsStream; + // The stream whose header block is still open, or 0. private int _headerBlockStream; @@ -64,6 +72,24 @@ 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): 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, 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; @@ -442,6 +468,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); @@ -452,6 +483,54 @@ private void RetireStream(PendingRequest pending) } } + private void RememberClosed(int streamId, int how) + { + _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. 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; + 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. + 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/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/Http2ControlFrameTests.cs b/tests/Ioxide.Tests.Unit/Http2ControlFrameTests.cs new file mode 100644 index 00000000..7d6c7a87 --- /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); + + internal 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/Http2FieldValidityTests.cs b/tests/Ioxide.Tests.Unit/Http2FieldValidityTests.cs new file mode 100644 index 00000000..0238997b --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2FieldValidityTests.cs @@ -0,0 +1,61 @@ +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// The bytes a field may carry (RFC 9113 8.2.1), and the pseudo-headers a request needs filled +/// (8.3.1, 8.5). A CR or LF in a value is what turns one field into two at an HTTP/1.1 hop behind the +/// handler, and a name outside visible ASCII is no token any hop agrees on; either makes the request +/// malformed, a stream error of PROTOCOL_ERROR before any handler runs. +/// +internal static class Http2FieldValidityTests +{ + // :method GET, :scheme http and :path / from the static table. + private static readonly byte[] Get = [0x82, 0x86, 0x84]; + + public static void Register(Runner runner) + { + foreach ((string name, byte[] malformed) in new[] + { + ("a field name with a space", (byte[])[.. Get, .. Raw("x a"u8, "1"u8)]), + ("a field name with a byte past ASCII", [.. Get, .. Raw([(byte)'x', 0xE9], "1"u8)]), + ("a field value with CR LF and a second field after it", [.. Get, .. Raw("x-a"u8, "b\r\ntransfer-encoding: chunked"u8)]), + ("a field value with a NUL", [.. Get, .. Raw("x-a"u8, "b\0c"u8)]), + ("a field value with a leading space", [.. Get, .. Raw("x-a"u8, " b"u8)]), + ("a field value with a trailing tab", [.. Get, .. Raw("x-a"u8, "b\t"u8)]), + ("a :path with LF", [0x82, 0x86, 0x04, 3, .. "/\na"u8]), + ("an empty :method", [0x02, 0x00, 0x86, 0x84]), + ("an empty :scheme", [0x82, 0x07, 0x00, 0x84]), + ("a CONNECT with :scheme and :path", [0x02, 7, .. "CONNECT"u8, 0x86, 0x84, 0x01, 15, .. "example.com:443"u8]), + ("a CONNECT without :authority", [0x02, 7, .. "CONNECT"u8]), + ("a CONNECT with an empty :authority", [0x02, 7, .. "CONNECT"u8, 0x01, 0x00]), + ("a :protocol, which nothing here takes", [0x02, 7, .. "CONNECT"u8, .. Raw(":protocol"u8, "websocket"u8), 0x86, 0x84, 0x01, 11, .. "example.com"u8]), + }) + { + string shape = name; + byte[] block = malformed; + runner.Test($"h2 field validity: {shape} resets the stream before the handler runs, and HPACK stays in step", () => + { + List served = Http2MalformedRequestTests.Serve([.. block, .. Http2TrailerTests.Field("x-b", "w", index: true)], out List frames, withReader: true); + + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual(["3 GET / x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + runner.Test("h2 field validity: control: te: TRAILERS, spaces inside a value and visible punctuation in a name are served", () => + { + List served = Http2MalformedRequestTests.Serve( + [.. Get, .. Raw("te"u8, "TRAILERS"u8), .. Raw("x-a"u8, "b c"u8), .. Raw("x-!#$%&'*+.^_`|~"u8, "1"u8), .. Http2TrailerTests.Field("x-b", "w", index: true)], + out List frames, withReader: true); + + Assert.Equal("", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual(["1 GET / te=TRAILERS x-a=b c x-!#$%&'*+.^_`|~=1 x-b=w", "3 GET / x-b=w"]), + $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + // A literal field without indexing, its name and value as raw bytes. + private static byte[] Raw(ReadOnlySpan name, ReadOnlySpan value) + => [0x00, (byte)name.Length, .. name, (byte)value.Length, .. value]; +} diff --git a/tests/Ioxide.Tests.Unit/Http2MalformedRequestTests.cs b/tests/Ioxide.Tests.Unit/Http2MalformedRequestTests.cs new file mode 100644 index 00000000..e92b8088 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2MalformedRequestTests.cs @@ -0,0 +1,103 @@ +using System.Text; +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// RFC 9113 8.1.1: a malformed request is a stream error of PROTOCOL_ERROR, and the server must not +/// process it. Malformed here are a field name with uppercase letters (8.2.1), a connection-specific +/// field or a te other than "trailers" (8.2.2), and a request without :method, :scheme or a :path that +/// is not empty (8.3.1). A transfer-encoding that reached a handler, or a proxy behind it, would frame +/// the body a second way. +/// +internal static class Http2MalformedRequestTests +{ + private const byte Headers = 0x1; + private const byte EndStream = 0x1, EndHeaders = 0x4; + + // :method GET, :scheme http and :path / from the static table. + private static readonly byte[] Get = [0x82, 0x86, 0x84]; + + public static void Register(Runner runner) + { + foreach ((string name, byte[] malformed) in new[] + { + ("a field name with uppercase letters", (byte[])[.. Get, .. Field("X-Trace", "1")]), + ("connection", [.. Get, .. Field("connection", "keep-alive")]), + ("keep-alive", [.. Get, .. Field("keep-alive", "timeout=5")]), + ("proxy-connection", [.. Get, .. Field("proxy-connection", "keep-alive")]), + ("transfer-encoding", [.. Get, .. Field("transfer-encoding", "chunked")]), + ("upgrade", [.. Get, .. Field("upgrade", "websocket")]), + ("a te other than trailers", [.. Get, .. Field("te", "gzip")]), + ("no :method", [0x86, 0x84]), + ("no :scheme", [0x82, 0x84]), + ("no :path", [0x82, 0x86]), + ("an empty :path", [0x82, 0x86, 0x04, 0x00]), + }) + { + string shape = name; + byte[] block = malformed; + runner.Test($"h2 malformed request: {shape} resets the stream before the handler runs, and HPACK stays in step", () => + { + List served = Serve([.. block, .. Insert], out List frames, withReader: true); + + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual(["3 GET / x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + // The buffered dispatch decodes through the same checks; one case stands for them. + runner.Test("h2 malformed request: transfer-encoding resets the stream before a buffered handler runs too", () => + { + List served = Serve([.. Get, .. Field("transfer-encoding", "chunked"), .. Insert], out List frames, withReader: false); + + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual(["3 GET / x-b=w"]), $"the handler was handed: {string.Join(", ", served)}"); + }); + + runner.Test("h2 malformed request: control: te: trailers, lowercase fields and a CONNECT with only :authority are served", () => + { + // :method CONNECT and :authority as literals on their static names; CONNECT carries no :scheme or :path (RFC 9113 8.5). + byte[] connect = [0x02, 7, .. "CONNECT"u8, 0x01, 15, .. "example.com:443"u8]; + List served = Serve([.. Get, .. Field("te", "trailers"), .. Field("x-trace", "1"), .. Insert], out List frames, withReader: true, last: connect); + + Assert.Equal("", Http2ControlFrameTests.Errors(frames)); + Assert.True(served.SequenceEqual(["1 GET / te=trailers x-trace=1 x-b=w", "3 GET / x-b=w", "5 CONNECT"]), + $"the handler was handed: {string.Join(", ", served)}"); + }); + } + + // Inserts x-b: w into the dynamic table, which stream 3 then names by index: it only resolves if the block was decoded whole. + private static byte[] Insert => Http2TrailerTests.Field("x-b", "w", index: true); + + private static byte[] Field(string name, string value) => Http2TrailerTests.Field(name, value); + + // The block on stream 1, a GET on stream 3 naming the inserted field, then `last` on stream 5, if given. + internal static List Serve(byte[] block, out List frames, bool withReader, byte[]? last = null) + { + using var client = new StrictClient(); + var served = new List(); + ValueTask Handle(Http2Request request) + { + served.Add($"{request.StreamId} {Ascii(request.Method)} {Ascii(request.Path)} {Http2ControlFrameTests.Fields(request)}".TrimEnd()); + return ValueTask.FromResult(Http2Response.Text("ok")); + } + Task run = withReader + ? client.Connection.RunAsync((request, _) => Handle(request)) + : client.Connection.RunAsync(Handle); + client.SendRequests(); + client.Drain(); + + client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndHeaders | EndStream, 1, block)); + client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndHeaders | EndStream, 3, [.. Get, 0xBE])); + if (last is not null) + { + client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndHeaders | EndStream, 5, last)); + } + frames = client.Drain(); + client.Close(run); + return served; + } + + private static string Ascii(ReadOnlyMemory bytes) => Encoding.ASCII.GetString(bytes.Span); +} diff --git a/tests/Ioxide.Tests.Unit/Http2MalformedTrailerTests.cs b/tests/Ioxide.Tests.Unit/Http2MalformedTrailerTests.cs new file mode 100644 index 00000000..b89480f0 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2MalformedTrailerTests.cs @@ -0,0 +1,159 @@ +using System.Text; +using ioxide.http2; + +namespace Ioxide.Tests; + +/// +/// The trailers of a request whose handler takes a body reader, which is already running when they +/// arrive. They are decoded aside and dropped, but checked as a buffered request's are: a pseudo-header +/// or a field no request may carry makes them malformed, and so does a block that does not end the +/// stream (RFC 9113 8.1). The stream is reset with PROTOCOL_ERROR and the handler's read says why, +/// rather than the handler waiting for an end that never comes. +/// +internal static class Http2MalformedTrailerTests +{ + private const byte Data = 0x0, Headers = 0x1, Continuation = 0x9; + private const byte EndStream = 0x1, EndHeaders = 0x4; + + public static void Register(Runner runner) + { + foreach ((string name, int flags, byte[] trailers, string reason) in new[] + { + ("a pseudo-header", EndHeaders | EndStream, (byte[])[0x04, 2, .. "/x"u8], "malformed"), + ("transfer-encoding", EndHeaders | EndStream, Http2TrailerTests.Field("transfer-encoding", "chunked"), "malformed"), + ("no END_STREAM", EndHeaders, Http2TrailerTests.Field("x-checksum", "abc"), "did not end its stream"), + ("an uppercase field name", EndHeaders | EndStream, Http2TrailerTests.Field("X-Checksum", "abc"), "malformed"), + ("a CR LF in a value", EndHeaders | EndStream, Http2TrailerTests.Field("x-checksum", "abc\r\nx-evil: 1"), "malformed"), + }) + { + string shape = name; + int trailerFlags = flags; + byte[] block = trailers; + string why = reason; + runner.Test($"h2 streamed request: trailers with {shape} reset the stream, and the handler's read says why", () => + { + (string outcome, List frames) = Upload(trailerFlags, block); + + Assert.True(outcome.StartsWith("abc|") && outcome.Contains(why), $"the handler read: {outcome}"); + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(0, frames.Count(f => f.Type == Headers && f.StreamId == 1)); // nothing more goes out on it + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 3)); + }); + } + + // END_STREAM rides the HEADERS and END_HEADERS the CONTINUATION: the body ends only once the block is in and checked. + runner.Test("h2 streamed request: trailers whose CONTINUATION carries a pseudo-header reset the stream, though their HEADERS ended it", () => + { + byte[] trailers = [.. Http2TrailerTests.Field("x-checksum", "abc"), 0x04, 2, .. "/x"u8]; + (string outcome, List frames) = Upload(EndHeaders | EndStream, trailers, splitAt: trailers.Length - 4); + + Assert.True(outcome.StartsWith("abc|") && outcome.Contains("malformed"), $"the handler read: {outcome}"); + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + }); + + runner.Test("h2 streamed request: control: well-formed trailers continued in CONTINUATION end the body, and HPACK stays in step", () => + { + byte[] trailers = Http2TrailerTests.Field("x-checksum", "abc", index: true); + (string outcome, List frames) = Upload(EndHeaders | EndStream, trailers, namesTheirEntry: true, splitAt: 4); + + Assert.Equal("abc", outcome); + Assert.Equal("", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 3)); + }); + + runner.Test("h2 streamed request: well-formed trailers on a body short of its content-length reset the stream, and the read says how short", () => + { + byte[] opening = [0x83, 0x86, 0x84, .. Http2TrailerTests.Field("content-length", "10")]; + (string outcome, List frames) = Upload(EndHeaders | EndStream, Http2TrailerTests.Field("x-checksum", "abc"), opening: opening); + + Assert.True(outcome.StartsWith("abc|") && outcome.Contains("7 bytes short"), $"the handler read: {outcome}"); + Assert.Equal("RST_STREAM 1 0x1", Http2ControlFrameTests.Errors(frames)); + }); + + runner.Test("h2 buffered request: trailers with an uppercase field name reset the stream, and no handler runs", () => + { + 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(Http2FlowControlTests.FrameBytes(Headers, EndHeaders, 1, [0x83, 0x86, 0x84])); + client.Feed(Http2FlowControlTests.FrameBytes(Data, 0, 1, "abc"u8)); + client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndHeaders | EndStream, 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)}"); + }); + + runner.Test("h2 streamed request: control: well-formed trailers end the body, and HPACK stays in step", () => + { + (string outcome, List frames) = Upload(EndHeaders | EndStream, Http2TrailerTests.Field("x-checksum", "abc", index: true), namesTheirEntry: true); + + Assert.Equal("abc", outcome); + Assert.Equal("", Http2ControlFrameTests.Errors(frames)); + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 1)); + Assert.Equal(1, frames.Count(f => f.Type == Headers && f.StreamId == 3)); + }); + } + + // A POST on stream 1 whose handler reads its body, three bytes, then the trailers - their block + // continued in a CONTINUATION from splitAt, if given - then a GET on stream 3, which can name the + // entry the trailers insert: it resolves only if they were decoded. + private static (string Outcome, List Frames) Upload(int trailerFlags, byte[] trailers, bool namesTheirEntry = false, + int splitAt = 0, byte[]? opening = null) + { + using var client = new StrictClient(); + var outcome = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + Task run = client.Connection.RunAsync(async (request, body) => + { + if (request.StreamId != 1) + { + return Http2Response.Text("ok"); + } + + var read = new StringBuilder(); + try + { + for (ReadOnlyMemory chunk = await body.ReadAsync(); !chunk.IsEmpty; chunk = await body.ReadAsync()) + { + read.Append(Encoding.ASCII.GetString(chunk.Span)); + } + } + catch (IOException e) + { + read.Append('|').Append(e.Message); + } + outcome.TrySetResult(read.ToString()); + return Http2Response.Text("ok"); + }); + client.SendRequests(); + client.Drain(); + + client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndHeaders, 1, opening ?? [0x83, 0x86, 0x84])); + client.Feed(Http2FlowControlTests.FrameBytes(Data, 0, 1, "abc"u8)); + if (splitAt > 0) + { + client.Feed(Http2FlowControlTests.FrameBytes(Headers, (byte)(trailerFlags & ~EndHeaders), 1, trailers.AsSpan(0, splitAt))); + client.Feed(Http2FlowControlTests.FrameBytes(Continuation, (byte)(trailerFlags & EndHeaders), 1, trailers.AsSpan(splitAt))); + } + else + { + client.Feed(Http2FlowControlTests.FrameBytes(Headers, (byte)trailerFlags, 1, trailers)); + } + byte[] get = namesTheirEntry ? [0x82, 0x86, 0x84, 0xBE] : [0x82, 0x86, 0x84]; + client.Feed(Http2FlowControlTests.FrameBytes(Headers, EndHeaders | EndStream, 3, get)); + List frames = client.Drain(); + + string result = outcome.Task.Wait(2_000) ? outcome.Task.Result : "(still waiting for the end of the body)"; + client.Close(run); + return (result, frames); + } +} 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/Http2StreamStateTests.cs b/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs new file mode 100644 index 00000000..0c08e549 --- /dev/null +++ b/tests/Ioxide.Tests.Unit/Http2StreamStateTests.cs @@ -0,0 +1,195 @@ +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)}"); + }); + + // 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, 0, 1, [1]), .. Frame(Data, EndStream, 1, [1])]); + + 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. + 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 95146068..742ccbdc 100644 --- a/tests/Ioxide.Tests.Unit/Program.cs +++ b/tests/Ioxide.Tests.Unit/Program.cs @@ -50,6 +50,13 @@ private static int Main() Nghttp2EarlyAnswerTests.Register(runner); Http2PeerClosedFlushTests.Register(runner); Http2StreamResetTests.Register(runner); + Http2ControlFrameTests.Register(runner); + Http2PriorityResetTests.Register(runner); + Http2StreamStateTests.Register(runner); + Http2ClosedStreamTests.Register(runner); + Http2MalformedRequestTests.Register(runner); + Http2MalformedTrailerTests.Register(runner); + Http2FieldValidityTests.Register(runner); HpackEncodeTests.Register(runner); QpackStaticEncodeTests.Register(runner); AlpnSelectTests.Register(runner);