diff --git a/src/app/pages/pathUtils.ts b/src/app/pages/pathUtils.ts index d01bff279..7e202ecd5 100644 --- a/src/app/pages/pathUtils.ts +++ b/src/app/pages/pathUtils.ts @@ -1,6 +1,7 @@ import type { Path } from 'react-router-dom'; import { generatePath } from 'react-router-dom'; import { trimLeadingSlash, trimTrailingSlash } from '$utils/common'; +import { getAppOrigin } from '$utils/platform'; import type { HashRouterConfig } from '$hooks/useClientConfig'; import type { SettingsPathSearchParams } from './paths'; import { @@ -44,7 +45,7 @@ export const encodeSearchParamValueArray = (ids: string[]): string => ids.join(' export const decodeSearchParamValueArray = (idsParam: string): string[] => idsParam.split(','); export const getOriginBaseUrl = (hashRouterConfig?: HashRouterConfig): string => { - const baseUrl = `${trimTrailingSlash(window.location.origin)}${import.meta.env.BASE_URL}`; + const baseUrl = `${trimTrailingSlash(getAppOrigin())}${import.meta.env.BASE_URL}`; if (hashRouterConfig?.enabled) { return `${trimTrailingSlash(baseUrl)}/#${hashRouterConfig.basename}`; diff --git a/src/app/plugins/call/CallEmbed.ts b/src/app/plugins/call/CallEmbed.ts index 75f054972..4f6091ec3 100644 --- a/src/app/plugins/call/CallEmbed.ts +++ b/src/app/plugins/call/CallEmbed.ts @@ -10,6 +10,7 @@ import { } from 'matrix-widget-api'; import { CallWidgetDriver } from './CallWidgetDriver'; import { trimTrailingSlash } from '../../utils/common'; +import { getAppOrigin } from '../../utils/platform'; import type { ElementCallThemeKind, ElementMediaStateDetail } from './types'; import { color, config } from 'folds'; import { ElementCallIntent, ElementWidgetActions } from './types'; @@ -96,12 +97,7 @@ export class CallEmbed { ): Widget { const userId = mx.getSafeUserId(); const deviceId = mx.getDeviceId() ?? ''; - // window.location.origin is "null" on Tauri (tauri:// is opaque per WHATWG). - // The widget does new URL(parentUrl), which throws on "null". - const clientOrigin = - window.location.origin === 'null' - ? `${window.location.protocol}//${window.location.host}` - : window.location.origin; + const clientOrigin = getAppOrigin(); const widgetId = 'call-embed'; const params = new URLSearchParams({ @@ -130,7 +126,7 @@ export class CallEmbed { let widgetUrl: URL; if (elementCallUrl && elementCallUrl.trim()) { try { - widgetUrl = new URL(elementCallUrl, window.location.origin); + widgetUrl = new URL(elementCallUrl, clientOrigin); } catch (error) { debugLog.warn( 'call', @@ -142,13 +138,13 @@ export class CallEmbed { ); widgetUrl = new URL( `${trimTrailingSlash(import.meta.env.BASE_URL)}/public/element-call/index.html`, - window.location.origin + clientOrigin ); } } else { widgetUrl = new URL( `${trimTrailingSlash(import.meta.env.BASE_URL)}/public/element-call/index.html`, - window.location.origin + clientOrigin ); } widgetUrl.search = params.toString(); diff --git a/src/app/utils/platform.ts b/src/app/utils/platform.ts index 6c45c356f..2d9853d53 100644 --- a/src/app/utils/platform.ts +++ b/src/app/utils/platform.ts @@ -8,3 +8,13 @@ export function hasServiceWorker(): boolean { export function hasControllingServiceWorker(): boolean { return hasServiceWorker() && navigator.serviceWorker.controller !== null; } + +// window.location.origin is the string "null" on Tauri 2.x macOS/Linux because +// tauri:// is a non-standard (opaque) URL scheme per the WHATWG URL spec. Code +// that feeds origin into new URL() or uses it as a redirect/client URI breaks +// silently. Fall back to protocol + host, which yields "tauri://localhost". +export function getAppOrigin(): string { + return window.location.origin === 'null' + ? `${window.location.protocol}//${window.location.host}` + : window.location.origin; +} diff --git a/src/client/oidcTokenRefresher.ts b/src/client/oidcTokenRefresher.ts index 0dc1f7f41..4e8295f2f 100644 --- a/src/client/oidcTokenRefresher.ts +++ b/src/client/oidcTokenRefresher.ts @@ -8,6 +8,7 @@ import { } from '$state/sessions'; import { getLocalStorageItem } from '$state/utils/atomWithLocalStorage'; import { pushSessionToSW } from '../sw-session'; +import { getAppOrigin } from '$utils/platform'; export class SessionOidcTokenRefresher extends OidcTokenRefresher { private readonly userId: string; @@ -21,7 +22,7 @@ export class SessionOidcTokenRefresher extends OidcTokenRefresher { super( session.oidc.issuer, session.oidc.clientId, - window.location.origin, + getAppOrigin(), session.deviceId, session.oidc.idTokenClaims ?? ({} as never) );