Skip to content

Commit

Permalink
Benchmark maliciously long ACRH header
Browse files Browse the repository at this point in the history
  • Loading branch information
jub0bs committed Mar 27, 2024
1 parent 8d33ca4 commit ad0e722
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions bench_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cors

import (
"math"
"net/http"
"strings"
"testing"
)

Expand Down Expand Up @@ -97,6 +99,21 @@ func BenchmarkPreflightHeader(b *testing.B) {
}
}

func BenchmarkPreflightAdversarialACRH(b *testing.B) {
resps := makeFakeResponses(b.N)
req, _ := http.NewRequest(http.MethodOptions, dummyEndpoint, nil)
req.Header.Add(headerOrigin, dummyOrigin)
req.Header.Add(headerACRM, http.MethodGet)
req.Header[headerACRH] = adversarialACRH
handler := Default().Handler(testHandler)

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
handler.ServeHTTP(resps[i], req)
}
}

func makeFakeResponses(n int) []*FakeResponse {
resps := make([]*FakeResponse, n)
for i := 0; i < n; i++ {
Expand All @@ -106,3 +123,15 @@ func makeFakeResponses(n int) []*FakeResponse {
}
return resps
}

var adversarialACRH []string

func init() { // populates adversarialACRH
n := int(math.Floor(math.Sqrt(http.DefaultMaxHeaderBytes)))
commas := strings.Repeat(",", n)
res := make([]string, n)
for i := range res {
res[i] = commas
}
adversarialACRH = res
}

0 comments on commit ad0e722

Please sign in to comment.