@@ -2,6 +2,7 @@ package cors
22
33import (
44 "context"
5+ "fmt"
56 "net/http"
67 "net/http/httptest"
78 "strings"
@@ -220,6 +221,29 @@ func TestGeneratePreflightHeaders_MaxAge(t *testing.T) {
220221 assert .Len (t , header , 2 )
221222}
222223
224+ func TestExtremeLengthOriginKillswitch (t * testing.T ) {
225+ cors := newCors (nil , Config {
226+ Enabled : true ,
227+ AllowOrigins : []string {"https://*.google.com" },
228+ })
229+
230+ shortSubdomain := strings .Repeat ("a" , 10 )
231+ longSubdomain := strings .Repeat ("a" , 500 )
232+ tooLongSubdomain := strings .Repeat ("a" , 4096 )
233+
234+ assert .True (t , cors .validateOrigin (fmt .Sprintf ("https://%s.google.com" , shortSubdomain )))
235+ assert .True (t , cors .validateOrigin (fmt .Sprintf ("https://%s.google.com" , longSubdomain )))
236+ assert .False (t , cors .validateOrigin (fmt .Sprintf ("https://%s.google.com" , tooLongSubdomain )))
237+
238+ // Should not affect strict origins
239+ cors = newCors (nil , Config {
240+ Enabled : true ,
241+ AllowOrigins : []string {fmt .Sprintf ("https://%s.google.com" , tooLongSubdomain )},
242+ })
243+
244+ assert .True (t , cors .validateOrigin (fmt .Sprintf ("https://%s.google.com" , tooLongSubdomain )))
245+ }
246+
223247func TestValidateOrigin (t * testing.T ) {
224248 cors := newCors (nil , Config {
225249 Enabled : true ,
@@ -519,29 +543,10 @@ func TestComplexWildcards(t *testing.T) {
519543 }
520544 for _ , tc := range testCasesList {
521545 w := performRequest (router , "GET" , tc .origin )
522- assert .Equal (t , tc .expectedCode , w .Code )
546+ assert .Equalf (t , tc .expectedCode , w . Code , "expected %d for %s, got %d" , tc . expectedCode , tc . origin , w .Code )
523547 }
524548}
525549
526- func TestMaxRecursionDepth (t * testing.T ) {
527- router := newTestRouter (Config {
528- Enabled : true ,
529- AllowOrigins : []string {
530- "https://*.example.*.*.com" , // multiple sequential wildcards
531- "https://*.*.*.*.com" ,
532- },
533- AllowMethods : []string {"GET" },
534- })
535-
536- maxRecursionDepth = 2
537- w := performRequest (router , "GET" , "https://subdomain.example.subdomain.example.com" )
538- assert .Equal (t , 403 , w .Code )
539-
540- maxRecursionDepth = 10
541- w = performRequest (router , "GET" , "https://subdomain.example.subdomain.example.com" )
542- assert .Equal (t , 200 , w .Code )
543- }
544-
545550func TestDisabled (t * testing.T ) {
546551 config := Config {
547552 Enabled : true ,
@@ -561,35 +566,26 @@ func TestDisabled(t *testing.T) {
561566 assert .Equal (t , 200 , w .Code )
562567}
563568
564- func BenchmarkCorsWithoutWildcards (b * testing.B ) {
565- b .ReportAllocs ()
566- b .ResetTimer ()
567-
568- b .Run ("without wildcards" , func (b * testing.B ) {
569+ func BenchmarkCorsWithWildcards (b * testing.B ) {
570+ b .Run ("with wildcards" , func (b * testing.B ) {
569571 router := newTestRouter (Config {
570572 Enabled : true ,
571573 AllowOrigins : []string {
572- "https://*.wgexample.com" ,
573- "https://wgexample.com" ,
574- "https://*.wgexample.io:*" ,
575- "https://*.wgexample.org" ,
576- "https://*.d2grknavcceso7.amplifyapp.com" ,
577574 "https://*.example.*.*.com" , // multiple sequential wildcards
578575 "https://*.*.*.*.com" ,
579576 },
580577 AllowMethods : []string {"GET" },
581578 })
582579
583- w := performRequest (router , "GET" , "https://wgexample.com" )
584- assert .Equal (b , 200 , w .Code )
580+ b .ReportAllocs ()
581+ b .ResetTimer ()
582+ for i := 0 ; i < b .N ; i ++ {
583+ w := performRequest (router , "GET" , "https://subdomain.test.example.subdomain.example.co.whatgoeshere.woohoo.com" )
584+ assert .Equal (b , 200 , w .Code )
585+ }
585586 })
586- }
587-
588- func BenchmarkCorsWithWildcards (b * testing.B ) {
589- b .ReportAllocs ()
590- b .ResetTimer ()
591587
592- b .Run ("with wildcards" , func (b * testing.B ) {
588+ b .Run ("with massive wildcards" , func (b * testing.B ) {
593589 router := newTestRouter (Config {
594590 Enabled : true ,
595591 AllowOrigins : []string {
@@ -599,7 +595,30 @@ func BenchmarkCorsWithWildcards(b *testing.B) {
599595 AllowMethods : []string {"GET" },
600596 })
601597
602- w := performRequest (router , "GET" , "https://subdomain.test.example.subdomain.example.co.whatgoeshere.woohoo.com" )
603- assert .Equal (b , 200 , w .Code )
598+ longString := strings .Repeat ("a" , 50000 )
599+
600+ b .ReportAllocs ()
601+ b .ResetTimer ()
602+ for i := 0 ; i < b .N ; i ++ {
603+ w := performRequest (router , "GET" , fmt .Sprintf ("https://%[1]s.%[1]s.%[1]s.%[1]s.com" , longString ))
604+ assert .Equal (b , 200 , w .Code )
605+ }
606+ })
607+
608+ b .Run ("without wildcards" , func (b * testing.B ) {
609+ router := newTestRouter (Config {
610+ Enabled : true ,
611+ AllowOrigins : []string {
612+ "https://wgexample.com" ,
613+ },
614+ AllowMethods : []string {"GET" },
615+ })
616+
617+ b .ReportAllocs ()
618+ b .ResetTimer ()
619+ for i := 0 ; i < b .N ; i ++ {
620+ w := performRequest (router , "GET" , "https://wgexample.com" )
621+ assert .Equal (b , 200 , w .Code )
622+ }
604623 })
605624}
0 commit comments