Use cryptographically secure randomness for PKCE, state, and nonce generation#894
Open
Use cryptographically secure randomness for PKCE, state, and nonce generation#894
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens OAuth2/OIDC flow security by switching PKCE code_verifier, OAuth2 state, and OIDC nonce generation from random (MT PRNG) to secrets (CSPRNG), and adds unit tests to validate the new behaviors.
Changes:
- Use
secrets.choice()for RFC 7636-compliant PKCEcode_verifiergeneration (supports 43–128 length correctly). - Use
secrets.token_urlsafe()for OAuth2stateand OIDCnonce. - Add tests validating character-set compliance, URL-safety, and basic non-determinism expectations.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
msal/oauth2cli/oauth2.py |
Switch PKCE/state generation to secrets APIs. |
msal/oauth2cli/oidc.py |
Switch nonce generation to secrets.token_urlsafe(). |
tests/test_oidc.py |
Add unit tests covering PKCE/state/nonce generation properties. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
… response_mode Agent-Logs-Url: https://github.com/AzureAD/microsoft-authentication-library-for-python/sessions/20f5cbfd-04ca-45f5-8c71-f8df1d00a01e Co-authored-by: ashok672 <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
gladjohn
approved these changes
Apr 7, 2026
4gust
approved these changes
Apr 7, 2026
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.
secretsCSPRNG for PKCE code_verifier, OAuth2 state, and OIDC nonce generationimport refromtests/test_oidc.pyimport stringfrommsal/oauth2cli/oidc.pytest_pkce_code_verifier_can_have_repeated_characters- remove unnecessary loop (pigeonhole principle guarantees repeat at length 128 with 66-char alphabet)response_mode="form_post"toinitiate_auth_code_flow()calls in test methods to suppressUserWarningspam