diff --git a/src/routes/(public)/(guest)/login/+page.svelte b/src/routes/(public)/(guest)/login/+page.svelte index ef0b9f465..aca660b82 100644 --- a/src/routes/(public)/(guest)/login/+page.svelte +++ b/src/routes/(public)/(guest)/login/+page.svelte @@ -71,8 +71,11 @@ if (redirect) { try { await sdk.forConsole.account.get(); - page.url.searchParams.delete('redirect'); - await goto(`${redirect}${page.url.search}`); + // Resume to the stored redirect exactly — it already carries + // its own query string. Appending the login page's remaining + // search params would leak login-only params (e.g. `message`) + // into the OAuth2 request route. + await goto(redirect); await invalidate(Dependencies.ACCOUNT); return; } catch (mfaError) { diff --git a/src/routes/(public)/oauth2/consent-card.svelte b/src/routes/(public)/oauth2/consent-card.svelte index d81549acc..d9eabec4f 100644 --- a/src/routes/(public)/oauth2/consent-card.svelte +++ b/src/routes/(public)/oauth2/consent-card.svelte @@ -61,12 +61,18 @@ async function approve() { if (busy) return; + // Pin the request we're acting on. If the parent swaps in a different + // grant while this call is in flight (the route moved to another + // consent request), drop the stale result so we never redirect with the + // previous grant's redirectUrl. + const grantId = grant.$id; error = null; approving = true; try { const result = await sdk.forConsole.oauth2.approve({ - grantId: grant.$id + grantId }); + if (grant.$id !== grantId) return; trackEvent(Submit.AccountOAuth2ConsentApprove, { app_id: grant.appId, flow @@ -77,6 +83,7 @@ } window.location.href = result.redirectUrl; } catch (e: unknown) { + if (grant.$id !== grantId) return; const message = (e as Error)?.message ?? 'Failed to authorize the application'; error = message; addNotification({ type: 'error', message }); @@ -88,12 +95,15 @@ async function reject() { if (busy) return; + // Pin the request we're acting on (see `approve`). + const grantId = grant.$id; error = null; rejecting = true; try { const result = await sdk.forConsole.oauth2.reject({ - grantId: grant.$id + grantId }); + if (grant.$id !== grantId) return; trackEvent(Submit.AccountOAuth2ConsentDeny, { app_id: grant.appId, flow @@ -104,6 +114,7 @@ } window.location.href = result.redirectUrl; } catch (e: unknown) { + if (grant.$id !== grantId) return; const message = (e as Error)?.message ?? 'Failed to cancel the request'; error = message; addNotification({ type: 'error', message }); diff --git a/src/routes/(public)/oauth2/consent/+page.svelte b/src/routes/(public)/oauth2/consent/+page.svelte index d06d86dbe..829362e2c 100644 --- a/src/routes/(public)/oauth2/consent/+page.svelte +++ b/src/routes/(public)/oauth2/consent/+page.svelte @@ -17,13 +17,23 @@ let account = $state | null>(null); let error = $state(null); + // OIDC `max_age` is a non-negative integer count of seconds. Reject anything + // else (e.g. `max_age=abc`) so we omit the param rather than forwarding NaN. + function parseMaxAge(raw: string | null): number | undefined { + if (!raw) return undefined; + const value = Number(raw); + return Number.isInteger(value) && value >= 0 ? value : undefined; + } + // Re-runs when the authorize params change (this route can stay mounted as // the router moves between requests). Reset to loading so a previously // loaded grant can never be approved against a different request. $effect(() => { - // Touch the search params so the effect re-runs on URL change. - page.url.searchParams.get('grant_id'); - page.url.searchParams.get('client_id'); + // Depend on the ENTIRE query string so the effect re-runs whenever any + // part of the authorize request changes — not just grant_id/client_id, + // but also redirect_uri, scope, state, nonce, PKCE fields, prompt, + // max_age and authorization_details. + page.url.searchParams.toString(); let cancelled = false; phase = 'loading'; @@ -106,7 +116,7 @@ codeChallenge: params.get('code_challenge') ?? undefined, codeChallengeMethod: params.get('code_challenge_method') ?? undefined, prompt: params.get('prompt') ?? undefined, - maxAge: params.get('max_age') ? Number(params.get('max_age')) : undefined, + maxAge: parseMaxAge(params.get('max_age')), authorizationDetails: params.get('authorization_details') ?? undefined }); if (cancelled) return; diff --git a/src/routes/(public)/oauth2/device/+page.svelte b/src/routes/(public)/oauth2/device/+page.svelte index 43ee89b25..cc06f4bad 100644 --- a/src/routes/(public)/oauth2/device/+page.svelte +++ b/src/routes/(public)/oauth2/device/+page.svelte @@ -68,24 +68,28 @@ }; }); - // Keep the shown code in sync when a new `user_code` arrives in the URL - // (e.g. the user follows a fresh `verification_uri_complete` link while this - // route stays mounted). A different code means a different request, so drop - // any loaded grant and return to confirmation rather than approving the old - // one. + // Sync state to the `user_code` in the URL. This tracks ONLY the URL param + // (not the locally-typed `code`), so typing in the field never re-triggers + // it. Whenever the URL code changes — including being removed — the previous + // request is no longer valid, so drop any loaded grant and return to + // confirmation rather than confirming/approving a stale one. + let lastUrlCode = $state(null); $effect(() => { - page.url.searchParams.get('user_code'); - const next = normalizeUserCode(page.url.searchParams.get('user_code') ?? ''); - if (!next || next === code) return; - code = next; + const urlCode = normalizeUserCode(page.url.searchParams.get('user_code') ?? ''); + if (urlCode === lastUrlCode) return; + lastUrlCode = urlCode; + code = urlCode; grant = null; app = null; error = null; - hasPrefilledCode = Boolean(next); - phase = phase === 'loading' ? phase : 'enter-code'; + hasPrefilledCode = Boolean(urlCode); + if (phase !== 'loading') phase = 'enter-code'; }); async function handleSubmit() { + // Guard against a double Enter / programmatic resubmit racing or + // invalidating the in-flight request. + if (submitting) return; const normalized = normalizeUserCode(code); if (!normalized) return; error = null; @@ -97,6 +101,9 @@ const loadedApp = await sdk.forConsole.apps.get({ appId: loadedGrant.appId }); + // A fresh `user_code` may have arrived while we awaited. Ignore this + // now-stale result so we never show consent for a superseded request. + if (normalizeUserCode(code) !== normalized) return; trackEvent(Submit.AccountOAuth2DeviceVerify, { app_id: loadedGrant.appId }); @@ -105,6 +112,8 @@ error = null; phase = 'consent'; } catch (e: unknown) { + // Drop errors from a submission the active code has already moved past. + if (normalizeUserCode(code) !== normalized) return; if (e instanceof AppwriteException && e.type === 'oauth2_invalid_user_code') { error = 'That code is invalid or has expired. Check your device and try again.'; } else {