Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(web): fall back when crypto.randomUUID is unavailable on non-secure origins (LFE-10858) (#15058)

Nikita Kabardin38e249a83cc8
diff --git a/web/src/features/datasets/components/NewDatasetItemForm.tsx b/web/src/features/datasets/components/NewDatasetItemForm.tsx
index 890648eed6..2bb1590c82 100644
--- a/web/src/features/datasets/components/NewDatasetItemForm.tsx
+++ b/web/src/features/datasets/components/NewDatasetItemForm.tsx
@@ -1,6 +1,6 @@
import { Button } from "@/src/components/ui/button";
import * as z from "zod";
-import { v4 as uuidv4 } from "uuid";
+import { safeRandomUUID } from "@/src/utils/safe-random-uuid";
import { zodResolver } from "@hookform/resolvers/zod";
import { type Control, useForm, useWatch } from "react-hook-form";
import {
@@ -158,9 +158,7 @@ export const NewDatasetItemForm = (props: {
const getDatasetItemId = useCallback((datasetId: string) => {
const existing = itemIdByDataset.current.get(datasetId);
if (existing) return existing;
- // uuid's v4() falls back to crypto.getRandomValues, so it works on
- // non-secure (HTTP) origins where crypto.randomUUID is unavailable.
- const id = uuidv4();
+ const id = safeRandomUUID();
itemIdByDataset.current.set(datasetId, id);
return id;
}, []);
diff --git a/web/src/features/datasets/hooks/useDatasetItemMediaUpload.ts b/web/src/features/datasets/hooks/useDatasetItemMediaUpload.ts
index da478fafe1..e2602ddd4f 100644
--- a/web/src/features/datasets/hooks/useDatasetItemMediaUpload.ts
+++ b/web/src/features/datasets/hooks/useDatasetItemMediaUpload.ts
@@ -1,10 +1,10 @@
import { Sha256 } from "@aws-crypto/sha256-browser";
import { useCallback, useState } from "react";
-import { v4 as uuidv4 } from "uuid";
import { showErrorToast } from "@/src/features/notifications/showErrorToast";
import { MediaContentType } from "@/src/features/media/validation";
import { api } from "@/src/utils/api";
+import { safeRandomUUID } from "@/src/utils/safe-random-uuid";
import { type DatasetItemMediaField } from "@langfuse/shared";
const SUPPORTED_CONTENT_TYPES = new Set<string>(
@@ -86,9 +86,7 @@ export function useDatasetItemMediaUpload({
return null;
}
- // uuid's v4() falls back to crypto.getRandomValues, so it works on
- // non-secure (HTTP) origins where crypto.randomUUID is unavailable.
- const pendingId = uuidv4();
+ const pendingId = safeRandomUUID();
setPendingUploads((prev) => [
...prev,
{ id: pendingId, fileName: file.name },
diff --git a/web/src/pages/_app.tsx b/web/src/pages/_app.tsx
index e0d054d587..a1740b1a42 100644
--- a/web/src/pages/_app.tsx
+++ b/web/src/pages/_app.tsx
@@ -1,3 +1,8 @@
+// Must stay the first import: installs a `crypto.randomUUID` fallback for
+// non-secure (plain-HTTP) origins before any other module can call it
+// (LFE-10858).
+import "@/src/polyfills/crypto-random-uuid";
+
import { type AppType } from "next/app";
import { type Session } from "next-auth";
import { SessionProvider } from "next-auth/react";
diff --git a/web/src/polyfills/crypto-random-uuid.clienttest.ts b/web/src/polyfills/crypto-random-uuid.clienttest.ts
new file mode 100644
index 0000000000..f2773b6c66
--- /dev/null
+++ b/web/src/polyfills/crypto-random-uuid.clienttest.ts
@@ -0,0 +1,58 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { installCryptoRandomUUIDPolyfill } from "@/src/polyfills/crypto-random-uuid";
+
+const UUID_V4_REGEX =
+ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
+
+describe("installCryptoRandomUUIDPolyfill", () => {
+ it("installs a uuid-v4 fallback when crypto.randomUUID is missing", () => {
+ const target: { randomUUID?: () => string } = {};
+
+ installCryptoRandomUUIDPolyfill(target);
+
+ expect(target.randomUUID).toBeTypeOf("function");
+ expect(target.randomUUID!()).toMatch(UUID_V4_REGEX);
+ expect(target.randomUUID!()).not.toBe(target.randomUUID!());
+ });
+
+ it("keeps the native implementation when present", () => {
+ const native = () => "native-uuid";
+ const target = { randomUUID: native };
+
+ installCryptoRandomUUIDPolyfill(target);
+
+ expect(target.randomUUID).toBe(native);
+ });
+
+ it("no-ops when no crypto object exists at all", () => {
+ // Stub the global itself: explicitly passing `undefined` would trigger
+ // the `globalThis.crypto` default parameter instead of the guard.
+ vi.stubGlobal("crypto", undefined);
+ try {
+ expect(() => installCryptoRandomUUIDPolyfill()).not.toThrow();
+ } finally {
+ vi.unstubAllGlobals();
+ }
+ });
+
+ it("polyfills the global crypto object on module import (non-secure context)", async () => {
+ // Simulate a non-secure browser context: crypto exists but randomUUID
+ // does not. Re-importing the module must install the fallback via its
+ // module-scope side effect — the mechanism _app.tsx relies on.
+ const realCrypto = globalThis.crypto;
+ const bareCrypto = {
+ getRandomValues: realCrypto.getRandomValues.bind(realCrypto),
+ } as Crypto;
+ vi.stubGlobal("crypto", bareCrypto);
+ vi.resetModules();
+ try {
+ await import("@/src/polyfills/crypto-random-uuid");
+ expect(bareCrypto.randomUUID).toBeTypeOf("function");
+ expect(bareCrypto.randomUUID()).toMatch(UUID_V4_REGEX);
+ } finally {
+ vi.unstubAllGlobals();
+ vi.resetModules();
+ }
+ });
+});
diff --git a/web/src/polyfills/crypto-random-uuid.ts b/web/src/polyfills/crypto-random-uuid.ts
new file mode 100644
index 0000000000..abda7b9ba6
--- /dev/null
+++ b/web/src/polyfills/crypto-random-uuid.ts
@@ -0,0 +1,33 @@
+import { v4 as uuidv4 } from "uuid";
+
+/**
+ * `crypto.randomUUID` polyfill for non-secure contexts.
+ *
+ * `crypto.randomUUID` is a secure-context-only Web API: it exists on HTTPS and
+ * localhost, but is undefined when a self-hosted instance is opened over plain
+ * HTTP (e.g. `http://<lan-ip>:3000`). Any unguarded call — from our code or a
+ * bundled dependency — then throws `TypeError: crypto.randomUUID is not a
+ * function` and white-screens the app (LFE-10858).
+ *
+ * Installing a fallback at the client entry point (first import in _app.tsx)
+ * protects every caller, including dependencies we don't control. uuid's
+ * `v4()` only needs `crypto.getRandomValues`, which is available in
+ * non-secure contexts too.
+ *
+ * For direct calls in our own code, prefer `safeRandomUUID()` from
+ * `@/src/utils/safe-random-uuid` over `crypto.randomUUID()` — it does not
+ * depend on this polyfill having run.
+ */
+export function installCryptoRandomUUIDPolyfill(
+ // Structural type so the uuid-backed fallback (plain `string`) is
+ // assignable — `Crypto["randomUUID"]` returns a template-literal type.
+ target: { randomUUID?: () => string } | undefined = globalThis.crypto,
+): void {
+ if (!target || typeof target.randomUUID === "function") return;
+ // Pass an options object so uuid's v4() takes its crypto.getRandomValues
+ // path unconditionally. A bare v4() consults crypto.randomUUID at call
+ // time — which is this polyfill once installed, so it would recurse.
+ target.randomUUID = () => uuidv4({});
+}
+
+installCryptoRandomUUIDPolyfill();
diff --git a/web/src/utils/safe-random-uuid.clienttest.ts b/web/src/utils/safe-random-uuid.clienttest.ts
new file mode 100644
index 0000000000..6fe93c731b
--- /dev/null
+++ b/web/src/utils/safe-random-uuid.clienttest.ts
@@ -0,0 +1,50 @@
+import { describe, expect, it, vi } from "vitest";
+
+import { safeRandomUUID } from "@/src/utils/safe-random-uuid";
+
+const UUID_V4_REGEX =
+ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
+
+describe("safeRandomUUID", () => {
+ it("returns RFC 4122 v4 ids", () => {
+ expect(safeRandomUUID()).toMatch(UUID_V4_REGEX);
+ });
+
+ it("returns unique ids", () => {
+ const ids = new Set(Array.from({ length: 100 }, () => safeRandomUUID()));
+ expect(ids.size).toBe(100);
+ });
+
+ it("uses the native crypto.randomUUID when present", async () => {
+ // Import a fresh module instance against the stubbed global so the test
+ // holds regardless of whether uuid binds crypto.randomUUID at module
+ // load (v11) or reads it at call time (v14).
+ const sentinel = "11111111-2222-4333-8444-555555555555";
+ vi.stubGlobal("crypto", { randomUUID: () => sentinel });
+ vi.resetModules();
+ try {
+ const { safeRandomUUID: fresh } =
+ await import("@/src/utils/safe-random-uuid");
+ expect(fresh()).toBe(sentinel);
+ } finally {
+ vi.unstubAllGlobals();
+ vi.resetModules();
+ }
+ });
+
+ it("falls back to crypto.getRandomValues when randomUUID is unavailable (non-secure context)", async () => {
+ const realCrypto = globalThis.crypto;
+ vi.stubGlobal("crypto", {
+ getRandomValues: realCrypto.getRandomValues.bind(realCrypto),
+ });
+ vi.resetModules();
+ try {
+ const { safeRandomUUID: fresh } =
+ await import("@/src/utils/safe-random-uuid");
+ expect(fresh()).toMatch(UUID_V4_REGEX);
+ } finally {
+ vi.unstubAllGlobals();
+ vi.resetModules();
+ }
+ });
+});
diff --git a/web/src/utils/safe-random-uuid.ts b/web/src/utils/safe-random-uuid.ts
new file mode 100644
index 0000000000..45c6936910
--- /dev/null
+++ b/web/src/utils/safe-random-uuid.ts
@@ -0,0 +1,19 @@
+import { v4 as uuidv4 } from "uuid";
+
+/**
+ * UUID v4 generator that also works on non-secure (plain-HTTP) origins.
+ *
+ * `crypto.randomUUID()` is a secure-context-only Web API — it is undefined
+ * when a self-hosted instance is accessed over plain HTTP via a LAN IP or
+ * hostname, and calling it there crashes the app (LFE-10858). uuid's `v4()`
+ * uses the native `crypto.randomUUID` when present and falls back to
+ * `crypto.getRandomValues`, which works in any context.
+ *
+ * Use this instead of `crypto.randomUUID()` in client-side code. (A safety-net
+ * polyfill also installs `crypto.randomUUID` at the app entry point — see
+ * `@/src/polyfills/crypto-random-uuid` — but direct calls should not rely on
+ * import order.)
+ */
+export function safeRandomUUID(): string {
+ return uuidv4();
+}