aspnetcore: ReadFormAsync continues reading after exception
Describe the bug
ReadFormAsync keeps reading form data after exception thrown for invalid data.
To Reproduce
Send binary data file to api with content-type “application/x-www-form-urlencoded”. File must be large to see the problem, 10MB file will do it. Since this is invalid data an exception System.IO.InvalidDataException is thrown.
System.IO.InvalidDataException
HResult=0x80131501
Message=Form key length limit 2048 exceeded.
Source=Microsoft.AspNetCore.WebUtilities
StackTrace:
at Microsoft.AspNetCore.WebUtilities.FormPipeReader.ThrowKeyTooLargeException() in /_/src/Http/WebUtilities/src/FormPipeReader.cs:line 308
The using the code snippet below, the exception will be caught and and 400 sent back.
app.Use(async (context, next) =>
{
try
{
// TODO: try-catch log unexpected errors
await next.Invoke().ConfigureAwait(true);
}
catch (InvalidDataException dataEx)
{
context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
await context.Response.WriteAsync("BadRequest").ConfigureAwait(false);
}
});
Since the data is large and ReadFormAsync doesn’t stop reading, we get this exception System.InvalidOperationException: ‘Reading is already in progress.’.
System.InvalidOperationException
HResult=0x80131509
Message=Reading is already in progress.
Source=Microsoft.AspNetCore.Server.Kestrel.Core
StackTrace:
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.TryReadInternal(ReadResult& readResult) in /_/src/Servers/Kestrel/Core/src/Internal/Http/Http1ContentLengthMessageBody.cs:line 126
Further technical details
- ASP.NET Core version 3
- Include the output of
dotnet --info.NET Core SDK (reflecting any global.json): Version: 3.1.100 Commit: cd82f021f4 Runtime Environment: OS Name: Windows OS Version: 10.0.18363 OS Platform: Windows RID: win10-x64 Base Path: C:\Program Files\dotnet\sdk\3.1.100
Host (useful for support): Version: 3.1.0 Commit: 65f04fb6db .NET Core SDKs installed: 2.1.202 [C:\Program Files\dotnet\sdk] 2.1.507 [C:\Program Files\dotnet\sdk] 2.1.509 [C:\Program Files\dotnet\sdk] 2.1.701 [C:\Program Files\dotnet\sdk] 2.1.801 [C:\Program Files\dotnet\sdk] 2.1.802 [C:\Program Files\dotnet\sdk] 2.2.401 [C:\Program Files\dotnet\sdk] 3.0.100 [C:\Program Files\dotnet\sdk] 3.1.100 [C:\Program Files\dotnet\sdk] .NET Core runtimes installed: Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App] Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] Microsoft.WindowsDesktop.App 3.1.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App] - The IDE (VS / VS Code/ VS4Mac) you’re running on, and it’s version Microsoft Visual Studio Enterprise 2019 Version 16.3.8 VisualStudio.16.Release/16.3.8+29503.13 Microsoft .NET Framework Version 4.8.03752
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 31 (18 by maintainers)
Fixed in https://github.com/dotnet/aspnetcore/pull/18939 which will be released in 3.1.3 (which is not the immediate next patch release unfortunately but it’s on the way!)
I agree, but at the very least Kestrel could provide a clearer error message so middleware authors can more easily understand the problem is the missing call to AdvanceTo.