fix: require bearer scheme for JWT auth#1035
Conversation
Signed-off-by: pm-ju <pmdevops29@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR tightens JWT extraction in the Kthena router auth filter by requiring the HTTP Authorization header to use the Bearer scheme before treating the value as a JWT, preventing malformed/incorrect schemes from reaching JWT parsing.
Changes:
- Update
extractTokenFromHeaderto only acceptAuthorization: Bearer <token>(case-insensitive), tolerating extra whitespace and rejecting all other formats. - Adjust existing unit expectation to reject raw tokens without a
Bearerscheme. - Add unit tests covering lowercase schemes, extra whitespace, embedded whitespace, and invalid schemes reaching the middleware.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pkg/kthena-router/filters/auth/authentication.go | Makes token extraction strict to the Bearer scheme (case-insensitive) and rejects malformed headers early. |
| pkg/kthena-router/filters/auth/authentication_test.go | Updates/extends tests to verify strict Bearer parsing and middleware rejection for invalid schemes. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Code Review
This pull request improves the robustness of JWT token extraction by replacing simple prefix trimming with a more flexible approach using strings.Fields, which correctly handles case-insensitive schemes and extra whitespace. Corresponding test cases were added to verify these scenarios and ensure invalid authorization schemes are rejected. The reviewer suggested adding a defensive nil check for the http.Request parameter in extractTokenFromHeader to prevent potential panics in future usage contexts.
| func extractTokenFromHeader(req *http.Request) string { | ||
| value := req.Header.Get(header) | ||
| return strings.TrimPrefix(value, prefix) | ||
| fields := strings.Fields(req.Header.Get(header)) |
There was a problem hiding this comment.
The function extractTokenFromHeader does not check if the req parameter is nil. While it is currently called with c.Request from a Gin middleware (which is typically non-nil), adding a defensive check would prevent potential panics if this helper is used in other contexts or if the request is not properly initialized.
| func extractTokenFromHeader(req *http.Request) string { | |
| value := req.Header.Get(header) | |
| return strings.TrimPrefix(value, prefix) | |
| fields := strings.Fields(req.Header.Get(header)) | |
| func extractTokenFromHeader(req *http.Request) string { | |
| if req == nil { | |
| return "" | |
| } | |
| fields := strings.Fields(req.Header.Get(header)) |
| value := req.Header.Get(header) | ||
| return strings.TrimPrefix(value, prefix) | ||
| fields := strings.Fields(req.Header.Get(header)) | ||
| if len(fields) != 2 || !strings.EqualFold(fields[0], bearerScheme) { |
There was a problem hiding this comment.
nice fix — strings.TrimPrefix silently passed through any token without a Bearer prefix, which was a real security hole. Using strings.Fields + EqualFold is cleaner and handles extra whitespace as a bonus. The case-insensitive check aligns with RFC 7235 too.
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: hzxuzhonghu The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/kind bug
Require the
Authorizationheader to use the Bearer scheme before extracting a JWT token.Previously, an Authorization header without the
Bearerprefix was treated as the token value. That allowed malformed schemes such as raw tokens orBasic ...values to reach JWT parsing instead of being rejected as an invalid auth header.This change:
Verification:

go test ./pkg/kthena-router/filters/auth