8 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
9 . "github.com/onsi/gomega"
12func TestValidateCookie(t *testing.T) {
13 alphabet := "abcdefghijklmnopqrstuvwxyz"
15 validName := "_oauth2_proxy"
16 invalidName := "_oauth2;proxy" // Separater character not allowed
17 // 10 times the alphabet should be longer than 256 characters
18 longName := strings.Repeat(alphabet, 10)
19 validSecret := "secretthirtytwobytes+abcdefghijk"
20 invalidSecret := "abcdef" // 6 bytes is not a valid size
21 validBase64Secret := "c2VjcmV0dGhpcnR5dHdvYnl0ZXMrYWJjZGVmZ2hpams" // Base64 encoding of "secretthirtytwobytes+abcdefghijk"
22 invalidBase64Secret := "YWJjZGVmCg" // Base64 encoding of "abcdef"
23 emptyDomains := []string{}
32 invalidNameMsg := "invalid cookie name: \"_oauth2;proxy\""
33 longNameMsg := "cookie name should be under 256 characters: cookie name is 260 characters"
34 missingSecretMsg := "missing setting: cookie-secret"
35 invalidSecretMsg := "cookie_secret must be 16, 24, or 32 bytes to create an AES cipher, but is 6 bytes"
36 invalidBase64SecretMsg := "cookie_secret must be 16, 24, or 32 bytes to create an AES cipher, but is 10 bytes"
37 refreshLongerThanExpireMsg := "cookie_refresh (\"1h0m0s\") must be less than cookie_expire (\"15m0s\")"
38 invalidSameSiteMsg := "cookie_samesite (\"invalid\") must be one of ['', 'lax', 'strict', 'none']"
40 testCases := []struct {
46 name: "with valid configuration",
47 cookie: options.Cookie{
53 Refresh: 15 * time.Minute,
58 errStrings: []string{},
61 name: "with no cookie secret",
62 cookie: options.Cookie{
65 Domains: emptyDomains,
68 Refresh: 15 * time.Minute,
78 name: "with an invalid cookie secret",
79 cookie: options.Cookie{
81 Secret: invalidSecret,
82 Domains: emptyDomains,
85 Refresh: 15 * time.Minute,
95 name: "with a valid Base64 secret",
96 cookie: options.Cookie{
98 Secret: validBase64Secret,
99 Domains: emptyDomains,
102 Refresh: 15 * time.Minute,
107 errStrings: []string{},
110 name: "with an invalid Base64 secret",
111 cookie: options.Cookie{
113 Secret: invalidBase64Secret,
114 Domains: emptyDomains,
117 Refresh: 15 * time.Minute,
122 errStrings: []string{
123 invalidBase64SecretMsg,
127 name: "with an invalid name",
128 cookie: options.Cookie{
131 Domains: emptyDomains,
134 Refresh: 15 * time.Minute,
139 errStrings: []string{
144 name: "with a name that is too long",
145 cookie: options.Cookie{
148 Domains: emptyDomains,
151 Refresh: 15 * time.Minute,
156 errStrings: []string{
161 name: "with refresh longer than expire",
162 cookie: options.Cookie{
165 Domains: emptyDomains,
167 Expire: 15 * time.Minute,
173 errStrings: []string{
174 refreshLongerThanExpireMsg,
178 name: "with samesite \"none\"",
179 cookie: options.Cookie{
182 Domains: emptyDomains,
185 Refresh: 15 * time.Minute,
190 errStrings: []string{},
193 name: "with samesite \"lax\"",
194 cookie: options.Cookie{
197 Domains: emptyDomains,
200 Refresh: 15 * time.Minute,
205 errStrings: []string{},
208 name: "with samesite \"strict\"",
209 cookie: options.Cookie{
212 Domains: emptyDomains,
215 Refresh: 15 * time.Minute,
220 errStrings: []string{},
223 name: "with samesite \"invalid\"",
224 cookie: options.Cookie{
227 Domains: emptyDomains,
230 Refresh: 15 * time.Minute,
235 errStrings: []string{
240 name: "with a combination of configuration errors",
241 cookie: options.Cookie{
243 Secret: invalidSecret,
246 Expire: 15 * time.Minute,
252 errStrings: []string{
255 refreshLongerThanExpireMsg,
261 for _, tc := range testCases {
262 t.Run(tc.name, func(t *testing.T) {
263 errStrings := validateCookie(tc.cookie)
266 g.Expect(errStrings).To(ConsistOf(tc.errStrings))
267 // Check domains were sorted to the right lengths
268 for i := 0; i < len(tc.cookie.Domains)-1; i++ {
269 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