mirror of
https://github.com/SableClient/Sable.git
synced 2026-11-26 03:46:25 -08:00
fix(auth): build the tauri sso redirect url without a relative base
This commit is contained in:
3 files changed
+35
-5
No files matched your search
@@ -0,0 +1,5 @@
|
||||
---
|
||||
default: patch
|
||||
---
|
||||
|
||||
Fix the login page crashing with "Invalid URL" on Android WebView below 130
|
||||
@@ -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({
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user