Skip to content

fix: replace Math.random() with crypto.getRandomValues() (WAPI-1127)#73

Merged
chakra-guy merged 1 commit intomainfrom
cyfrin/wapi-1127
Feb 27, 2026
Merged

fix: replace Math.random() with crypto.getRandomValues() (WAPI-1127)#73
chakra-guy merged 1 commit intomainfrom
cyfrin/wapi-1127

Conversation

@chakra-guy
Copy link
Collaborator

@chakra-guy chakra-guy commented Feb 25, 2026

Summary

  • Replaces Math.random() with crypto.getRandomValues() for OTP generation in the untrusted connection handler
  • globalThis.crypto is guaranteed available in all target runtimes (Node 20+, all modern browsers, React Native)

Background

The Cyfrin audit flagged Math.random() usage for OTP generation. Math.random() uses a non-cryptographic PRNG and its output is predictable, making OTP values potentially guessable. crypto.getRandomValues() provides cryptographically secure randomness.

Since Node 18 support was dropped in #76 (WAPI-1133), globalThis.crypto is always available and no runtime fallback is needed.

Changes

  • packages/wallet-client/src/handlers/untrusted-connection-handler.ts: OTP generation uses Uint32Array + crypto.getRandomValues()
  • packages/wallet-client/src/handlers/untrusted-connection-handler.test.ts: Tests for OTP generation

Test plan

  • yarn build passes
  • yarn test:unit passes (62/62 tests)
  • Verified zero Math.random() calls remain in source files

Note

Medium Risk
Changes OTP generation in the untrusted handshake flow; while small, it affects a security-sensitive step and could impact runtime compatibility if crypto is unavailable.

Overview
Updates the untrusted connection handshake to generate 6-digit OTPs using globalThis.crypto.getRandomValues() instead of Math.random().

Adds a changelog entry noting the OTP randomness fix.

Written by Cursor Bugbot for commit 3c1336b. This will update automatically on new commits. Configure here.

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

adonesky1
adonesky1 previously approved these changes Feb 25, 2026
Base automatically changed from cyfrin/wapi-1118 to main February 27, 2026 10:38
@chakra-guy chakra-guy dismissed adonesky1’s stale review February 27, 2026 10:38

The base branch was changed.

@chakra-guy chakra-guy force-pushed the cyfrin/wapi-1127 branch 2 times, most recently from 85e924e to f27f24d Compare February 27, 2026 10:42
Math.random() is not cryptographically secure and should never be used
for security-sensitive values like OTP generation. Replace with
crypto.getRandomValues() which is available in all target runtimes
(Node 20+, modern browsers, React Native).
@chakra-guy chakra-guy merged commit 46f8111 into main Feb 27, 2026
12 checks passed
@chakra-guy chakra-guy deleted the cyfrin/wapi-1127 branch February 27, 2026 10:50
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.

3 participants