v2 signer: correctly sort headers

The current code determines the header order for the
"string-to-sign" payload by sorting on the concatenation
of headers and values, whereas it should only happen on the
key.

During multipart uploads, since `x-amz-copy-source-range` and
`x-amz-copy-source` headers are present, V2 signatures fail to
validate since header order is swapped.

This patch reverts to the expected behavior.

Signed-off-by: Pierre-Yves Ritschard <pyr@spootnik.org>
This commit is contained in:
Pierre-Yves Ritschard 2016-11-02 17:01:34 +01:00
parent 8234784a1a
commit 775cc6d632

View file

@ -124,6 +124,8 @@ func (v2 *signer) Sign() error {
md5, ctype, date, xamz string md5, ctype, date, xamz string
xamzDate bool xamzDate bool
sarray []string sarray []string
smap map[string]string
sharray []string
) )
headers := v2.Request.Header headers := v2.Request.Header
@ -136,6 +138,7 @@ func (v2 *signer) Sign() error {
v2.Request.Header["Host"] = []string{host} v2.Request.Header["Host"] = []string{host}
v2.Request.Header["date"] = []string{v2.Time.In(time.UTC).Format(time.RFC1123)} v2.Request.Header["date"] = []string{v2.Time.In(time.UTC).Format(time.RFC1123)}
smap = make(map[string]string)
for k, v := range headers { for k, v := range headers {
k = strings.ToLower(k) k = strings.ToLower(k)
switch k { switch k {
@ -150,16 +153,20 @@ func (v2 *signer) Sign() error {
default: default:
if strings.HasPrefix(k, "x-amz-") { if strings.HasPrefix(k, "x-amz-") {
vall := strings.Join(v, ",") vall := strings.Join(v, ",")
sarray = append(sarray, k+":"+vall) smap[k] = k+":"+vall
if k == "x-amz-date" { if k == "x-amz-date" {
xamzDate = true xamzDate = true
date = "" date = ""
} }
sharray = append(sharray, k)
} }
} }
} }
if len(sarray) > 0 { if len(sharray) > 0 {
sort.StringSlice(sarray).Sort() sort.StringSlice(sharray).Sort()
for _, h := range(sharray) {
sarray = append(sarray, smap[h])
}
xamz = strings.Join(sarray, "\n") + "\n" xamz = strings.Join(sarray, "\n") + "\n"
} }