Repository navigation
Commit 79a355b
fix(auth): SSO sign-in keeps the safe pre-login redirect (#36778)
<!-- claude-projects-attribution: {"github_login":"os-warren"} -->
_Requested by **Warren** · [project
thread](https://claude.ai/code/project/chan_01U5RdnAAmkSQmZbqh6FtqZ8?thread=cmsg_01U5RdnAAmkSQmZbqh6FtqZ8VwKpuGTiLu4nqkGWLoyEMK)_
Fixes #36754
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 未登录的人打开一条记录链接,用工作邮箱走 SSO 登录后,现在回到那条记录;以前落到上次用的应用。
- 补齐登录体验:密码登录和第三方账号按钮早就会带人回原链接,只有邮箱 SSO 漏了。
- 只认本站内的链接,外站地址一律丢弃、照常落到默认应用;出错最多是落回默认应用,可直接撤回。
- 无。
## What changes
`LoginForm`'s "Sign in with SSO" (email-domain SSO, `POST
/api/v1/auth/sign-in/sso`) built its `callbackURL` from the console base
plus a fixed `/`, so the `?redirect=` the sign-in page carries was lost.
It now:
- with a **safe** `?redirect=` on the sign-in page, sends `callbackURL`
= the sign-in page's own path with only that redirect
(`/login?redirect=ENCODED`, or `/_console/login?redirect=ENCODED` under
a mount). The identity provider returns the browser to the sign-in page,
which finishes sign-in its own way (org auto-select, the mobile
hand-off, the OAuth-provider replay) and then lands on the link through
its own `isSafeRedirect`. This is the same return the provider buttons
(`SocialSignInButtons`, default callback = the current page) and direct
sign-in (`directSignInStartUrl`) already use;
- with **no** redirect or an **unsafe** one, keeps `callbackURL` = base
+ `/` exactly as before (#36732).
"Safe" is the console `LoginPage`'s own rule, byte for byte: starts with
`/` and not `//`. It is not loosened, and the console page applies it
again on the final hop. The predicate stays module-private (no new
export). Only `redirect` travels, never the rest of the query (a
previous attempt's `error` would otherwise come back with a successful
sign-in).
Why back to the sign-in page rather than straight to `base + redirect`:
the mobile hand-off rules (`handoffRedirect.ts`, rule 2) say the
hand-off route is never handed to a provider as its `callbackURL`; going
through the sign-in page keeps that, and keeps the org auto-select step
a direct landing would skip.
## Tests
New `ui/packages/auth/src/__tests__/ssoCallbackRedirect-36754.test.tsx`
(8 tests) drives the real form: sets the page URL, presses "Sign in with
SSO", reads the request body.
- safe redirect at `/login` returns to `/login?redirect=ENCODED`, and
the redirect decodes back to the record path;
- under `/_console/login` with `error` / `error_description` beside it:
returns to `/_console/login?redirect=ENCODED`, nothing else carried;
- dropped, callback `/`: absolute URL, protocol-relative `//host`, a
non-root-relative path, an empty value;
- unsafe redirect under the mount: callback `/_console/`;
- CONTROL: no redirect gives `/` and `/_console/`, as before.
<details><summary>Readings (head dcd0d78)</summary>
- `pnpm exec vitest run
packages/auth/src/__tests__/ssoCallbackRedirect-36754.test.tsx
packages/auth/src/__tests__/LoginForm.test.tsx` (from `ui/`): Test Files
2 passed (2), Tests 46 passed (46).
- `pnpm --filter @object-ui/auth type-check` then `pnpm exec vitest run
packages/auth/`: exit 0, Test Files 49 passed (49), Tests 502 passed
(502).
- Ablation (fix committed first, `LoginForm.tsx` taken from
`origin/main`, `ssoCallbackURL` count 0 on disk): the two carry tests
turn red (`2 failed | 6 passed (8)`); the drop and control tests stay
green, as expected, since main always drops. Restored with `git checkout
HEAD --`, blob hash equal to HEAD's (`eaba863d`).
- Local gates: `check-changeset-no-major`, `check-empty-changeset`,
`check-adr-0087-registration` (all `--base origin/main`),
`check:nul-bytes`, `check-sdui-manifest`: exit 0. `overlap-check`:
VERDICT clean. Every other derived family is left to this PR's CI.
</details>
## Acceptance notes
- **Browser check: NOT MEASURED, no SSO provider locally.** Email-domain
SSO needs a registered identity provider (`@better-auth/sso`), which a
local dev stack does not have, so the browser leg is pinned by the test
above, as the card allows.
- better-auth accepts the new callback: its relative-path check
(`isSafeRelativeURL`, better-auth 1.7.12) refuses protocol-relative
values and encoded separators in the path; the redirect rides the query,
percent-encoded by `encodeURIComponent`.
- The package's other sign-in page, app-shell's exported
`DefaultLoginPage`, has no in-repo user and does not honour `?redirect=`
for any sign-in (its password success navigates to `/`). Through it, SSO
now returns to that page like its provider buttons already do. Noted,
not changed.
## Verification
Read first: `ui/packages/auth/src/LoginForm.tsx` `ssoCallbackURL` (the
only behaviour change), then the new test file.
Two-minute check: from `ui/`, `pnpm exec vitest run
packages/auth/src/__tests__/ssoCallbackRedirect-36754.test.tsx`: expect
8 passed.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01JQwxbWcpizQKnJmTb6cTpa)_
Co-authored-by: Claude <noreply@anthropic.com>1 parent 38a0081 commit 79a355b
3 files changed
Lines changed: 145 additions & 5 deletions
File tree
- .changeset
- ui/packages/auth/src
- __tests__
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
198 | 225 | | |
199 | 226 | | |
200 | 227 | | |
| |||
554 | 581 | | |
555 | 582 | | |
556 | 583 | | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | 584 | | |
562 | 585 | | |
563 | 586 | | |
564 | | - | |
| 587 | + | |
565 | 588 | | |
566 | 589 | | |
567 | 590 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
0 commit comments