Skip to content

fix(studio): guest-only /login and kept deep links (#388) - #389

Merged
Madraka merged 1 commit into
mainfrom
claude/login-guest-388-f4c8e6
Dec 2, 2026
Merged

Madraka merged 1 commit into
mainfrom
claude/login-guest-388-f4c8e6

Conversation

@Madraka

@Madraka Madraka commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Refs #388 (steps 1–2). Steps 3–5 (remembered accounts, passkeys, multi-session) are not part of this PR.

What changes

Step 1: guest-only /login, and deep links kept across sign-in

  • /login, /forgot-password and /reset-password get beforeLoad: guestOnly (apps/studio/src/lib/route-guards.ts):
    • It uses cachedSession, as requireSession does, so a cached session costs no request.
    • With a session, it redirects to the validated redirect, or else to /sites (replace: true).
    • An RFC-0044 authorization request (client_id + sig in the query) stays on the page and does not even check the session.
    • A playground (RFC-0036 §5) goes to /sites.
    • /invite is unchanged.
  • requireSession now redirects to /login?redirect=<path+search+hash>, taken from location.href. The / index route still redirects to plain /login. requireSession and isPlayground moved out of router.tsx into route-guards.ts. Their behaviour is the same, apart from the new redirect param.
  • apps/studio/src/lib/login-redirect.ts holds safeRedirect, a pure check:
    • The value must be a string of at most 2048 characters (length checked first).
    • It must start with /, but not with // or /\.
    • It must contain no backslash and no C0/DEL/C1 control character anywhere. The URL parser drops tabs and rewrites \, so /\t/evil would become //evil.
    • Finally, new URL(value, base).origin must equal the base.
    • It is one linear pass with no regex (hard rule 13).
  • loginRoute.validateSearch returns { redirect: safeRedirect(search.redirect) }. The key is always set, so a rejected raw value never survives the router's search merge.
  • LoginPage reads redirect with useSearch({ strict: false }), checks it again with safeRedirect, and after sign-in or sign-up goes there with navigate({ href }); otherwise it goes to /sites. The OAuth next URL still wins.
  • Sign-out: unchanged. lib/auth.ts already calls forgetSession on every auth call except get-session, and every sign-out path (UserMenu, AccountPage) calls authClient.signOut() before going to /login. A new test covers this.

Step 2: field hint

  • In sign-in mode the email field uses autoComplete="username". Sign-up keeps email and new-password.

No UI strings changed. Changeset: @cloudhal/cli patch, since Studio ships in the CLI, as with earlier Studio changes.

Worth a look

  • As the issue asks, /reset-password also sends a signed-in member to /sites. A reset link opened in a browser that is already signed in therefore lands on /sites. That member can change the password under Account instead.
  • InvitePage's "already a member → sign in" button now ends on /sites when the member is signed in.

Tests

  • test/login-redirect.test.ts (the validation):
    • accepts same-origin paths;
    • rejects //evil.example, /\evil.example, https://evil.example, javascript:…, data:, backslashes, tab/newline/NUL/DEL, and non-strings;
    • accepts exactly 2048 characters and rejects 2049;
    • rejects three 100k-character inputs in under 50 ms.
  • test/login-guest.test.tsx (router, LoginPage and sign-out). It installs a forwarding fetch with vi.hoisted, because Better Auth captures fetch when its client is created.
    • With a session:
      • /login goes to /sites;
      • ?redirect=/sites/a goes to /sites/a;
      • ?redirect=//evil.example goes to /sites;
      • ?client_id=x&sig=y stays, with no session request;
      • /forgot-password and /reset-password go on;
      • /invite has no guard;
      • a cached session costs one request for two checks.
    • Without a session:
      • the form shows;
      • a playground goes to /sites;
      • after sign-in, sign-out, then /login, the form shows (cache cleared).
    • Deep links:
      • requireSession throws to: "/login", search: { redirect: "/sites/a/collections/posts/e1?tab=x#top" };
      • a full router.load() from /sites/a/pages lands on /login with redirect=/sites/a/pages;
      • validateSearch keeps or drops values as it should.
    • LoginPage:
      • after sign-in it goes to /sites/a, or to /sites when the redirect is unsafe;
      • the autocomplete values are correct in both modes.

Local checks, run one at a time:

  • biome check and pnpm design:lint on the touched files: clean.
  • tsc --noEmit for apps/studio: clean.
  • pnpm size:check: ok.
  • Full apps/studio vitest: 88 files, 1446 tests passed.

🤖 Generated with Claude Code

A signed-in member who opened /login saw the sign-in form again, and a
signed-out member's deep link was lost after sign-in.

- /login, /forgot-password and /reset-password check the session in
  beforeLoad (the cached one, so no extra request) and go on to the
  validated `redirect`, or to /sites. An RFC-0044 authorization request
  (?client_id=…&sig=…) stays; a playground goes to /sites. /invite is
  unchanged.
- requireSession redirects to /login?redirect=<path+search+hash>. The
  login route keeps `redirect` only when it is a same-origin path
  (lib/login-redirect.ts: one linear pass, at most 2 kB, no `//`, `/\`,
  scheme, backslash or control character). LoginPage goes there after
  sign-in and sign-up; the OAuth `next` URL still wins.
- The session guards move from router.tsx to lib/route-guards.ts.
- The email field says autocomplete="username" in sign-in mode, so
  password managers pair it with the password; sign-up keeps `email`
  and `new-password`.

Tests cover the redirects, the validation (including a 100k-character
value) and that sign-out clears the cached session.

Refs #388 (steps 1–2)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant