From f39e6df81487f0d5580f3c0b39887d524cd51b52 Mon Sep 17 00:00:00 2001 From: bymyself Date: Sun, 30 Aug 2026 05:54:14 +0000 Subject: [PATCH] fix: require Web Crypto for UUID generation --- src/utils/uuid.test.ts | 48 ++++++++++++++++++++++++++++++++++++++++++ src/utils/uuid.ts | 15 +++++++------ 2 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 src/utils/uuid.test.ts diff --git a/src/utils/uuid.test.ts b/src/utils/uuid.test.ts new file mode 100644 index 00000000000..8df201464ff --- /dev/null +++ b/src/utils/uuid.test.ts @@ -0,0 +1,48 @@ +import { describe, expect, it, vi } from 'vitest' + +import { createUuidv4 } from './uuid' + +const UUID_V4_PATTERN = + /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ + +describe('createUuidv4', () => { + it('uses crypto.randomUUID when available', () => { + const randomUUID = vi.fn(() => '12345678-1234-4abc-8def-123456789abc') + const getRandomValues = vi.fn() + vi.stubGlobal('crypto', { randomUUID, getRandomValues }) + + expect(createUuidv4()).toBe('12345678-1234-4abc-8def-123456789abc') + expect(randomUUID).toHaveBeenCalledOnce() + expect(getRandomValues).not.toHaveBeenCalled() + }) + + it('uses crypto.getRandomValues when randomUUID is unavailable', () => { + const getRandomValues = vi.fn((values: Uint32Array) => { + values.forEach((_, index) => { + values[index] = index * 2654435761 + }) + return values + }) + vi.stubGlobal('crypto', { randomUUID: undefined, getRandomValues }) + + const uuid = createUuidv4() + + expect(getRandomValues).toHaveBeenCalledOnce() + expect(uuid).toMatch(UUID_V4_PATTERN) + }) + + it('throws when Web Crypto is unavailable', () => { + vi.stubGlobal('crypto', undefined) + + expect(() => createUuidv4()).toThrow( + 'Web Crypto is required to generate a UUID' + ) + }) + + it('mints unique UUIDv4 values across a sanity sample', () => { + const uuids = Array.from({ length: 1_000 }, () => createUuidv4()) + + expect(uuids.every((uuid) => UUID_V4_PATTERN.test(uuid))).toBe(true) + expect(new Set(uuids).size).toBe(uuids.length) + }) +}) diff --git a/src/utils/uuid.ts b/src/utils/uuid.ts index 941b127b7d6..58ee5fb28e7 100644 --- a/src/utils/uuid.ts +++ b/src/utils/uuid.ts @@ -14,12 +14,15 @@ const randomStorage = new Uint32Array(31) * Original implementation from https://gist.github.com/jed/982883?permalink_comment_id=852670#gistcomment-852670 * * Prefers the {@link crypto.randomUUID} method if available, falling back to - * {@link crypto.getRandomValues}, then finally the legacy {@link Math.random} method. + * {@link crypto.getRandomValues} for insecure contexts. */ export function createUuidv4(): UUID { - if (typeof crypto?.randomUUID === 'function') return crypto.randomUUID() - if (typeof crypto?.getRandomValues === 'function') { - const random = crypto.getRandomValues(randomStorage) + const webCrypto = globalThis.crypto + if (typeof webCrypto?.randomUUID === 'function') { + return webCrypto.randomUUID() + } + if (typeof webCrypto?.getRandomValues === 'function') { + const random = webCrypto.getRandomValues(randomStorage) let i = 0 return '10000000-1000-4000-8000-100000000000'.replaceAll(/[018]/g, (a) => ( @@ -28,7 +31,5 @@ export function createUuidv4(): UUID { ).toString(16) ) } - return '10000000-1000-4000-8000-100000000000'.replaceAll(/[018]/g, (a) => - (Number(a) ^ ((Math.random() * 16) >> (Number(a) * 0.25))).toString(16) - ) + throw new Error('Web Crypto is required to generate a UUID') }