Skip to content

Commit c133bf9

Browse files
committed
feat(auth, users): replace random number generation with cryptographically secure methods
1 parent 3f9c427 commit c133bf9

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/auth/auth.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { Injectable } from '@nestjs/common';
1111
import { JwtService } from '@nestjs/jwt';
1212
import Ajv from 'ajv';
1313
import { readFileSync } from 'fs';
14+
import crypto from 'node:crypto';
1415
import { existsSync } from 'node:fs';
1516
import path from 'node:path';
1617
import {
@@ -248,7 +249,10 @@ export class AuthService {
248249

249250
// 添加 0-5 分钟随机偏移
250251
const baseValidSeconds = 15 * 60;
251-
const randomOffset = Math.floor(Math.random() * 300);
252+
// Use cryptographically secure random number generator
253+
const randomOffsetBytes = crypto.randomBytes(2);
254+
const randomOffset =
255+
((randomOffsetBytes[0] << 8) | randomOffsetBytes[1]) % 300;
252256
const sudoValidSeconds = baseValidSeconds + randomOffset;
253257

254258
const newAuthorization: Authorization = {

src/users/users.service.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { isEmail } from 'class-validator';
3333
import { Request } from 'express';
3434
import Redis from 'ioredis';
3535
import assert from 'node:assert';
36+
import crypto from 'node:crypto';
3637
import { AnswerService } from '../answer/answer.service';
3738
import {
3839
InvalidCredentialsError,
@@ -131,7 +132,8 @@ export class UsersService {
131132
private generateVerifyCode(): string {
132133
let code: string = '';
133134
for (let i = 0; i < 6; i++) {
134-
code += Math.floor(Math.random() * 10).toString()[0];
135+
const randomByte = crypto.randomBytes(1)[0];
136+
code += (randomByte % 10).toString();
135137
}
136138
return code;
137139
}
@@ -1859,7 +1861,7 @@ export class UsersService {
18591861
providerUserId: string,
18601862
): string {
18611863
const timestamp = Date.now();
1862-
const random = Math.random().toString(36).substring(2);
1864+
const random = crypto.randomBytes(8).toString('hex');
18631865
return `oauth_${type}_${providerId}_${providerUserId}_${timestamp}_${random}`;
18641866
}
18651867

@@ -2040,7 +2042,7 @@ export class UsersService {
20402042
userId,
20412043
providerId,
20422044
providerUserId: userInfo.id,
2043-
rawProfile: userInfo,
2045+
rawProfile: userInfo as any,
20442046
},
20452047
});
20462048
}
@@ -2260,7 +2262,8 @@ export class UsersService {
22602262
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*';
22612263
let password = '';
22622264
for (let i = 0; i < 16; i++) {
2263-
password += chars.charAt(Math.floor(Math.random() * chars.length));
2265+
const randomByte = crypto.randomBytes(1)[0];
2266+
password += chars.charAt(randomByte % chars.length);
22642267
}
22652268
return password;
22662269
}

0 commit comments

Comments
 (0)