Skip to content

Commit f40440b

Browse files
authored
Merge pull request #822 from nextcloud-libraries/fix/insecure-crypto-env
2 parents fb3033b + b0deb5b commit f40440b

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

lib/guest.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class GuestUser implements NextcloudUser {
1717

1818
constructor() {
1919
if (!browserStorage.getItem('guestUid')) {
20-
browserStorage.setItem('guestUid', self.crypto.randomUUID())
20+
browserStorage.setItem('guestUid', randomUUID())
2121
}
2222

2323
this._displayName = browserStorage.getItem('guestNickname') || ''
24-
this.uid = browserStorage.getItem('guestUid') || self.crypto.randomUUID()
24+
this.uid = browserStorage.getItem('guestUid') || randomUUID()
2525
this.isAdmin = false
2626

2727
subscribe('user:info:changed', (guest) => {
@@ -73,3 +73,24 @@ export function setGuestNickname(nickname: string): void {
7373

7474
getGuestUser().displayName = nickname
7575
}
76+
77+
/**
78+
* Generate a random UUID (version 4) if the crypto API is not available.
79+
* If the crypto API is available, it uses the less secure `randomUUID` method.
80+
* Crypto API is available in modern browsers on secure contexts (HTTPS).
81+
*
82+
* @return {string} A random UUID.
83+
*/
84+
function randomUUID(): string {
85+
// Use the crypto API if available
86+
if (globalThis.crypto?.randomUUID) {
87+
return globalThis.crypto.randomUUID()
88+
}
89+
90+
// Generate a random UUID (version 4)
91+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
92+
const r = Math.random() * 16 | 0
93+
const v = c === 'x' ? r : (r & 0x3 | 0x8)
94+
return v.toString(16)
95+
})
96+
}

0 commit comments

Comments
 (0)