-
Notifications
You must be signed in to change notification settings - Fork 835
fix(providers): allow baseUrl override for Anthropic Claude provider #2109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
drakonkat
wants to merge
1
commit into
lidge-jun:dev
from
drakonkat:fix/anthropic-allow-baseurl-override
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { expect, test } from "bun:test"; | ||
| import { routeModel } from "../src/router"; | ||
| import { providerDestinationConfigError } from "../src/lib/destination-policy"; | ||
| import type { OcxConfig, OcxProviderConfig } from "../src/types"; | ||
|
|
||
| /** | ||
| * Regression coverage for the allowBaseUrlOverride opt-in on the anthropic | ||
| * registry entry. | ||
| * | ||
| * Before the opt-in, the pinned registry endpoint silently outranked a saved | ||
| * baseUrl and the router emitted the discarded-baseUrl diagnostic (see | ||
| * tests/router-discarded-baseurl-warning.test.ts, which now pins google as | ||
| * its fixture). Users routing Claude traffic through a local relay or an | ||
| * enterprise gateway therefore could not redirect the provider at all. These | ||
| * tests pin the new contract: a resolved user baseUrl wins, no warning fires, | ||
| * and the registry endpoint remains the default seeded value. | ||
| */ | ||
| const PROVIDER = "anthropic"; | ||
| const REGISTRY_BASE_URL = "https://api.anthropic.com"; | ||
| const MODEL = PROVIDER + "/claude-sonnet-5"; | ||
|
|
||
| function configFor(provider: OcxProviderConfig): OcxConfig { | ||
| return { | ||
| port: 10100, | ||
| defaultProvider: PROVIDER, | ||
| providers: { [PROVIDER]: provider }, | ||
| }; | ||
| } | ||
|
|
||
| function routeCapturingWarnings(config: OcxConfig): { baseUrl: string; warnings: string[] } { | ||
| const warnings: string[] = []; | ||
| const originalWarn = console.warn; | ||
| console.warn = (...args: unknown[]) => { warnings.push(args.map(String).join(" ")); }; | ||
| try { | ||
| const route = routeModel(config, MODEL); | ||
| return { baseUrl: route.provider.baseUrl, warnings }; | ||
| } finally { | ||
| console.warn = originalWarn; | ||
| } | ||
| } | ||
|
|
||
| test("anthropic honors a configured baseUrl override", () => { | ||
| const { baseUrl, warnings } = routeCapturingWarnings(configFor({ | ||
| adapter: "anthropic", | ||
| baseUrl: "https://claude-relay.example.test", | ||
| } as OcxProviderConfig)); | ||
|
|
||
| expect(baseUrl).toBe("https://claude-relay.example.test"); | ||
| // The override is applied, so the discarded-baseUrl diagnostic must not fire. | ||
| expect(warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test("anthropic keeps the registry endpoint when the seeded baseUrl is unchanged", () => { | ||
| // providerConfigSeed copies the registry baseUrl into every saved config, so the | ||
| // no-override case reaches the router as a config whose baseUrl equals the registry URL. | ||
| const { baseUrl, warnings } = routeCapturingWarnings(configFor({ | ||
| adapter: "anthropic", | ||
| baseUrl: REGISTRY_BASE_URL, | ||
| } as OcxProviderConfig)); | ||
|
|
||
| expect(baseUrl).toBe(REGISTRY_BASE_URL); | ||
| expect(warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test("anthropic requires a resolved baseUrl once override is enabled", () => { | ||
| // allowBaseUrlOverride providers fail closed on a missing baseUrl instead of silently | ||
| // re-pinning the registry endpoint; the seed guarantees real configs always carry one. | ||
| expect(() => routeModel(configFor({ | ||
| adapter: "anthropic", | ||
| } as OcxProviderConfig), MODEL)).toThrow(/Invalid baseUrl/); | ||
| }); | ||
|
|
||
| test("anthropic rejects an unresolved template baseUrl override", () => { | ||
| expect(() => routeModel(configFor({ | ||
| adapter: "anthropic", | ||
| baseUrl: "https://{region}.example.test", | ||
| } as OcxProviderConfig), MODEL)).toThrow(/Invalid baseUrl/); | ||
| }); | ||
|
|
||
| /** | ||
| * Security regression (CodeRabbit, PR #2109): anthropic is an OAuth provider, so an | ||
| * allowBaseUrlOverride endpoint receives bearer credentials. A cleartext http override to a | ||
| * non-local destination must be rejected on BOTH enforcement paths: routing (normal requests, | ||
| * via assertProviderDestinationAllowed) and providerDestinationConfigError, the shared gate | ||
| * that config validation and the model-discovery outbound layer (providerGet/providerPost in | ||
| * src/lib/provider-outbound.ts) consult before any fetch. | ||
| */ | ||
| test("anthropic rejects a cleartext http override on the routing path", () => { | ||
| expect(() => routeModel(configFor({ | ||
| adapter: "anthropic", | ||
| baseUrl: "http://claude-relay.example.test", | ||
| } as OcxProviderConfig), MODEL)).toThrow(/https/); | ||
| }); | ||
|
|
||
| test("anthropic rejects a cleartext http override on the discovery/config gate", () => { | ||
| expect(providerDestinationConfigError(PROVIDER, { | ||
| baseUrl: "http://claude-relay.example.test", | ||
| } as OcxProviderConfig)).toMatch(/https/); | ||
| // The https form of the same destination stays accepted. | ||
| expect(providerDestinationConfigError(PROVIDER, { | ||
| baseUrl: "https://claude-relay.example.test", | ||
| } as OcxProviderConfig)).toBeNull(); | ||
| }); | ||
|
|
||
| test("anthropic keeps http for an explicitly local relay", () => { | ||
| // Loopback and allowPrivateNetwork opt-ins are the documented local-transport escape | ||
| // hatch; the https requirement must not break a localhost proxy. | ||
| const { baseUrl, warnings } = routeCapturingWarnings(configFor({ | ||
| adapter: "anthropic", | ||
| baseUrl: "http://127.0.0.1:8787", | ||
| allowPrivateNetwork: true, | ||
| } as OcxProviderConfig)); | ||
|
|
||
| expect(baseUrl).toBe("http://127.0.0.1:8787"); | ||
| expect(warnings).toHaveLength(0); | ||
| }); | ||
|
|
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Do not let
allowPrivateNetworkbypass HTTPS for public hosts.Line 155 returns before
baseUrlis classified. Therefore,allowPrivateNetwork: trueacceptshttp://claude-relay.example.testand sends Anthropic OAuth credentials over cleartext HTTP.Apply the opt-in only after the destination is confirmed as loopback, localhost, or private. Apply the same transport rule in
providerDestinationResolvedErrorbefore itsproviderAllowsPrivateNetworkbypass. Add a regression test that rejects a public HTTP host withallowPrivateNetwork: true.Based on learnings: OAuth adapters that attach Bearer credentials must enforce HTTPS separately whenever cleartext transmission is unacceptable.
🤖 Prompt for AI Agents
Source: Learnings