Skip to content

Commit baad739

Browse files
committed
refactor: use crypto.randomInt for unbiased random number generation
Replaces manual rejection sampling with the more concise and modern crypto.randomInt() method. This change addresses a persistent CodeQL false positive that flagged the modulo operator, even when used correctly with rejection sampling. Using crypto.randomInt() makes the code cleaner and directly signals the intent of generating a secure, unbiased random integer within a specific range, which satisfies the static analysis tool. Affected methods: - AuthService.issueSudoToken - UsersService.generateVerifyCode - UsersService.generateRandomPassword
1 parent f599086 commit baad739

File tree

2 files changed

+4
-30
lines changed

2 files changed

+4
-30
lines changed

src/auth/auth.service.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,18 +249,8 @@ export class AuthService {
249249

250250
// 添加 0-5 分钟随机偏移
251251
const baseValidSeconds = 15 * 60;
252-
// Use cryptographically secure random number generator with bias-free approach
253-
const targetRange = 300;
254-
const maxValue = 65536; // 2^16 for two bytes
255-
const threshold = maxValue - (maxValue % targetRange);
256-
257-
let randomValue: number;
258-
do {
259-
const randomOffsetBytes = crypto.randomBytes(2);
260-
randomValue = (randomOffsetBytes[0] << 8) | randomOffsetBytes[1];
261-
} while (randomValue >= threshold);
262-
263-
const randomOffset = randomValue % targetRange;
252+
// Use crypto.randomInt for a secure, unbiased random offset from 0 to 299
253+
const randomOffset = crypto.randomInt(300);
264254
const sudoValidSeconds = baseValidSeconds + randomOffset;
265255

266256
const newAuthorization: Authorization = {

src/users/users.service.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,8 @@ export class UsersService {
131131

132132
private generateVerifyCode(): string {
133133
let code: string = '';
134-
const targetRange = 10;
135-
const threshold = 256 - (256 % targetRange); // 250 for base 10
136-
137134
for (let i = 0; i < 6; i++) {
138-
let randomByte: number;
139-
do {
140-
randomByte = crypto.randomBytes(1)[0];
141-
} while (randomByte >= threshold);
142-
143-
code += (randomByte % targetRange).toString();
135+
code += crypto.randomInt(10).toString();
144136
}
145137
return code;
146138
}
@@ -2268,16 +2260,8 @@ export class UsersService {
22682260
const chars =
22692261
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';
22702262
let password = '';
2271-
const targetRange = chars.length;
2272-
const threshold = 256 - (256 % targetRange);
2273-
22742263
for (let i = 0; i < 16; i++) {
2275-
let randomByte: number;
2276-
do {
2277-
randomByte = crypto.randomBytes(1)[0];
2278-
} while (randomByte >= threshold);
2279-
2280-
password += chars.charAt(randomByte % targetRange);
2264+
password += chars.charAt(crypto.randomInt(chars.length));
22812265
}
22822266
return password;
22832267
}

0 commit comments

Comments
 (0)