git.iko.soy/typeable/oauth2-proxy
on master help sign in
Count complete cookie content in byte splitting
commit48a2aaadc1a8dcb917870fcab60848c7dbede5dd authorNick Meves <nick.meves@greenhouse.io> dateFri 3 Jul 2020, 23:41 (6 years ago) parentc6f1dab
2 files changed+53−49
pkg/sessions/cookie/session_store.go+17−9 pkg/sessions/cookie/session_store_test.go+36−40
pkg/sessions/cookie/session_store.go+17−9
@@ -15,6 +15,13 @@ import (
1515 "github.com/oauth2-proxy/oauth2-proxy/pkg/logger"
1616)
1717
18+const (
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
23+)
24+
1825// Ensure CookieSessionStore implements the interface
1926var _ sessions.SessionStore = &SessionStore{}
2027
@@ -102,9 +109,7 @@ func (s *SessionStore) makeSessionCookie(req *http.Request, value string, now ti
102109 }
103110 c := s.makeCookie(req, s.CookieOptions.Name, value, s.CookieOptions.Expire, now)
104111
105− // Cookies are limited to 4kb including the length of the cookie name,
106− // the cookie name can be up to 256 bytes
107− if len(c.Value) > 4096-len(s.CookieOptions.Name) {
112+ if len(c.String()) > maxCookieLength {
108113 return splitCookie(c)
109114 }
110115 return []*http.Cookie{c}
@@ -139,7 +144,7 @@ func NewCookieSessionStore(opts *options.SessionOptions, cookieOpts *options.Coo
139144// it into a slice of cookies which fit within the 4kb cookie limit indexing
140145// the cookies from 0
141146func splitCookie(c *http.Cookie) []*http.Cookie {
142− if len(c.Value) < 4096-len(c.Name) {
147+ if len(c.String()) < maxCookieLength {
143148 return []*http.Cookie{c}
144149 }
145150
@@ -153,13 +158,16 @@ func splitCookie(c *http.Cookie) []*http.Cookie {
153158 newCookie.Name = splitCookieName(c.Name, count)
154159 count++
155160
156− maxCookieLength := 4096 - len(newCookie.Name)
157− if len(valueBytes) < maxCookieLength {
158− newCookie.Value = string(valueBytes)
161+ newCookie.Value = string(valueBytes)
162+ cookieLength := len(newCookie.String())
163+ if cookieLength <= maxCookieLength {
159164 valueBytes = []byte{}
160165 } else {
161− newValue := valueBytes[:maxCookieLength]
162− valueBytes = valueBytes[maxCookieLength:]
166+ overflow := cookieLength - maxCookieLength
167+ valueSize := len(valueBytes) - overflow
168+
169+ newValue := valueBytes[:valueSize]
170+ valueBytes = valueBytes[valueSize:]
163171 newCookie.Value = string(newValue)
164172 }
165173 cookies = append(cookies, newCookie)
pkg/sessions/cookie/session_store_test.go+36−40
@@ -54,59 +54,55 @@ func Test_copyCookie(t *testing.T) {
5454}
5555
5656func Test_splitCookie(t *testing.T) {
57− testCases := map[string]struct {
58− Cookie *http.Cookie
59− ExpectedSizes []int
60− }{
57+ testCases := map[string]*http.Cookie{
6158 "Short cookie name": {
62− Cookie: &http.Cookie{
63− Name: "short",
64− Value: strings.Repeat("v", 10000),
65− },
66− ExpectedSizes: []int{4089, 4089, 1822},
59+ Name: "short",
60+ Value: strings.Repeat("v", 10000),
6761 },
6862 "Long cookie name": {
69− Cookie: &http.Cookie{
70− Name: strings.Repeat("n", 251),
71− Value: strings.Repeat("a", 10000),
72− },
73− ExpectedSizes: []int{3843, 3843, 2314},
63+ Name: strings.Repeat("n", 251),
64+ Value: strings.Repeat("a", 10000),
7465 },
7566 "Max cookie name": {
76− Cookie: &http.Cookie{
77− Name: strings.Repeat("n", 256),
78− Value: strings.Repeat("a", 10000),
79− },
80− ExpectedSizes: []int{3840, 3840, 2320},
67+ Name: strings.Repeat("n", 256),
68+ Value: strings.Repeat("a", 10000),
8169 },
8270 "Suffix overflow cookie name": {
83− Cookie: &http.Cookie{
84− Name: strings.Repeat("n", 255),
85− Value: strings.Repeat("a", 10000),
86− },
87− ExpectedSizes: []int{3840, 3840, 2320},
71+ Name: strings.Repeat("n", 255),
72+ Value: strings.Repeat("a", 10000),
73+ },
74+ "Double digit suffix cookie name overflow": {
75+ Name: strings.Repeat("n", 253),
76+ Value: strings.Repeat("a", 50000),
8877 },
89− "Double digit suffix cookie name results in different sizes": {
90− Cookie: &http.Cookie{
91− Name: strings.Repeat("n", 253),
92− Value: strings.Repeat("a", 50000),
93− },
94− ExpectedSizes: []int{3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841, 3841,
95− 3840, 3840, 3840, 70},
78+ "With short name and attributes": {
79+ Name: "short",
80+ Value: strings.Repeat("v", 10000),
81+ Path: "/path",
82+ Domain: "x.y.z",
83+ Secure: true,
84+ HttpOnly: true,
85+ SameSite: http.SameSiteLaxMode,
9686 },
97− "Double digit suffix overflow cookie name results in same sizes": {
98− Cookie: &http.Cookie{
99− Name: strings.Repeat("n", 254),
100− Value: strings.Repeat("a", 50000),
101− },
102− ExpectedSizes: []int{3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840, 3840,
103− 3840, 3840, 3840, 80},
87+ "With max length name and attributes": {
88+ Name: strings.Repeat("n", 256),
89+ Value: strings.Repeat("v", 10000),
90+ Path: "/path",
91+ Domain: "x.y.z",
92+ Secure: true,
93+ HttpOnly: true,
94+ SameSite: http.SameSiteLaxMode,
10495 },
10596 }
10697 for testName, tc := range testCases {
10798 t.Run(testName, func(t *testing.T) {
108− for i, cookie := range splitCookie(tc.Cookie) {
109− assert.Equal(t, tc.ExpectedSizes[i], len(cookie.Value))
99+ splitCookies := splitCookie(tc)
100+ for i, cookie := range splitCookies {
101+ if i < len(splitCookies)-1 {
102+ assert.Equal(t, 4000, len(cookie.String()))
103+ } else {
104+ assert.GreaterOrEqual(t, 4000, len(cookie.String()))
105+ }
110106 }
111107 })
112108 }