From be0e1185589500978311f8d6e31cef4314aaa042 Mon Sep 17 00:00:00 2001 From: Leonhard Breuer Date: Tue, 10 Nov 2026 21:58:06 +0100 Subject: [PATCH] fix(web): return to the requested page after sign-in A signed-out visit to a page such as /settings/providers, which the native apps now link to, redirects to /connect?next=, and signing in returns there instead of to the home page. An ended session does the same. returnPath follows only paths on this site, so next can't send the browser to another origin. --- CHANGELOG.md | 1 + apps/web/app/connect.tsx | 6 ++++-- apps/web/app/router.tsx | 13 +++++++++---- apps/web/lib/session.test.ts | 28 ++++++++++++++++++++++++++++ apps/web/lib/session.ts | 24 +++++++++++++++++++++++- 5 files changed, 65 insertions(+), 7 deletions(-) create mode 100644 apps/web/lib/session.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 654f1afe..3fcea122 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Signing in to the web app returns to the page that asked for it, such as a settings page the native apps open, instead of the home page. Only paths on the same site are followed. - The server serves its OpenAPI document at `GET /openapi.json` without authentication. - Contributors no longer need oasdiff. The contract compatibility check, its CI workflow, and the automatic `openapi/` regeneration in `pre-commit` are gone; run `bun run openapi:generate` yourself. - GitHub owner sign-out removes local authorization before attempting remote revocation. Older refreshes and sign-in completions can no longer restore signed-out state or overwrite a newer sign-in. diff --git a/apps/web/app/connect.tsx b/apps/web/app/connect.tsx index 5ae0ec21..d4b831d6 100644 --- a/apps/web/app/connect.tsx +++ b/apps/web/app/connect.tsx @@ -9,7 +9,7 @@ import { FieldLabel, } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; -import { resetSession } from "@/lib/session"; +import { resetSession, returnPath } from "@/lib/session"; import { authClient, INVITE_HEADER, @@ -44,7 +44,9 @@ const invite = new URLSearchParams(location.search).get("invite") ?? undefined; function enter() { resetSession(); - window.location.assign("/"); + window.location.assign( + returnPath(new URLSearchParams(location.search).get("next")), + ); } function usePasskeySignIn(mode: Mode) { diff --git a/apps/web/app/router.tsx b/apps/web/app/router.tsx index 80ffbca6..e5b5d8c7 100644 --- a/apps/web/app/router.tsx +++ b/apps/web/app/router.tsx @@ -15,6 +15,7 @@ import { EmptyHeader, EmptyTitle, } from "@/components/ui/empty"; +import { connectPath, returnPath } from "@/lib/session"; import { Connect } from "./connect"; import { Home } from "./home"; import { Shell } from "./shell"; @@ -62,10 +63,14 @@ const rootRoute = createRootRoute(); const connectRoute = createRoute({ getParentRoute: () => rootRoute, path: "/connect", - beforeLoad: async () => { + validateSearch: z.object({ + next: z.string().optional(), + invite: z.string().optional(), + }), + beforeLoad: async ({ search }) => { const session = await authClient.getSession(); - if (session.data) throw redirect({ to: "/" }); + if (session.data) throw redirect({ href: returnPath(search.next) }); }, component: Connect, }); @@ -73,10 +78,10 @@ const connectRoute = createRoute({ const appRoute = createRoute({ getParentRoute: () => rootRoute, id: "app", - beforeLoad: async () => { + beforeLoad: async ({ location }) => { const session = await authClient.getSession(); - if (!session.data) throw redirect({ to: "/connect" }); + if (!session.data) throw redirect({ href: connectPath(location.href) }); }, component: Shell, }); diff --git a/apps/web/lib/session.test.ts b/apps/web/lib/session.test.ts new file mode 100644 index 00000000..b555502a --- /dev/null +++ b/apps/web/lib/session.test.ts @@ -0,0 +1,28 @@ +import { expect, test } from "bun:test"; +import { connectPath, returnPath } from "./session"; + +test("sign-in returns only to a path on this site", () => { + expect(returnPath("/settings/providers")).toBe("/settings/providers"); + expect(returnPath("/bots/a1?tab=x#y")).toBe("/bots/a1?tab=x#y"); + + for (const next of [ + undefined, + null, + "", + "settings", + "https://evil.example", + "//evil.example", + "/\\evil.example", + "/connect", + "/connect?next=/x", + ]) + expect(returnPath(next)).toBe("/"); +}); + +test("the sign-in page carries where to return", () => { + expect(connectPath("/")).toBe("/connect"); + expect(connectPath("/settings/providers")).toBe( + "/connect?next=%2Fsettings%2Fproviders", + ); + expect(connectPath("//evil.example")).toBe("/connect"); +}); diff --git a/apps/web/lib/session.ts b/apps/web/lib/session.ts index 0af71d6a..d15debe7 100644 --- a/apps/web/lib/session.ts +++ b/apps/web/lib/session.ts @@ -8,9 +8,31 @@ export function resetSession() { for (const reset of resets) reset(); } +/** Where sign-in returns to: a path on this site, never another origin or the sign-in page. */ +export function returnPath(next: string | null | undefined): string { + if (!next?.startsWith("/")) return "/"; + + if (next.startsWith("//") || next.startsWith("/\\")) return "/"; + + if (next === "/connect" || next.startsWith("/connect?")) return "/"; + + return next; +} + +/** The sign-in page, returning to `path` afterwards. */ +export function connectPath(path: string): string { + const next = returnPath(path); + + return next === "/" + ? "/connect" + : `/connect?next=${encodeURIComponent(next)}`; +} + export function signedOut() { resetSession(); if (!location.pathname.endsWith("/connect")) - window.location.assign("/connect"); + window.location.assign( + connectPath(location.pathname + location.search + location.hash), + ); }