diff --git a/src/lib/helpers/oauth2-cimd.ts b/src/lib/helpers/oauth2-cimd.ts index bf0d9881c..7d631f678 100644 --- a/src/lib/helpers/oauth2-cimd.ts +++ b/src/lib/helpers/oauth2-cimd.ts @@ -1,20 +1,14 @@ import { sdk } from '$lib/stores/sdk'; import type { Models } from '@appwrite.io/console'; -/** - * CIMD (Client ID Metadata Document) support: a `client_id` may be an HTTPS - * URL pointing to a JSON document of RFC 7591 client metadata. The Appwrite - * API no longer resolves these — `apps.get()` accepts plain app IDs only — so - * the console fetches the document itself for consent-screen branding. The - * server still validates the client during authorization, so a failed fetch - * only degrades branding, never security. - */ +// CIMD (Client ID Metadata Document): a client_id may be an HTTPS URL pointing +// to a JSON document of RFC 7591 client metadata. The Appwrite API no longer +// resolves these, so the console fetches the document itself for branding. const FETCH_TIMEOUT = 10_000; - const DEVICE_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code'; +const HTTP_URL = /^https?:\/\//i; -/** The RFC 7591 metadata fields the console renders. */ type CimdDocument = { client_id?: unknown; client_name?: unknown; @@ -29,67 +23,17 @@ type CimdDocument = { grant_types?: unknown; }; -function isLoopback(hostname: string): boolean { - return hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '[::1]'; -} - -/** - * Plain app IDs are at most 36 chars of `[a-zA-Z0-9._-]` and never parse as - * absolute URLs, so anything URL-shaped is a CIMD client_id. HTTP is only - * accepted for loopback (local development). - */ +// Plain app IDs never parse as URLs; http is allowed for local development only. export function isCimdClientId(clientId: string): boolean { - let url: URL; try { - url = new URL(clientId); + const url = new URL(clientId); + return ( + url.protocol === 'https:' || + (url.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(url.hostname)) + ); } catch { return false; } - return url.protocol === 'https:' || (url.protocol === 'http:' && isLoopback(url.hostname)); -} - -/** Documents are untrusted input — only pass through values safe to render in href/src. */ -function httpUrl(value: unknown): string { - if (typeof value !== 'string') return ''; - try { - const url = new URL(value); - return url.protocol === 'https:' || url.protocol === 'http:' ? value : ''; - } catch { - return ''; - } -} - -function stringArray(value: unknown): string[] { - return Array.isArray(value) ? value.filter((item) => typeof item === 'string') : []; -} - -/** A minimal app so consent can still render when the document is unavailable. */ -function fallbackApp(clientId: string): Models.App { - return { - $id: clientId, - $createdAt: '', - $updatedAt: '', - name: new URL(clientId).hostname, - description: '', - clientUri: '', - logoUri: '', - privacyPolicyUrl: '', - termsUrl: '', - contacts: [], - tagline: '', - tags: [], - images: [], - supportUrl: '', - dataDeletionUrl: '', - redirectUris: [], - postLogoutRedirectUris: [], - enabled: true, - type: 'public', - deviceFlow: false, - teamId: '', - userId: '', - secrets: [] - }; } export function cimdDocumentToApp(clientId: string, document: unknown): Models.App { @@ -97,32 +41,55 @@ export function cimdDocumentToApp(clientId: string, document: unknown): Models.A throw new Error('CIMD document is not a JSON object'); } const doc = document as CimdDocument; - // The document's client_id MUST equal the URL it was fetched from — a - // mismatch means the document describes a different client. + // The document's client_id must equal the URL it was fetched from. if (doc.client_id !== clientId) { throw new Error('CIMD document client_id does not match its URL'); } const name = typeof doc.client_name === 'string' ? doc.client_name.trim() : ''; return { - ...fallbackApp(clientId), + $id: clientId, + $createdAt: '', + $updatedAt: '', name: name || new URL(clientId).hostname, - clientUri: httpUrl(doc.client_uri), - logoUri: httpUrl(doc.logo_uri), - privacyPolicyUrl: httpUrl(doc.policy_uri), - termsUrl: httpUrl(doc.tos_uri), - contacts: stringArray(doc.contacts), - redirectUris: stringArray(doc.redirect_uris), - postLogoutRedirectUris: stringArray(doc.post_logout_redirect_uris), + description: '', + // Untrusted values rendered in href/src must be http(s) URLs. + clientUri: + typeof doc.client_uri === 'string' && HTTP_URL.test(doc.client_uri) + ? doc.client_uri + : '', + logoUri: + typeof doc.logo_uri === 'string' && HTTP_URL.test(doc.logo_uri) ? doc.logo_uri : '', + privacyPolicyUrl: + typeof doc.policy_uri === 'string' && HTTP_URL.test(doc.policy_uri) + ? doc.policy_uri + : '', + termsUrl: typeof doc.tos_uri === 'string' && HTTP_URL.test(doc.tos_uri) ? doc.tos_uri : '', + contacts: Array.isArray(doc.contacts) + ? doc.contacts.filter((contact) => typeof contact === 'string') + : [], + tagline: '', + tags: [], + images: [], + supportUrl: '', + dataDeletionUrl: '', + redirectUris: Array.isArray(doc.redirect_uris) + ? doc.redirect_uris.filter((uri) => typeof uri === 'string') + : [], + postLogoutRedirectUris: Array.isArray(doc.post_logout_redirect_uris) + ? doc.post_logout_redirect_uris.filter((uri) => typeof uri === 'string') + : [], + enabled: true, type: doc.token_endpoint_auth_method === 'none' ? 'public' : 'confidential', - deviceFlow: stringArray(doc.grant_types).includes(DEVICE_GRANT_TYPE) + deviceFlow: Array.isArray(doc.grant_types) && doc.grant_types.includes(DEVICE_GRANT_TYPE), + teamId: '', + userId: '', + secrets: [] }; } -/** - * Resolve an app for display: plain IDs via the API, CIMD URLs by fetching - * the document directly. CIMD fetch/validation failures fall back to - * hostname-only branding rather than blocking the flow. - */ +// Plain IDs resolve via the API; CIMD URLs are fetched directly. Fetch or +// validation failures fall back to hostname-only branding rather than blocking +// the flow — the server still validates the client during authorization. export async function getOAuth2App(appId: string): Promise { if (!isCimdClientId(appId)) { return sdk.forConsole.apps.get({ appId }); @@ -136,6 +103,6 @@ export async function getOAuth2App(appId: string): Promise { if (!response.ok) throw new Error(`CIMD document request failed: ${response.status}`); return cimdDocumentToApp(appId, await response.json()); } catch { - return fallbackApp(appId); + return cimdDocumentToApp(appId, { client_id: appId }); } }