9 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
10 . "github.com/onsi/gomega"
13func TestValidateCookie(t *testing.T) {
14 alphabet := "abcdefghijklmnopqrstuvwxyz"
16 validName := "_oauth2_proxy"
17 invalidName := "_oauth2;proxy" // Separater character not allowed
18 // 10 times the alphabet should be longer than 256 characters
19 longName := strings.Repeat(alphabet, 10)
20 validSecret := "secretthirtytwobytes+abcdefghijk"
21 invalidSecret := "abcdef" // 6 bytes is not a valid size
22 validBase64Secret := "c2VjcmV0dGhpcnR5dHdvYnl0ZXMrYWJjZGVmZ2hpams" // Base64 encoding of "secretthirtytwobytes+abcdefghijk"
23 invalidBase64Secret := "YWJjZGVmCg" // Base64 encoding of "abcdef"
24 emptyDomains := []string{}
33 // Create a temporary file for the valid secret file test
34 tmpfile, err := os.CreateTemp("", "cookie-secret-test")
36 t.Fatalf("Failed to create temporary file: %v", err)
38 defer os.Remove(tmpfile.Name())
40 // Write a valid 32-byte secret to the file
41 _, err = tmpfile.Write([]byte(validSecret))
43 t.Fatalf("Failed to write to temporary file: %v", err)
47 invalidNameMsg := "invalid cookie name: \"_oauth2;proxy\""
48 longNameMsg := "cookie name should be under 256 characters: cookie name is 260 characters"
49 missingSecretMsg := "missing setting: cookie-secret or cookie-secret-file"
50 invalidSecretMsg := "cookie_secret must be 16, 24, or 32 bytes to create an AES cipher, but is 6 bytes"
51 invalidBase64SecretMsg := "cookie_secret must be 16, 24, or 32 bytes to create an AES cipher, but is 10 bytes"
52 refreshLongerThanExpireMsg := "cookie_refresh (\"1h0m0s\") must be less than cookie_expire (\"15m0s\")"
53 invalidSameSiteMsg := "cookie_samesite (\"invalid\") must be one of ['', 'lax', 'strict', 'none']"
55 testCases := []struct {
61 name: "with valid configuration",
62 cookie: options.Cookie{
68 Refresh: 15 * time.Minute,
73 errStrings: []string{},
76 name: "with no cookie secret",
77 cookie: options.Cookie{
80 Domains: emptyDomains,
83 Refresh: 15 * time.Minute,
93 name: "with an invalid cookie secret",
94 cookie: options.Cookie{
96 Secret: invalidSecret,
97 Domains: emptyDomains,
100 Refresh: 15 * time.Minute,
105 errStrings: []string{
110 name: "with a valid Base64 secret",
111 cookie: options.Cookie{
113 Secret: validBase64Secret,
114 Domains: emptyDomains,
117 Refresh: 15 * time.Minute,
122 errStrings: []string{},
125 name: "with an invalid Base64 secret",
126 cookie: options.Cookie{
128 Secret: invalidBase64Secret,
129 Domains: emptyDomains,
132 Refresh: 15 * time.Minute,
137 errStrings: []string{
138 invalidBase64SecretMsg,
142 name: "with an invalid name",
143 cookie: options.Cookie{
146 Domains: emptyDomains,
149 Refresh: 15 * time.Minute,
154 errStrings: []string{
159 name: "with a name that is too long",
160 cookie: options.Cookie{
163 Domains: emptyDomains,
166 Refresh: 15 * time.Minute,
171 errStrings: []string{
176 name: "with refresh longer than expire",
177 cookie: options.Cookie{
180 Domains: emptyDomains,
182 Expire: 15 * time.Minute,
188 errStrings: []string{
189 refreshLongerThanExpireMsg,
193 name: "with samesite \"none\"",
194 cookie: options.Cookie{
197 Domains: emptyDomains,
200 Refresh: 15 * time.Minute,
205 errStrings: []string{},
208 name: "with samesite \"lax\"",
209 cookie: options.Cookie{
212 Domains: emptyDomains,
215 Refresh: 15 * time.Minute,
220 errStrings: []string{},
223 name: "with samesite \"strict\"",
224 cookie: options.Cookie{
227 Domains: emptyDomains,
230 Refresh: 15 * time.Minute,
235 errStrings: []string{},
238 name: "with samesite \"invalid\"",
239 cookie: options.Cookie{
242 Domains: emptyDomains,
245 Refresh: 15 * time.Minute,
250 errStrings: []string{
255 name: "with a combination of configuration errors",
256 cookie: options.Cookie{
258 Secret: invalidSecret,
261 Expire: 15 * time.Minute,
267 errStrings: []string{
270 refreshLongerThanExpireMsg,
275 name: "with session cookie configuration",
276 cookie: options.Cookie{
282 Refresh: 15 * time.Minute,
287 errStrings: []string{},
290 name: "with valid secret file",
291 cookie: options.Cookie{
294 SecretFile: tmpfile.Name(),
297 Expire: 24 * time.Hour,
303 errStrings: []string{},
306 name: "with nonexistent secret file",
307 cookie: options.Cookie{
310 SecretFile: "/nonexistent/file.txt",
313 Expire: 24 * time.Hour,
319 errStrings: []string{"could not read cookie secret file: /nonexistent/file.txt"},
323 for _, tc := range testCases {
324 t.Run(tc.name, func(t *testing.T) {
325 errStrings := validateCookie(tc.cookie)
328 g.Expect(errStrings).To(ConsistOf(tc.errStrings))
329 // Check domains were sorted to the right lengths
330 for i := 0; i < len(tc.cookie.Domains)-1; i++ {
331 g.Expect(len(tc.cookie.Domains[i])).To(BeNumerically(">=", len(tc.cookie.Domains[i+1])))
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list