Repository navigation
fix(studio): guest-only /login and kept deep links (#388) - #389
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-passwordand/reset-passwordgetbeforeLoad: guestOnly(apps/studio/src/lib/route-guards.ts):cachedSession, asrequireSessiondoes, so a cached session costs no request.redirect, or else to/sites(replace: true).client_id+sigin the query) stays on the page and does not even check the session./sites./inviteis unchanged.requireSessionnow redirects to/login?redirect=<path+search+hash>, taken fromlocation.href. The/index route still redirects to plain/login.requireSessionandisPlaygroundmoved out ofrouter.tsxintoroute-guards.ts. Their behaviour is the same, apart from the newredirectparam.apps/studio/src/lib/login-redirect.tsholdssafeRedirect, a pure check:/, but not with//or/\.\, so/\t/evilwould become//evil.new URL(value, base).originmust equal the base.loginRoute.validateSearchreturns{ redirect: safeRedirect(search.redirect) }. The key is always set, so a rejected raw value never survives the router's search merge.LoginPagereadsredirectwithuseSearch({ strict: false }), checks it again withsafeRedirect, and after sign-in or sign-up goes there withnavigate({ href }); otherwise it goes to/sites. The OAuthnextURL still wins.lib/auth.tsalready callsforgetSessionon every auth call except get-session, and every sign-out path (UserMenu,AccountPage) callsauthClient.signOut()before going to/login. A new test covers this.Step 2: field hint
autoComplete="username". Sign-up keepsemailandnew-password.No UI strings changed. Changeset:
@cloudhal/clipatch, since Studio ships in the CLI, as with earlier Studio changes.Worth a look
/reset-passwordalso 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/siteswhen the member is signed in.Tests
test/login-redirect.test.ts(the validation)://evil.example,/\evil.example,https://evil.example,javascript:…,data:, backslashes, tab/newline/NUL/DEL, and non-strings;test/login-guest.test.tsx(router, LoginPage and sign-out). It installs a forwardingfetchwithvi.hoisted, because Better Auth capturesfetchwhen its client is created./logingoes to/sites;?redirect=/sites/agoes to/sites/a;?redirect=//evil.examplegoes to/sites;?client_id=x&sig=ystays, with no session request;/forgot-passwordand/reset-passwordgo on;/invitehas no guard;/sites;/login, the form shows (cache cleared).requireSessionthrowsto: "/login", search: { redirect: "/sites/a/collections/posts/e1?tab=x#top" };router.load()from/sites/a/pageslands on/loginwithredirect=/sites/a/pages;validateSearchkeeps or drops values as it should./sites/a, or to/siteswhen the redirect is unsafe;autocompletevalues are correct in both modes.Local checks, run one at a time:
biome checkandpnpm design:linton the touched files: clean.tsc --noEmitfor apps/studio: clean.pnpm size:check: ok.apps/studiovitest: 88 files, 1446 tests passed.🤖 Generated with Claude Code