Fix signature v2 handling for resource names (#4965)

Previously we were wrongly adding `?` as part
of the resource name, add a test case to check
if this is handled properly.

Thanks to @kannappanr for reproducing this.

Without this change presigned URL generated with following
command would fail with signature mismatch.
```
aws s3 presign s3://testbucket/functional-tests.sh
```
This commit is contained in:
Harshavardhana
2017-09-26 11:00:07 -07:00
committed by Dee Koder
parent 0bf981278e
commit 6dcfaa877c
3 changed files with 66 additions and 17 deletions

View File

@@ -287,7 +287,7 @@ func canonicalizedAmzHeadersV2(headers http.Header) string {
}
// Return canonical resource string.
func canonicalizedResourceV2(encodedQuery string) string {
func canonicalizedResourceV2(encodedResource, encodedQuery string) string {
queries := strings.Split(encodedQuery, "&")
keyval := make(map[string]string)
for _, query := range queries {
@@ -316,7 +316,11 @@ func canonicalizedResourceV2(encodedQuery string) string {
// The queries will be already sorted as resourceList is sorted, if canonicalQueries
// is empty strings.Join returns empty.
return strings.Join(canonicalQueries, "&")
canonicalQuery := strings.Join(canonicalQueries, "&")
if canonicalQuery != "" {
return encodedResource + "?" + canonicalQuery
}
return encodedResource
}
// Return string to sign under two different conditions.
@@ -350,16 +354,5 @@ func getStringToSignV2(method string, encodedResource, encodedQuery string, head
canonicalHeaders,
}, "\n")
// For presigned signature no need to filter out based on resourceList,
// just sign whatever is with the request.
if expires != "" {
return stringToSign + encodedResource + "?" + encodedQuery
}
canonicalResource := canonicalizedResourceV2(encodedQuery)
if canonicalResource != "" {
return stringToSign + encodedResource + "?" + canonicalResource
}
return stringToSign + encodedResource
return stringToSign + canonicalizedResourceV2(encodedResource, encodedQuery)
}