fix(auth): preserve intended URL through login redirects

Closed
BR
1mo ago
+149-42

Fixes #1649

Summary

  • Preserve the original path and query string when auth guards send users to login.
  • Keep search and Ask/chat URLs across OAuth, credentials, magic-link, and email-code flows.
  • Validate callback URLs as same-origin relative paths.

Verification

  • yarn workspace @sourcebot/web test src/lib/authRedirect.test.ts --run
  • yarn workspace @sourcebot/web exec tsc --noEmit
  • yarn workspace @sourcebot/web lint

No visual changes.


[!NOTE]
Medium Risk
Touches authentication redirect boundaries across login guards and callback URL handling; validation reduces open-redirect risk but mis-handled callbacks could still send users to the wrong page after sign-in.

Overview
Fixes login redirects dropping query strings so users return to the same search or Ask/chat URL after signing in.

Introduces normalizeCallbackUrl and createLoginUrl in authRedirect.ts (with unit tests) to keep only safe same-origin relative paths and reject open-redirect targets. The edge proxy now stamps x-sourcebot-request-path with the current path and search; the app layout, auth middleware, GCP IAP, and account-linking flows use that value when sending users to login or post-auth callbacks.

Login, signup, magic-link verification, OAuth/credentials flows, and in-app sign-in links (chat share, banners, MCP connectors, login dialog) now pass the full pathname?query through callbackUrl instead of path-only or /.

Reviewed by Cursor Bugbot for commit b6b0b934a2c80fa44eadd371d5dd6841379af900. Bugbot is set up for automated code reviews on this repo. Configure here.


Summary by cubic

Fixes #1649. Auth guards now preserve the original path and query string when redirecting to login, so users land back on the page they intended after authenticating instead of the home page. Callback URLs are now validated as same-origin relative paths before use to prevent open redirects.

Bug Fixes

  • Adds normalizeCallbackUrl and createLoginUrl helpers with unit tests in packages/web/src/lib/authRedirect.ts.
  • Captures the intended URL server-side via the x-sourcebot-request-path header set in proxy.ts and read by the app layout and authenticated page middleware.
  • Applies the same normalizing logic to the login, signup, magic-link verification, and in-page sign-in prompt and dialog flows.
  • Already-authenticated users visiting /login or /signup are now redirected to the requested URL instead of /.

Summary by CodeRabbit

  • Bug Fixes

    • Preserved the current page and search parameters when signing in, using magic links, verifying accounts, or creating an account.
    • Returned users to their original destination after authentication instead of always sending them to the home page.
    • Improved login redirects from chat, sharing, connector, and account-linking flows.
    • Prevented unsafe or external callback URLs from being used.
  • Tests

    • Added coverage for preserving valid callback URLs and rejecting unsafe redirect destinations.
  • Documentation

    • Documented the updated login redirect behavior in the unreleased changelog.