#18

Fix Gmail OAuth redirect origin

MergedstylesshOwnermerged intofrom

Summary

  • build the Gmail completion redirect from the configured public BETTER_AUTH_URL
  • avoid leaking Railway's internal container origin through request.url
  • cover connected, error, and unauthorized completion destinations
  • document how to diagnose an internal-host OAuth redirect

Root cause

Railway received the callback on redakt-staging.up.railway.app, but Next exposed the upstream container URL as request.url. The completion route used that internal URL as the base for its final redirect.

Validation

  • pnpm test (63 tests)
  • focused ESLint
  • pnpm exec tsc --noEmit
  • pnpm build
  • git diff --check

Activity

4
mikeyreviewed3mo ago
mikey3mo ago

Reviewed the full diff (app/api/mail/accounts/google/complete/route.ts, lib/google/oauth.ts, lib/google/oauth.test.mjs, docs/gmail.md) plus the dependencies it touches (lib/env.ts:getAppUrl, lib/mail/routes.ts:mailAccountsHref, lib/google/mail-accounts.ts:completeGoogleMailAccounts).

Verification

  • Traced all three outcomes (connected, error, unauthorized) through gmailCompletionRedirectUrl against the new unit tests in oauth.test.mjs — the produced URLs match exactly (/mail/settings/account?connected=1, ?error=google, /?google=unauthorized).
  • Confirmed the redirect base now comes solely from getAppUrl() (i.e. BETTER_AUTH_URL), so request.url — which exposed Railway's internal container origin — is no longer read anywhere in this handler. This is a real fix for the stated root cause; the base URL is server-configured, not derived from user input, so there's no open-redirect regression either.
  • GET() dropping the unused request: Request parameter is safe — nothing else in the removed code read query params or headers off it.
  • Doc addition accurately describes the new behavior and gives a concrete staging URL to check against.

Non-blocking note getAppUrl() silently falls back to http://localhost:3000 if BETTER_AUTH_URL is unset (lib/env.ts:38), so a missing/misconfigured env var in a new environment would reproduce a similar-looking wrong-origin redirect (pointing at localhost) with no error surfaced. Pre-existing behavior, not introduced by this PR, but worth a startup assertion at some point since this route now depends on it entirely for correctness.

No blocking correctness, security, or test-coverage issues found in this pass.

stylessh merged commit ee2af65 into main3mo ago

Branch stylessh/fix-gmail-oauth-redirect has been merged.

Checking branch