Skip to content

Use cryptographically secure randomness for OAuth values - #634

Merged
plhery merged 1 commit into
masterfrom
fix/oauth-randomness-631
Aug 4, 2026
Merged

Use cryptographically secure randomness for OAuth values#634
plhery merged 1 commit into
masterfrom
fix/oauth-randomness-631

Conversation

@plhery

@plhery plhery commented Aug 4, 2026

Copy link
Copy Markdown
Owner

Fixes #631.

What changed

  • Replace Math.random() with crypto.randomBytes() for OAuth 1.0a nonces.
  • Replace Math.random() with crypto.randomBytes() for OAuth 2.0 state and PKCE code verifiers.
  • Encode generated values using URL-safe Base64 while preserving the existing 32- and 128-character lengths.
  • Add regression tests that fail if OAuth value generation calls Math.random().

Why

RFC 7636 requires PKCE code verifiers to be generated from a high-entropy cryptographic random source. OAuth 2.0 state values used for CSRF protection must likewise be non-guessable.

For OAuth 1.0a, nonce uniqueness is the principal replay-protection property, but using the same cryptographically secure source is straightforward hardening and avoids weak randomness in security-related values.

@plhery
plhery marked this pull request as ready for review August 4, 2026 16:15
@plhery
plhery merged commit 0e8ccb9 into master Aug 4, 2026
1 check passed
@plhery
plhery deleted the fix/oauth-randomness-631 branch August 4, 2026 16:18
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.

[bug] Math.random() used for OAuth1 and OAuth2 PKCE/state generation (not cryptographically secure)

1 participant