Skip to content

Throw BadHttpRequestException in HttpSys #24213

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 4 commits into from
Jul 30, 2020
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
10 changes: 9 additions & 1 deletion src/Servers/HttpSys/src/RequestProcessing/RequestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,15 @@ public async void Execute()
Response.Trailers.IsReadOnly = false;
Response.Headers.Clear();
Response.Trailers.Clear();
SetFatalResponse(500);

if (ex is BadHttpRequestException badHttpRequestException)
{
SetFatalResponse(badHttpRequestException.StatusCode);
}
else
{
SetFatalResponse(StatusCodes.Status500InternalServerError);
}
}
}
finally
Expand Down
10 changes: 7 additions & 3 deletions src/Servers/HttpSys/src/RequestProcessing/RequestStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpSys.Internal;
using Microsoft.Extensions.Logging;

Expand Down Expand Up @@ -432,8 +433,9 @@ private void CheckSizeLimit()
var contentLength = RequestContext.Request.ContentLength;
if (contentLength.HasValue && _maxSize.HasValue && contentLength.Value > _maxSize.Value)
{
throw new IOException(
$"The request's Content-Length {contentLength.Value} is larger than the request body size limit {_maxSize.Value}.");
throw new BadHttpRequestException(
$"The request's Content-Length {contentLength.Value} is larger than the request body size limit {_maxSize.Value}.",
StatusCodes.Status413PayloadTooLarge);
}

