Repository navigation
Percent-encode the username in the user realm discovery URL - #994
Open
rumaisaaaaa263 wants to merge 1 commit into
Open
rumaisaaaaa263 wants to merge 1 commit into
rumaisaaaaa263 wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues were identified for the change that percent-encodes usernames in user realm discovery URLs.
No open issues 🎉
What changed in this PR
This PR percent-encodes usernames in user realm discovery URLs, preventing embedded URL delimiters from changing the requested path, query, or fragment. It complements the username escaping in WS-Trust requests from #901.
Changes:
- Encode usernames with
quote(username, safe="@"), preserving ordinary UPN URLs. - Add regression tests for unchanged ordinary usernames and usernames containing path, query, and fragment delimiters.
| File | Description |
|---|---|
| tests/test_authority.py | Adds URL-encoding regression tests for user realm discovery. |
| msal/authority.py | Imports quote with a Python 2 fallback and encodes the username in the discovery URL. |
Review effort: Balanced
This branch has not been deployed
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.
Username is not escaped in the user realm discovery URL
Authority.user_realm_discoveryformats the username straight intohttps://<instance>/common/userrealm/<username>?api-version=1.0, so/,..,?and#in the username change which URL is requested. For examplealice@contoso.com/../bob@fabrikam.comis sent ashttps://login.microsoftonline.com/common/userrealm/bob@fabrikam.com?api-version=1.0(urllib3 collapses the dot segments), so realm discovery answers for fabrikam.com, and if that realm is federated,acquire_token_by_username_passwordposts the WS-Trust username and password to the endpoint from fabrikam's metadata. A#in the username (for example a#EXT#guest UPN) cuts off the rest of the URL, includingapi-version.The username is now percent-encoded with
quote(username, safe="@"), so ordinary UPNs produce the same URL as before. #901 already escapes the same username in the WS-Trust request body. The new tests intests/test_authority.pyfail without the change and pass with it.