Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion web/app/api/cli/config/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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";
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
url.port = "";
}
return url.toString();
}

function unavailableResponse(): Response {
return Response.json(
{ error: "cli_auth_unavailable" },
Expand Down
57 changes: 57 additions & 0 deletions web/tests/cli-config-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down Expand Up @@ -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(
{
Expand Down
Loading