diff --git a/.changeset/fix-sso-redirect-url-on-older-webviews.md b/.changeset/fix-sso-redirect-url-on-older-webviews.md new file mode 100644 index 000000000..6f0f0f026 --- /dev/null +++ b/.changeset/fix-sso-redirect-url-on-older-webviews.md @@ -0,0 +1,5 @@ +--- +default: patch +--- + +Fix the login page crashing with "Invalid URL" on Android WebView below 130 diff --git a/src/app/pages/auth/SSOTauri.test.ts b/src/app/pages/auth/SSOTauri.test.ts index 577cfa841..7398f5fda 100644 --- a/src/app/pages/auth/SSOTauri.test.ts +++ b/src/app/pages/auth/SSOTauri.test.ts @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'; import { type as osType } from '@tauri-apps/plugin-os'; import { buildTauriSsoRedirectUrl, + isTauriSsoCallbackTarget, parseTauriOidcCallback, parseTauriSsoCallback, rememberTauriSsoNonce, @@ -48,6 +49,10 @@ describe('buildTauriSsoRedirectUrl', () => { expect(url.searchParams.get('server')).toBeNull(); expect(url.searchParams.get('sso_nonce')).toBeTruthy(); }); + + it('builds the url without resolving against a non-special base', () => { + expect(buildTauriSsoRedirectUrl()).toMatch(/^sable:\/\/login\/lp\/sso-callback\?/); + }); }); describe('takeTauriSsoNonce', () => { @@ -78,6 +83,21 @@ describe('parseTauriSsoCallback', () => { }); }); +describe('isTauriSsoCallbackTarget', () => { + it('accepts the authority shape', () => { + expect(isTauriSsoCallbackTarget('login', '/lp/sso-callback')).toBe(true); + }); + + it('accepts the opaque-path shape Chromium below 130 produces', () => { + expect(isTauriSsoCallbackTarget('', '//login/lp/sso-callback')).toBe(true); + }); + + it('rejects another host and another opaque path', () => { + expect(isTauriSsoCallbackTarget('evil', '/lp/sso-callback')).toBe(false); + expect(isTauriSsoCallbackTarget('', '//evil/lp/sso-callback')).toBe(false); + }); +}); + describe('parseTauriOidcCallback', () => { it('parses code and state from single-slash format', () => { expect(parseTauriOidcCallback('moe.sable.app:/login?code=c1&state=s1')).toEqual({ diff --git a/src/app/pages/auth/SSOTauri.ts b/src/app/pages/auth/SSOTauri.ts index 5f6ea3284..cf61d6462 100644 --- a/src/app/pages/auth/SSOTauri.ts +++ b/src/app/pages/auth/SSOTauri.ts @@ -39,18 +39,19 @@ export const takeTauriSsoNonce = (): string | undefined => { } }; +// Built as a string: resolving a relative path against a non-special base throws on Chromium < 130. export const buildTauriSsoRedirectUrl = (server?: string): string => { - const redirectUrl = new URL(SSO_CALLBACK_PATH, getAppBaseUrl()); + const params = new URLSearchParams(); if (server) { - redirectUrl.searchParams.set('server', server); + params.set('server', server); } const nonce = crypto.randomUUID(); rememberTauriSsoNonce(nonce); - redirectUrl.searchParams.set('sso_nonce', nonce); + params.set('sso_nonce', nonce); - return redirectUrl.toString(); + return `${getAppBaseUrl()}${SSO_CALLBACK_PATH}?${params}`; }; export const TAURI_OIDC_CLIENT_URI = 'https://app.sable.moe'; @@ -103,11 +104,15 @@ export const parseTauriOidcCallback = ( } }; +// Chromium < 130 gives a non-special scheme an opaque path, leaving the authority in the pathname. +export const isTauriSsoCallbackTarget = (hostname: string, pathname: string): boolean => + hostname === TAURI_SSO_HOST || pathname === `//${TAURI_SSO_HOST}${SSO_CALLBACK_PATH}`; + export const parseTauriSsoCallback = (rawUrl: string): TauriSsoCallback | undefined => { try { const callbackUrl = new URL(rawUrl); if (callbackUrl.protocol !== TAURI_SSO_PROTOCOL) return undefined; - if (callbackUrl.hostname !== TAURI_SSO_HOST) return undefined; + if (!isTauriSsoCallbackTarget(callbackUrl.hostname, callbackUrl.pathname)) return undefined; const loginToken = callbackUrl.searchParams.get('loginToken'); if (!loginToken) return undefined;