Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/authorize/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func NewAuthorizeClientHandler(logger log.Logger, authorizer ClientAuthorizer, n
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
logger := log.With(logger, "request", middleware.GetReqID(req.Context()))

auth := strings.SplitN(req.Header.Get("Authorization"), " ", 2)
if strings.ToLower(auth[0]) != "bearer" {
http.Error(w, "Only bearer authorization allowed", http.StatusUnauthorized)
level.Debug(logger).Log("msg", "Only bearer authorization allowed", "auth", auth[0])
auth := strings.Fields(req.Header.Get("Authorization"))
if len(auth) != 2 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Reject headers with extra parts before authorization.

SplitN(..., 2) accepts Bearer token extra as two entries. It passes token extra to AuthorizeClient instead of returning the malformed-header response. Use strings.Fields and require exactly two fields.

Proposed fix
-		auth := strings.SplitN(req.Header.Get("Authorization"), " ", 2)
+		auth := strings.Fields(req.Header.Get("Authorization"))

http.Error(w, "Invalid Authorization header", http.StatusUnauthorized)
level.Debug(logger).Log("msg", "Invalid Authorization header", "reason", fmt.Sprintf("failed to parse the header value, expected 2 fields but got %d", len(auth)))
return
}
if len(auth) != 2 || len(strings.TrimSpace(auth[1])) == 0 {
http.Error(w, "Invalid Authorization header", http.StatusUnauthorized)
level.Debug(logger).Log("msg", "Invalid Authorization header", "auth", auth)
if strings.ToLower(auth[0]) != "bearer" {
http.Error(w, "Invalid authorization scheme", http.StatusUnauthorized)
level.Debug(logger).Log("msg", "Only bearer authorization allowed", "reason", "unsupported scheme")
return
}

Expand Down