Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Kestrel's ParseHeaders throwing ArgumentOutOfRange exception #61316

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/Servers/Kestrel/Core/src/Internal/Http/HttpParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public bool ParseHeaders(TRequestHandler handler, ref SequenceReader<byte> reade
else
{
// Include the thing after the CR in the rejection exception.
var stopIndex = crIndex + 2;
var stopIndex = Math.Min(crIndex + 2, span.Length);
RejectRequestHeader(span[..stopIndex]);
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/Servers/Kestrel/Core/test/HttpParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,27 @@ public void ParseHeadersWithSplitBuffersThrowsForSmallHeader()
Assert.Equal(StatusCodes.Status400BadRequest, exception.StatusCode);
}

[Fact]
public void ParseMultispanHeaderWithCrAtSpanEnd()
{
var parser = CreateParser(CreateEnabledTrace(), false);

var buffer = ReadOnlySequenceFactory.CreateSegments(
Encoding.ASCII.GetBytes("Head\r"),
Encoding.ASCII.GetBytes("va\r"));
var requestHandler = new RequestHandler();

var reader = new SequenceReader<byte>(buffer);

#pragma warning disable CS0618 // Type or member is obsolete
var exception = Assert.Throws<BadHttpRequestException>(() =>
#pragma warning restore CS0618 // Type or member is obsolete
{
var reader = new SequenceReader<byte>(buffer);
parser.ParseHeaders(requestHandler, ref reader);
});
}

private bool ParseRequestLine(IHttpParser<RequestHandler> parser, RequestHandler requestHandler, ReadOnlySequence<byte> readableBuffer, out SequencePosition consumed, out SequencePosition examined)
{
var reader = new SequenceReader<byte>(readableBuffer);
Expand Down
Loading