Skip to content

Commit bbb68ed

Browse files
committed
Add check for secure cookie structure
A secure cookie has three parts separated by the pipe ("|") character. Before trying to parse it, ensure there are actually three parts. This is a potential fix for hoisie#163
1 parent 07b2986 commit bbb68ed

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

web.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ func (ctx *Context) GetSecureCookie(name string) (string, bool) {
145145
}
146146

147147
parts := strings.SplitN(cookie.Value, "|", 3)
148+
if len(parts) != 3 {
149+
return "", false
150+
}
148151

149152
val := parts[0]
150153
timestamp := parts[1]

web_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,17 @@ func TestSecureCookie(t *testing.T) {
496496
}
497497
}
498498

499+
func TestEmptySecureCookie(t *testing.T) {
500+
mainServer.Config.CookieSecret = "7C19QRmwf3mHZ9CPAaPQ0hsWeufKd"
501+
cookies := makeCookie(map[string]string{"empty": ""})
502+
503+
resp2 := getTestResponse("GET", "/securecookie/get/empty", "", nil, cookies)
504+
505+
if resp2.body != "" {
506+
t.Fatalf("Expected an empty secure cookie")
507+
}
508+
}
509+
499510
func TestEarlyClose(t *testing.T) {
500511
var server1 Server
501512
server1.Close()

0 commit comments

Comments
 (0)