Skip to content

Commit

Permalink
aws/signer/v4: Fix out of bounds panic in stripExcessSpaces
Browse files Browse the repository at this point in the history
Fixes the out of bands panic in stripExcessSpaces caused by an incorrect
calculation of the stripToIdx value. Simplified to code also.

Fix #1411
  • Loading branch information
jasdel committed Jul 21, 2017
1 parent 0aadb9e commit 6802c15
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
26 changes: 14 additions & 12 deletions aws/signer/v4/v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,23 +735,25 @@ func stripExcessSpaces(headerVals []string) []string {

buf := []byte(trimmed)
for idx > -1 {
stripToIdx := -1
for j := idx + 1; j < len(buf); j++ {
idx++ // Start on the second space

stripped := false
for j := idx; j < len(buf); j++ {
if buf[j] != ' ' {
buf = append(buf[:idx+1], buf[j:]...)
stripToIdx = j - idx - 1
buf = append(buf[:idx], buf[j:]...)
stripped = true
break
}
}
if !stripped {
break
}

if stripToIdx >= 0 {
// Find next double space
idx = bytes.Index(buf[stripToIdx:], doubleSpaceBytes)
if idx >= 0 {
idx += stripToIdx
}
} else {
idx = -1
// Find next double space
origIdx := idx
idx = bytes.Index(buf[idx:], doubleSpaceBytes)
if idx > 0 {
idx += origIdx
}
}

Expand Down
4 changes: 4 additions & 0 deletions aws/signer/v4/v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestStripExcessHeaders(t *testing.T) {
" 1 2 ",
"12 3",
"12 3 1",
"12 3 1",
"12 3 1abc123",
}

expected := []string{
Expand All @@ -43,6 +45,8 @@ func TestStripExcessHeaders(t *testing.T) {
"1 2",
"12 3",
"12 3 1",
"12 3 1",
"12 3 1abc123",
}

newVals := stripExcessSpaces(vals)
Expand Down

0 comments on commit 6802c15

Please sign in to comment.