fix(web): polyfill crypto.randomUUID for non-secure HTTP contexts

Replace direct crypto.randomUUID() calls with a randomUUID() helper in
utils.ts that falls back to crypto.getRandomValues() when the browser
is not in a secure context (e.g. plain HTTP on a non-localhost address).

Signed-off-by: Hiram Chirino <[email protected]>
This commit is contained in:
Hiram Chirino authored and Julius Marminge committed 2026-03-09 22:50:23 -07:00
1 parent dcccca3f7b
commit 9d2c2cae37
2 files changed
+23 -10

No files matched your search

+6 -6
View File
@@ -176,7 +176,7 @@ import {
VisualStudioCode,
Zed,
} from "./Icons";
import { cn, isMacPlatform, isWindowsPlatform } from "~/lib/utils";
import { cn, isMacPlatform, isWindowsPlatform, randomUUID } from "~/lib/utils";
import { Badge } from "./ui/badge";
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
import { Command, CommandItem, CommandList } from "./ui/command";
@@ -426,7 +426,7 @@ function readFileAsDataUrl(file: File): Promise<string> {
function buildTemporaryWorktreeBranchName(): string {
// Keep the 8-hex suffix shape for backend temporary-branch detection.
const token = crypto.randomUUID().slice(0, 8).toLowerCase();
const token = randomUUID().slice(0, 8).toLowerCase();
return `${WORKTREE_BRANCH_PREFIX}/${token}`;
}
@@ -1363,13 +1363,13 @@ export default function ChatView({ threadId }: ChatViewProps) {
}, [activeThreadId, setTerminalOpen, terminalState.terminalOpen]);
const splitTerminal = useCallback(() => {
if (!activeThreadId || hasReachedTerminalLimit) return;
const terminalId = `terminal-${crypto.randomUUID()}`;
const terminalId = `terminal-${randomUUID()}`;
storeSplitTerminal(activeThreadId, terminalId);
setTerminalFocusRequestId((value) => value + 1);
}, [activeThreadId, storeSplitTerminal, hasReachedTerminalLimit]);
const createNewTerminal = useCallback(() => {
if (!activeThreadId || hasReachedTerminalLimit) return;
const terminalId = `terminal-${crypto.randomUUID()}`;
const terminalId = `terminal-${randomUUID()}`;
storeNewTerminal(activeThreadId, terminalId);
setTerminalFocusRequestId((value) => value + 1);
}, [activeThreadId, storeNewTerminal, hasReachedTerminalLimit]);
@@ -1438,7 +1438,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
const shouldCreateNewTerminal =
wantsNewTerminal && terminalState.terminalIds.length < MAX_THREAD_TERMINAL_COUNT;
const targetTerminalId = shouldCreateNewTerminal
? `terminal-${crypto.randomUUID()}`
? `terminal-${randomUUID()}`
: baseTerminalId;
setTerminalOpen(true);
@@ -2331,7 +2331,7 @@ export default function ChatView({ threadId }: ChatViewProps) {
const previewUrl = URL.createObjectURL(file);
nextImages.push({
type: "image",
id: crypto.randomUUID(),
id: randomUUID(),
name: file.name || "image",
mimeType: file.type,
sizeBytes: file.size,
+17 -4
View File
@@ -14,10 +14,23 @@ export function isWindowsPlatform(platform: string): boolean {
return /^win(dows)?/i.test(platform);
}
export const newCommandId = (): CommandId => CommandId.makeUnsafe(crypto.randomUUID());
export function randomUUID(): string {
if (typeof crypto.randomUUID === "function") {
return crypto.randomUUID();
}
// Fallback for non-secure contexts (plain HTTP on non-localhost)
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
bytes[6] = (bytes[6] & 0x0f) | 0x40;
bytes[8] = (bytes[8] & 0x3f) | 0x80;
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
}
export const newProjectId = (): ProjectId => ProjectId.makeUnsafe(crypto.randomUUID());
export const newCommandId = (): CommandId => CommandId.makeUnsafe(randomUUID());
export const newThreadId = (): ThreadId => ThreadId.makeUnsafe(crypto.randomUUID());
export const newProjectId = (): ProjectId => ProjectId.makeUnsafe(randomUUID());
export const newMessageId = (): MessageId => MessageId.makeUnsafe(crypto.randomUUID());
export const newThreadId = (): ThreadId => ThreadId.makeUnsafe(randomUUID());
export const newMessageId = (): MessageId => MessageId.makeUnsafe(randomUUID());