From 9dde99783019ee763318df2c1a6b04077aa2d6b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matej=20Ba=C4=8Do?= Date: Tue, 11 Aug 2026 16:32:34 +0200 Subject: [PATCH] Fix compatibility with external CIMD URLs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Appwrite's apps.get()/apps.list() no longer resolve CIMD URLs — appId is always a plain ID now. Detect URL-shaped OAuth2 client IDs in the console and fetch the Client ID Metadata Document directly from the browser, mapping its RFC 7591 fields onto Models.App for consent/device/applications rendering. Fetch or validation failures fall back to hostname-only branding so the flow never blocks. Co-Authored-By: Claude Fable 5 --- src/lib/helpers/oauth2-cimd.test.ts | 76 ++++++++++ src/lib/helpers/oauth2-cimd.ts | 141 ++++++++++++++++++ .../(console)/account/applications/+page.ts | 4 +- .../(public)/oauth2/consent/+page.svelte | 7 +- .../(public)/oauth2/device/+page.svelte | 5 +- 5 files changed, 224 insertions(+), 9 deletions(-) create mode 100644 src/lib/helpers/oauth2-cimd.test.ts create mode 100644 src/lib/helpers/oauth2-cimd.ts diff --git a/src/lib/helpers/oauth2-cimd.test.ts b/src/lib/helpers/oauth2-cimd.test.ts new file mode 100644 index 000000000..fce9d1b2a --- /dev/null +++ b/src/lib/helpers/oauth2-cimd.test.ts @@ -0,0 +1,76 @@ +import { cimdDocumentToApp, isCimdClientId } from '$lib/helpers/oauth2-cimd'; +import { describe, expect, it } from 'vitest'; + +describe('isCimdClientId', () => { + it('accepts https URLs', () => { + expect(isCimdClientId('https://example.com/oauth/client-metadata.json')).toBe(true); + }); + + it('accepts http only for loopback', () => { + expect(isCimdClientId('http://localhost:3000/client.json')).toBe(true); + expect(isCimdClientId('http://127.0.0.1/client.json')).toBe(true); + expect(isCimdClientId('http://example.com/client.json')).toBe(false); + }); + + it('rejects plain app IDs and non-http schemes', () => { + expect(isCimdClientId('my-app_1.0')).toBe(false); + expect(isCimdClientId('64f1e2a9b3c4d5e6f7a8')).toBe(false); + expect(isCimdClientId('javascript:alert(1)')).toBe(false); + }); +}); + +describe('cimdDocumentToApp', () => { + const clientId = 'https://example.com/oauth/client-metadata.json'; + + it('maps RFC 7591 metadata onto the App model', () => { + const app = cimdDocumentToApp(clientId, { + client_id: clientId, + client_name: 'Example App', + client_uri: 'https://example.com', + logo_uri: 'https://example.com/logo.png', + policy_uri: 'https://example.com/privacy', + tos_uri: 'https://example.com/terms', + contacts: ['support@example.com'], + redirect_uris: ['https://example.com/callback'], + token_endpoint_auth_method: 'none', + grant_types: ['authorization_code', 'urn:ietf:params:oauth:grant-type:device_code'] + }); + + expect(app.$id).toBe(clientId); + expect(app.name).toBe('Example App'); + expect(app.clientUri).toBe('https://example.com'); + expect(app.logoUri).toBe('https://example.com/logo.png'); + expect(app.privacyPolicyUrl).toBe('https://example.com/privacy'); + expect(app.termsUrl).toBe('https://example.com/terms'); + expect(app.contacts).toEqual(['support@example.com']); + expect(app.redirectUris).toEqual(['https://example.com/callback']); + expect(app.type).toBe('public'); + expect(app.deviceFlow).toBe(true); + expect(app.enabled).toBe(true); + }); + + it('falls back to the hostname when client_name is missing', () => { + const app = cimdDocumentToApp(clientId, { client_id: clientId }); + expect(app.name).toBe('example.com'); + expect(app.deviceFlow).toBe(false); + }); + + it('rejects a document whose client_id does not match its URL', () => { + expect(() => + cimdDocumentToApp(clientId, { client_id: 'https://evil.example/other.json' }) + ).toThrow(); + expect(() => cimdDocumentToApp(clientId, 'not an object')).toThrow(); + }); + + it('drops unrenderable URI values', () => { + const app = cimdDocumentToApp(clientId, { + client_id: clientId, + logo_uri: 'javascript:alert(1)', + client_uri: 'not a url', + contacts: ['ok', 42] + }); + expect(app.logoUri).toBe(''); + expect(app.clientUri).toBe(''); + expect(app.contacts).toEqual(['ok']); + }); +}); diff --git a/src/lib/helpers/oauth2-cimd.ts b/src/lib/helpers/oauth2-cimd.ts new file mode 100644 index 000000000..bf0d9881c --- /dev/null +++ b/src/lib/helpers/oauth2-cimd.ts @@ -0,0 +1,141 @@ +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. + */ + +const FETCH_TIMEOUT = 10_000; + +const DEVICE_GRANT_TYPE = 'urn:ietf:params:oauth:grant-type:device_code'; + +/** The RFC 7591 metadata fields the console renders. */ +type CimdDocument = { + client_id?: unknown; + client_name?: unknown; + client_uri?: unknown; + logo_uri?: unknown; + policy_uri?: unknown; + tos_uri?: unknown; + contacts?: unknown; + redirect_uris?: unknown; + post_logout_redirect_uris?: unknown; + token_endpoint_auth_method?: unknown; + 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). + */ +export function isCimdClientId(clientId: string): boolean { + let url: URL; + try { + url = new URL(clientId); + } 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 { + if (typeof document !== 'object' || document === null) { + 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. + 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), + 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), + type: doc.token_endpoint_auth_method === 'none' ? 'public' : 'confidential', + deviceFlow: stringArray(doc.grant_types).includes(DEVICE_GRANT_TYPE) + }; +} + +/** + * 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. + */ +export async function getOAuth2App(appId: string): Promise { + if (!isCimdClientId(appId)) { + return sdk.forConsole.apps.get({ appId }); + } + try { + const response = await fetch(appId, { + headers: { accept: 'application/json' }, + credentials: 'omit', + signal: AbortSignal.timeout(FETCH_TIMEOUT) + }); + if (!response.ok) throw new Error(`CIMD document request failed: ${response.status}`); + return cimdDocumentToApp(appId, await response.json()); + } catch { + return fallbackApp(appId); + } +} diff --git a/src/routes/(console)/account/applications/+page.ts b/src/routes/(console)/account/applications/+page.ts index 14645b446..c453ee958 100644 --- a/src/routes/(console)/account/applications/+page.ts +++ b/src/routes/(console)/account/applications/+page.ts @@ -1,5 +1,5 @@ import { Dependencies } from '$lib/constants'; -import { sdk } from '$lib/stores/sdk'; +import { getOAuth2App } from '$lib/helpers/oauth2-cimd'; import type { Models } from '@appwrite.io/console'; import type { PageLoad } from './$types'; @@ -19,7 +19,7 @@ export const load: PageLoad = async ({ depends, parent }) => { const connectedApps = await Promise.all( grants.map(async (identity) => { const appId = identity.provider.slice(OAUTH2_PREFIX.length); - const app = await sdk.forConsole.apps.get({ appId }).catch(() => null); + const app = await getOAuth2App(appId).catch(() => null); return { identity, appId, app }; }) ); diff --git a/src/routes/(public)/oauth2/consent/+page.svelte b/src/routes/(public)/oauth2/consent/+page.svelte index 81553b172..0599bd277 100644 --- a/src/routes/(public)/oauth2/consent/+page.svelte +++ b/src/routes/(public)/oauth2/consent/+page.svelte @@ -9,6 +9,7 @@ import { sdk } from '$lib/stores/sdk'; import { logout } from '$lib/helpers/logout'; import { isWebRedirect } from '$lib/helpers/oauth2-redirect'; + import { getOAuth2App } from '$lib/helpers/oauth2-cimd'; import OAuth2ConsentCard, { type OAuth2Outcome } from '../consent-card.svelte'; import OAuth2OutcomeCard from '../outcome-card.svelte'; import { OAuth2ErrorMessage, OAuth2ErrorType } from '../errors'; @@ -85,7 +86,7 @@ ): Promise { const loadedGrant = await sdk.forConsole.oauth2.getGrant({ grantId }); const [loadedApp, loadedAccount] = await Promise.all([ - sdk.forConsole.apps.get({ appId: loadedGrant.appId }), + getOAuth2App(loadedGrant.appId), knownAccount !== undefined ? Promise.resolve(knownAccount) : getAccount() ]); if (cancelled()) return; @@ -122,9 +123,7 @@ if (!isWebRedirect(result.redirectUrl)) { completedRedirectUrl = result.redirectUrl; account = loggedInAccount; - app = clientId - ? await sdk.forConsole.apps.get({ appId: clientId }).catch(() => null) - : null; + app = clientId ? await getOAuth2App(clientId).catch(() => null) : null; if (cancelled()) return; phase = 'approved'; } diff --git a/src/routes/(public)/oauth2/device/+page.svelte b/src/routes/(public)/oauth2/device/+page.svelte index 15f37b0d4..15e0c43c0 100644 --- a/src/routes/(public)/oauth2/device/+page.svelte +++ b/src/routes/(public)/oauth2/device/+page.svelte @@ -10,6 +10,7 @@ import { addNotification } from '$lib/stores/notifications'; import { sdk } from '$lib/stores/sdk'; import { Submit, trackError, trackEvent } from '$lib/actions/analytics'; + import { getOAuth2App } from '$lib/helpers/oauth2-cimd'; import OAuth2ConsentCard, { type OAuth2Flow, type OAuth2Outcome } from '../consent-card.svelte'; import OAuth2OutcomeCard from '../outcome-card.svelte'; @@ -95,9 +96,7 @@ const loadedGrant = await sdk.forConsole.oauth2.createGrant({ userCode: normalized }); - const loadedApp = await sdk.forConsole.apps.get({ - appId: loadedGrant.appId - }); + const loadedApp = await getOAuth2App(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;