[x] I have verified that I am running the latest version of Nancy
[x] I have verified if the problem exist in both DEBUG and RELEASE mode
[x] I have searched open and closed issues to ensure it has not already been reported
Description
When uploading a large file using Nancy (>2GB) I get a "Stream was too long" error as it looks like the entire upload request stream is attempting to be loaded into memory.
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLPQTF9FBMQ6", Request id "0HLPQTF9FBMQ6:00000001": An unhandled exception was thrown by the application.
System.IO.IOException: Stream was too long.
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.Stream.CopyTo(Stream destination, Int32 bufferSize)
at Nancy.IO.RequestStream..ctor(Stream stream, Int64 expectedLength, Int64 thresholdLength, Boolean disableStreamSwitching)
at Nancy.Owin.NancyMiddleware.<>c__DisplayClass2_1.<<UseNancy>b__1>d.MoveNext()
I've narrowed this down to the NancyMiddleware class, ExpectedLength method. It's has a long return type but is parsing the content length string using int:
return int.TryParse(header, NumberStyles.Any, CultureInfo.InvariantCulture, out int contentLength) ? contentLength : (long?)null;
This cannot parse a long value and returns null, which passes an expected length of 0 to RequestStream which causes it to try to read the whole upload stream into memory.
I'm not sure if this is intentional or just overlooked, but I figured I would raise an issue before opening a pull request. Checking out Nancy locally and building with long.TryParse works correctly (despite being slow).
Steps to Reproduce
Run a Nancy sample app.
POST a > 2GB file to any endpoint.
Get 500 response and see "Stream was too large" error in console.
System Configuration
Running in Visual Studio 2019 and uploading a 4gb file with postman (doesn't currently support chunked transfer encoding).
Nancy version: 2.0.0
Nancy host
[ ] Nancy.Hosting.Aspnet
[ ] Nancy.Hosting.Self
[x] Nancy.Owin (Kestrel)
[ ] Other:
Other Nancy packages and versions: Nancy.Serialization.JsonNet 2.0.0-clinteastwood
Environment (Operating system, version and so on): Windows 10
Prerequisites
DEBUG
andRELEASE
modeDescription
When uploading a large file using Nancy (>2GB) I get a "Stream was too long" error as it looks like the entire upload request stream is attempting to be loaded into memory.
I've narrowed this down to the NancyMiddleware class, ExpectedLength method. It's has a
long
return type but is parsing the content length string usingint
:This cannot parse a
long
value and returnsnull
, which passes an expected length of0
toRequestStream
which causes it to try to read the whole upload stream into memory.I'm not sure if this is intentional or just overlooked, but I figured I would raise an issue before opening a pull request. Checking out Nancy locally and building with
long.TryParse
works correctly (despite being slow).Steps to Reproduce
Run a Nancy sample app. POST a > 2GB file to any endpoint. Get 500 response and see "Stream was too large" error in console.
System Configuration
Running in Visual Studio 2019 and uploading a 4gb file with postman (doesn't currently support chunked transfer encoding).