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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 123 additions & 10 deletions src/protocols/ioxide.http2/Http2Connection.Frames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,76 @@ private void Handle(in FrameHeader header, ReadOnlySpan<byte> 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;
}
}

/// <summary>
/// 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.
/// </summary>
private void HandlePriority(in FrameHeader header, ReadOnlySpan<byte> 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 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<byte> priority)
=> (int)(BinaryPrimitives.ReadUInt32BigEndian(priority) & 0x7FFFFFFF);

/// <summary>
/// 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).
/// </summary>
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<byte> payload)
{
if (header.StreamId == 0)
Expand Down Expand Up @@ -141,16 +199,32 @@ private void HandleHeaders(in FrameHeader header, ReadOnlySpan<byte> payload)
block = block[..^padding];
}

bool selfDependent = false;
if ((header.Flags & FrameFlags.Priority) != 0)
{
if (block.Length < 5)
{
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
Expand Down Expand Up @@ -251,12 +325,14 @@ private void DiscardHeaderBlock(in FrameHeader header, ReadOnlySpan<byte> 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
}
}
Expand Down Expand Up @@ -470,9 +546,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)
Expand All @@ -497,8 +571,21 @@ private void TryComplete(PendingRequest pending)

private void HandleSettings(in FrameHeader header, ReadOnlySpan<byte> 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)
Expand Down Expand Up @@ -527,6 +614,15 @@ private void HandleSettings(in FrameHeader header, ReadOnlySpan<byte> 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)
{
Expand Down Expand Up @@ -627,6 +723,11 @@ private void HandleWindowUpdate(in FrameHeader header, ReadOnlySpan<byte> payloa

private void HandlePing(in FrameHeader header, ReadOnlySpan<byte> 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);
Expand All @@ -644,8 +745,20 @@ private void HandlePing(in FrameHeader header, ReadOnlySpan<byte> payload)
Stage(frame);
}

private void HandleRstStream(in FrameHeader header)
private void HandleRstStream(in FrameHeader header, ReadOnlySpan<byte> 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
Expand Down
4 changes: 3 additions & 1 deletion src/protocols/ioxide.http2/Http2Connection.Write.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@
{
Span<byte> 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;
Expand Down Expand Up @@ -406,7 +408,7 @@
}
}

Span<byte> header = stackalloc byte[FrameHeader.Size];

Check warning on line 411 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 411 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 411 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 411 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 411 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 411 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 411 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
new FrameHeader(chunk, first ? FrameType.Headers : FrameType.Continuation, flags, streamId)
.Write(header);

Expand Down Expand Up @@ -447,7 +449,7 @@

bool last = offset + chunk >= body.Length;

Span<byte> header = stackalloc byte[FrameHeader.Size];

Check warning on line 452 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 452 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 452 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 452 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 452 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)

Check warning on line 452 in src/protocols/ioxide.http2/Http2Connection.Write.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-24.04-arm)

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
new FrameHeader(chunk, FrameType.Data, last ? FrameFlags.EndStream : FrameFlags.None, streamId)
.Write(header);

Expand Down
4 changes: 4 additions & 0 deletions src/protocols/ioxide.http2/Http2Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading
Loading