Skip to content

Commit c8992bb

Browse files
authored
refactor(api): use secure RNG, closes #1356 (#1398)
1 parent 4e9d31c commit c8992bb

File tree

4 files changed

+17
-42
lines changed

4 files changed

+17
-42
lines changed

.changes/secure-rng.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"api": patch
3+
---
4+
5+
Use secure RNG on callback function names.

api/src/tauri.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,12 @@ declare global {
77
}
88
}
99

10-
function s4(): string {
11-
return Math.floor((1 + Math.random()) * 0x10000)
12-
.toString(16)
13-
.substring(1)
14-
}
15-
1610
function uid(): string {
17-
return (
18-
s4() +
19-
s4() +
20-
'-' +
21-
s4() +
22-
'-' +
23-
s4() +
24-
'-' +
25-
s4() +
26-
'-' +
27-
s4() +
28-
s4() +
29-
s4()
30-
)
11+
const length = new Int8Array(1)
12+
window.crypto.getRandomValues(length)
13+
const array = new Uint8Array(Math.max(16, Math.abs(length[0])))
14+
window.crypto.getRandomValues(array)
15+
return array.join('')
3116
}
3217

3318
function transformCallback(

tauri/scripts/bundle.js

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

tauri/scripts/core.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,14 @@ if (!String.prototype.startsWith) {
77
}
88

99
(function () {
10-
function s4() {
11-
return Math.floor((1 + Math.random()) * 0x10000)
12-
.toString(16)
13-
.substring(1);
10+
function uid() {
11+
const length = new Int8Array(1)
12+
window.crypto.getRandomValues(length)
13+
const array = new Uint8Array(Math.max(16, Math.abs(length[0])))
14+
window.crypto.getRandomValues(array)
15+
return array.join('')
1416
}
1517

16-
var uid = function () {
17-
return (
18-
s4() +
19-
s4() +
20-
"-" +
21-
s4() +
22-
"-" +
23-
s4() +
24-
"-" +
25-
s4() +
26-
"-" +
27-
s4() +
28-
s4() +
29-
s4()
30-
);
31-
};
32-
3318
function ownKeys(object, enumerableOnly) {
3419
var keys = Object.keys(object);
3520
if (Object.getOwnPropertySymbols) {

0 commit comments

Comments
 (0)