Skip to content

fix(anthropic): support bearer key auth for gateways - #595

Merged
Wibias merged 4 commits into
lidge-jun:devfrom
iF2007:fix/anthropic-bearer-discovery
Jul 28, 2026
Merged

fix(anthropic): support bearer key auth for gateways#595
Wibias merged 4 commits into
lidge-jun:devfrom
iF2007:fix/anthropic-bearer-discovery

Conversation

@iF2007

@iF2007 iF2007 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Support bearer API-key auth for Anthropic-compatible gateways across Messages, model discovery, and key validation.
  • Expose apiKeyTransport in the Dashboard, provider API, and CLI; document the option.
  • Normalize /v1 base URLs when discovering Anthropic models.

Validation

  • bun run typecheck
  • bun run lint:gui
  • bun run privacy:scan
  • bun run test (5610 pass, 0 fail)
  • cd gui && bun test && bun run build && bun run lint

Use apiKeyTransport when building Anthropic key-auth requests and normalize Anthropic model discovery URLs so /v1 base URLs do not become /v1/v1/models.
@github-actions github-actions Bot added the bug Something isn't working label Jul 28, 2026
@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Anthropic provider configuration now supports selectable x-api-key or bearer authentication. The setting propagates through registry, CLI, GUI, management APIs, request validation, message/models requests, URL normalization, tests, and localized documentation.

Changes

Anthropic authentication routing

Layer / File(s) Summary
Authentication contract and provider propagation
src/types.ts, src/providers/registry.ts, src/providers/derive.ts, src/oauth/login-cli.ts
Adds optional apiKeyTransport configuration and carries it through registry seeds, derived providers, enriched providers, and key-login configurations.
Configuration and provider management
src/config.ts, src/server/auth-cors.ts, src/server/management/provider-routes.ts, src/cli/provider.ts, src/cli/provider-runtime.ts
Validates Anthropic key-auth usage, exposes the setting through safe configuration and management responses, and supports adding, editing, and clearing it through CLI and API routes.
Provider UI and payload integration
gui/src/components/*, gui/src/provider-payload.ts, gui/src/provider-workspace/*, gui/src/i18n/*
Adds transport selectors for Anthropic key authentication, preserves the value during editing, conditionally serializes bearer mode, and adds localized labels.
Anthropic request authentication routing
src/adapters/anthropic.ts, src/oauth/index.ts, src/oauth/key-providers.ts
Selects bearer or x-api-key headers for message, models, and validation requests; preserves OAuth-specific headers and normalizes the /v1/models?limit=1000 URL.
Validation and documentation
tests/*, docs-site/src/content/docs/*
Tests configuration, management, CLI, payload, propagation, URL normalization, header selection, and validation behavior; reference documentation describes the option across supported locales.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ProviderConfig
  participant ProviderUI
  participant ManagementAPI
  participant RequestBuilder
  participant AnthropicAPI
  ProviderUI->>ManagementAPI: submit apiKeyTransport
  ManagementAPI->>ProviderConfig: validate and persist setting
  ProviderConfig->>RequestBuilder: provide auth mode and transport
  RequestBuilder->>AnthropicAPI: send x-api-key or Authorization Bearer
Loading

Suggested reviewers: wibias, ingwannu, lidge-jun

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 7.69% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: Anthropic gateway key auth now supports bearer transport.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/types.ts`:
- Around line 876-881: Propagate the `apiKeyTransport` option from the
credential configuration into `KeyLoginProvider`, and update its Anthropic
validation request to select `x-api-key` or `Authorization: Bearer` consistently
with `createAnthropicAdapter` and `buildModelsRequest`. Add a regression test
covering bearer-based key validation while preserving native header behavior by
default.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 88efee9c-3a54-4774-82dc-17e6158d0fc0

📥 Commits

Reviewing files that changed from the base of the PR and between 903b62c and 4ff6535.

📒 Files selected for processing (5)
  • src/adapters/anthropic.ts
  • src/oauth/index.ts
  • src/types.ts
  • tests/google-models-listing.test.ts
  • tests/umans-provider.test.ts

Comment thread src/types.ts

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4ff653577b

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

Comment thread src/types.ts
* Defaults to the native Anthropic `x-api-key`; gateways may require
* `Authorization: Bearer <key>` instead.
*/
apiKeyTransport?: "x-api-key" | "bearer";

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Document the new provider auth knob

This introduces apiKeyTransport as a user-facing provider config field that bearer-only Anthropic gateways now rely on, but the commit does not update docs-site/ (and rg apiKeyTransport docs-site has no hits). Users following the configuration reference will still have no documented way to discover that setting apiKeyTransport: "bearer" fixes these gateway 401s, so please add the option and keep the localized docs in sync.

AGENTS.md reference: AGENTS.md:L96-L97

Useful? React with 👍 / 👎.

