Skip to content

fix: keep authority port in user realm discovery - #990

Open
Chad Elliott (THEtheChad) wants to merge 1 commit into
AzureAD:devfrom
THEtheChad:fix/user-realm-discovery-port
Open

Chad Elliott (THEtheChad) wants to merge 1 commit into
AzureAD:devfrom
THEtheChad:fix/user-realm-discovery-port

Conversation

@THEtheChad

Copy link
Copy Markdown

Fixes #989.

User Realm Discovery built its URL from Authority.instance, which canonicalize() sets to the authority's host name without its port. A username/password sign-in against an authority on a non-default port, such as https://localhost:8443/organizations, therefore asked port 443 for the user realm. #484 fixed the same problem for tenant discovery, for Azure CLI custom clouds whose activeDirectory is on another port (Azure/azure-cli#22553). This fixes the one path it didn't cover.

Changes

  • Both authority initializers record the authority's netloc (host plus port, lowercased).
  • user_realm_discovery() builds https://{netloc}/common/userrealm/... from it.
  • The _domains_without_user_realm_discovery memo is keyed by netloc too, so a 404 from host:8443 doesn't turn User Realm Discovery off for host:443.
  • Authority.instance is unchanged. The token cache stores it as the account's environment, so changing it would split existing cache entries.

For an authority on 443, netloc is the host name the URL was built from before, so the request doesn't change.

Tests

TestUserRealmDiscoveryOnNonDefaultPort runs offline: tenant discovery is patched and the HTTP client is a mock. It checks that:

  • the user realm URL keeps a non-default port;
  • the URL is unchanged on the default port;
  • a 404 on one port doesn't turn off discovery on another.

The first and third fail on dev without this change.

pytest tests/test_authority.py tests/test_application.py tests/test_client.py tests/test_token_cache.py
242 passed, 8 skipped

I also checked it end to end. The repro script from #989 was pointed at a local Entra-compatible server on https://localhost:8443. It hangs on MSAL 1.39.0 because the user realm request goes to localhost:443. With this branch it gets a token, and the server logs GET /common/userrealm/alice@contoso.com?api-version=1.0 -> 200 on 8443.

🤖 Generated with Claude Code

User Realm Discovery built its URL from Authority.instance, the
authority's host name without its port. Username/password sign-in
against an authority on a non-default port therefore asked port 443
for the user realm. AzureAD#484 fixed the same thing for tenant discovery.

Build the URL from the authority's netloc instead, and key the memo of
hosts without User Realm Discovery the same way, so that a 404 from one
port doesn't turn it off for another. Authority.instance is unchanged:
the token cache uses it as the account's environment.

Fixes AzureAD#989

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings November 13, 2026 18:59

Copilot AI 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.

🟢 Approval recommended

The authority-port preservation change has no active review issues.

No open issues 🎉

What changed in this PR

Fixes #989 by preserving authority ports during User Realm Discovery, extending the earlier tenant-discovery fix in #484 without changing token-cache account environments.

Changes:

  • Store the lowercased authority host and port in both initializers and use it for user realm requests.
  • Key the discovery-disable cache by host and port so a 404 does not affect other ports.
  • Add offline regression tests for non-default ports, unchanged default-port URLs, and cache isolation.
File Description
tests/​test_authority.py Adds mocked tests for port preservation and discovery-cache isolation.
msal/​authority.py Preserves ports in user realm URLs and cache keys while leaving Authority.instance unchanged.

Review effort: Balanced

@THEtheChad

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@THEtheChad
Chad Elliott (THEtheChad) marked this pull request as ready for review November 13, 2026 19:18
@THEtheChad
Chad Elliott (THEtheChad) requested a review from a team as a code owner November 13, 2026 19:18

This branch has not been deployed

No deployments
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] User Realm Discovery drops the authority's port, so username/password sign-in fails on non-443 authorities

2 participants