Skip to content

Commit 32aca7a

Browse files
committed
Merge pull request #21 from sunfmin/master
Base64 URLEncoding could generate "-", So it's not safe to use "--" as sign separator. I encounter this tricky bug quite frequently
2 parents f7e26f6 + c5e0930 commit 32aca7a

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

sessions.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package mango
22

33
import (
44
"bytes"
5-
"hash"
65
"crypto/hmac"
76
"crypto/sha1"
87
"encoding/base64"
8+
"encoding/gob"
99
"fmt"
10+
"hash"
1011
"io/ioutil"
11-
"encoding/gob"
1212
"net/http"
1313
"strings"
1414
)
@@ -50,7 +50,7 @@ func decode64(value string) (result string) {
5050
func decodeCookie(value, secret string) (cookie map[string]interface{}) {
5151
cookie = make(map[string]interface{})
5252

53-
split := strings.Split(string(value), "--")
53+
split := strings.Split(string(value), "/")
5454

5555
if len(split) < 2 {
5656
return cookie
@@ -91,7 +91,7 @@ func encode64(value string) (result string) {
9191
func encodeCookie(value map[string]interface{}, secret string) (cookie string) {
9292
data := encodeGob(value)
9393

94-
return fmt.Sprintf("%s--%s", encode64(data), encode64(hashCookie(data, secret)))
94+
return fmt.Sprintf("%s/%s", encode64(data), encode64(hashCookie(data, secret)))
9595
}
9696

9797
func prepareSession(env Env, key, secret string) {
@@ -106,19 +106,31 @@ func prepareSession(env Env, key, secret string) {
106106
env["mango.session"] = make(map[string]interface{})
107107
}
108108

109-
func commitSession(headers Headers, env Env, key, secret, domain string) {
109+
func commitSession(headers Headers, env Env, key, secret string, options *CookieOptions) {
110110
cookie := new(http.Cookie)
111111
cookie.Name = key
112112
cookie.Value = encodeCookie(env["mango.session"].(map[string]interface{}), secret)
113-
cookie.Domain = domain
113+
cookie.Path = options.Path
114+
cookie.Domain = options.Domain
115+
cookie.MaxAge = options.MaxAge
116+
cookie.Secure = options.Secure
117+
cookie.HttpOnly = options.HttpOnly
114118
headers.Add("Set-Cookie", cookie.String())
115119
}
116120

117-
func Sessions(secret, key, domain string) Middleware {
121+
type CookieOptions struct {
122+
Domain string
123+
Path string
124+
MaxAge int
125+
Secure bool
126+
HttpOnly bool
127+
}
128+
129+
func Sessions(secret, key string, options *CookieOptions) Middleware {
118130
return func(env Env, app App) (status Status, headers Headers, body Body) {
119131
prepareSession(env, key, secret)
120132
status, headers, body = app(env)
121-
commitSession(headers, env, key, secret, domain)
133+
commitSession(headers, env, key, secret, options)
122134
return status, headers, body
123135
}
124136
}

sessions_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func TestSessions(t *testing.T) {
3232

3333
// Compile the stack
3434
sessionsStack := new(Stack)
35-
sessionsStack.Middleware(Sessions("my_secret", "my_key", ".my.domain.com"))
35+
sessionsStack.Middleware(Sessions("my_secret", "my_key", &CookieOptions{Domain: ".my.domain.com"}))
3636
sessionsApp := sessionsStack.Compile(sessionsTestServer)
3737

3838
// Request against it
@@ -83,7 +83,7 @@ func BenchmarkSessions(b *testing.B) {
8383
}
8484

8585
sessionsStack := new(Stack)
86-
sessionsStack.Middleware(Sessions("my_secret", "my_key", ".my.domain.com"))
86+
sessionsStack.Middleware(Sessions("my_secret", "my_key", &CookieOptions{Domain: ".my.domain.com"}))
8787
sessionsApp := sessionsStack.Compile(sessionsTestServer)
8888

8989
request, _ := http.NewRequest("GET", "http://localhost:3000/", nil)

0 commit comments

Comments
 (0)