fix(client): mint tab ids without crypto.randomUUID (closes #1166)

Side Chat and the bottom terminal called crypto.randomUUID() unguarded. In an insecure context — a GUI served over plain http:// on a LAN address — that throws, so the host left a dead data-slot-error instead of a tab. Mint both ids through one local freshId() helper: randomUUID when available, otherwise a base36 timestamp plus two getRandomValues draws (getRandomValues is exempt from the secure-context restriction). The helper stays internal; the public service surface is unchanged.

The regression test drives the real built-in createTab factories with randomUUID removed and checks unique ids plus a non-empty meta[INSTANCE_FIELD].
This commit is contained in:
Menghuan1918 committed 2026-11-18 00:07:48 +08:00
1 parent 58cf84b733
commit ddfcb67017
2 files changed
+59 -3

No files matched your search

+28 -3
View File
@@ -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
+31
View File
@@ -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<typeof createBetterSidebarService>; store: ReturnType<typeof createSidebarStore>; 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 <div data-slot-error> 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 ?? []