Skip to content
Merged
Show file tree
Hide file tree
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
35 changes: 35 additions & 0 deletions internal/httpapi/bearer_scheme_case_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package httpapi

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"

"github.com/smart-mcp-proxy/mcpproxy-go/internal/config"
)

// The Authorization scheme name is case-insensitive (RFC 7235 §2.1), so a
// client that sends "bearer <key>" must be treated like "Bearer <key>".
func TestBearerScheme_CaseInsensitive(t *testing.T) {
const apiKey = "admin-key-12345"

for _, scheme := range []string{"Bearer", "bearer", "BEARER"} {
t.Run(scheme, func(t *testing.T) {
req := httptest.NewRequest("GET", "/api/v1/status", nil)
req.Header.Set("Authorization", scheme+" "+apiKey)
assert.Equal(t, apiKey, ExtractToken(req))

srv := NewServer(&testControllerWithConfig{cfg: &config.Config{APIKey: apiKey}}, zap.NewNop().Sugar(), nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "admin key sent with %q scheme should authenticate", scheme)
})
}

req := httptest.NewRequest("GET", "/api/v1/status", nil)
req.Header.Set("Authorization", "Basic "+apiKey)
assert.Equal(t, "", ExtractToken(req), "non-Bearer schemes must not yield a token")
}
24 changes: 14 additions & 10 deletions internal/httpapi/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,10 +679,7 @@ func (s *Server) authenticateExplicitToken(w http.ResponseWriter, r *http.Reques
// global admin key, or a user JWT resolved as a session principal
// (kind=bearer_jwt) through the edition hook.
func (s *Server) authenticateBearer(w http.ResponseWriter, r *http.Request, next http.Handler, cfg *config.Config, authHeader string) {
var token string
if strings.HasPrefix(authHeader, "Bearer ") {
token = strings.TrimPrefix(authHeader, "Bearer ")
}
token := bearerToken(authHeader)

if token != "" && strings.HasPrefix(token, auth.ClientTokenPrefixStr) {
s.rejectClientCredentialOnREST(w, r)
Expand Down Expand Up @@ -813,12 +810,8 @@ func ExtractToken(r *http.Request) string {
}

// 2. Check Authorization: Bearer header
if authHeader := r.Header.Get("Authorization"); authHeader != "" {
if strings.HasPrefix(authHeader, "Bearer ") {
if token := strings.TrimPrefix(authHeader, "Bearer "); token != "" {
return token
}
}
if token := bearerToken(r.Header.Get("Authorization")); token != "" {
return token
}

// 3. Check query parameter (for SSE and Web UI initial load)
Expand All @@ -829,6 +822,17 @@ func ExtractToken(r *http.Request) string {
return ""
}

// bearerToken returns the credential from an Authorization header value that
// uses the Bearer scheme, or "" otherwise. The scheme name is matched
// case-insensitively (RFC 7235 §2.1, RFC 6750 §2.1).
func bearerToken(authHeader string) string {
const scheme = "Bearer "
if len(authHeader) < len(scheme) || !strings.EqualFold(authHeader[:len(scheme)], scheme) {
return ""
}
return authHeader[len(scheme):]
}

// correlationIDMiddleware injects correlation ID and request source into context
func (s *Server) correlationIDMiddleware() func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
Expand Down
6 changes: 1 addition & 5 deletions internal/httpapi/sse_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ func sessionCredentialValue(r *http.Request, kind auth.CredentialKind) string {
}
return ""
case auth.CredentialKindBearerJWT:
authHeader := r.Header.Get("Authorization")
if len(authHeader) > len("Bearer ") && authHeader[:7] == "Bearer " {
return authHeader[7:]
}
return ""
return bearerToken(r.Header.Get("Authorization"))
default:
return ""
}
Expand Down
Loading