Skip to content

Commit a7d0f4b

Browse files
MaxwellCalkinclaude
andcommitted
fix(security): use CSPRNG for password and OTP generation
Replace Math.random() with cryptographically secure alternatives in two security-critical code paths: - generatePassword() in encryption.ts: use randomBytes() (already imported) instead of Math.random() for deployment password generation - generateOTP() in chat OTP route: use crypto.randomInt() instead of Math.random() for authentication code generation Math.random() is not cryptographically secure — its output is predictable and can be reconstructed from a few observed values. Both functions generate authentication material that directly protects user accounts and deployed workflows. This PR was authored by Claude Opus 4.6 (AI), operated by @MaxwellCalkin Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8c0a2e0 commit a7d0f4b

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

apps/sim/app/api/chat/[identifier]/otp/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { randomUUID } from 'crypto'
1+
import { randomInt, randomUUID } from 'crypto'
22
import { db } from '@sim/db'
33
import { chat, verification } from '@sim/db/schema'
44
import { createLogger } from '@sim/logger'
@@ -17,7 +17,7 @@ import { createErrorResponse, createSuccessResponse } from '@/app/api/workflows/
1717
const logger = createLogger('ChatOtpAPI')
1818

1919
function generateOTP() {
20-
return Math.floor(100000 + Math.random() * 900000).toString()
20+
return randomInt(100000, 1000000).toString()
2121
}
2222

2323
const OTP_EXPIRY = 15 * 60 // 15 minutes

apps/sim/lib/core/security/encryption.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ export function generatePassword(length = 24): string {
7676
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_-+='
7777
let result = ''
7878

79+
const bytes = randomBytes(length)
7980
for (let i = 0; i < length; i++) {
80-
result += chars.charAt(Math.floor(Math.random() * chars.length))
81+
result += chars.charAt(bytes[i] % chars.length)
8182
}
8283

8384
return result

0 commit comments

Comments
 (0)