aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

TestHost response body does not support 0-byte reads

Open MihaZupan opened this issue 3 years ago • 8 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

Similar to #41287 and #41305, TestHost also doesn't expect/support 0-byte reads on the body.

https://github.com/dotnet/aspnetcore/blob/3888fda44771dd6d45f305a831b2f37427303ae4/src/Hosting/TestHost/src/ResponseBodyReaderStream.cs#L118-L121

Expected Behavior

The 0-byte read should not throw and subsequent reads should return available data.

Steps To Reproduce

See yarp.zip (repro attached in the original Yarp issue)

Exceptions (if any)

Yarp.ReverseProxy.Forwarder.HttpForwarder[48]
ResponseBodyDestination: The destination reported an error when copying the response body.
System.ArgumentOutOfRangeException: (Parameter 'count')
Actual value was 0.
at Microsoft.AspNetCore.TestHost.ResponseBodyReaderStream.VerifyBuffer(Byte[] buffer, Int32 offset, Int32 count)
at Microsoft.AspNetCore.TestHost.ResponseBodyReaderStream.ReadAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at Yarp.ReverseProxy.Forwarder.StreamCopier.CopyAsync(Stream input, Stream output, Int64 promisedContentLength, StreamCopierTelemetry telemetry, ActivityCancellationTokenSource activityToken, CancellationToken cancellation)

.NET Version

6.0.5

Anything else?

Discovered by a user writing unit tests that include Yarp https://github.com/microsoft/reverse-proxy/issues/1720

cc: @Tratcher @adityamandaleeka

There kind of failures can be caught by StreamConformanceTests we run in runtime. For example, here are 0-byte read tests for HttpClient response streams. Are there any plans to include such tests in AspNetCore (for this specific kind of issue, it may be easier to just manually audit all stream implementations)?

MihaZupan avatar May 16 '22 06:05 MihaZupan

Any news on this issue?

Erwinvandervalk avatar Jun 20 '22 05:06 Erwinvandervalk

I have a branch where I've started working on it, but I have some higher priorities at the moment.

Tratcher avatar Jun 20 '22 21:06 Tratcher

This affects us as well

leastprivilege avatar Jun 22 '22 08:06 leastprivilege

Any update on this? This prevents us to move to YARP 1.1 in our product (and must effect everyone doing testing with the test host).

leastprivilege avatar Aug 20 '22 07:08 leastprivilege

I did some testing preparing to fix this and realized that it was partially fixed in 7.0 preview1 by https://github.com/dotnet/aspnetcore/blame/8eadd8f7e2b46387ea6f121a8d9f9bf003cf3f2c/src/Hosting/TestHost/src/ResponseBodyReaderStream.cs#L76. This introduced new Memory overloads and bypassed the problematic validation. YARP is already calling those overloads. Can folks try this on 7.0 to confirm everything works for them end-to-end?

Tratcher avatar Sep 15 '22 22:09 Tratcher

Not anytime soon - we need this to work on an LTS version.

leastprivilege avatar Sep 16 '22 06:09 leastprivilege

The patch has been approved for 6.0.11 (Nov). I'm not planning to port this to 7.0 since YARP doesn't seem to be affected there, unless someone reports otherwise.

Tratcher avatar Sep 21 '22 17:09 Tratcher

thanks, Chris!

leastprivilege avatar Sep 22 '22 10:09 leastprivilege

Merged for 6.0.11. Leaving this open to remind me to port the fix to main.

Tratcher avatar Oct 04 '22 20:10 Tratcher