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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions apps/web/app/connect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 9 additions & 4 deletions apps/web/app/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -62,21 +63,25 @@ 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,
});

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,
});
Expand Down
28 changes: 28 additions & 0 deletions apps/web/lib/session.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
24 changes: 23 additions & 1 deletion apps/web/lib/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}
Loading