Skip to content

Commit e8cf15a

Browse files
fix(serveredition): accept the Bearer scheme case-insensitively for user JWTs (#1755)
## Bug `ServerEditionAuthMiddleware.authenticateFromBearer` only recognises an `Authorization` header that starts with the exact bytes `Bearer `. The auth-scheme name is case-insensitive (RFC 7235 §2.1, RFC 6750 §2.1), so a valid user JWT sent as `bearer <jwt>` or `BEARER <jwt>` is ignored and the request gets `401 Authentication required` on the server edition. ## Trigger Build with `-tags server`, log in, mint a JWT with `POST /api/v1/auth/token`, then: ``` curl -H 'Authorization: bearer <jwt>' http://host/api/v1/servers # 401 curl -H 'Authorization: Bearer <jwt>' http://host/api/v1/servers # 200 ``` ## Fix Compare the scheme prefix with `strings.EqualFold` instead of `strings.HasPrefix`. Nothing else changes: empty tokens and agent tokens are still skipped, and the JWT is validated exactly as before. This is the server-edition counterpart of #1754 (which fixes the core `internal/httpapi` parser). The two are independent and can land in either order. ## Test `TestMiddleware_ValidJWT_SchemeCaseInsensitive` sends a valid JWT with the `bearer` and `BEARER` schemes through `Middleware()` and checks for 200 and the right `user_id`. ## Commands run - `CGO_ENABLED=0 go test -count=1 -tags server ./internal/serveredition/auth`: ok on 3 of 4 runs. The one failing run was `TestHandleCallback_Unavailable_ProviderErrorRenders503`, an OAuth-callback timing test that does not go through this middleware. It passes alone (`-count=3`) and passes on the other three full runs. - `CGO_ENABLED=0 go test -count=1 -tags server -skip TestHandleCallback_Unavailable_ProviderErrorRenders503 ./internal/serveredition/auth`: ok - `CGO_ENABLED=0 go vet -tags server ./internal/serveredition/auth`: clean - With the fix reverted (`git stash` of `middleware.go`), `-run SchemeCaseInsensitive` fails with `bearer: expected 200 ..., got 401` and `BEARER: expected 200 ..., got 401`. - `gofmt -l` on the edited files: no output Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 83ccbc2 commit e8cf15a

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

‎internal/serveredition/auth/middleware.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,14 @@ func (m *ServerEditionAuthMiddleware) authenticateFromSession(r *http.Request) (
188188
// Authorization header and returns an AuthContext if successful.
189189
// Returns (nil, nil) if no Bearer token is present.
190190
func (m *ServerEditionAuthMiddleware) authenticateFromBearer(r *http.Request) (*coreauth.AuthContext, error) {
191+
// The auth-scheme name is case-insensitive (RFC 7235 §2.1).
191192
authHeader := r.Header.Get("Authorization")
192-
if !strings.HasPrefix(authHeader, "Bearer ") {
193+
const scheme = "Bearer "
194+
if len(authHeader) < len(scheme) || !strings.EqualFold(authHeader[:len(scheme)], scheme) {
193195
return nil, nil
194196
}
195197

196-
token := strings.TrimPrefix(authHeader, "Bearer ")
198+
token := authHeader[len(scheme):]
197199
if token == "" {
198200
return nil, nil
199201
}

‎internal/serveredition/auth/middleware_test.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,3 +800,24 @@ func TestMiddleware_BearerContextFieldParity(t *testing.T) {
800800
}
801801
}
802802
}
803+
804+
func TestMiddleware_ValidJWT_SchemeCaseInsensitive(t *testing.T) {
805+
s := setupMiddlewareTest(t)
806+
token := s.generateJWT(t, s.testUser.ID, s.testUser.Email, s.testUser.DisplayName, "user", s.testUser.Provider, 1*time.Hour)
807+
handler := s.middleware.Middleware()(authContextHandler())
808+
809+
for _, scheme := range []string{"bearer", "BEARER"} {
810+
req := httptest.NewRequest(http.MethodGet, "/api/v1/servers", nil)
811+
req.Header.Set("Authorization", scheme+" "+token)
812+
rr := httptest.NewRecorder()
813+
handler.ServeHTTP(rr, req)
814+
815+
var body map[string]interface{}
816+
if err := json.Unmarshal(rr.Body.Bytes(), &body); err != nil {
817+
t.Fatalf("%s: failed to unmarshal response (%d): %v", scheme, rr.Code, err)
818+
}
819+
if rr.Code != http.StatusOK || body["user_id"] != s.testUser.ID {
820+
t.Errorf("%s: expected 200 for user %q, got %d: %s", scheme, s.testUser.ID, rr.Code, rr.Body.String())
821+
}
822+
}
823+
}

0 commit comments

Comments
 (0)