Skip to content

fix(gateway): accept x-api-key on the Anthropic endpoint - #453

Merged
argszero merged 1 commit into
mainfrom
fix/anthropic-inbound-x-api-key
Dec 3, 2026
Merged

argszero merged 1 commit into
mainfrom
fix/anthropic-inbound-x-api-key

Conversation

@argszero

@argszero argszero commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Summary

POST /anthropic/v1/messages speaks the Anthropic protocol but accepted only one way of carrying a credential: 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"}, 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::forward sends x-api-key to an Anthropic upstream and Bearer otherwise. 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:01 in 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 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. A malformed Bearer is 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 names ANTHROPIC_API_KEY beside ANTHROPIC_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 says Bearer or x-api-key.

Reach, stated so it reads as a decision: AuthUser is 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 that x-api-key becomes 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 only unauthorized, so it cannot tell which header was read):

leg request expected
1 x-api-key only 200
2 no credential header 401
3 valid Bearer + junk x-api-key 200 (Bearer priority)
4 Authorization: Basic … + valid x-api-key 200 (the fallback's trigger condition)

Shown failing without the change: with src/routes/mod.rs reverted 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 — clean
  • cargo clippy --all-targets -- -D warnings — clean

No 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

  • Branch name follows the convention (fix/<description>)
  • Commit message uses Conventional Commits
  • Single purpose, minimal change
  • No config or schema change, so config/config.example.toml needs no sync

`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.
@argszero
argszero merged commit 94dd76e into main Dec 3, 2026
2 checks passed
@argszero argszero mentioned this pull request Dec 3, 2026
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>
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.

1 participant