Skip to content

Personal AI, On Personal Devices

Python

fix(frontend): polyfill crypto.randomUUID in insecure contexts (#1071)

Wagner Gadea Lorenz1f6139bee21b
diff --git a/frontend/src/lib/polyfills.test.ts b/frontend/src/lib/polyfills.test.ts
new file mode 100644
index 00000000..4322689d
--- /dev/null
+++ b/frontend/src/lib/polyfills.test.ts
@@ -0,0 +1,35 @@
+import { describe, expect, it } from 'vitest';
+import { ensureRandomUUID } from './polyfills';
+
+const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
+
+describe('ensureRandomUUID', () => {
+ it('adds a v4 randomUUID when the context is insecure (no crypto.randomUUID)', () => {
+ const fake = {
+ getRandomValues: (a: Uint8Array) => {
+ for (let i = 0; i < a.length; i++) a[i] = (i * 37 + 11) & 0xff;
+ return a;
+ },
+ } as unknown as Crypto;
+ expect(typeof fake.randomUUID).toBe('undefined');
+ ensureRandomUUID(fake);
+ expect(fake.randomUUID()).toMatch(UUID_V4);
+ });
+
+ it('generates different values with real randomness', () => {
+ const real = {
+ getRandomValues: (a: Uint8Array<ArrayBuffer>) => globalThis.crypto.getRandomValues(a),
+ } as unknown as Crypto;
+ ensureRandomUUID(real);
+ const ids = new Set(Array.from({ length: 50 }, () => real.randomUUID()));
+ expect(ids.size).toBe(50);
+ ids.forEach((id) => expect(id).toMatch(UUID_V4));
+ });
+
+ it('does not replace an existing randomUUID', () => {
+ const original = () => '00000000-0000-4000-8000-000000000000' as ReturnType<Crypto['randomUUID']>;
+ const secure = { randomUUID: original, getRandomValues: () => new Uint8Array(0) } as unknown as Crypto;
+ ensureRandomUUID(secure);
+ expect(secure.randomUUID).toBe(original);
+ });
+});
diff --git a/frontend/src/lib/polyfills.ts b/frontend/src/lib/polyfills.ts
new file mode 100644
index 00000000..89818f12
--- /dev/null
+++ b/frontend/src/lib/polyfills.ts
@@ -0,0 +1,22 @@
+// Polyfills for browser APIs that only exist in a "secure context" (HTTPS or localhost).
+//
+// The UI can be reached over plain HTTP on a LAN (e.g. http://192.168.x.x:8000 from a phone),
+// where `crypto.randomUUID` is undefined. `store.ts` calls it while the module is being
+// evaluated, so without this the whole page stays blank with
+// "TypeError: crypto.randomUUID is not a function".
+// `crypto.getRandomValues` is available in insecure contexts, so build a v4 UUID from it.
+
+export function ensureRandomUUID(c: Crypto | undefined = globalThis.crypto): void {
+ if (!c || typeof c.randomUUID === 'function') return;
+ const uuid = (): `${string}-${string}-${string}-${string}-${string}` => {
+ const b = new Uint8Array(16);
+ c.getRandomValues(b);
+ b[6] = (b[6] & 0x0f) | 0x40; // version 4
+ b[8] = (b[8] & 0x3f) | 0x80; // variant 10xx
+ const h = Array.from(b, (x) => x.toString(16).padStart(2, '0'));
+ return `${h.slice(0, 4).join('')}-${h.slice(4, 6).join('')}-${h.slice(6, 8).join('')}-${h.slice(8, 10).join('')}-${h.slice(10).join('')}`;
+ };
+ Object.defineProperty(c, 'randomUUID', { value: uuid, configurable: true, writable: true });
+}
+
+ensureRandomUUID();
diff --git a/frontend/src/main.tsx b/frontend/src/main.tsx
index b1ea950d..1a57be9f 100644
--- a/frontend/src/main.tsx
+++ b/frontend/src/main.tsx
@@ -1,3 +1,5 @@
+// Must stay the first import: fills in crypto.randomUUID before any module uses it.
+import './lib/polyfills';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { BrowserRouter } from 'react-router';