Skip to content

security: Replace insecure Math.random() with cryptographic alternatives#325

Open
jose-compu wants to merge 1 commit into
different-ai:mainfrom
jose-compu:security/fix-insecure-random-invoice-numbers
Open

security: Replace insecure Math.random() with cryptographic alternatives#325
jose-compu wants to merge 1 commit into
different-ai:mainfrom
jose-compu:security/fix-insecure-random-invoice-numbers

Conversation

@jose-compu

Copy link
Copy Markdown

Summary

Replaces insecure Math.random() usage with cryptographically secure alternatives from Node.js crypto module.

Problem

Math.random() is not cryptographically secure and can produce predictable values. In security-sensitive contexts like generating invoice IDs and email handles, this could enable:

  • Invoice ID enumeration attacks
  • Predictable identifier generation
  • Potential data scraping via sequential guessing

Changes

File Change
packages/web/src/lib/ai-email/invoice-service.ts Replace Math.random() with crypto.randomBytes() for invoice number generation
packages/web/src/lib/ai-email/workspace-mapping.ts Replace Math.random() with crypto.randomInt() for AI email handle fallback

Testing

  • No linter errors
  • Functions maintain same output format (just with secure randomness)
  • randomBytes(3).toString('hex') produces 6-char hex string (same length as before)
  • randomInt(n) produces same range as Math.floor(Math.random() * n)

Security Impact

  • Low-Medium severity fix - Prevents potential invoice enumeration
  • No breaking changes to API contracts

Potential Reviewers: @benjaminshafii
Related: Security audit / code review initiative

- Use crypto.randomBytes for invoice number generation in invoice-service.ts
- Use crypto.randomInt for AI email handle fallback generation in workspace-mapping.ts

Math.random() is not cryptographically secure and can produce predictable values.
For security-sensitive operations like generating invoice IDs and email handles,
cryptographic randomness is required to prevent enumeration attacks.

Fixes potential invoice ID prediction vulnerability.

Related: Review of security best practices
@vercel

vercel Bot commented Jan 21, 2026

Copy link
Copy Markdown

@jose-blockchain is attempting to deploy a commit to the 0 Finance Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant