Repository navigation
fix(gateway): accept x-api-key on the Anthropic endpoint - #453
Merged
Merged
Conversation
`POST /anthropic/v1/messages` is the Anthropic-protocol endpoint, but its
inbound credential was Bearer-only: `AuthUser::from_request_parts` read
`Authorization` and required a `Bearer ` prefix, with no `x-api-key` branch.
The official Anthropic SDKs send `x-api-key` (that is what `ANTHROPIC_API_KEY`
configures), so a valid key was answered 401 `{"error":"unauthorized"}` and
surfaced to the user as "invalid API key" — changing the key could never help.
The outbound direction already splits by protocol: `gateway::forward` sends
`x-api-key` to an Anthropic upstream and `Bearer` otherwise. Only the inbound
half was missing.
The key is now resolved by a new `presented_key` helper: `Authorization:
Bearer <key>` first, falling back to `x-api-key: <key>` only when
`Authorization` is absent or is not in `Bearer` form. Validation is unchanged
(`dao::find_api_key_user_and_id`), so this widens how the same credential may
be carried, not what it is worth; both headers present means Bearer wins, so
existing clients behave exactly as before.
Reported as rant 2026-12-03T17:32:01.
Test: the_anthropic_endpoint_accepts_the_protocols_own_credential_header —
x-api-key only => 200; no credential => 401; valid Bearer + junk x-api-key =>
200; `Authorization: Basic` + valid x-api-key => 200. With the production
change reverted the first leg fails 401 vs 200.
The prose that described inbound auth as Bearer-only moves with the change
(docs/architecture.md, docs/plan-api-matrix.md, dao.rs, routes/api_keys.rs,
the gateway module header).
cargo test 616/0, fmt and clippy clean.
Merged
argszero
added a commit
that referenced
this pull request
Dec 3, 2026
Five commits since v0.7.35 (#449, #451, #452, #453, #454). No schema change (18 either side). The version string moves in the five carriers the deploy gate guards: Cargo.toml, Cargo.lock, Dockerfile, docker-compose.yml and the new CHANGELOG heading. Co-authored-by: argszero <argszero@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POST /anthropic/v1/messagesspeaks the Anthropic protocol but accepted only one way of carrying a credential:AuthUser::from_request_partsreadAuthorizationand required aBearerprefix, with nox-api-keybranch. The official Anthropic SDKs sendx-api-key— that is whatANTHROPIC_API_KEYconfigures — so a valid key was answered401 {"error":"unauthorized"}, which the UI renders as "invalid API key". The user changes the key and the new one fails the same way.The outbound direction already splits by protocol:
gateway::forwardsendsx-api-keyto an Anthropic upstream andBearerotherwise. Only the inbound half was missing.Reported as rant
2026-12-03T17:32:01.Related Issue
No issue — this arrived as rant
2026-12-03T17:32:01in this project's development queue, which names the endpoint, both headers, the file and the fix. This project's own contribution notes describe that flow (rant → implement → PR that cites the rant), so the PR cites the rant instead of closing an issue.Changes
src/routes/mod.rs— the extractor now resolves the key through a newpresented_keyhelper:Authorization: Bearer <key>first, falling back tox-api-key: <key>only whenAuthorizationis absent or is not inBearerform. Validation is unchanged (dao::find_api_key_user_and_id), so this widens how the same credential may be carried, not what it is worth. Both headers present means Bearer wins, so existing clients behave exactly as before. A malformedBeareris deliberately not rescued by the other header: "wrong credential" and "no credential" must stay distinguishable when someone is debugging a 401.docs/plan-api-matrix.md— the client table namesANTHROPIC_API_KEYbesideANTHROPIC_AUTH_TOKEN, and a note states both carrying methods. The working configuration was previously outside the documented set, so it had to be found by trial.docs/architecture.md,src/dao.rs,src/routes/api_keys.rs,src/gateway.rs— the prose that described inbound auth as Bearer-only now saysBearerorx-api-key.Reach, stated so it reads as a decision:
AuthUseris the shared extractor, so the fallback applies to every authenticated route, not only the Anthropic endpoint. That is the shape the rant asked for, and it avoids a second credential rule that would then have to be kept in step with the first. What it costs is thatx-api-keybecomes a second accepted spelling of the same credential everywhere; since it is the same key through the same lookup and yields the same role, it is a change of spelling rather than of authority.Authority: no document conflict had to be resolved here — the rant's account matched the code on the current tip (inbound Bearer-only, outbound already split by protocol, the client table listing one variable).
Tests
New:
gateway::tests::the_anthropic_endpoint_accepts_the_protocols_own_credential_header, four legs judged by status code (the body says onlyunauthorized, so it cannot tell which header was read):x-api-keyonlyBearer+ junkx-api-keyAuthorization: Basic …+ validx-api-keyShown failing without the change: with
src/routes/mod.rsreverted to the parent commit and the test kept, leg 1 fails —只带 x-api-key 必须通过认证: {"error":"unauthorized"},left: 401, right: 200. It fails on the behaviour this PR adds, not on a missing symbol.cargo test— 616 passed, 0 failed (615 on the parent commit)cargo fmt --check— cleancargo clippy --all-targets -- -D warnings— cleanNo live system was touched: the test drives the repository's own fake upstream, and the failure measurement used the parent commit's file in the same clone.
Checklist
fix/<description>)config/config.example.tomlneeds no sync