-
Notifications
You must be signed in to change notification settings - Fork 853
fix(providers): allow a baseUrl override for Anthropic and Antigravity #2148
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
Merged
Merged
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
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,144 @@ | ||
| 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); | ||
| }); | ||
|
|
||
|
|
||
| test("a public http override cannot buy transport security with allowPrivateNetwork", () => { | ||
| // allowPrivateNetwork states that a destination is intentionally LOCAL. It is not a waiver of | ||
| // transport security. Reading it before classifying the address let http://attacker.example | ||
| // carry this provider's OAuth bearer in cleartext to a public host. | ||
| // | ||
| // Routing REFUSES rather than downgrading: a request must not reach an endpoint that would | ||
| // receive the token in the clear, so this fails closed at the route boundary. | ||
| expect(() => routeCapturingWarnings(configFor({ | ||
| adapter: "anthropic", | ||
| baseUrl: "http://attacker.example/v1", | ||
| allowPrivateNetwork: true, | ||
| } as OcxProviderConfig))).toThrow(/must use https/); | ||
| }); | ||
|
|
||
| test("the seeded https endpoint is still reachable with the opt-in set", () => { | ||
| // Guard against over-correcting: the fix must refuse cleartext to a public host without | ||
| // refusing an ordinary https override that happens to carry the flag. | ||
| const { baseUrl, warnings } = routeCapturingWarnings(configFor({ | ||
| adapter: "anthropic", | ||
| baseUrl: "https://gateway.example/v1", | ||
| allowPrivateNetwork: true, | ||
| } as OcxProviderConfig)); | ||
|
|
||
| expect(baseUrl).toBe("https://gateway.example/v1"); | ||
| 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
| 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 | ||
| * google-antigravity 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). Users routing Antigravity | ||
| * traffic through a local relay or region-specific proxy 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 when nothing is configured. | ||
| */ | ||
| const PROVIDER = "google-antigravity"; | ||
| const REGISTRY_BASE_URL = "https://daily-cloudcode-pa.googleapis.com"; | ||
| const MODEL = PROVIDER + "/gemini-3.7-flash"; | ||
|
|
||
| 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("google-antigravity honors a configured baseUrl override", () => { | ||
| const { baseUrl, warnings } = routeCapturingWarnings(configFor({ | ||
| adapter: "google", | ||
| baseUrl: "https://antigravity-relay.example.test", | ||
| } as OcxProviderConfig)); | ||
|
|
||
| expect(baseUrl).toBe("https://antigravity-relay.example.test"); | ||
| // The override is applied, so the discarded-baseUrl diagnostic must not fire. | ||
| expect(warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test("google-antigravity 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: "google", | ||
| baseUrl: REGISTRY_BASE_URL, | ||
| } as OcxProviderConfig)); | ||
|
|
||
| expect(baseUrl).toBe(REGISTRY_BASE_URL); | ||
| expect(warnings).toHaveLength(0); | ||
| }); | ||
|
|
||
| test("google-antigravity 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: "google", | ||
| } as OcxProviderConfig), MODEL)).toThrow(/Invalid baseUrl/); | ||
| }); | ||
|
|
||
| test("google-antigravity rejects an unresolved template baseUrl override", () => { | ||
| expect(() => routeModel(configFor({ | ||
| adapter: "google", | ||
| baseUrl: "https://{region}.example.test", | ||
| } as OcxProviderConfig), MODEL)).toThrow(/Invalid baseUrl/); | ||
| }); | ||
|
|
||
| /** | ||
| * Security regression (CodeRabbit, PR #2110): google-antigravity 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 outbound layer (providerGet/providerPost in | ||
| * src/lib/provider-outbound.ts) consult before any fetch. | ||
| */ | ||
| test("google-antigravity rejects a cleartext http override on the routing path", () => { | ||
| expect(() => routeModel(configFor({ | ||
| adapter: "google", | ||
| baseUrl: "http://antigravity-relay.example.test", | ||
| } as OcxProviderConfig), MODEL)).toThrow(/https/); | ||
| }); | ||
|
|
||
| test("google-antigravity rejects a cleartext http override on the discovery/config gate", () => { | ||
| expect(providerDestinationConfigError(PROVIDER, { | ||
| baseUrl: "http://antigravity-relay.example.test", | ||
| } as OcxProviderConfig)).toMatch(/https/); | ||
| // The https form of the same destination stays accepted. | ||
| expect(providerDestinationConfigError(PROVIDER, { | ||
| baseUrl: "https://antigravity-relay.example.test", | ||
| } as OcxProviderConfig)).toBeNull(); | ||
| }); | ||
|
|
||
| test("google-antigravity keeps http for an explicitly local relay", () => { | ||
| // The local proxy (127.0.0.1) is the motivating use case for this override; the https | ||
| // requirement must not break it. allowPrivateNetwork is the documented local opt-in. | ||
| const { baseUrl, warnings } = routeCapturingWarnings(configFor({ | ||
| adapter: "google", | ||
| baseUrl: "http://127.0.0.1:47821", | ||
| allowPrivateNetwork: true, | ||
| } as OcxProviderConfig)); | ||
|
|
||
| expect(baseUrl).toBe("http://127.0.0.1:47821"); | ||
| 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
Oops, something went wrong.
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Add RFC1918 relay regression coverage.
Both tests cover
127.0.0.1, but neither covers the separateprivatebranch insrc/lib/destination-policy.ts. A regression that rejects or permits RFC1918 relays incorrectly would pass this suite.tests/anthropic-baseurl-override.test.ts#L105-L116: Add a case for anhttp://192.168.x.xoverride withallowPrivateNetwork: true.tests/antigravity-baseurl-override.test.ts#L105-L116: Add the equivalent case for Google Antigravity.As per path instructions, “A behavior change in src/ should come with a focused regression test near the existing tests for that subsystem.”
📍 Affects 2 files
tests/anthropic-baseurl-override.test.ts#L105-L116(this comment)tests/antigravity-baseurl-override.test.ts#L105-L116🤖 Prompt for AI Agents
Source: Path instructions