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 Microsoft.AspNetCore.Http.Results.File() fails to determine the correct file size of symlinks #39170 Microsoft.AspNetCore.Http.Results.File() fails to determine the correct file size of symlinks #39170 anorm opened this issue Dec 23, 2021 · 5 comments · Fixed by #39330

Describe the bug

If a filename given to Microsoft.AspNetCore.Http.Results.File() is a symlink, the size retrieved for the Content-Length header is wrong.

Expected Behavior

Results.File() should follow the symlink to the real file and determine the actual file size.

Steps To Reproduce

Example

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/foo", () => { return Results.File("/foo", "application/octet-strean", "foo"); });
app.Run();

Run the above with a symlink named /foo in the filesystem, pointing to a real file. When requesting the path, the following exception is produced.

InvalidOperationException: Response Content-Length mismatch: too many bytes written (558 of 13).

Exceptions (if any)

System.InvalidOperationException: Response Content-Length mismatch: too many bytes written (558 of 13).
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.VerifyAndUpdateWrite(Int32 count)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.FirstWriteAsyncInternal(ReadOnlyMemory`1 data, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.WritePipeAsync(ReadOnlyMemory`1 data, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseStream.WriteAsync(ReadOnlyMemory`1 source, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.StreamCopyOperationInternal.CopyToAsync(Stream source, Stream destination, Nullable`1 count, Int32 bufferSize, CancellationToken cancel)
   at Microsoft.AspNetCore.Http.SendFileFallback.SendFileAsync(Stream destination, String filePath, Int64 offset, Nullable`1 count, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.SendFileResponseExtensions.SendFileAsyncCore(HttpResponse response, String fileName, Int64 offset, Nullable`1 count, CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteResultWriteResponse(IResult result, HttpContext httpContext)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

.NET Version

6.0.101

Anything else?

Marry Christmas 🎄

changed the title Microsoft.AspNetCore.Http.Results.File fails to determine the correct file size of symlinks Microsoft.AspNetCore.Http.Results.File() fails to determine the correct file size of symlinks Dec 23, 2021

@anorm what you can do right now, while the fix is not available, is use one of the other Results.File overloads that calculates the file size using different approaches.

File(Stream, String, String, Nullable, EntityTagHeaderValue, Boolean)

File(Byte[], String, String, Boolean, Nullable, EntityTagHeaderValue)

Thanks for contacting us.

We're moving this issue to the .NET 7 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

Using symbolic link APIs on PhysicalFileResult to obtain the real Length and LastModifiedTime #39330 Using symbolic link APIs on PhysicalFileResult to obtain the real Length and LastModifiedTime brunolins16/aspnetcore