Skip to content

Commit 8be604f

Browse files
lvijnckeshepelyuk
authored andcommitted
fix: ignore authorization header prefix capitalization
1 parent 6f1ff2a commit 8be604f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

jwt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ func (jwtPlugin *JwtPlugin) extractTokenFromHeader(request *http.Request) (strin
476476
return "", fmt.Errorf("authorization header missing")
477477
}
478478
auth := authHeader[0]
479-
if !strings.HasPrefix(auth, "Bearer ") {
479+
if !strings.HasPrefix(strings.ToLower(auth), "bearer ") {
480480
return "", fmt.Errorf("authorization type not Bearer")
481481
}
482482
return auth[7:], nil

jwt_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestServeHTTPOK(t *testing.T) {
2121
name string
2222
remoteAddr string
2323
forwardedFor string
24+
authPrefix string
2425
}{
2526
{
2627
name: "x-forwarded-for, ipv4, no port",
@@ -62,6 +63,10 @@ func TestServeHTTPOK(t *testing.T) {
6263
name: "remoteAddr, ipv6, with port",
6364
remoteAddr: "[1fff:0:a88:85a3::ac1f]:8001",
6465
},
66+
{
67+
name: "Authorization, uppercase prefix",
68+
authPrefix: "BEARER",
69+
},
6570
}
6671

6772
for _, tt := range tests {
@@ -85,7 +90,11 @@ func TestServeHTTPOK(t *testing.T) {
8590
if err != nil {
8691
t.Fatal(err)
8792
}
88-
req.Header["Authorization"] = []string{"Bearer eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.JlX3gXGyClTBFciHhknWrjo7SKqyJ5iBO0n-3S2_I7cIgfaZAeRDJ3SQEbaPxVC7X8aqGCOM-pQOjZPKUJN8DMFrlHTOdqMs0TwQ2PRBmVAxXTSOZOoEhD4ZNCHohYoyfoDhJDP4Qye_FCqu6POJzg0Jcun4d3KW04QTiGxv2PkYqmB7nHxYuJdnqE3704hIS56pc_8q6AW0WIT0W-nIvwzaSbtBU9RgaC7ZpBD2LiNE265UBIFraMDF8IAFw9itZSUCTKg1Q-q27NwwBZNGYStMdIBDor2Bsq5ge51EkWajzZ7ALisVp-bskzUsqUf77ejqX_CBAqkNdH1Zebn93A"}
93+
authPrefix := "Bearer"
94+
if len(tt.authPrefix) > 0 {
95+
authPrefix = tt.authPrefix
96+
}
97+
req.Header["Authorization"] = []string{fmt.Sprintf("%s eyJhbGciOiJSUzUxMiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImlhdCI6MTUxNjIzOTAyMn0.JlX3gXGyClTBFciHhknWrjo7SKqyJ5iBO0n-3S2_I7cIgfaZAeRDJ3SQEbaPxVC7X8aqGCOM-pQOjZPKUJN8DMFrlHTOdqMs0TwQ2PRBmVAxXTSOZOoEhD4ZNCHohYoyfoDhJDP4Qye_FCqu6POJzg0Jcun4d3KW04QTiGxv2PkYqmB7nHxYuJdnqE3704hIS56pc_8q6AW0WIT0W-nIvwzaSbtBU9RgaC7ZpBD2LiNE265UBIFraMDF8IAFw9itZSUCTKg1Q-q27NwwBZNGYStMdIBDor2Bsq5ge51EkWajzZ7ALisVp-bskzUsqUf77ejqX_CBAqkNdH1Zebn93A", authPrefix)}
8998
if len(tt.forwardedFor) > 0 {
9099
req.Header["X-Forwarded-For"] = []string{tt.forwardedFor}
91100
}

0 commit comments

Comments
 (0)