Skip to content

Replace math.random with a crypographically secure rng - #127

Merged
route443 merged 1 commit into
nginxinc:mainfrom
javorszky:fix/update-pkce-hash-computation
Jun 18, 2026
Merged

Replace math.random with a crypographically secure rng#127
route443 merged 1 commit into
nginxinc:mainfrom
javorszky:fix/update-pkce-hash-computation

Conversation

@javorszky

@javorszky javorszky commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Use CSPRNG base64url for PKCE secrets

Replace the HMAC/hash-wrapped Math.random() PKCE values with
crypto.getRandomValues via a small randomB64url() helper. Hashing
does not add entropy to a CSPRNG, and the 32 bytes from
getRandomValues are a ready-to-use secret, so createHmac/createHash
are dropped for the verifier and state (pkce_id). The code_challenge
remains BASE64URL(SHA256(code_verifier)) as required by PKCE S256.

Adjusts the test assertion for code_verifier from 64-hex to
43-char base64url to match the new format.

@route443 route443 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hi @javorszky , Thank you for the hardening, however, there are a few points that could be improved.
Also, take a look at the commit, I'd choose something like "Use a CSPRNG for the PKCE verifier and state."

Comment thread openid_connect.js Outdated
Comment thread openid_connect.js Outdated
Comment thread openid_connect.js Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR replaces non-cryptographic randomness (Math.random()) used in PKCE/state generation with a cryptographically secure RNG intended to be backed by WebCrypto.

Changes:

  • Added a secureRandomHex(byteLen) helper to generate random hex strings using a CSPRNG.
  • Updated PKCE verifier and state (pkce_id) generation to use secureRandomHex(32) instead of Math.random().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread openid_connect.js Outdated
@javorszky
javorszky force-pushed the fix/update-pkce-hash-computation branch from aaab33e to 9c0d8a5 Compare June 18, 2026 11:29
@javorszky
javorszky requested a review from Copilot June 18, 2026 11:30

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread openid_connect.js
Math.random() is not a CSPRNG and its output is predictable. That made the
PKCE code_verifier and the state value guessable.

Derive both from the WebCrypto CSPRNG. The verifier is now 32 random bytes,
base64url-encoded. The previous HMAC/SHA-256 wrapping added no entropy and
is dropped.
@route443
route443 force-pushed the fix/update-pkce-hash-computation branch from 9c0d8a5 to 4f7f61e Compare June 18, 2026 20:40
@route443
route443 merged commit 90ec58a into nginxinc:main Jun 18, 2026
1 check passed
pull Bot pushed a commit to Stars1233/kubernetes-ingress that referenced this pull request Jun 23, 2026
Replace the HMAC/hash-wrapped Math.random() PKCE values with
crypto.getRandomValues via a small randomB64url() helper. Hashing
does not add entropy to a CSPRNG, and the 32 bytes from
getRandomValues are a ready-to-use secret, so createHmac/createHash
are dropped for the verifier and state (pkce_id). The code_challenge
remains BASE64URL(SHA256(code_verifier)) as required by PKCE S256.

Adjusts the test assertion for code_verifier from 64-hex to
43-char base64url to match the new format.

Porting nginxinc/nginx-openid-connect#127
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