fix(desktop): isolate remote runtime auth and embeds

Fix remote Desktop runtime bootstrapping across context-panel session chats, additional windows, and host switches.\n\n- Bootstrap embedded session-chat frames through a same-origin parent handshake that supplies the active endpoint, bearer token, runtime headers, local origin, and a credential-free relay descriptor.\n- Keep relay pairing grants out of iframe state and explicitly rebind the SDK after embedded bootstrap or relay restoration.\n- Preserve each additional and Mini Chat window's own init script instead of overwriting it when the main window's host configuration changes.\n- Replace direct iframe global calls with same-origin postMessage synchronization for theme, chat settings, and visibility.\n\nHarden Desktop host authentication and probing.\n\n- Bind password, passkey, session-status, and token-persistence completions to the runtime identity that started them, so a late result cannot alter a newly selected host.\n- Cancel active passkey operations and reset transient auth UI state on endpoint changes.\n- Verify stored client authentication via /auth/session for direct and relay host probes, distinguishing reachable hosts from hosts that require re-authentication.\n- Bound every relay probe request with an aborting timeout so a stalled auth request cannot hang refresh or host switching.\n\nAdd regression coverage for the embedded bootstrap handshake, credential-free relay descriptor exposure, runtime configuration, stale password completion after an A-to-B switch, and SDK errors that carry a zero response status.\n\nAlso preserve SDK response status on session-message loader errors so callers can distinguish transport and server failures.
This commit is contained in:
Bohdan Triapitsyn
2026-07-30 17:43:39 +03:00
parent 4ae3debf54
commit 3b00c91893
19 changed files with 819 additions and 159 deletions
@@ -1,4 +1,5 @@
import type { Theme } from '@/types/theme';
import type { RelayRuntimeDescriptor } from '@/lib/relay/runtime-tunnel';
export type EmbeddedSessionChatThemeBootstrap = {
mode: 'light' | 'dark' | 'system';
@@ -12,6 +13,93 @@ export type EmbeddedSessionChatURLCacheEntry = {
src: string;
};
export type EmbeddedSessionRuntimeBootstrap = {
apiBaseUrl: string;
clientToken: string;
localOrigin: string;
runtimeHeaders?: Record<string, string>;
relayHostId: string;
relay?: Omit<RelayRuntimeDescriptor, 'grant'>;
};
export const EMBEDDED_RUNTIME_BOOTSTRAP_REQUEST = 'openchamber:embedded-runtime-bootstrap-request';
export const EMBEDDED_RUNTIME_BOOTSTRAP_RESPONSE = 'openchamber:embedded-runtime-bootstrap-response';
const EMBEDDED_RUNTIME_BOOTSTRAP_TIMEOUT_MS = 5_000;
const EMBEDDED_RUNTIME_BOOTSTRAP_RETRY_MS = 100;
const isStringRecord = (value: unknown): value is Record<string, string> => (
value !== null
&& typeof value === 'object'
&& !Array.isArray(value)
&& Object.values(value).every((entry) => typeof entry === 'string')
);
const isRuntimeBootstrap = (value: unknown): value is EmbeddedSessionRuntimeBootstrap => {
if (!value || typeof value !== 'object') return false;
const candidate = value as Partial<EmbeddedSessionRuntimeBootstrap>;
if (
typeof candidate.apiBaseUrl !== 'string'
|| typeof candidate.clientToken !== 'string'
|| typeof candidate.localOrigin !== 'string'
|| typeof candidate.relayHostId !== 'string'
) {
return false;
}
if (candidate.runtimeHeaders !== undefined && !isStringRecord(candidate.runtimeHeaders)) {
return false;
}
const relay = candidate.relay;
if (relay === undefined) return true;
return relay !== null
&& typeof relay === 'object'
&& !('grant' in relay)
&& typeof relay.relayUrl === 'string'
&& typeof relay.serverId === 'string'
&& relay.hostEncPubJwk !== null
&& typeof relay.hostEncPubJwk === 'object'
&& !Array.isArray(relay.hostEncPubJwk);
};
export const requestEmbeddedSessionRuntimeBootstrap = (): Promise<EmbeddedSessionRuntimeBootstrap | null> => {
if (!isEmbeddedSessionChat() || typeof window === 'undefined' || window.parent === window) {
return Promise.resolve(null);
}
const requestId = typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function'
? crypto.randomUUID()
: `${Date.now()}-${Math.random()}`;
return new Promise((resolve) => {
let settled = false;
let retry = 0;
let timeout = 0;
const finish = (value: EmbeddedSessionRuntimeBootstrap | null) => {
if (settled) return;
settled = true;
window.clearTimeout(timeout);
window.clearInterval(retry);
window.removeEventListener('message', handleMessage);
resolve(value);
};
const handleMessage = (event: MessageEvent) => {
if (event.origin !== window.location.origin || event.source !== window.parent) return;
const data = event.data as { type?: unknown; requestId?: unknown; payload?: unknown };
if (data?.type !== EMBEDDED_RUNTIME_BOOTSTRAP_RESPONSE || data.requestId !== requestId) return;
finish(isRuntimeBootstrap(data.payload) ? data.payload : null);
};
timeout = window.setTimeout(() => finish(null), EMBEDDED_RUNTIME_BOOTSTRAP_TIMEOUT_MS);
const sendRequest = () => {
window.parent.postMessage({ type: EMBEDDED_RUNTIME_BOOTSTRAP_REQUEST, requestId }, window.location.origin);
};
retry = window.setInterval(sendRequest, EMBEDDED_RUNTIME_BOOTSTRAP_RETRY_MS);
window.addEventListener('message', handleMessage);
sendRequest();
});
};
const buildEmbeddedSessionChatURLSignature = (
sessionID: string,
directory: string | null,
@@ -125,4 +213,4 @@ export const getEmbeddedSessionChatOriginSessionId = (): string | null => {
} catch {
return null;
}
};
};