diff --git a/src/client/builtins/tabs.tsx b/src/client/builtins/tabs.tsx index 25b87bb5..d7ade19e 100644 --- a/src/client/builtins/tabs.tsx +++ b/src/client/builtins/tabs.tsx @@ -105,6 +105,31 @@ function SideChatTab(props: TabComponentProps) { ) } +/** + * A fresh, collision-resistant id for a tab minted at open time. + * + * `crypto.randomUUID` is restricted to secure contexts: a GUI served over + * plain `http://` on a LAN address exposes `crypto` but no `randomUUID`, + * and the unguarded calls used to take Side Chat and the bottom terminal down + * entirely (#1166). `getRandomValues` carries no such restriction, so the + * fallback keeps real entropy and only adds a base36 timestamp for + * readability — the ids only have to stay unique within one tab strip. + */ +function freshId(): string { + if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') { + return crypto.randomUUID() + } + const random = new Uint32Array(2) + if (typeof crypto !== 'undefined' && typeof crypto.getRandomValues === 'function') { + crypto.getRandomValues(random) + } else { + // No Web Crypto at all: two draws still stagger ids minted together. + random[0] = Math.floor(Math.random() * 0x1_0000_0000) + random[1] = Math.floor(Math.random() * 0x1_0000_0000) + } + return `${Date.now().toString(36)}-${Array.from(random, part => part.toString(36)).join('-')}` +} + /** The 6 built-in tab descriptors (the host owns the right-Sidebar terminal and browser). */ export function builtinTabs(ctx: Context): readonly TabDescriptor[] { return [ @@ -309,7 +334,7 @@ export function builtinTabs(ctx: Context): readonly TabDescriptor[] { } return { tab: { - id: `sidechat:new-${crypto.randomUUID()}`, + id: `sidechat:new-${freshId()}`, type: 'sidechat', title: t('sideChatUntitled'), meta: { autoCreate: true }, @@ -399,10 +424,10 @@ export function builtinTabs(ctx: Context): readonly TabDescriptor[] { // host binding by instance+run). createTab: () => ({ tab: { - id: `terminal-bottom:${crypto.randomUUID()}`, + id: `terminal-bottom:${freshId()}`, type: 'terminal-bottom', title: t('terminal'), - meta: { [INSTANCE_FIELD]: crypto.randomUUID() }, + meta: { [INSTANCE_FIELD]: freshId() }, }, }), // The service is probed, never injected (it is absent on deployments diff --git a/tests/builtins.spec.ts b/tests/builtins.spec.ts index d7dce8c1..b4ce0b12 100644 --- a/tests/builtins.spec.ts +++ b/tests/builtins.spec.ts @@ -28,6 +28,7 @@ import { createSidebarStore } from '../src/client/state.ts' import { builtinTabs } from '../src/client/builtins/tabs.tsx' import { registerBuiltins } from '../src/client/builtins/index.ts' import { parkSidechatReopen } from '../src/client/SideChatView.tsx' +import { terminalInstanceOf } from '../src/client/terminal-client.ts' function setup(): { service: ReturnType; store: ReturnType; dispose: () => void } { const store = createSidebarStore() @@ -175,6 +176,36 @@ describe('built-in tab registrations', () => { expect(sidechat?.dedupeKey?.(first!.tab)).toBeUndefined() }) + it('mints Side Chat and terminal tab ids without crypto.randomUUID (#1166)', () => { + // A GUI served over plain http:// on a LAN address is an INSECURE context: + // `crypto` exists but `randomUUID` does not. The unguarded calls threw + // here, so the host left a dead
instead of a tab. + // getRandomValues is exempt from the secure-context restriction. + const realCrypto = globalThis.crypto + const insecure = { + getRandomValues: (array: Uint32Array) => realCrypto.getRandomValues(array), + } + vi.stubGlobal('crypto', insecure) + try { + const tabs = builtinTabs({} as Context) + const sidechat = tabs.find(tab => tab.id === 'sidechat') + const terminal = tabs.find(tab => tab.id === 'terminal-bottom') + const firstChat = sidechat?.createTab?.(undefined as never) + const secondChat = sidechat?.createTab?.(undefined as never) + expect(firstChat?.tab.id).toBeTruthy() + expect(firstChat?.tab.id).not.toBe(secondChat?.tab.id) + const firstShell = terminal?.createTab?.({} as never) + const secondShell = terminal?.createTab?.({} as never) + expect(firstShell?.tab.id).toBeTruthy() + expect(firstShell?.tab.id).not.toBe(secondShell?.tab.id) + const firstInstance = terminalInstanceOf(firstShell!.tab) + expect(firstInstance).toBeTruthy() + expect(firstInstance).not.toBe(terminalInstanceOf(secondShell!.tab)) + } finally { + vi.unstubAllGlobals() + } + }) + it('the subagent tab declares its auto-open, view, folding, and jobs settings', () => { const { service } = setup() const toggles = service.getTab('subagent')?.settings?.toggles ?? []