Skip to content

Commit

Permalink
Fix spaces in V4 query encoding
Browse files Browse the repository at this point in the history
Go's url.QueryEscape encodes a space as '+' but Amazon require '%20' when
calculating the canonical request.
  • Loading branch information
kuwerty committed Oct 17, 2014
1 parent eaf3e74 commit 1b83e03
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,13 @@ func shouldEscape(c byte) bool {
}
return true
}

func normquery(v url.Values) string {
qs := v.Encode()

// Go encodes a space as '+' but Amazon require '%20'. Luckily any '+' in the
// original query string has been percent escaped so all '+' chars that are left
// were originally spaces.

return strings.Replace(qs, "+", "%20", -1)
}
2 changes: 1 addition & 1 deletion sign4.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func hashedCanonicalRequestV4(req *http.Request, meta *metadata) string {
headersToSign += key + ":" + value + "\n"
}
meta.signedHeaders = concat(";", sortedHeaderKeys...)
canonicalRequest := concat("\n", req.Method, normuri(req.URL.Path), req.URL.Query().Encode(), headersToSign, meta.signedHeaders, payloadHash)
canonicalRequest := concat("\n", req.Method, normuri(req.URL.Path), normquery(req.URL.Query()), headersToSign, meta.signedHeaders, payloadHash)

return hashSHA256([]byte(canonicalRequest))
}
Expand Down

0 comments on commit 1b83e03

Please sign in to comment.