fix: replace Math.random() with crypto.getRandomValues() (WAPI-1127)#73
Merged
chakra-guy merged 1 commit intomainfrom Feb 27, 2026
Merged
fix: replace Math.random() with crypto.getRandomValues() (WAPI-1127)#73chakra-guy merged 1 commit intomainfrom
chakra-guy merged 1 commit intomainfrom
Conversation
adonesky1
reviewed
Feb 25, 2026
packages/wallet-client/src/handlers/untrusted-connection-handler.test.ts
Outdated
Show resolved
Hide resolved
adonesky1
previously approved these changes
Feb 25, 2026
4 tasks
fcf6205 to
90bf894
Compare
85e924e to
f27f24d
Compare
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).
b13cab4 to
3c1336b
Compare
wenfix
approved these changes
Feb 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Math.random()withcrypto.getRandomValues()for OTP generation in the untrusted connection handlerglobalThis.cryptois 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.cryptois always available and no runtime fallback is needed.Changes
packages/wallet-client/src/handlers/untrusted-connection-handler.ts: OTP generation usesUint32Array+crypto.getRandomValues()packages/wallet-client/src/handlers/untrusted-connection-handler.test.ts: Tests for OTP generationTest plan
yarn buildpassesyarn test:unitpasses (62/62 tests)Math.random()calls remain in source filesNote
Medium Risk
Changes OTP generation in the untrusted handshake flow; while small, it affects a security-sensitive step and could impact runtime compatibility if
cryptois unavailable.Overview
Updates the untrusted connection handshake to generate 6-digit OTPs using
globalThis.crypto.getRandomValues()instead ofMath.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.