Skip to content

Commit 33ddce4

Browse files
authored
Merge pull request #53635 from nextcloud/fix/insecure-crypto-envs
fix(files_sharing): fallback self.crypto.getRandomValues
2 parents 4eda352 + f35d164 commit 33ddce4

File tree

9 files changed

+32
-13
lines changed

9 files changed

+32
-13
lines changed

apps/files_sharing/src/utils/GeneratePassword.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,29 @@ export default async function(verbose = false): Promise<string> {
3838

3939
const array = new Uint8Array(10)
4040
const ratio = passwordSet.length / 255
41-
self.crypto.getRandomValues(array)
41+
getRandomValues(array)
4242
let password = ''
4343
for (let i = 0; i < array.length; i++) {
4444
password += passwordSet.charAt(array[i] * ratio)
4545
}
4646
return password
4747
}
48+
49+
/**
50+
* Fills the given array with cryptographically secure random values.
51+
* If the crypto API is not available, it falls back to less secure Math.random().
52+
* Crypto API is available in modern browsers on secure contexts (HTTPS).
53+
*
54+
* @param {Uint8Array} array - The array to fill with random values.
55+
*/
56+
function getRandomValues(array: Uint8Array): void {
57+
if (self?.crypto?.getRandomValues) {
58+
self.crypto.getRandomValues(array)
59+
return
60+
}
61+
62+
let len = array.length
63+
while (len--) {
64+
array[len] = Math.floor(Math.random() * 256)
65+
}
66+
}

dist/519-519.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/519-519.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/5792-5792.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/5792-5792.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files_sharing-files_sharing_tab.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files_sharing-files_sharing_tab.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files_sharing-init.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/files_sharing-init.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)