From e17e09ea3c170085611cb3e69bb82ecc193f5efb Mon Sep 17 00:00:00 2001 From: Harshavardhana Date: Wed, 1 Aug 2018 14:19:11 -0700 Subject: [PATCH] Handle POST object upload without filename param (#6221) POST mime/multipart upload style can have filename value optional which leads to implementation issues in Go releases in their standard mime/multipart library. When `filename` doesn't exist Go doesn't update `form.File` which we rely on to extract the incoming file data, strangely when `filename` is not specified this data is buffered in memory and is now part of `form.Value` instead of `form.File` which creates an inconsistent behavior. This PR tries to fix this in our code for the time being, but ideal PR would be to fix the upstream mime/multipart library to handle the above situation consistently. --- cmd/handler-utils.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cmd/handler-utils.go b/cmd/handler-utils.go index 591f826ce..491ba9963 100644 --- a/cmd/handler-utils.go +++ b/cmd/handler-utils.go @@ -17,8 +17,10 @@ package cmd import ( + "bytes" "context" "io" + "io/ioutil" "mime/multipart" "net" "net/http" @@ -235,6 +237,19 @@ func extractPostPolicyFormValues(ctx context.Context, form *multipart.Form) (fil return nil, "", 0, nil, err } + // this means that filename="" was not specified for file key and Go has + // an ugly way of handling this situation. Refer here + // https://golang.org/src/mime/multipart/formdata.go#L61 + if len(form.File) == 0 { + var b = &bytes.Buffer{} + for _, v := range formValues["File"] { + b.WriteString(v) + } + fileSize = int64(b.Len()) + filePart = ioutil.NopCloser(b) + return filePart, fileName, fileSize, formValues, nil + } + // Iterator until we find a valid File field and break for k, v := range form.File { canonicalFormName := http.CanonicalHeaderKey(k) @@ -269,7 +284,6 @@ func extractPostPolicyFormValues(ctx context.Context, form *multipart.Form) (fil break } } - return filePart, fileName, fileSize, formValues, nil }