-
Notifications
You must be signed in to change notification settings - Fork 3.4k
fix(security): use CSPRNG for password and OTP generation #3477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,8 +76,9 @@ export function generatePassword(length = 24): string { | |
| const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_-+=' | ||
| let result = '' | ||
|
|
||
| const bytes = randomBytes(length) | ||
| for (let i = 0; i < length; i++) { | ||
| result += chars.charAt(Math.floor(Math.random() * chars.length)) | ||
| result += chars.charAt(bytes[i] % chars.length) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Modulo bias reduces character distribution uniformity
While this is astronomically more secure than for (let i = 0; i < length; i++) {
result += chars.charAt(randomInt(0, chars.length))
}Note that |
||
| } | ||
|
|
||
| return result | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Modulo bias in cryptographic password generation
Medium Severity
bytes[i] % chars.lengthintroduces modulo bias because 256 is not evenly divisible by 76 (the charset length). The first 28 characters have a ~33% higher chance of being selected than the remaining 48 characters, reducing the effective entropy of generated passwords. Ironically, this PR aims to improve cryptographic security but introduces a well-known CSPRNG pitfall.crypto.randomInt(0, chars.length)avoids this by using rejection sampling internally.