Skip to content

Commit

Permalink
Fixes aws#642. Issue with spacing in headers for s3 v4 signing
Browse files Browse the repository at this point in the history
  • Loading branch information
xibz committed Apr 21, 2016
1 parent 4511363 commit 13939e5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
22 changes: 21 additions & 1 deletion private/signer/v4/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ func (v4 *signer) buildCanonicalHeaders(r rule, header http.Header) {
}
}

v4.canonicalHeaders = strings.Join(headerValues, "\n")
v4.canonicalHeaders = strings.Join(stripExcessSpaces(headerValues), "\n")
}

func (v4 *signer) buildCanonicalString() {
Expand Down Expand Up @@ -443,3 +443,23 @@ func makeSha256Reader(reader io.ReadSeeker) []byte {
io.Copy(hash, reader)
return hash.Sum(nil)
}

func stripExcessSpaces(headerVals []string) []string {
vals := make([]string, len(headerVals))
for i, str := range headerVals {
stripped := ""
found := false
str = strings.TrimSpace(str)
for _, c := range str {
if !found && c == ' ' {
stripped += string(c)
found = true
} else if c != ' ' {
stripped += string(c)
found = false
}
}
vals[i] = stripped
}
return vals
}
29 changes: 29 additions & 0 deletions private/signer/v4/v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,35 @@ func TestResignRequestExpiredRequest(t *testing.T) {
assert.NotEqual(t, querySig, r.HTTPRequest.Header.Get("Authorization"))
}

func TestStripExcessHeaders(t *testing.T) {
vals := []string{
"123",
"1 2 3",
" 1 2 3",
"1 2 3",
"1 23",
"1 2 3",
"1 2 ",
" 1 2 ",
}

expected := []string{
"123",
"1 2 3",
"1 2 3",
"1 2 3",
"1 23",
"1 2 3",
"1 2",
"1 2",
}

newVals := stripExcessSpaces(vals)
for i := 0; i < len(newVals); i++ {
assert.Equal(t, newVals[i], expected[i])
}
}

func BenchmarkPresignRequest(b *testing.B) {
signer := buildSigner("dynamodb", "us-east-1", time.Now(), 300*time.Second, "{}")
for i := 0; i < b.N; i++ {
Expand Down

0 comments on commit 13939e5

Please sign in to comment.