-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware_bench_test.go
466 lines (453 loc) · 12.8 KB
/
middleware_bench_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
package cors_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/jub0bs/cors"
)
func BenchmarkMiddleware(b *testing.B) {
cases := []MiddlewareTestCase{
{
desc: "no CORS",
newHandler: newDummyHandler(),
cases: []ReqTestCase{
{
desc: "preflight",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
},
},
},
}, {
desc: "single origin some req headers",
newHandler: newDummyHandler(),
cfg: &cors.Config{
Origins: []string{"https://example.com"},
RequestHeaders: requestHeadersAllowedByDefaultInRsCORS,
},
cases: []ReqTestCase{
{
desc: "preflight from allowed",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual from allowed",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
},
}, {
desc: "preflight from disallowed",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.computer"},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual from disallowed",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://example.computer"},
},
}, {
desc: "preflight with adversarial ACRH: same allowed name repeated many times",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {strings.Repeat("accept,", http.DefaultMaxHeaderBytes/len("accept,"))},
},
}, {
desc: "preflight with adversarial ACRH: lots of OWS",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {"accept," + strings.Repeat(" ", http.DefaultMaxHeaderBytes) + "content-type"},
},
}, {
desc: "preflight with adversarial ACRH: lots of empty ACRH lines",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: make([]string, 1024),
},
},
},
}, {
desc: "multiple origins some req headers",
newHandler: newDummyHandler(),
cfg: &cors.Config{
Origins: severalOrigins,
RequestHeaders: requestHeadersAllowedByDefaultInRsCORS,
},
cases: []ReqTestCase{
{
desc: "preflight from allowed",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual from allowed",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
},
}, {
desc: "preflight from disallowed",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.computer"},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual from disallowed",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://example.computer"},
},
},
},
}, {
desc: "two pathological origins some req headers",
newHandler: newDummyHandler(),
cfg: &cors.Config{
Origins: []string{
"https://a" + strings.Repeat(".a", hostMaxLen/2),
"https://b" + strings.Repeat(".a", hostMaxLen/2),
},
RequestHeaders: requestHeadersAllowedByDefaultInRsCORS,
},
cases: []ReqTestCase{
{
desc: "preflight",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://a" + strings.Repeat(".a", hostMaxLen/2)},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://a" + strings.Repeat(".a", hostMaxLen/2)},
},
}, {
desc: "preflight from disallowed",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://c" + strings.Repeat(".a", hostMaxLen/2)},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual from disallowed",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://c" + strings.Repeat(".a", hostMaxLen/2)},
},
},
},
}, {
desc: "many origins some req headers",
newHandler: newDummyHandler(),
cfg: &cors.Config{
Origins: manyOrigins,
RequestHeaders: requestHeadersAllowedByDefaultInRsCORS,
},
cases: []ReqTestCase{
{
desc: "preflight",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {manyOrigins[0]},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {manyOrigins[0]},
},
}, {
desc: "preflight from disallowed",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.computer"},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual from disallowed",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://example.computer"},
},
},
},
}, {
desc: "credentialed any req headers",
newHandler: newDummyHandler(),
cfg: &cors.Config{
Origins: []string{"https://example.com"},
Credentialed: true,
RequestHeaders: []string{"*"}, //, "Authorization"},
},
cases: []ReqTestCase{
{
desc: "preflight",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "preflight with adversarial ACRH: same allowed name repeated many times",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {strings.Repeat("a,", http.DefaultMaxHeaderBytes/len("a,"))},
},
}, {
desc: "preflight with adversarial ACRH: lots of OWS",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {"a," + strings.Repeat(" ", http.DefaultMaxHeaderBytes) + "b"},
},
}, {
desc: "preflight with adversarial ACRH: lots of empty ACRH lines",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: make([]string, 1024),
},
}, {
desc: "actual",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
},
},
},
}, {
desc: "no CORS, outer Vary",
outerMw: &varyMiddleware,
newHandler: newDummyHandler(),
cases: []ReqTestCase{
{
desc: "preflight",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "actual",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
},
},
},
}, {
desc: "outer Vary",
outerMw: &varyMiddleware,
newHandler: newDummyHandler(),
cfg: &cors.Config{
Origins: []string{"https://example.com"},
Credentialed: true,
RequestHeaders: []string{"*", "Authorization"},
},
cases: []ReqTestCase{
{
desc: "preflight",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {"authorization"},
},
}, {
desc: "preflight with adversarial ACRH: same allowed name repeated many times",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {strings.Repeat("a,", http.DefaultMaxHeaderBytes/len("a,"))},
},
}, {
desc: "preflight with adversarial ACRH: lots of OWS",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: {"a," + strings.Repeat(" ", http.DefaultMaxHeaderBytes) + "b"},
},
}, {
desc: "preflight with adversarial ACRH: lots of empty ACRH lines",
reqMethod: http.MethodOptions,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
headerACRM: {http.MethodGet},
headerACRH: make([]string, 1024),
},
}, {
desc: "actual",
reqMethod: http.MethodGet,
reqHeaders: http.Header{
headerOrigin: {"https://example.com"},
},
},
},
},
}
for _, mwbc := range cases {
if mwbc.cfg == nil {
continue
}
var mw *cors.Middleware
// benchmark initialization
f := func(b *testing.B) {
b.ReportAllocs()
var err error
for range b.N {
mw, err = cors.NewMiddleware(*mwbc.cfg)
if err != nil {
b.Fatal(err)
}
}
}
b.Run("initialization "+mwbc.desc, f)
// benchmark config
f = func(b *testing.B) {
b.ReportAllocs()
for range b.N {
mw.Config()
}
}
b.Run("config "+mwbc.desc, f)
}
// benchmark execution
for _, mwbc := range cases {
var handler http.Handler = mwbc.newHandler()
var mw *cors.Middleware
if mwbc.cfg != nil {
var err error
mw, err = cors.NewMiddleware(*mwbc.cfg)
if err != nil {
b.Fatal(err)
}
handler = mw.Wrap(handler)
}
if mwbc.outerMw != nil {
handler = mwbc.outerMw.Wrap(handler)
}
for _, bc := range mwbc.cases {
f := func(b *testing.B) {
req := newRequest(bc.reqMethod, bc.reqHeaders)
b.ReportAllocs()
b.ResetTimer()
// We run benchmarks in parallel because typical workloads
// for HTTP handlers are concurrent.
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rec := httptest.NewRecorder()
handler.ServeHTTP(rec, req)
}
})
}
desc := fmt.Sprintf("exec %s vs %s", mwbc.desc, bc.desc)
if mw == nil {
b.Run(desc, f)
continue
}
// Run the benchmark outside debug mode.
mw.SetDebug(false)
b.Run(desc, f)
// Run the benchmark in debug mode.
desc = fmt.Sprintf("exec debug %s vs %s", mwbc.desc, bc.desc)
mw.SetDebug(true)
b.Run(desc, f)
}
}
}
// see https://github.com/rs/cors/blob/1562b1715b353146f279ff7d445b7412e0f1a842/cors.go#L197
var requestHeadersAllowedByDefaultInRsCORS = []string{
"Accept",
"Content-Type",
"X-Requested-With",
}
var severalOrigins = []string{
"https://example.com",
"https://*.example.com",
"https://google.com",
"https://*.google.com",
}
const hostMaxLen = 253
var manyOrigins []string
func init() {
const n = 100
for i := 0; i < n; i++ {
manyOrigins = append(
manyOrigins,
// https
fmt.Sprintf("https://%d.example.com", i),
fmt.Sprintf("https://%d.example.com:7070", i),
fmt.Sprintf("https://%d.example.com:8080", i),
fmt.Sprintf("https://%d.example.com:9090", i),
// one subdomain deep
fmt.Sprintf("https://%d.foo.example.com", i),
fmt.Sprintf("https://%d.foo.example.com:6060", i),
fmt.Sprintf("https://%d.foo.example.com:7070", i),
fmt.Sprintf("https://%d.foo.example.com:9090", i),
// two subdomains deep
fmt.Sprintf("https://%d.foo.bar.example.com", i),
fmt.Sprintf("https://%d.foo.bar.example.com:6060", i),
fmt.Sprintf("https://%d.foo.bar.example.com:7070", i),
fmt.Sprintf("https://%d.foo.bar.example.com:9090", i),
// arbitrary subdomains
"https://*.foo.bar.example.com",
"https://*.foo.bar.example.com:6060",
"https://*.foo.bar.example.com:7070",
"https://*.foo.bar.example.com:9090",
)
}
}
var dummyHandler = http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
io.WriteString(w, "Hello, World!")
})
func newDummyHandler() func() http.Handler {
return func() http.Handler {
return dummyHandler
}
}