Skip to content

security: replace Math.random with crypto.randomBytes for PKCE - #413

Open
AidanDalyAus wants to merge 1 commit into
TA2k:masterfrom
AidanDalyAus:security/fix-weak-pkce
Open

security: replace Math.random with crypto.randomBytes for PKCE#413
AidanDalyAus wants to merge 1 commit into
TA2k:masterfrom
AidanDalyAus:security/fix-weak-pkce

Conversation

@AidanDalyAus

Copy link
Copy Markdown

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) replaces Math.random() loop
  • getCodeChallengev2(): crypto.randomBytes(32) replaces Math.random() loop
  • getNonce(): crypto.randomBytes(32) replaces SHA256(timestamp)
  • Removed debug logging of auth code and code_verifier

Minimal diff: 1 file, +9/-9 lines. No API changes.

…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>
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