Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
Fixed timing attack in cookie validation.
Browse files Browse the repository at this point in the history
- Changed from using string == to hmac.Equal
- See more details here: http://verboselogging.com/2012/08/20/a-timing-attack-in-action
  • Loading branch information
Vikrum Nijjar authored and jehiah committed Nov 8, 2014
1 parent 2f16534 commit ad57a93
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cookies.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func validateCookie(cookie *http.Cookie, seed string) (string, bool) {
return "", false
}
sig := cookieSignature(seed, cookie.Name, parts[0], parts[1])
if parts[2] == sig {
if checkHmac(parts[2], sig) {
ts, err := strconv.Atoi(parts[1])
if err == nil && int64(ts) > time.Now().Add(time.Duration(24)*7*time.Hour*-1).Unix() {
// it's a valid cookie. now get the contents
Expand Down Expand Up @@ -48,3 +48,14 @@ func cookieSignature(args ...string) string {
b = h.Sum(b)
return base64.URLEncoding.EncodeToString(b)
}

func checkHmac(input, expected string) bool {
inputMAC, err1 := base64.URLEncoding.DecodeString(input)
if err1 == nil {
expectedMAC, err2 := base64.URLEncoding.DecodeString(expected)
if err2 == nil {
return hmac.Equal(inputMAC, expectedMAC)
}
}
return false
}

0 comments on commit ad57a93

Please sign in to comment.