security: replace Math.random with crypto.randomBytes for PKCE - #413
Open
AidanDalyAus wants to merge 1 commit into
Open
security: replace Math.random with crypto.randomBytes for PKCE#413AidanDalyAus wants to merge 1 commit into
AidanDalyAus wants to merge 1 commit into
Conversation
…nonce Math.random() is not cryptographically secure (CWE-330). PKCE code verifiers and nonces generated with Math.random are predictable, weakening the OAuth authorization code flow. This fix: - getCodeChallenge(): crypto.randomBytes(48) instead of Math.random loop - getCodeChallengev2(): crypto.randomBytes(32) instead of Math.random loop - getNonce(): crypto.randomBytes(32) instead of SHA256(timestamp) - Removed debug logging of auth code and code_verifier (CWE-532) Co-Authored-By: Aidan Daly <me@aidandaly.com>
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.
Security Fix
Problem
PKCE code verifiers and OAuth nonces are generated using
Math.random(), which is not cryptographically secure (CWE-330). An attacker who can predict the PRNG state can forge PKCE challenges and bypass the authorization code protection.Additionally, the nonce is
SHA256(Date.now())— deterministic from the timestamp, not random.Auth code and code_verifier are logged at debug level (CWE-532), defeating PKCE protection via log exposure.
Fix
getCodeChallenge():crypto.randomBytes(48)replacesMath.random()loopgetCodeChallengev2():crypto.randomBytes(32)replacesMath.random()loopgetNonce():crypto.randomBytes(32)replacesSHA256(timestamp)Minimal diff: 1 file, +9/-9 lines. No API changes.