From b63c37b28ee0f9c76ac6ed1ac3ff1c9837421cad Mon Sep 17 00:00:00 2001 From: kannappanr <30541348+kannappanr@users.noreply.github.com> Date: Mon, 13 Nov 2017 16:30:20 -0800 Subject: [PATCH] Return MethodNotAllowed error in PostPolicyBucketHandler if URL contains object name (#5142) S3 spec requires that MethodNotAllowed error be return if object name is part of the URL. Fix postpolicy related unit tests to not set object name as part of target URL. Fixes #5141 --- cmd/bucket-handlers.go | 9 ++++++++- cmd/post-policy_test.go | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cmd/bucket-handlers.go b/cmd/bucket-handlers.go index de596a112..b150f32bf 100644 --- a/cmd/bucket-handlers.go +++ b/cmd/bucket-handlers.go @@ -24,6 +24,7 @@ import ( "net/http" "net/url" "path" + "path/filepath" "reflect" "strings" "sync" @@ -438,6 +439,13 @@ func (api objectAPIHandlers) PostPolicyBucketHandler(w http.ResponseWriter, r *h return } + // Make sure that the URL does not contain object name. + bucket := mux.Vars(r)["bucket"] + if bucket != filepath.Clean(r.URL.Path[1:]) { + writeErrorResponse(w, ErrMethodNotAllowed, r.URL) + return + } + // Require Content-Length to be set in the request size := r.ContentLength if size < 0 { @@ -482,7 +490,6 @@ func (api objectAPIHandlers) PostPolicyBucketHandler(w http.ResponseWriter, r *h // Close multipart file defer fileBody.Close() - bucket := mux.Vars(r)["bucket"] formValues.Set("Bucket", bucket) if fileName != "" && strings.Contains(formValues.Get("Key"), "${filename}") { diff --git a/cmd/post-policy_test.go b/cmd/post-policy_test.go index 9b2509b4a..11e908b62 100644 --- a/cmd/post-policy_test.go +++ b/cmd/post-policy_test.go @@ -558,7 +558,7 @@ func newPostRequestV2(endPoint, bucketName, objectName string, accessKey, secret // Set the body equal to the created policy. reader := bytes.NewReader(buf.Bytes()) - req, err := http.NewRequest("POST", makeTestTargetURL(endPoint, bucketName, objectName, nil), reader) + req, err := http.NewRequest("POST", makeTestTargetURL(endPoint, bucketName, "", nil), reader) if err != nil { return nil, err } @@ -636,7 +636,7 @@ func newPostRequestV4Generic(endPoint, bucketName, objectName string, objData [] // Set the body equal to the created policy. reader := bytes.NewReader(buf.Bytes()) - req, err := http.NewRequest("POST", makeTestTargetURL(endPoint, bucketName, objectName, nil), reader) + req, err := http.NewRequest("POST", makeTestTargetURL(endPoint, bucketName, "", nil), reader) if err != nil { return nil, err }