Skip to content

Chunked request parsing does not properly update examined #8318

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

Merged
merged 2 commits into from
Mar 8, 2019
Merged
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 eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@
<Uri>https://github.com/dotnet/corefx</Uri>
<Sha>00000</Sha>
</Dependency>
<Dependency Name="System.IO.Pipelines" Version="4.6.0-preview4.19127.11">
<Dependency Name="System.IO.Pipelines" Version="4.6.0-preview4.19156.2">
<Uri>https://github.com/dotnet/corefx</Uri>
<Sha>00000</Sha>
</Dependency>
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<SystemComponentModelAnnotationsPackageVersion>4.6.0-preview4.19156.2</SystemComponentModelAnnotationsPackageVersion>
<SystemDataSqlClientPackageVersion>4.7.0-preview4.19156.2</SystemDataSqlClientPackageVersion>
<SystemDiagnosticsEventLogPackageVersion>4.6.0-preview4.19156.2</SystemDiagnosticsEventLogPackageVersion>
<SystemIOPipelinesPackageVersion>4.6.0-preview4.19127.11</SystemIOPipelinesPackageVersion>
<SystemIOPipelinesPackageVersion>4.6.0-preview4.19156.2</SystemIOPipelinesPackageVersion>
<SystemNetHttpWinHttpHandlerPackageVersion>4.6.0-preview4.19156.2</SystemNetHttpWinHttpHandlerPackageVersion>
<SystemNetWebSocketsWebSocketProtocolPackageVersion>4.6.0-preview4.19156.2</SystemNetWebSocketsWebSocketProtocolPackageVersion>
<SystemReflectionMetadataPackageVersion>1.7.0-preview4.19156.2</SystemReflectionMetadataPackageVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ private void ParseChunkedPrefix(ReadOnlySequence<byte> buffer, out SequencePosit
return;
}

// Assigned this before calculating the chunk size since that can throw
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try-finally ?

examined = reader.Position;

var chunkSize = CalculateChunkSize(ch1, 0);
ch1 = ch2;

Expand Down Expand Up @@ -360,6 +363,9 @@ private void ParseChunkedPrefix(ReadOnlySequence<byte> buffer, out SequencePosit
ch1 = ch2;
}

// Set examined so that we capture the progress that way made
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We

examined = reader.Position;

// At this point, 10 bytes have been consumed which is enough to parse the max value "7FFFFFFF\r\n".
BadHttpRequestException.Throw(RequestRejectionReason.BadChunkSizeData);
}
Expand Down
88 changes: 88 additions & 0 deletions src/Servers/Kestrel/Core/test/MessageBodyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,94 @@ public async Task CanReadFromChunkedEncoding()
}
}

[Fact]
public async Task BadChunkPrefixThrowsBadRequestException()
{
using (var input = new TestInput())
{
var body = Http1MessageBody.For(HttpVersion.Http11, new HttpRequestHeaders { HeaderTransferEncoding = "chunked" }, input.Http1Connection);
var mockBodyControl = new Mock<IHttpBodyControlFeature>();
mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(true);
var reader = new HttpRequestPipeReader();
var stream = new HttpRequestStream(mockBodyControl.Object, reader);
reader.StartAcceptingReads(body);
var buffer = new byte[1024];
var task = stream.ReadAsync(buffer, 0, buffer.Length);

input.Add("g");
input.Add("g");

await Assert.ThrowsAsync<BadHttpRequestException>(() => task);

await body.StopAsync();
}
}

[Fact]
public async Task WritingChunkOverMaxChunkSizeThrowsBadRequest()
{
using (var input = new TestInput())
{
var body = Http1MessageBody.For(HttpVersion.Http11, new HttpRequestHeaders { HeaderTransferEncoding = "chunked" }, input.Http1Connection);
var mockBodyControl = new Mock<IHttpBodyControlFeature>();
mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(true);
var reader = new HttpRequestPipeReader();
var stream = new HttpRequestStream(mockBodyControl.Object, reader);
reader.StartAcceptingReads(body);
var buffer = new byte[1024];
var task = stream.ReadAsync(buffer, 0, buffer.Length);

// Max is 10 bytes
for (int i = 0; i < 11; i++)
{
input.Add(i.ToString());
}

await Assert.ThrowsAsync<BadHttpRequestException>(() => task);

await body.StopAsync();
}
}

[Fact]
public async Task InvalidChunkSuffixThrowsBadRequest()
{
using (var input = new TestInput())
{
var body = Http1MessageBody.For(HttpVersion.Http11, new HttpRequestHeaders { HeaderTransferEncoding = "chunked" }, input.Http1Connection);
var mockBodyControl = new Mock<IHttpBodyControlFeature>();
mockBodyControl.Setup(m => m.AllowSynchronousIO).Returns(true);
var reader = new HttpRequestPipeReader();
var stream = new HttpRequestStream(mockBodyControl.Object, reader);
reader.StartAcceptingReads(body);
var buffer = new byte[1024];

async Task ReadAsync()
{
while (true)
{
await stream.ReadAsync(buffer, 0, buffer.Length);
}
}

var task = ReadAsync();

input.Add("1");
input.Add("\r");
input.Add("\n");
input.Add("h");
input.Add("0");
input.Add("\r");
input.Add("\n");
input.Add("\r");
input.Add("n");

await Assert.ThrowsAsync<BadHttpRequestException>(() => task);

await body.StopAsync();
}
}

[Fact]
public async Task CanReadAsyncFromChunkedEncoding()
{
Expand Down