fix(web): fall back to getRandomValues when crypto.randomUUID is unavailable (#523)

crypto.randomUUID is only exposed in secure contexts (HTTPS or
localhost). When the web app is served over HTTP on a LAN IP the
attachment adapter, toast provider, message localId helper, file
attachment metadata and terminal id creation all call
crypto.randomUUID() synchronously and throw TypeError, so the UI
silently does nothing (e.g. the file picker opens and closes with no
chip).

Add a small web/src/lib/randomId helper that tries crypto.randomUUID
first, then falls back to crypto.getRandomValues-derived UUID v4,
and finally to a Date.now/Math.random string for very old
environments. Route all five call sites through it. Output format is
identical for secure contexts and UUID v4 for the getRandomValues
path, so existing DB/SSE/RPC consumers see the same shape.
This commit is contained in:
Junmo Kim
2026-04-24 13:59:19 +08:00
committed by GitHub
parent 11e1d50e46
commit b712ee67a5
7 changed files with 108 additions and 16 deletions
+2 -6
View File
@@ -8,6 +8,7 @@ import { useSession } from '@/hooks/queries/useSession'
import { useTerminalSocket } from '@/hooks/useTerminalSocket'
import { useLongPress } from '@/hooks/useLongPress'
import { useTranslation } from '@/lib/use-translation'
import { randomId } from '@/lib/randomId'
import { TerminalView } from '@/components/Terminal/TerminalView'
import { LoadingState } from '@/components/LoadingState'
import { Button } from '@/components/ui/button'
@@ -186,12 +187,7 @@ export default function TerminalPage() {
const goBack = useAppGoBack()
const { session } = useSession(api, sessionId)
const terminalSupported = isRemoteTerminalSupported(session?.metadata)
const terminalId = useMemo(() => {
if (typeof crypto?.randomUUID === 'function') {
return crypto.randomUUID()
}
return `${Date.now()}-${Math.random().toString(16).slice(2)}`
}, [sessionId])
const terminalId = useMemo(() => randomId(), [sessionId])
const terminalRef = useRef<Terminal | null>(null)
const inputDisposableRef = useRef<{ dispose: () => void } | null>(null)
const connectOnceRef = useRef(false)