@@ -10,15 +10,16 @@ import (
1010
1111 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
1212 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/sessions"
13− "github.com/oauth2-proxy/oauth2-proxy/pkg/cookies"
13+ pkgcookies "github.com/oauth2-proxy/oauth2-proxy/pkg/cookies"
1414 "github.com/oauth2-proxy/oauth2-proxy/pkg/encryption"
1515 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
1616)
1717
1818const (
19− // Cookies are limited to 4kb including the length of the cookie name,
20− // the cookie name can be up to 256 bytes
21− maxCookieLength = 3840
19+ // Cookies are limited to 4kb for all parts
20+ // including the cookie name, value, attributes; IE (http.cookie).String()
21+ // Most browsers' max is 4096 -- but we give ourselves some leeway
22+ maxCookieLength = 4000
2223)
2324
2425// Ensure CookieSessionStore implements the interface
@@ -107,14 +108,15 @@ func (s *SessionStore) makeSessionCookie(req *http.Request, value string, now ti
107108 value = encryption.SignedValue(s.CookieOptions.Secret, s.CookieOptions.Name, []byte(value), now)
108109 }
109110 c := s.makeCookie(req, s.CookieOptions.Name, value, s.CookieOptions.Expire, now)
110− if len(c.Value) > 4096-len(s.CookieOptions.Name) {
111+
112+ if len(c.String()) > maxCookieLength {
111113 return splitCookie(c)
112114 }
113115 return []*http.Cookie{c}
114116}
115117
116118func (s *SessionStore) makeCookie(req *http.Request, name string, value string, expiration time.Duration, now time.Time) *http.Cookie {
117− return cookies.MakeCookieFromOptions(
119+ return pkgcookies.MakeCookieFromOptions(
118120 req,
119121 name,
120122 value,
@@ -142,23 +144,30 @@ func NewCookieSessionStore(opts *options.SessionOptions, cookieOpts *options.Coo
142144// it into a slice of cookies which fit within the 4kb cookie limit indexing
143145// the cookies from 0
144146func splitCookie(c *http.Cookie) []*http.Cookie {
145− logger.Printf("WARNING: Multiple cookies are required for this session as it exceeds the 4kb cookie limit. Please use server side session storage (eg. Redis) instead.")
146− if len(c.Value) < maxCookieLength {
147+ if len(c.String()) < maxCookieLength {
147148 return []*http.Cookie{c}
148149 }
150+
151+ logger.Printf("WARNING: Multiple cookies are required for this session as it exceeds the 4kb cookie limit. Please use server side session storage (eg. Redis) instead.")
152+
149153 cookies := []*http.Cookie{}
150154 valueBytes := []byte(c.Value)
151155 count := 0
152156 for len(valueBytes) > 0 {
153157 newCookie := copyCookie(c)
154− newCookie.Name = fmt.Sprintf("%s_%d", c.Name, count)
158+ newCookie.Name = splitCookieName(c.Name, count)
155159 count++
156− if len(valueBytes) < maxCookieLength {
157− newCookie.Value = string(valueBytes)
160+
161+ newCookie.Value = string(valueBytes)
162+ cookieLength := len(newCookie.String())
163+ if cookieLength <= maxCookieLength {
158164 valueBytes = []byte{}
159165 } else {
160− newValue := valueBytes[:maxCookieLength]
161− valueBytes = valueBytes[maxCookieLength:]
166+ overflow := cookieLength - maxCookieLength
167+ valueSize := len(valueBytes) - overflow
168+
169+ newValue := valueBytes[:valueSize]
170+ valueBytes = valueBytes[valueSize:]
162171 newCookie.Value = string(newValue)
163172 }
164173 cookies = append(cookies, newCookie)
@@ -166,6 +175,15 @@ func splitCookie(c *http.Cookie) []*http.Cookie {
166175 return cookies
167176}
168177
178+func splitCookieName(name string, count int) string {
179+ splitName := fmt.Sprintf("%s_%d", name, count)
180+ overflow := len(splitName) - 256
181+ if overflow > 0 {
182+ splitName = fmt.Sprintf("%s_%d", name[:len(name)-overflow], count)
183+ }
184+ return splitName
185+}
186+
169187// loadCookie retreieves the sessions state cookie from the http request.
170188// If a single cookie is present this will be returned, otherwise it attempts
171189// to reconstruct a cookie split up by splitCookie
@@ -179,7 +197,7 @@ func loadCookie(req *http.Request, cookieName string) (*http.Cookie, error) {
179197 count := 0
180198 for err == nil {
181199 var c *http.Cookie
182− c, err = req.Cookie(fmt.Sprintf("%s_%d", cookieName, count))
200+ c, err = req.Cookie(splitCookieName(cookieName, count))
183201 if err == nil {
184202 cookies = append(cookies, c)
185203 count++