Permalink
Browse files

Due to bug in golang base64 library we must manage cookie padding our…

…selves
  • Loading branch information...
paulbellamy committed Jun 16, 2011
1 parent c53edfe commit 3b7d44e606b2801b3641d5effdd62b015e2877eb
Showing with 22 additions and 5 deletions.
  1. +21 −4 sessions.go
  2. +1 −1 sessions_test.go
View
@@ -29,9 +29,18 @@ func decodeGob(value string) (result map[string]interface{}) {
return result
}
// Due to a bug in golang where when using
// base64.URLEncoding padding is still added
// (it shouldn't be), we have to strip and add
// it ourselves.
func pad64(value string) (result string) {
padding := strings.Repeat("=", len(value)%4)
return strings.Join([]string{value, padding}, "")
}
func decode64(value string) (result string) {
buffer := bytes.NewBufferString(value)
encoder := base64.NewDecoder(base64.StdEncoding, buffer)
buffer := bytes.NewBufferString(pad64(value))
encoder := base64.NewDecoder(base64.URLEncoding, buffer)
decoded, _ := ioutil.ReadAll(encoder)
return string(decoded)
}
@@ -61,12 +70,20 @@ func encodeGob(value interface{}) (result string) {
return buffer.String()
}
// Due to a bug in golang where when using
// base64.URLEncoding padding is still added
// (it shouldn't be), we have to strip and add
// it ourselves.
func dePad64(value string) (result string) {
return strings.TrimRight(value, "=")
}
func encode64(value string) (result string) {
buffer := &bytes.Buffer{}
encoder := base64.NewEncoder(base64.StdEncoding, buffer)
encoder := base64.NewEncoder(base64.URLEncoding, buffer)
encoder.Write([]byte(value))
encoder.Close()
return buffer.String()
return dePad64(buffer.String())
}
func encodeCookie(value map[string]interface{}, secret string) (cookie string) {
View
@@ -35,7 +35,7 @@ func TestSessions(t *testing.T) {
// base 64 encoded, hmac-hashed, and gob encoded stuff
cookie := headers.Get("Set-Cookie")
expected_cookie := "my_key=Dv+BBAEC/4IAAQwBEAAANf+CAAEOdGVzdF9hdHRyaWJ1dGUGc3RyaW5nDBkAF05ldmVyIGdvbm5hIGdpdmUgeW91IHVw--bdHyJ5lvPpk6EoZiSSSiHKZtQHk=; Domain=.my.domain.com;"
expected_cookie := "my_key=Dv-BBAEC_4IAAQwBEAAANf-CAAEOdGVzdF9hdHRyaWJ1dGUGc3RyaW5nDBkAF05ldmVyIGdvbm5hIGdpdmUgeW91IHVw--bdHyJ5lvPpk6EoZiSSSiHKZtQHk; Domain=.my.domain.com;"
if cookie != expected_cookie {
t.Error("Expected Set-Cookie to equal: \"", expected_cookie, "\" got: \"", cookie, "\"")
}

0 comments on commit 3b7d44e

Please sign in to comment.