HasStarted = true;
Expand All @@ -450,7 +452,9 @@ internal bool TryCheckSizeLimit(int bytesRead, out Exception exception)
_totalRead += bytesRead;
if (_maxSize.HasValue && _totalRead > _maxSize.Value)
{
exception = new IOException($"The total number of bytes read {_totalRead} has exceeded the request body size limit {_maxSize.Value}.");
exception = new BadHttpRequestException(
$"The total number of bytes read {_totalRead} has exceeded the request body size limit {_maxSize.Value}.",
StatusCodes.Status413PayloadTooLarge);
return true;
}
exception = null;
Expand Down
43 changes: 29 additions & 14 deletions src/Servers/HttpSys/test/FunctionalTests/RequestBodyLimitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Testing;
using Xunit;
Expand Down Expand Up @@ -159,10 +160,12 @@ public async Task ContentLengthExceedsLimit_ReadSync_ThrowsImmediately()
Assert.False(feature.IsReadOnly);
Assert.Equal(11, httpContext.Request.ContentLength);
byte[] input = new byte[100];
var ex = Assert.Throws<IOException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
var ex = Assert.Throws<BadHttpRequestException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
Assert.Equal("The request's Content-Length 11 is larger than the request body size limit 10.", ex.Message);
ex = Assert.Throws<IOException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
ex = Assert.Throws<BadHttpRequestException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
Assert.Equal("The request's Content-Length 11 is larger than the request body size limit 10.", ex.Message);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
return Task.FromResult(0);
}, options => options.MaxRequestBodySize = 10))
{
Expand All @@ -182,10 +185,12 @@ public async Task ContentLengthExceedsLimit_ReadAsync_ThrowsImmediately()
Assert.False(feature.IsReadOnly);
Assert.Equal(11, httpContext.Request.ContentLength);
byte[] input = new byte[100];
var ex = Assert.Throws<IOException>(() => { var t = httpContext.Request.Body.ReadAsync(input, 0, input.Length); });
var ex = Assert.Throws<BadHttpRequestException>(() => { var t = httpContext.Request.Body.ReadAsync(input, 0, input.Length); });
Assert.Equal("The request's Content-Length 11 is larger than the request body size limit 10.", ex.Message);
ex = Assert.Throws<IOException>(() => { var t = httpContext.Request.Body.ReadAsync(input, 0, input.Length); });
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
ex = Assert.Throws<BadHttpRequestException>(() => { var t = httpContext.Request.Body.ReadAsync(input, 0, input.Length); });
Assert.Equal("The request's Content-Length 11 is larger than the request body size limit 10.", ex.Message);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
return Task.FromResult(0);
}, options => options.MaxRequestBodySize = 10))
{
Expand All @@ -205,10 +210,12 @@ public async Task ContentLengthExceedsLimit_ReadBeginEnd_ThrowsImmediately()
Assert.False(feature.IsReadOnly);
Assert.Equal(11, httpContext.Request.ContentLength);
byte[] input = new byte[100];
var ex = Assert.Throws<IOException>(() => httpContext.Request.Body.BeginRead(input, 0, input.Length, null, null));
var ex = Assert.Throws<BadHttpRequestException>(() => httpContext.Request.Body.BeginRead(input, 0, input.Length, null, null));
Assert.Equal("The request's Content-Length 11 is larger than the request body size limit 10.", ex.Message);
ex = Assert.Throws<IOException>(() => httpContext.Request.Body.BeginRead(input, 0, input.Length, null, null));
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
ex = Assert.Throws<BadHttpRequestException>(() => httpContext.Request.Body.BeginRead(input, 0, input.Length, null, null));
Assert.Equal("The request's Content-Length 11 is larger than the request body size limit 10.", ex.Message);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
return Task.FromResult(0);
}, options => options.MaxRequestBodySize = 10))
{
Expand All @@ -229,10 +236,12 @@ public async Task ChunkedExceedsLimit_ReadSync_ThrowsAtLimit()
Assert.False(feature.IsReadOnly);
Assert.Null(httpContext.Request.ContentLength);
byte[] input = new byte[100];
var ex = Assert.Throws<IOException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
var ex = Assert.Throws<BadHttpRequestException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
Assert.Equal("The total number of bytes read 11 has exceeded the request body size limit 10.", ex.Message);
ex = Assert.Throws<IOException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
ex = Assert.Throws<BadHttpRequestException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
Assert.Equal("The total number of bytes read 11 has exceeded the request body size limit 10.", ex.Message);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
return Task.FromResult(0);
}, options => options.MaxRequestBodySize = 10))
{
Expand All @@ -252,9 +261,11 @@ public async Task ChunkedExceedsLimit_ReadAsync_ThrowsAtLimit()
Assert.False(feature.IsReadOnly);
Assert.Null(httpContext.Request.ContentLength);
byte[] input = new byte[100];
var ex = await Assert.ThrowsAsync<IOException>(() => httpContext.Request.Body.ReadAsync(input, 0, input.Length));
var ex = await Assert.ThrowsAsync<BadHttpRequestException>(() => httpContext.Request.Body.ReadAsync(input, 0, input.Length));
Assert.Equal("The total number of bytes read 11 has exceeded the request body size limit 10.", ex.Message);
ex = await Assert.ThrowsAsync<IOException>(() => httpContext.Request.Body.ReadAsync(input, 0, input.Length));
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
ex = await Assert.ThrowsAsync<BadHttpRequestException>(() => httpContext.Request.Body.ReadAsync(input, 0, input.Length));
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
Assert.Equal("The total number of bytes read 11 has exceeded the request body size limit 10.", ex.Message);
}, options => options.MaxRequestBodySize = 10))
{
Expand All @@ -275,10 +286,12 @@ public async Task ChunkedExceedsLimit_ReadBeginEnd_ThrowsAtLimit()
Assert.Null(httpContext.Request.ContentLength);
byte[] input = new byte[100];
var body = httpContext.Request.Body;
var ex = Assert.Throws<IOException>(() => body.EndRead(body.BeginRead(input, 0, input.Length, null, null)));
var ex = Assert.Throws<BadHttpRequestException>(() => body.EndRead(body.BeginRead(input, 0, input.Length, null, null)));
Assert.Equal("The total number of bytes read 11 has exceeded the request body size limit 10.", ex.Message);
ex = Assert.Throws<IOException>(() => body.EndRead(body.BeginRead(input, 0, input.Length, null, null)));
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
ex = Assert.Throws<BadHttpRequestException>(() => body.EndRead(body.BeginRead(input, 0, input.Length, null, null)));
Assert.Equal("The total number of bytes read 11 has exceeded the request body size limit 10.", ex.Message);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
return Task.FromResult(0);
}, options => options.MaxRequestBodySize = 10))
{
Expand All @@ -303,8 +316,9 @@ public async Task Chunked_ReadSyncPartialBodyUnderLimit_ThrowsAfterLimit()
int read = httpContext.Request.Body.Read(input, 0, input.Length);
Assert.Equal(10, read);
content.Block.Release();
var ex = Assert.Throws<IOException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
var ex = Assert.Throws<BadHttpRequestException>(() => httpContext.Request.Body.Read(input, 0, input.Length));
Assert.Equal("The total number of bytes read 20 has exceeded the request body size limit 10.", ex.Message);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
return Task.FromResult(0);
}, options => options.MaxRequestBodySize = 10))
{
Expand All @@ -328,8 +342,9 @@ public async Task Chunked_ReadAsyncPartialBodyUnderLimit_ThrowsAfterLimit()
int read = await httpContext.Request.Body.ReadAsync(input, 0, input.Length);
Assert.Equal(10, read);
content.Block.Release();
var ex = await Assert.ThrowsAsync<IOException>(() => httpContext.Request.Body.ReadAsync(input, 0, input.Length));
var ex = await Assert.ThrowsAsync<BadHttpRequestException>(() => httpContext.Request.Body.ReadAsync(input, 0, input.Length));
Assert.Equal("The total number of bytes read 20 has exceeded the request body size limit 10.", ex.Message);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ex.StatusCode);
}, options => options.MaxRequestBodySize = 10))
{
string response = await SendRequestAsync(address, content, chunked: true);
Expand Down
26 changes: 24 additions & 2 deletions src/Servers/HttpSys/test/FunctionalTests/ServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,33 @@ public async Task Server_AppException_ClientReset()
}))
{
Task<string> requestTask = SendRequestAsync(address);
await Assert.ThrowsAsync<HttpRequestException>(async () => await requestTask);
var ex = await Assert.ThrowsAsync<HttpRequestException>(async () => await requestTask);
Assert.Equal(StatusCodes.Status500InternalServerError, (int)ex.StatusCode);

// Do it again to make sure the server didn't crash
requestTask = SendRequestAsync(address);
await Assert.ThrowsAsync<HttpRequestException>(async () => await requestTask);
ex = await Assert.ThrowsAsync<HttpRequestException>(async () => await requestTask);
Assert.Equal(StatusCodes.Status500InternalServerError, (int)ex.StatusCode);
}
}

[ConditionalFact]
public async Task Server_BadHttpRequestException_SetStatusCode()
{
string address;
using (Utilities.CreateHttpServer(out address, httpContext =>
{
throw new BadHttpRequestException("Something happened", StatusCodes.Status418ImATeapot);
}))
{
Task<string> requestTask = SendRequestAsync(address);
var ex = await Assert.ThrowsAsync<HttpRequestException>(async () => await requestTask);
Assert.Equal(StatusCodes.Status418ImATeapot, (int)ex.StatusCode);

// Do it again to make sure the server didn't crash
requestTask = SendRequestAsync(address);
ex = await Assert.ThrowsAsync<HttpRequestException>(async () => await requestTask);
Assert.Equal(StatusCodes.Status418ImATeapot, (int)ex.StatusCode);
}
}

Expand Down