Skip to content

Studio: accept sk-unsloth API keys in the x-api-key header - #13842

Merged
danielhanchen merged 2 commits into
mainfrom
studio-x-api-key-auth
Oct 15, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-x-api-key-auth

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Fixes #6399

Studio only read the API key from Authorization: Bearer. Anthropic clients send it in x-api-key instead: the Anthropic SDKs (api_key=), CLIs that read ANTHROPIC_API_KEY, and the Claude for Microsoft 365 add-ins (their gateway mode sends x-api-key by default). So GET /v1/models and POST /v1/messages answered 401 Not authenticated even with a valid sk-unsloth-... key. This is also what tripped #8663.

What changes

A small pure ASGI middleware, registered outermost, copies the x-api-key value into Authorization: Bearer when:

  • there is exactly one x-api-key header,
  • its value starts with sk-unsloth-, and
  • the request has no usable Authorization (absent or an empty Bearer).

Every existing reader of Authorization then 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:

  • A request that already carries a Bearer (UI session, desktop JWT, API key) is left alone, so Bearer always wins.
  • Any x-api-key value that is not an sk-unsloth- key (for example a dummy such as not-needed sent to keyless access, or a cloud sk-ant-... key) is ignored, exactly as today.
  • No UI, Desktop, or notebook flow is touched. The rewrite is idempotent: once Authorization is set, a second pass is a no-op.

GET /v1/models already existed. CORS already allows any origin outside the locked-down Desktop API mode, and the preflight echoes x-api-key and anthropic-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.py covers the following against a real auth DB with a real API key:

  • x-api-key authenticates exactly like Bearer.
  • An empty Bearer plus x-api-key still works.
  • Missing, invalid, dummy and non-Unsloth keys are still refused.
  • A session Bearer wins over x-api-key.
  • Keyless access is unchanged for dummy keys, and a real key is not treated as keyless.
  • The rewrite is idempotent.
  • The middleware is the outermost layer in main.py. Removing the registration fails this test.

Real Studio app (main.app), merge base vs this branch:

Request Before After
GET /v1/models with x-api-key: sk-unsloth-... 401 Not authenticated 200, model list
POST /v1/messages with x-api-key: sk-unsloth-... 401 authentication_error reaches the handler (503 No GGUF model loaded in the test, no model)
GET /v1/models with an invalid sk-unsloth- key 401 401 Invalid or expired API key
GET /v1/models with no credentials 401 401
GET /v1/models with Bearer 200 200
CORS preflight from https://pivot.claude.ai 200, echoes x-api-key,anthropic-version,content-type same

Also ran test_keyless_api_access.py, test_keyless_api_access_adversarial.py, test_middleware.py, test_mcp_http_integration.py and test_python39_compatibility.py, all passing. The rewrite costs about 1.6 us per HTTP request (once per request, not per token).

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-15T11:54:51.764554Z fe5a7ff Manual request
🔒 Security Review ✅ Completed 2026-10-15T11:55:48.703180Z fe5a7ff Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

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.
@danielhanchen danielhanchen self-assigned this Oct 15, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex security review

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: fe5a7ff214

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review

Security review completed. No security issues were found in this pull request.

Reviewed commit: fe5a7ff214

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Support Claude for Microsoft Office

1 participant