Skip to content

InvalidOperationException when decompressed data exceeds request body size limit #61812

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 3 commits into from
May 12, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ private async Task InvokeCore(HttpContext context, Stream decompressionStream)
context.GetEndpoint()?.Metadata?.GetMetadata<IRequestSizeLimitMetadata>()?.MaxRequestBodySize
?? context.Features.Get<IHttpMaxRequestBodySizeFeature>()?.MaxRequestBodySize;

context.Request.Body = new SizeLimitedStream(decompressionStream, sizeLimit);
context.Request.Body = new SizeLimitedStream(decompressionStream, sizeLimit, static (long sizeLimit) => throw new BadHttpRequestException(
$"The decompressed request body is larger than the request body size limit {sizeLimit}.",
StatusCodes.Status413PayloadTooLarge));
await _next(context);
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,8 @@ public async Task Endpoint_HasRequestSizeLimit_UsedForRequest(bool exceedsLimit)
if (exceedsLimit)
{
Assert.NotNull(exception);
Assert.IsAssignableFrom<InvalidOperationException>(exception);
Assert.Equal("The maximum number of bytes have been read.", exception.Message);
Assert.IsAssignableFrom<BadHttpRequestException>(exception);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ((BadHttpRequestException)exception).StatusCode);
}
else
{
Expand Down Expand Up @@ -583,8 +583,8 @@ public async Task Feature_HasRequestSizeLimit_UsedForRequest(bool exceedsLimit)
if (exceedsLimit)
{
Assert.NotNull(exception);
Assert.IsAssignableFrom<InvalidOperationException>(exception);
Assert.Equal("The maximum number of bytes have been read.", exception.Message);
Assert.IsAssignableFrom<BadHttpRequestException>(exception);
Assert.Equal(StatusCodes.Status413PayloadTooLarge, ((BadHttpRequestException)exception).StatusCode);
}
else
{
Expand Down
25 changes: 21 additions & 4 deletions src/Shared/SizeLimitedStream.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

internal sealed class SizeLimitedStream : Stream
{
private readonly Stream _innerStream;
private readonly long? _sizeLimit;

private readonly Action<long>? _handleSizeLimit;
private long _totalBytesRead;

public SizeLimitedStream(Stream innerStream, long? sizeLimit)
public SizeLimitedStream(Stream innerStream, long? sizeLimit, Action<long>? handleSizeLimit = null)
{
ArgumentNullException.ThrowIfNull(innerStream);

_innerStream = innerStream;
_sizeLimit = sizeLimit;
_handleSizeLimit = handleSizeLimit;
}

public override bool CanRead => _innerStream.CanRead;
Expand Down Expand Up @@ -48,7 +51,14 @@ public override int Read(byte[] buffer, int offset, int count)
_totalBytesRead += bytesRead;
if (_totalBytesRead > _sizeLimit)
{
throw new InvalidOperationException("The maximum number of bytes have been read.");
if (_handleSizeLimit != null)
{
_handleSizeLimit(_sizeLimit.Value);
}
else
{
throw new InvalidOperationException("The maximum number of bytes have been read.");
}
}

return bytesRead;
Expand Down Expand Up @@ -81,7 +91,14 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
_totalBytesRead += bytesRead;
if (_totalBytesRead > _sizeLimit)
{
throw new InvalidOperationException("The maximum number of bytes have been read.");
if (_handleSizeLimit != null)
{
_handleSizeLimit(_sizeLimit.Value);
}
else
{
throw new InvalidOperationException("The maximum number of bytes have been read.");
}
}

return bytesRead;
Expand Down
Loading