Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/cookie/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ func Cookie(r *http.Request) (string, error) {
)
xOFy := strings.Replace(cookie.Name, cookieUnder, "", 1)
xyArray := strings.Split(xOFy, "of")
if len(xyArray) != 2 {
return "", fmt.Errorf("multipart cookie fail: invalid part count %s", xOFy)
}
if numParts == -1 { // then its uninitialized
if numParts, err = strconv.Atoi(xyArray[1]); err != nil {
return "", fmt.Errorf("multipart cookie fail: %s", err)
Expand All @@ -121,7 +124,7 @@ func Cookie(r *http.Request) (string, error) {
if i, err = strconv.Atoi(xyArray[0]); err != nil {
return "", fmt.Errorf("multipart cookie fail: %s", err)
}
if i > numParts {
if i < 1 || i > numParts {
return "", fmt.Errorf("multipart cookie fail: invalid part count %s", xOFy)
}
cookieParts[i-1] = cookie.Value
Expand Down
24 changes: 24 additions & 0 deletions pkg/cookie/malformed_cookie_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cookie

import (
"net/http"
"testing"

"github.com/vouch/vouch-proxy/pkg/cfg"
)

func TestMalformedMultipartCookieRejected(t *testing.T) {
cfg.Cfg.Cookie.Name = "vouch"
for _, ck := range []string{"vouch_1=x", "vouch_0of1=x", "vouch_-1of2=x", "vouch_2of1=x"} {
r := &http.Request{Header: map[string][]string{"Cookie": {ck}}}
if _, err := Cookie(r); err == nil {
t.Errorf("malformed cookie %q should return an error, got nil", ck)
}
}
// a valid multipart cookie still reassembles
r := &http.Request{Header: map[string][]string{"Cookie": {"vouch_1of2=foo", "vouch_2of2=bar"}}}
v, err := Cookie(r)
if err != nil || v != "foobar" {
t.Errorf("valid multipart cookie broke: got %q err=%v", v, err)
}
}