Skip to content

Commit 9829236

Browse files
Fix code scanning alert no. 2: Insecure randomness
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
1 parent 833a662 commit 9829236

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

test/utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
'use strict';
22

3+
const crypto = require('crypto');
4+
35
function getRandomString(text){
4-
return text + Math.floor((Math.random() * 100000) + 1);
6+
const randomInt = crypto.randomBytes(4).readUInt32BE(0) % 100000 + 1;
7+
return text + randomInt;
58
}
69

710
function getRandomInt(){
8-
return Math.floor((Math.random() * 100000) + 1);
11+
return crypto.randomBytes(4).readUInt32BE(0) % 100000 + 1;
912
}
1013

1114
function getRandomAmount(){
12-
return ((Math.random() * 1000) + 1).toFixed(2);
15+
const randomFloat = crypto.randomBytes(4).readUInt32BE(0) / 0xFFFFFFFF;
16+
return ((randomFloat * 1000) + 1).toFixed(2);
1317
}
1418

1519
function getDate(){

0 commit comments

Comments
 (0)