Repository navigation
Studio: accept sk-unsloth API keys in the x-api-key header - #13842
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Anthropic clients (the Anthropic SDKs, CLIs reading ANTHROPIC_API_KEY, the Claude for Microsoft 365 add-ins) send the key in x-api-key, which Studio ignored, so /v1/messages and /v1/models answered 401. An outermost ASGI middleware now copies one sk-unsloth x-api-key into Authorization: Bearer when no usable Authorization is present, so every existing auth path handles it unchanged. A Bearer always wins and any other x-api-key value keeps today's behaviour.
64e92bb to
fd51e68
Compare
|
@codex review |
|
@codex security review |
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
🛡️ Codex Security ReviewSecurity review completed. No security issues were found in this pull request. Reviewed commit: Only the user who started this review can view the report in Codex. ℹ️ About Codex security reviews in GitHubThis is an experimental Codex feature. Security reviews are triggered when:
Once complete, Codex will leave suggestions, or a comment if no findings are found. |
Fixes #6399
Studio only read the API key from
Authorization: Bearer. Anthropic clients send it inx-api-keyinstead: the Anthropic SDKs (api_key=), CLIs that readANTHROPIC_API_KEY, and the Claude for Microsoft 365 add-ins (their gateway mode sendsx-api-keyby default). SoGET /v1/modelsandPOST /v1/messagesanswered401 Not authenticatedeven with a validsk-unsloth-...key. This is also what tripped #8663.What changes
A small pure ASGI middleware, registered outermost, copies the
x-api-keyvalue intoAuthorization: Bearerwhen:x-api-keyheader,sk-unsloth-, andAuthorization(absent or an emptyBearer).Every existing reader of
Authorizationthen handles the request unchanged: the auth dependency, the keyless access classifier and tool clamp, keep-warm, and the MCP gate. Doing this once at the edge avoids teaching each of those readers a second header.Nothing changes for current clients:
x-api-keyvalue that is not ansk-unsloth-key (for example a dummy such asnot-neededsent to keyless access, or a cloudsk-ant-...key) is ignored, exactly as today.Authorizationis set, a second pass is a no-op.GET /v1/modelsalready existed. CORS already allows any origin outside the locked-down Desktop API mode, and the preflight echoesx-api-keyandanthropic-version.This supersedes #7656 by @ousamabenyounes (closed, and not editable by maintainers). Thanks for the original work and the test ideas. That PR threaded the header through each auth dependency; this version keeps the change to one place.
Testing
tests/test_x_api_key_auth.pycovers the following against a real auth DB with a real API key:x-api-keyauthenticates exactly like Bearer.x-api-keystill works.x-api-key.main.py. Removing the registration fails this test.Real Studio app (
main.app), merge base vs this branch:GET /v1/modelswithx-api-key: sk-unsloth-...Not authenticatedPOST /v1/messageswithx-api-key: sk-unsloth-...authentication_errorNo GGUF model loadedin the test, no model)GET /v1/modelswith an invalidsk-unsloth-keyInvalid or expired API keyGET /v1/modelswith no credentialsGET /v1/modelswith Bearerhttps://pivot.claude.aix-api-key,anthropic-version,content-typeAlso ran
test_keyless_api_access.py,test_keyless_api_access_adversarial.py,test_middleware.py,test_mcp_http_integration.pyandtest_python39_compatibility.py, all passing. The rewrite costs about 1.6 us per HTTP request (once per request, not per token).