From edb52be36e516f918b78e20f97e38e776133785f Mon Sep 17 00:00:00 2001 From: skjnldsv Date: Sat, 21 Jun 2025 10:15:27 +0200 Subject: [PATCH 1/2] fix(files_sharing): fallback self.crypto.randomUUID Signed-off-by: skjnldsv --- lib/guest.ts | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/guest.ts b/lib/guest.ts index 110c5cfc..6d6e8b9a 100644 --- a/lib/guest.ts +++ b/lib/guest.ts @@ -16,11 +16,11 @@ class GuestUser implements NextcloudUser { constructor() { if (!browserStorage.getItem('guestUid')) { - browserStorage.setItem('guestUid', self.crypto.randomUUID()) + browserStorage.setItem('guestUid', randomUUID()) } this._displayName = browserStorage.getItem('guestNickname') || '' - this.uid = browserStorage.getItem('guestUid') || self.crypto.randomUUID() + this.uid = browserStorage.getItem('guestUid') || randomUUID() this.isAdmin = false subscribe('user:info:changed', (guest) => { @@ -73,3 +73,24 @@ export function setGuestNickname(nickname: string): void { getGuestUser().displayName = nickname } + +/** + * Generate a random UUID (version 4) if the crypto API is not available. + * If the crypto API is available, it uses the less secure `randomUUID` method. + * Crypto API is available in modern browsers on secure contexts (HTTPS). + * + * @return {string} A random UUID. + */ +function randomUUID(): string { + // Use the crypto API if available + if (self?.crypto?.randomUUID) { + return self.crypto.randomUUID() + } + + // Generate a random UUID (version 4) + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => { + const r = Math.random() * 16 | 0 + const v = c === 'x' ? r : (r & 0x3 | 0x8) + return v.toString(16) + }) +} From b0deb5b409b8eab3b59a1fb91c5a243920616609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6?= Date: Sat, 21 Jun 2025 15:44:41 +0200 Subject: [PATCH 2/2] fix: use `globalThis` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ferdinand Thiessen Signed-off-by: John Molakvoæ --- lib/guest.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/guest.ts b/lib/guest.ts index 6d6e8b9a..c23cffd6 100644 --- a/lib/guest.ts +++ b/lib/guest.ts @@ -83,8 +83,8 @@ export function setGuestNickname(nickname: string): void { */ function randomUUID(): string { // Use the crypto API if available - if (self?.crypto?.randomUUID) { - return self.crypto.randomUUID() + if (globalThis.crypto?.randomUUID) { + return globalThis.crypto.randomUUID() } // Generate a random UUID (version 4)