From 8c3ceebcfcaf622ecbbb5f6434cfba62585a886b Mon Sep 17 00:00:00 2001 From: Adam Jensen Date: Sun, 3 Jun 2018 12:37:45 -0400 Subject: [PATCH] If size is deferred, then constrain how much of the request body to read --- unrouted_handler.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/unrouted_handler.go b/unrouted_handler.go index dd5acab..da07812 100644 --- a/unrouted_handler.go +++ b/unrouted_handler.go @@ -5,6 +5,7 @@ import ( "errors" "io" "log" + "math" "net" "net/http" "os" @@ -504,6 +505,18 @@ func (handler *UnroutedHandler) writeChunk(id string, info FileInfo, w http.Resp } maxSize := info.Size - offset + // If the upload's length is deferred and the PATCH request does not contain the Content-Length + // header (which is allowed if 'Transfer-Encoding: chunked' is used), we still need to set limits for + // the body size. + if info.SizeIsDeferred { + if handler.config.MaxSize > 0 { + // Ensure that the upload does not exceed the maximum upload size + maxSize = handler.config.MaxSize - offset + } else { + // If no upload limit is given, we allow arbitrary sizes + maxSize = math.MaxInt64 + } + } if length > 0 { maxSize = length }