From dc980cc2ca06409d6f9b4680d1bd49b41fd91c09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Garc=C3=ADa=20Hinestrosa?= Date: Sat, 14 Oct 2023 13:41:00 +0200 Subject: [PATCH 1/5] fix(utils): Support crypto.getRandomValues in old Chromium versions --- packages/utils/src/misc.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/utils/src/misc.ts b/packages/utils/src/misc.ts index 9799df421c31..ba9d87aad71c 100644 --- a/packages/utils/src/misc.ts +++ b/packages/utils/src/misc.ts @@ -31,7 +31,13 @@ export function uuid4(): string { return crypto.randomUUID().replace(/-/g, ''); } if (crypto && crypto.getRandomValues) { - getRandomByte = () => crypto.getRandomValues(new Uint8Array(1))[0]; + getRandomByte = () => { + // crypto.getRandomValues might return undefined instead of the typed array + // in old Chromium versions (e.g. 23.0.1235.0 (151422)) + const typedArray = new Uint8Array(1); + crypto.getRandomValues(typedArray); + return typedArray[0]; + } } } catch (_) { // some runtimes can crash invoking crypto From aba94670ef0535d6f6c9d20220d1972af52783ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Garc=C3=ADa=20Hinestrosa?= Date: Sat, 14 Oct 2023 15:04:01 +0200 Subject: [PATCH 2/5] fix(utils): Apply prettier --- packages/utils/src/misc.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/src/misc.ts b/packages/utils/src/misc.ts index ba9d87aad71c..ee7ec136fa6b 100644 --- a/packages/utils/src/misc.ts +++ b/packages/utils/src/misc.ts @@ -33,11 +33,11 @@ export function uuid4(): string { if (crypto && crypto.getRandomValues) { getRandomByte = () => { // crypto.getRandomValues might return undefined instead of the typed array - // in old Chromium versions (e.g. 23.0.1235.0 (151422)) + // in old Chromium versions (e.g. 23.0.1235.0 (151422)) const typedArray = new Uint8Array(1); crypto.getRandomValues(typedArray); return typedArray[0]; - } + }; } } catch (_) { // some runtimes can crash invoking crypto From c72d7b2b2a2b067c887ede7605f69b491df9cd56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Garc=C3=ADa=20Hinestrosa?= Date: Sat, 21 Oct 2023 20:35:10 +0200 Subject: [PATCH 3/5] fix(utils): Extend the comment explaining why the fix works --- packages/utils/src/misc.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/utils/src/misc.ts b/packages/utils/src/misc.ts index ee7ec136fa6b..c8afc0818909 100644 --- a/packages/utils/src/misc.ts +++ b/packages/utils/src/misc.ts @@ -34,6 +34,8 @@ export function uuid4(): string { getRandomByte = () => { // crypto.getRandomValues might return undefined instead of the typed array // in old Chromium versions (e.g. 23.0.1235.0 (151422)) + // However, `typedArray` is still filled in-place. + // @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#typedarray const typedArray = new Uint8Array(1); crypto.getRandomValues(typedArray); return typedArray[0]; From 9bcaa71b653800567d8ecdafad03abcd687e012b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Garc=C3=ADa=20Hinestrosa?= Date: Sat, 21 Oct 2023 21:48:58 +0200 Subject: [PATCH 4/5] fix(utils): Add unit tests --- packages/utils/test/misc.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/utils/test/misc.test.ts b/packages/utils/test/misc.test.ts index dc75b70d4286..809a5994179e 100644 --- a/packages/utils/test/misc.test.ts +++ b/packages/utils/test/misc.test.ts @@ -343,6 +343,23 @@ describe('uuid4 generation', () => { expect(uuid4()).toMatch(uuid4Regex); } }); + + // Corner case related to crypto.getRandomValues being only + // semi-implemented (e.g. Chromium 23.0.1235.0 (151422)) + it('returns valid uuid v4 even if crypto.getRandomValues does not return a typed array', () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + const cryptoMod = require('crypto'); + + const getRandomValues = (typedArray: Uint8Array) => { + cryptoMod.getRandomValues(typedArray); + }; + + (global as any).crypto = { getRandomValues }; + + for (let index = 0; index < 1_000; index++) { + expect(uuid4()).toMatch(uuid4Regex); + } + }); }); describe('arrayify()', () => { From a9429473396727db5222643a8fc3372163cba87b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Garc=C3=ADa=20Hinestrosa?= Date: Sat, 28 Oct 2023 21:48:43 +0200 Subject: [PATCH 5/5] fix(utils): Fix unit tests for Node < 18 --- packages/utils/test/misc.test.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/utils/test/misc.test.ts b/packages/utils/test/misc.test.ts index 809a5994179e..c1eb978dcdbe 100644 --- a/packages/utils/test/misc.test.ts +++ b/packages/utils/test/misc.test.ts @@ -351,7 +351,9 @@ describe('uuid4 generation', () => { const cryptoMod = require('crypto'); const getRandomValues = (typedArray: Uint8Array) => { - cryptoMod.getRandomValues(typedArray); + if (cryptoMod.getRandomValues) { + cryptoMod.getRandomValues(typedArray); + } }; (global as any).crypto = { getRandomValues };