diff --git a/web/app/api/cli/config/route.ts b/web/app/api/cli/config/route.ts index 5ebe585dc2c1..7f9263185abb 100644 --- a/web/app/api/cli/config/route.ts +++ b/web/app/api/cli/config/route.ts @@ -5,6 +5,10 @@ import { const DEFAULT_STACK_API_URL = "https://api.stack-auth.com/api/v1"; +const PRODUCTION_CODEROUTER_HOSTS = new Set([ + "coderouter.dev", + "www.coderouter.dev", +]); // This metadata is informational for now. Keep the legacy `version` field // below for clients that already consume the config response, and do not gate @@ -45,7 +49,7 @@ export function GET(request: Request): Response { publishableClientKey, // Start and complete the CLI login against the same deployment so the // confirmation page uses the Stack project that issued the login code. - confirmUrl: new URL("/handler/cli-auth-confirm", request.url).toString(), + confirmUrl: cliAuthConfirmationURL(request), }, coderouter: { sessionUrl: new URL("/api/coderouter/session", request.url).toString(), @@ -74,6 +78,24 @@ export function GET(request: Request): Response { ); } +/** + * Keep the CodeRouter data plane on its dedicated host while sending browser + * sign-in through cmux.com's canonical auth surface. The Stack login code is + * issued by the same deployment and remains in the CLI confirmation query. + * Non-production and explicitly configured hosts stay on their own origin. + */ +function cliAuthConfirmationURL(request: Request): string { + const url = new URL("/handler/cli-auth-confirm", request.url); + if ( + url.protocol === "https:" && + PRODUCTION_CODEROUTER_HOSTS.has(url.hostname) + ) { + url.hostname = "cmux.com"; + url.port = ""; + } + return url.toString(); +} + function unavailableResponse(): Response { return Response.json( { error: "cli_auth_unavailable" }, diff --git a/web/tests/cli-config-route.test.ts b/web/tests/cli-config-route.test.ts index d7feba6daae9..30c3f51f32b5 100644 --- a/web/tests/cli-config-route.test.ts +++ b/web/tests/cli-config-route.test.ts @@ -48,6 +48,46 @@ async function withCliConfigEnvironment( } describe("CLI config route", () => { + test("uses cmux.com for production CodeRouter browser auth and preserves the login code", async () => { + await withCliConfigEnvironment(testEnvironment, async () => { + const response = GET( + new Request("https://coderouter.dev/api/cli/config"), + ); + + expect(response.status).toBe(200); + const body = await response.json(); + expect(body.auth.confirmUrl).toBe( + "https://cmux.com/handler/cli-auth-confirm", + ); + expect(body.coderouter.sessionUrl).toBe( + "https://coderouter.dev/api/coderouter/session", + ); + expect(body.coderouter.openaiBaseUrl).toBe("https://coderouter.dev/v1"); + + const nonDefaultPortResponse = GET( + new Request("https://coderouter.dev:4443/api/cli/config"), + ); + const nonDefaultPortBody = await nonDefaultPortResponse.json(); + expect(nonDefaultPortBody.auth.confirmUrl).toBe( + "https://cmux.com/handler/cli-auth-confirm", + ); + expect(nonDefaultPortBody.coderouter.sessionUrl).toBe( + "https://coderouter.dev:4443/api/coderouter/session", + ); + + const confirmation = new URL(body.auth.confirmUrl); + confirmation.searchParams.set("login_code", "fresh-test-login-code"); + const signIn = new URL("https://cmux.com/handler/sign-in"); + signIn.searchParams.set( + "after_auth_return_to", + `${confirmation.pathname}${confirmation.search}`, + ); + expect(signIn.toString()).toBe( + "https://cmux.com/handler/sign-in?after_auth_return_to=%2Fhandler%2Fcli-auth-confirm%3Flogin_code%3Dfresh-test-login-code", + ); + }); + }); + test("publishes CodeRouter and the hosted Subrouter POST contract", async () => { await withCliConfigEnvironment(testEnvironment, async () => { const response = GET(new Request("https://cmux.com/api/cli/config")); @@ -128,6 +168,23 @@ describe("CLI config route", () => { }); }); + test("keeps confirmation on a custom staging origin", async () => { + await withCliConfigEnvironment(testEnvironment, async () => { + const response = GET( + new Request("https://coderouter-staging.example.test/api/cli/config"), + ); + + expect(response.status).toBe(200); + const body = await response.json(); + expect(body.auth.confirmUrl).toBe( + "https://coderouter-staging.example.test/handler/cli-auth-confirm", + ); + expect(body.coderouter.sessionUrl).toBe( + "https://coderouter-staging.example.test/api/coderouter/session", + ); + }); + }); + test("returns 503 instead of advertising incomplete Stack configuration", async () => { await withCliConfigEnvironment( {