Repository navigation
fix(server): accept case-insensitive Bearer auth scheme (#671) - #717
kimi wang (wangkangmao) wants to merge 2 commits into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the Bearer authentication parsing update.
No open issues 🎉
What changed in this PR
Fixes #671 by allowing the server’s optional API-key authentication to accept case-insensitive Bearer schemes and multiple spaces before credentials.
Changes:
- Parse the Authorization header by whitespace and compare the Bearer scheme case-insensitively, leaving
x-api-keyauthentication unchanged. - Add regression tests for scheme casing, extra spaces, unsupported schemes, incorrect keys, and missing credentials.
| File | Description |
|---|---|
tests/server/test_endpoints.py |
Adds positive and negative Bearer authentication regression tests. |
agentlightning/server/app.py |
Normalizes Bearer scheme casing and credential whitespace before checking the key. |
Review effort: Balanced
|
kimi wang (@wangkangmao) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
Fixes #671
What
The server's optional API-key dependency compared the
Authorizationheader withstartswith("Bearer "), which treats the auth-scheme token as case-sensitive and assumesexactly one separator space. A request carrying a perfectly valid key as
authorization: bearer <key>orBeArEr <key>was rejected with401 Invalid or missing API key.RFC 7235 §2.1 defines the auth scheme as a case-insensitive token and permits one or more
spaces between the scheme and the credentials. Gateways/proxies that normalize the scheme to
lowercase therefore could not authenticate.
Root cause
agentlightning/server/app.py,_build_auth_dependency().verify_key():Two issues in one comparison: literal
"Bearer "(case-sensitive, exactly one space) and ahard-coded
[7:]slice that does not normalize additional whitespace.Change
agentlightning/server/app.py: split the header on whitespace (split(None, 1)), comparethe scheme with
.lower() == "bearer", and.strip()the credential before comparing itwith the configured key. Unrecognized schemes, missing credentials and wrong keys still
produce 401; the
x-api-keypath is unchanged.tests/server/test_endpoints.py: add regression coverage forBearer/bearer/BEARER/
BeArEr, for multiple spaces after the scheme, and for rejectingBasic, a wrong key,a bare scheme and an empty credential.
No behavior change for correctly formatted
Bearer <key>orx-api-keyrequests.Verification
Environment: Python 3.13, editable install of this checkout with
[dev]extras.Negative control (restore only the old
app.py, keep the new tests):python -m py_compile agentlightning/server/app.py tests/server/test_endpoints.pyexits 0.Both files are uploaded with LF line endings, matching the upstream blobs (0 CRLF), so the
diff contains only the intended lines.
The full test suite and Ray/VERL integration paths were not run in this environment; the change
is confined to the optional auth dependency and its endpoint tests.