Comment thread src/adapters/anthropic.ts
headers["x-client-request-id"] = crypto.randomUUID();
} else {
headers["x-api-key"] = provider.apiKey;
if (anthropicKeyUsesBearer(provider)) headers["Authorization"] = `Bearer ${provider.apiKey}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Expose bearer transport through provider setup

This branch only activates when the saved provider already has apiKeyTransport: "bearer", but the normal Dashboard/CLI setup paths still cannot set that field (buildProviderPayload has no payload member and ocx provider edit has no option, while an apiKeyTransport-only PATCH is unrecognized). For an Anthropic-compatible gateway added through those supported flows, the provider is saved without the flag and requests continue to send x-api-key, so the 401 this patch targets remains unless the user hand-edits config; please wire the field through provider setup/edit or seed it for affected gateways.

Useful? React with 👍 / 👎.

Comment thread src/adapters/anthropic.ts
headers["x-client-request-id"] = crypto.randomUUID();
} else {
headers["x-api-key"] = provider.apiKey;
if (anthropicKeyUsesBearer(provider)) headers["Authorization"] = `Bearer ${provider.apiKey}`;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Request security review for auth-header changes

This line changes how a provider API key is serialized into upstream authentication headers for Anthropic-compatible providers, so it falls under the repository security boundary for authentication and credential/token handling changes. I don't see explicit security-review evidence in this commit; please record that review/signoff before merging this auth-path change.

AGENTS.md reference: AGENTS.md:L83-L87

Useful? React with 👍 / 👎.

iF2007 added 2 commits July 28, 2026 11:39
Carry apiKeyTransport through key-login provider derivation and use the same bearer vs x-api-key selection during Anthropic API-key validation.
@iF2007

iF2007 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the remaining setup and documentation feedback for apiKeyTransport.

Changes:

  • Exposed apiKeyTransport through supported setup/edit paths: Dashboard add/edit forms, POST/PATCH /api/providers, ocx provider add --api-key-transport, and ocx provider edit --api-key-transport.
  • Added config/management validation so the field is accepted only for Anthropic key-auth providers and rejected for OAuth/forward/local or non-Anthropic adapters.
  • Updated English, zh-CN, ja, ko, and ru docs for provider config and Anthropic adapter auth behavior.
  • Added regression coverage for GUI payloads, CLI parity, disk config validation, and management API PATCH behavior.

Validation run locally:

  • bun run typecheck
  • bun run lint:gui
  • bun run privacy:scan
  • bun run test (5610 pass, 0 fail)
  • bun test tests/cli-provider.test.ts tests/cli-headless-parity.test.ts
  • git diff --check

Note: the remaining security-review/signoff request is a maintainer process item for the auth-header change, not something code can self-certify.

@iF2007

iF2007 commented Jul 28, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up CI fix pushed in 7b602baf.

It addresses the check failures from the previous run:

  • Added the missing Russian GUI locale keys for modal.apiKeyTransport, modal.apiKeyTransportNative, and modal.apiKeyTransportBearer.
  • This fixes the GUI type/build errors and the locale parity test failure (ru:1450 expected vs ru:1447 received).

Focused validation run locally after the fix:

  • bun run typecheck
  • cd gui && bun test
  • cd gui && bun run build
  • cd gui && bun run lint

@iF2007 iF2007 changed the title fix(anthropic): honor bearer key auth for gateway models fix(anthropic): support bearer key auth for gateways Jul 28, 2026
@Wibias

Wibias commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Maintainer review — bug + security (7b602baf)

Verdict: approve / merge-ready (CI green on Linux/Windows/macOS + npm-global). Not merging from this comment.

Open bot threads

Source Item Status on tip
CodeRabbit Propagate apiKeyTransport through key validation Resolvedkey-providers.ts + bearer validation test landed in 24c71bba
Codex P2 Document apiKeyTransport in docs-site Stale / fixed — EN + zh-CN/ja/ko/ru configuration + adapters docs in fbcc8311
Codex P2 Expose via Dashboard/CLI/management PATCH Stale / fixed — GUI selector, ocx provider add/edit --api-key-transport, POST/PATCH validation in fbcc8311
Codex P1 Security review for auth-header change Done below

Please resolve the three remaining Codex threads; they no longer describe tip.

Security (auth boundary)

Scoped to this PR’s credential path (apiKeyTransport → Anthropic key auth headers):

  • Transport restricted to Anthropic + key auth (config Zod, management, CLI); OAuth path unchanged
  • Key mode sends either x-api-key or Authorization: Bearer, not both
  • SENSITIVE_PROVIDER_HEADERS still blocks auth-header override via provider.headers
  • No new key/token logging; GET responses expose transport mode only

No medium+ security findings in #595. (A loopback Host-gate note from review tooling is out of scope here — that is already on dev via #573, not this diff.)

Bug review

No merge-blocking functional defect for the stated goals (bearer gateways + /v1 models URL normalize + setup/docs).

One latent consistency note (not a tip bug today — no registry preset currently seeds apiKeyTransport: "bearer"):

  • enrichProviderFromRegistry backfills apiKeyTransport, but routedProviderConfig does not. If a future preset seeds bearer and a persisted provider omits the field, catalog/models enrichment could use Bearer while live routed turns still default to x-api-key. Optional follow-up: mirror the googleMode-style backfill in src/router.ts.

Summary

Ship it. Author already closed the actionable Rabbit/Codex code gaps; security signoff recorded for the auth-header change.

@Wibias

Wibias commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the PR — nice fix for Anthropic-compatible gateways (bearer transport + /v1 models URL), and for following through on the validation/setup/docs feedback. Squash-merging into dev now.

@Wibias
Wibias merged commit 2a404c7 into lidge-jun:dev Jul 28, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants