-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
633 lines (623 loc) · 17.1 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
package cors_test
import (
"encoding/json"
"io"
"net/http"
"reflect"
"sort"
"testing"
"github.com/jub0bs/cors"
)
var cfgTypes = []reflect.Type{
reflect.TypeFor[cors.Config](),
reflect.TypeFor[cors.ExtraConfig](),
}
// We want our exported struct types to be incomparable because, otherwise,
// client code could rely on their comparability.
func TestIncomparability(t *testing.T) {
for _, typ := range cfgTypes {
f := func(t *testing.T) {
if typ.Comparable() {
t.Errorf("type %v is comparable, but should not be", typ)
}
}
t.Run(typ.String(), f)
}
}
// We don't want client code to rely on unkeyed literals
// of our exported struct types.
func TestImpossibilityOfUnkeyedStructLiterals(t *testing.T) {
for _, typ := range cfgTypes {
f := func(t *testing.T) {
var unexportedFields bool
for i := 0; i < typ.NumField(); i++ {
if !typ.Field(i).IsExported() {
unexportedFields = true
break
}
}
if !unexportedFields {
t.Errorf("type %v has no unexported fields, but should have at least one", typ)
}
}
t.Run(typ.String(), f)
}
}
// Some clients rely on the ability to marshal configuration to JSON;
// see, for instance, https://github.com/rs/cors/pull/164.
func TestPossibilityToMarshalConfig(t *testing.T) {
cfg := cors.Config{
Origins: []string{"https://example.com"},
Credentialed: true,
Methods: []string{http.MethodPost},
RequestHeaders: []string{"Authorization"},
MaxAgeInSeconds: 30,
ResponseHeaders: []string{"X-Response-Time"},
ExtraConfig: cors.ExtraConfig{
PrivateNetworkAccess: true,
},
}
enc := json.NewEncoder(io.Discard)
if err := enc.Encode(cfg); err != nil {
t.Error("cors.Config cannot be marshaled to JSON, but should be")
}
}
func TestIncorrectConfig(t *testing.T) {
type InvalidConfigTestCase struct {
desc string
cfg *cors.Config
msgs []string
}
var cases = []InvalidConfigTestCase{
{
desc: "no origin pattern specified",
cfg: &cors.Config{},
msgs: []string{
`cors: at least one origin pattern must be specified`,
},
}, {
desc: "null origin",
cfg: &cors.Config{
Origins: []string{"null"},
},
msgs: []string{
`cors: prohibited origin pattern "null"`,
},
}, {
desc: "invalid origin pattern",
cfg: &cors.Config{
Origins: []string{"http://example.com:6060/path"},
},
msgs: []string{
`cors: invalid origin pattern "http://example.com:6060/path"`,
},
}, {
desc: "wildcard origin in addition to other origin pattern",
cfg: &cors.Config{
Origins: []string{
"*",
"https://example.com",
},
},
msgs: []string{
`cors: specifying origin patterns in addition to * is prohibited`,
},
}, {
desc: "origin pattern in addition to wildcard origin",
cfg: &cors.Config{
Origins: []string{
"https://example.com",
"*",
},
},
msgs: []string{
`cors: specifying origin patterns in addition to * is prohibited`,
},
}, {
desc: "empty method name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
Methods: []string{""},
},
msgs: []string{
`cors: invalid method name ""`,
},
}, {
desc: "invalid method name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
Methods: []string{"résumé"},
},
msgs: []string{
`cors: invalid method name "résumé"`,
},
}, {
desc: "forbidden method name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
Methods: []string{
http.MethodGet,
http.MethodConnect,
},
},
msgs: []string{
`cors: forbidden method name "CONNECT"`,
},
}, {
desc: "wildcard in addition to other method",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
Methods: []string{
"*",
http.MethodGet,
},
},
msgs: []string{
`cors: specifying methods in addition to * is prohibited`,
},
}, {
desc: "method in addition wildcard",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
Methods: []string{
http.MethodGet,
"*",
},
},
msgs: []string{
`cors: specifying methods in addition to * is prohibited`,
},
}, {
desc: "empty request-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{""},
},
msgs: []string{
`cors: invalid request-header name ""`,
},
}, {
desc: "invalid request-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{"résumé"},
},
msgs: []string{
`cors: invalid request-header name "résumé"`,
},
}, {
desc: "forbidden request-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{"Connection"},
},
msgs: []string{
`cors: forbidden request-header name "Connection"`,
},
}, {
desc: "forbidden request-header name with Sec- prefix",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{"Sec-Foo"},
},
msgs: []string{
`cors: forbidden request-header name "Sec-Foo"`,
},
}, {
desc: "forbidden request-header name with Proxy- prefix",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{"Proxy-Foo"},
},
msgs: []string{
`cors: forbidden request-header name "Proxy-Foo"`,
},
}, {
desc: "prohibited request-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{"Access-Control-Allow-Origin"},
},
msgs: []string{
`cors: prohibited request-header name "Access-Control-Allow-Origin"`,
},
}, {
desc: "wildcard in addition to request-header name other than Authorization",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{
"*",
"Content-Type",
},
},
msgs: []string{
`cors: specifying request-header names (other than Authorization) in addition to * is prohibited`,
},
}, {
desc: "request-header name other than Authorization in addition to wildcard",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{
"Content-Type",
"*",
},
},
msgs: []string{
`cors: specifying request-header names (other than Authorization) in addition to * is prohibited`,
},
}, {
desc: "wildcard and Authorization in addition to other request-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{
"*",
"Authorization",
"Content-Type",
},
},
msgs: []string{
`cors: specifying request-header names (other than Authorization) in addition to * is prohibited`,
},
}, {
desc: "request-header name other than Authorization in addition to Authorization and wildcard",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{
"Content-Type",
"Authorization",
"*",
},
},
msgs: []string{
`cors: specifying request-header names (other than Authorization) in addition to * is prohibited`,
},
}, {
desc: "max age less than -1",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{
"Content-Type",
"Authorization",
},
MaxAgeInSeconds: -2,
},
msgs: []string{
`cors: specified max-age value -2 is invalid`,
},
}, {
desc: "max age exceeds upper bound",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: []string{
"Content-Type",
"Authorization",
},
MaxAgeInSeconds: 86_401,
},
msgs: []string{
`cors: specified max-age value 86401 exceeds upper bound 86400`,
},
}, {
desc: "empty response-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ResponseHeaders: []string{""},
},
msgs: []string{
`cors: invalid response-header name ""`,
},
}, {
desc: "invalid response-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ResponseHeaders: []string{"résumé"},
},
msgs: []string{
`cors: invalid response-header name "résumé"`,
},
}, {
desc: "forbidden response-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ResponseHeaders: []string{"Set-Cookie"},
},
msgs: []string{
`cors: forbidden response-header name "Set-Cookie"`,
},
}, {
desc: "prohibited response-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ResponseHeaders: []string{"Access-Control-Request-Method"},
},
msgs: []string{
`cors: prohibited response-header name "Access-Control-Request-Method"`,
},
}, {
desc: "safelisted response-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ResponseHeaders: []string{"Cache-Control"},
},
msgs: []string{
`cors: response-header name "Cache-Control" needs not be explicitly exposed`,
},
}, {
desc: "wildcard in addition to other response-header name",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ResponseHeaders: []string{
"*",
"X-Response-Time",
},
},
msgs: []string{
`cors: specifying response-header names in addition to * is prohibited`,
},
}, {
desc: "response-header name in addition to wildcard",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ResponseHeaders: []string{
"X-Response-Time",
"*",
},
},
msgs: []string{
`cors: specifying response-header names in addition to * is prohibited`,
},
}, {
desc: "preflight success status less than 200",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ExtraConfig: cors.ExtraConfig{
PreflightSuccessStatus: 199,
},
},
msgs: []string{
`cors: specified status 199 lies outside the 2xx range`,
},
}, {
desc: "preflight success status greater than 299",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ExtraConfig: cors.ExtraConfig{
PreflightSuccessStatus: 300,
},
},
msgs: []string{
`cors: specified status 300 lies outside the 2xx range`,
},
}, {
desc: "wildcard origin with Credentialed",
cfg: &cors.Config{
Origins: []string{"*"},
Credentialed: true,
},
msgs: []string{
`cors: for security reasons, you cannot both allow all origins ` +
`and enable credentialed access`,
},
}, {
desc: "wildcard origin with PrivateNetworkAccess",
cfg: &cors.Config{
Origins: []string{"*"},
ExtraConfig: cors.ExtraConfig{
PrivateNetworkAccess: true,
},
},
msgs: []string{
`cors: for security reasons, you cannot both allow all origins ` +
`and enable Private-Network Access`,
},
}, {
desc: "wildcard origin with PrivateNetworkAccessInNoCORSModeOnly",
cfg: &cors.Config{
Origins: []string{"*"},
ExtraConfig: cors.ExtraConfig{
PrivateNetworkAccessInNoCORSModeOnly: true,
},
},
msgs: []string{
`cors: for security reasons, you cannot both allow all origins ` +
`and enable Private-Network Access`,
},
}, {
desc: "insecure origin with Credentialed without DangerouslyTolerateInsecureOrigins",
cfg: &cors.Config{
Origins: []string{
"http://example.com:6060",
"http://*.example.com:6060",
},
Credentialed: true,
},
msgs: []string{
`cors: for security reasons, insecure origin patterns like ` +
`"http://example.com:6060" and "http://*.example.com:6060" ` +
`are by default prohibited when credentialed access is enabled`,
},
}, {
desc: "insecure origin with PrivateNetworkAccess without DangerouslyTolerateInsecureOrigins",
cfg: &cors.Config{
Origins: []string{
"http://example.com:6060",
"http://*.example.com:6060",
},
ExtraConfig: cors.ExtraConfig{
PrivateNetworkAccess: true,
},
},
msgs: []string{
`cors: for security reasons, insecure origin patterns like ` +
`"http://example.com:6060" and "http://*.example.com:6060" ` +
`are by default prohibited when Private-Network Access is enabled`,
},
}, {
desc: "insecure origin with PrivateNetworkAccessInNoCORSModeOnly without DangerouslyTolerateInsecureOrigins",
cfg: &cors.Config{
Origins: []string{
"http://example.com:6060",
"http://*.example.com:6060",
},
ExtraConfig: cors.ExtraConfig{
PrivateNetworkAccessInNoCORSModeOnly: true,
},
},
msgs: []string{
`cors: for security reasons, insecure origin patterns like ` +
`"http://example.com:6060" and "http://*.example.com:6060" ` +
`are by default prohibited when Private-Network Access is enabled`,
},
}, {
desc: "insecure origin with Credentialed and PrivateNetworkAccess without DangerouslyTolerateInsecureOrigins",
cfg: &cors.Config{
Origins: []string{
"http://example.com:6060",
"http://*.example.com:6060",
},
Credentialed: true,
ExtraConfig: cors.ExtraConfig{
PrivateNetworkAccess: true,
},
},
msgs: []string{
`cors: for security reasons, insecure origin patterns like ` +
`"http://example.com:6060" and "http://*.example.com:6060" are ` +
`by default prohibited when credentialed access is enabled ` +
`and/or Private-Network Access is enabled`,
},
}, {
desc: "insecure origin with Credentialed and PrivateNetworkAccessInNoCORSModeOnly without DangerouslyTolerateInsecureOrigins",
cfg: &cors.Config{
Origins: []string{
"http://example.com:6060",
"http://*.example.com:6060",
},
Credentialed: true,
ExtraConfig: cors.ExtraConfig{
PrivateNetworkAccessInNoCORSModeOnly: true,
},
},
msgs: []string{
`cors: for security reasons, insecure origin patterns like ` +
`"http://example.com:6060" and "http://*.example.com:6060" are ` +
`by default prohibited when credentialed access is enabled ` +
`and/or Private-Network Access is enabled`,
},
}, {
desc: "wildcard pattern encompassing subdomains of a public suffix without DangerouslyTolerateSubdomainsOfPublicSuffixes",
cfg: &cors.Config{
Origins: []string{"https://*.com"},
},
msgs: []string{
`cors: for security reasons, origin patterns like ` +
`"https://*.com" that encompass subdomains of a ` +
`public suffix are by default prohibited`,
},
}, {
desc: "conjunct use of PrivateNetworkAccess and PrivateNetworkAccessInNoCORSModeOnly",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
ExtraConfig: cors.ExtraConfig{
PrivateNetworkAccess: true,
PrivateNetworkAccessInNoCORSModeOnly: true,
},
},
msgs: []string{
`cors: at most one form of Private-Network Access can be enabled`,
},
}, {
desc: "wildcard response-header name with Credentialed",
cfg: &cors.Config{
Origins: []string{"https://example.com"},
Credentialed: true,
ResponseHeaders: []string{"*"},
},
msgs: []string{
`cors: you cannot both expose all response headers and enable credentialed access`,
},
}, {
desc: "multiple configuration issues",
cfg: &cors.Config{
Origins: []string{
"http://example.com",
"https://example.com/",
},
Methods: []string{
http.MethodConnect,
"résumé",
},
RequestHeaders: []string{
"résumé",
"Access-Control-Allow-Origin",
},
MaxAgeInSeconds: 86_401,
},
msgs: []string{
`cors: invalid origin pattern "https://example.com/"`,
`cors: forbidden method name "CONNECT"`,
`cors: invalid method name "résumé"`,
`cors: invalid request-header name "résumé"`,
`cors: prohibited request-header name "Access-Control-Allow-Origin"`,
`cors: specified max-age value 86401 exceeds upper bound 86400`,
},
},
}
for _, tc := range cases {
f := func(t *testing.T) {
mw, err := cors.NewMiddleware(*tc.cfg)
if mw != nil {
t.Error("got non-nil *Middleware; want nil *Middleware")
}
if err == nil {
t.Error("got nil error; want non-nil error")
return
}
msgs := flatten(err)
sort.Strings(msgs) // the order doesn't matter
sort.Strings(tc.msgs)
res, same := diff(msgs, tc.msgs)
if !same {
t.Error("unexpected error message(s):")
for _, s := range res {
t.Logf("\t%s", s)
}
}
}
t.Run(tc.desc, f)
}
}
func flatten(err error) []string {
return flattenRec(err, nil)
}
func flattenRec(err error, res []string) []string {
type wrapper interface{ Unwrap() []error }
errs, ok := err.(wrapper)
if !ok {
return append(res, err.Error())
}
for _, err := range errs.Unwrap() {
res = flattenRec(err, res)
}
return res
}
// diff reports whether x and y contain the same elements in the same order
// and returns a visual summary of the difference y-x.
func diff(x, y []string) (res []string, same bool) {
same = len(x) == len(y)
for 0 < len(x) && 0 < len(y) {
if x[0] == y[0] {
res = append(res, " "+y[0])
y = y[1:]
x = x[1:]
continue
}
same = false
res = append(res, "- "+y[0]) // missing element
y = y[1:]
}
for _, s := range x {
res = append(res, "+ "+s) // extraneous element
}
return res, same
}