Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cookie splitting to account for _ #234

Merged
merged 1 commit into from
Apr 8, 2020
Merged
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
11 changes: 6 additions & 5 deletions pkg/cookie/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func setCookie(w http.ResponseWriter, r *http.Request, val string, maxAge int) {
// Cookie get the vouch jwt cookie
func Cookie(r *http.Request) (string, error) {

var cookieParts []string
cookieParts := make([]string, 0)
var numParts = -1

var err error
Expand All @@ -91,12 +91,13 @@ func Cookie(r *http.Request) (string, error) {
if cookie.Name == cfg.Cfg.Cookie.Name {
return cookie.Value, nil
}
if strings.HasPrefix(cookie.Name, fmt.Sprintf("%s_", cfg.Cfg.Cookie.Name)) {
cookieUnder := fmt.Sprintf("%s_", cfg.Cfg.Cookie.Name)
if strings.HasPrefix(cookie.Name, cookieUnder) {
log.Debugw("cookie",
"cookieName", cookie.Name,
"cookieValue", cookie.Value,
)
xOFy := strings.Split(cookie.Name, "_")[1]
xOFy := strings.Replace(cookie.Name, cookieUnder, "", 1)
xyArray := strings.Split(xOFy, "of")
if numParts == -1 { // then its uninitialized
if numParts, err = strconv.Atoi(xyArray[1]); err != nil {
Expand All @@ -116,7 +117,7 @@ func Cookie(r *http.Request) (string, error) {
// combinedCookieStr := combinedCookie.String()
combinedCookieStr := strings.Join(cookieParts, "")
if combinedCookieStr == "" {
return "", errors.New("Cookie token empty")
return "", errors.New("cookie token empty")
}

log.Debugw("combined cookie",
Expand Down Expand Up @@ -153,7 +154,7 @@ func ClearCookie(w http.ResponseWriter, r *http.Request) {

// SplitCookie separate string into several strings of specified length
func SplitCookie(longString string, maxLen int) []string {
splits := []string{}
splits := make([]string, 0)

var l, r int
for l, r = 0, maxLen; r < len(longString); l, r = r, r+maxLen {
Expand Down
27 changes: 27 additions & 0 deletions pkg/cookie/cookie_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cookie

import (
"fmt"
"net/http"
"reflect"
"testing"

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

func TestSplitCookie(t *testing.T) {
Expand All @@ -25,3 +29,26 @@ func TestSplitCookie(t *testing.T) {
})
}
}

func TestCookie(t *testing.T) {
cfg.Cfg.Cookie.Name = "_alpha_beta"
ckValue1 := "charlie"
ckValue2 := "delta"
expectedValue := fmt.Sprintf("%s%s", ckValue1, ckValue2)
r := &http.Request{
Header: map[string][]string{
"Cookie": {
fmt.Sprintf("%s_1of2=%s", cfg.Cfg.Cookie.Name, ckValue1),
fmt.Sprintf("%s_2of2=%s", cfg.Cfg.Cookie.Name, ckValue2),
},
},
}
r.Cookies()
s, err := Cookie(r)
if err != nil {
t.Error(err)
}
if expectedValue != s {
t.Errorf("expected \"%s\" received \"%s\"", expectedValue, s)
}
}