Replace math.random with a crypographically secure rng - #127
Merged
route443 merged 1 commit intoJun 18, 2026
Conversation
route443
requested changes
Jun 10, 2026
route443
left a comment
Contributor
There was a problem hiding this comment.
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."
There was a problem hiding this comment.
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 usesecureRandomHex(32)instead ofMath.random().
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
javorszky
force-pushed
the
fix/update-pkce-hash-computation
branch
from
June 18, 2026 11:29
aaab33e to
9c0d8a5
Compare
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
force-pushed
the
fix/update-pkce-hash-computation
branch
from
June 18, 2026 20:40
9c0d8a5 to
4f7f61e
Compare
6 tasks
This was referenced Jun 23, 2026
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
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.
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.