Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(web): preserve CLI callback params across Google OAuth redirect (MUL-3313) (#4167)

Willow Lopez089832d6ec04
diff --git a/apps/web/app/(auth)/login/page.tsx b/apps/web/app/(auth)/login/page.tsx
index a726bd4f65..4273410f8a 100644
--- a/apps/web/app/(auth)/login/page.tsx
+++ b/apps/web/app/(auth)/login/page.tsx
@@ -125,11 +125,18 @@ function LoginPageContent() {
router.push(await resolveLoggedInDestination(qc, onboarded, list));
};
- // Build Google OAuth state: encode platform + next URL so the callback
- // can redirect to the right place after login.
+ // Build Google OAuth state: encode platform, next URL, and CLI callback
+ // params so the callback can redirect to the right place after login.
+ // CLI callback/state must survive the Google OAuth round-trip so the
+ // post-login callback page can redirect the JWT back to the CLI's local
+ // HTTP listener (critical for headless / WSL2 environments).
const googleState = [
platform === "desktop" ? "platform:desktop" : "",
nextUrl ? `next:${nextUrl}` : "",
+ cliCallbackRaw && validateCliCallback(cliCallbackRaw)
+ ? `cli_callback:${encodeURIComponent(cliCallbackRaw)}`
+ : "",
+ cliState ? `cli_state:${encodeURIComponent(cliState)}` : "",
]
.filter(Boolean)
.join(",") || undefined;
diff --git a/apps/web/app/auth/callback/page.test.tsx b/apps/web/app/auth/callback/page.test.tsx
index 9ba984a4b8..8e6bf069fd 100644
--- a/apps/web/app/auth/callback/page.test.tsx
+++ b/apps/web/app/auth/callback/page.test.tsx
@@ -197,6 +197,104 @@ describe("CallbackPage", () => {
});
});
+ it("redirects to CLI callback with token when state contains valid cli_callback", async () => {
+ const { api: mockedApi } = await import("@multica/core/api");
+ const mockGoogleLogin = mockedApi.googleLogin as ReturnType<typeof vi.fn>;
+
+ const hrefSetter = vi.fn();
+ const originalLocation = window.location;
+ Object.defineProperty(window, "location", {
+ configurable: true,
+ writable: true,
+ value: { ...originalLocation, set href(value: string) { hrefSetter(value); } },
+ });
+
+ try {
+ mockSearchParams.set(
+ "state",
+ "cli_callback:http://127.0.0.1:46233/callback,cli_state:abc123",
+ );
+ mockGoogleLogin.mockResolvedValue({ token: "cli-jwt-token" });
+
+ render(<CallbackPage />);
+
+ await waitFor(() => {
+ expect(mockGoogleLogin).toHaveBeenCalledWith(
+ "test-code",
+ expect.stringContaining("/auth/callback"),
+ );
+ });
+
+ await waitFor(() => {
+ expect(hrefSetter).toHaveBeenCalledWith(
+ "http://127.0.0.1:46233/callback?token=cli-jwt-token&state=abc123",
+ );
+ });
+ } finally {
+ Object.defineProperty(window, "location", {
+ configurable: true,
+ value: originalLocation,
+ });
+ }
+ });
+
+ it("falls through to normal web flow when state contains invalid cli_callback", async () => {
+ mockSearchParams.set("state", "cli_callback:https://evil.com/callback");
+ mockLoginWithGoogle.mockResolvedValue(makeUser());
+ mockListWorkspaces.mockResolvedValue([]);
+ mockListMyInvitations.mockResolvedValue([]);
+
+ render(<CallbackPage />);
+
+ await waitFor(() => {
+ // Normal web flow: loginWithGoogle is called (not googleLogin)
+ expect(mockLoginWithGoogle).toHaveBeenCalled();
+ });
+ await waitFor(() => {
+ expect(mockPush).toHaveBeenCalledWith(paths.onboarding());
+ });
+ });
+
+ it("redirects to CLI callback even when state also contains platform:desktop", async () => {
+ // cli_callback takes precedence over platform:desktop — the CLI flow
+ // is a specific user intent that should not be derailed by desktop flag.
+ const { api: mockedApi } = await import("@multica/core/api");
+ const mockGoogleLogin = mockedApi.googleLogin as ReturnType<typeof vi.fn>;
+
+ const hrefSetter = vi.fn();
+ const originalLocation = window.location;
+ Object.defineProperty(window, "location", {
+ configurable: true,
+ writable: true,
+ value: { ...originalLocation, set href(value: string) { hrefSetter(value); } },
+ });
+
+ try {
+ mockSearchParams.set(
+ "state",
+ "platform:desktop,cli_callback:http://localhost:12345/callback,cli_state:mystate",
+ );
+ mockGoogleLogin.mockResolvedValue({ token: "mixed-jwt" });
+
+ render(<CallbackPage />);
+
+ await waitFor(() => {
+ expect(mockGoogleLogin).toHaveBeenCalled();
+ });
+
+ await waitFor(() => {
+ expect(hrefSetter).toHaveBeenCalledWith(
+ "http://localhost:12345/callback?token=mixed-jwt&state=mystate",
+ );
+ });
+ } finally {
+ Object.defineProperty(window, "location", {
+ configurable: true,
+ value: originalLocation,
+ });
+ }
+ });
+
it("onboarded users with missing source land in the workspace; the source-backfill modal is mounted there", async () => {
// Source attribution backfill is now an in-workspace modal — see
// `<SourceBackfillModal />` mounted inside `DashboardLayout`. The
diff --git a/apps/web/app/auth/callback/page.tsx b/apps/web/app/auth/callback/page.tsx
index 2dca05f143..4d3c2d3c1d 100644
--- a/apps/web/app/auth/callback/page.tsx
+++ b/apps/web/app/auth/callback/page.tsx
@@ -7,6 +7,7 @@ import { sanitizeNextUrl, useAuthStore } from "@multica/core/auth";
import { workspaceKeys } from "@multica/core/workspace/queries";
import { paths, resolvePostAuthDestination } from "@multica/core/paths";
import { api } from "@multica/core/api";
+import { validateCliCallback, redirectToCliCallback } from "@multica/views/auth";
import {
Card,
CardHeader,
@@ -46,9 +47,39 @@ function CallbackContent() {
// so an attacker-controlled `state=next:https://evil` cannot redirect here.
const nextUrl = sanitizeNextUrl(nextPart ? nextPart.slice(5) : null);
+ // CLI callback params — carried across the Google OAuth round-trip so
+ // headless/WSL2 `multica login` can receive the JWT after browser-based
+ // Google auth completes.
+ const cliCallbackPart = stateParts.find((p) => p.startsWith("cli_callback:"));
+ const cliStatePart = stateParts.find((p) => p.startsWith("cli_state:"));
+ const cliCallbackRaw = cliCallbackPart
+ ? decodeURIComponent(cliCallbackPart.slice("cli_callback:".length))
+ : null;
+ const cliState = cliStatePart
+ ? decodeURIComponent(cliStatePart.slice("cli_state:".length))
+ : "";
+
const redirectUri = `${window.location.origin}/auth/callback`;
- if (isDesktop) {
+ // Validate the CLI callback URL before redirecting — the state parameter
+ // passes through Google OAuth and must be treated as attacker-controlled.
+ const cliCallback =
+ cliCallbackRaw && validateCliCallback(cliCallbackRaw)
+ ? cliCallbackRaw
+ : null;
+
+ if (cliCallback) {
+ // CLI login flow: exchange the Google code for a JWT, then redirect the
+ // token back to the CLI's local HTTP listener (e.g. WSL2 host).
+ api
+ .googleLogin(code, redirectUri)
+ .then(({ token }) => {
+ redirectToCliCallback(cliCallback, token, cliState);
+ })
+ .catch((err) => {
+ setError(err instanceof Error ? err.message : "Login failed");
+ });
+ } else if (isDesktop) {
// Desktop flow: exchange code for token, then redirect via deep link
api
.googleLogin(code, redirectUri)
diff --git a/packages/views/auth/index.ts b/packages/views/auth/index.ts
index b449949a95..caf941e103 100644
--- a/packages/views/auth/index.ts
+++ b/packages/views/auth/index.ts
@@ -1,2 +1,2 @@
-export { LoginPage, validateCliCallback } from "./login-page";
+export { LoginPage, validateCliCallback, redirectToCliCallback } from "./login-page";
export { useLogout } from "./use-logout";
diff --git a/packages/views/auth/login-page.tsx b/packages/views/auth/login-page.tsx
index e3e99dea10..23f1bf486a 100644
--- a/packages/views/auth/login-page.tsx
+++ b/packages/views/auth/login-page.tsx
@@ -67,7 +67,7 @@ interface LoginPageProps {
// Helpers
// ---------------------------------------------------------------------------
-function redirectToCliCallback(url: string, token: string, state: string) {
+export function redirectToCliCallback(url: string, token: string, state: string) {
const separator = url.includes("?") ? "&" : "?";
window.location.href = `${url}${separator}token=${encodeURIComponent(token)}&state=${encodeURIComponent(state)}`;
}