mirror of
https://github.com/appwrite/console.git
synced 2026-11-18 20:47:21 +00:00
fix: harden OAuth2 consent/device flows against stale requests
Addresses code review findings on the OAuth2 screens: - consent: re-run the load effect on ANY authorize param change (full query string), not just grant_id/client_id, so a same-client request with a new redirect_uri/state/scope can't approve the previous grant - consent: validate max_age as a non-negative integer, omitting it instead of forwarding NaN to the SDK - consent-card: pin approve/reject to the grant id captured at call time and drop the result (and errors) if the parent swaps in a different grant mid-flight - device: track only the URL user_code and reset loaded grant/app/phase when it changes or is removed, so stale device requests can't be confirmed - device: ignore createGrant results/errors when the active code changed while awaiting, and guard against duplicate concurrent submits - login: resume to the stored redirect exactly instead of appending the login page's leftover query params (e.g. message=) to the OAuth2 route
This commit is contained in:
4 files changed
+52
-19
No files matched your search
@@ -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) {
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -17,13 +17,23 @@
|
||||
let account = $state<Models.User<Models.Preferences> | null>(null);
|
||||
let error = $state<string | null>(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;
|
||||
|
||||
@@ -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<string | null>(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 {
|
||||
|
||||
Reference in new issue
Block a user