[Bug] User Realm Discovery drops the authority's port, so username/password sign-in fails on non-443 authorities

未關閉
#989 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 2 天內回覆

@THEtheChad 已經在處理了。

開始於 2026年11月13日。

  • #990 來自 @THEtheChad —— 未關閉

評估

難度
3/5
預估耗時
1-2 天
新手友好度
25/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
azure, python

研究方向

The bug is in msal/authority.py, where the User Realm Discovery URL is built from Authority.instance (hostname only) instead of netloc, and the _domains_without_user_realm_discovery memo is keyed the same way. Read canonicalize() and the user realm lookup, then reproduce with a local HTTPS server on a non-443 port. Done when the userrealm request goes to the authority's port and tests cover a non-default port. Note that PR #990 is already open against this issue.

由索引模型根據 Issue 內容生成。

描述

Describe the bug
acquire_token_by_username_password() ignores the port of an authority on a non-default port when it does User Realm Discovery. With authority="https://localhost:8443/organizations", tenant discovery correctly goes to https://localhost:8443/organizations/v2.0/.well-known/openid-configuration, but the user realm request goes to https://localhost/common/userrealm/{username}?api-version=1.0, which is port 443.

#484 fixed the same thing for tenant discovery, for an Azure CLI custom cloud whose activeDirectory endpoint is https://localhost:3001 (Azure/azure-cli#22553). User Realm Discovery still builds its URL from Authority.instance, which canonicalize() sets to authority.hostname, without the port:

https://github.com/AzureAD/microsoft-authentication-library-for-python/blob/e86a1aa/msal/authority.py#L191-L200

So in that same custom-cloud setup, az login --use-device-code and az login --service-principal work, but az login -u USER -p PASSWORD dials port 443 instead.

To Reproduce
Any OIDC-compliant authority served on a port other than 443 works. Here it is a local Entra-compatible server behind HTTPS on 8443:

import msal
app = msal.PublicClientApplication(
    "04b07795-8ddb-461a-bbee-02f9e1bf7b46",
    authority="https://localhost:8443/organizations",
    instance_discovery=False)
app.acquire_token_by_username_password(
    "[email protected]", "password",
    scopes=["https://management.core.windows.net//.default"])

Expected behavior
GET https://localhost:8443/common/userrealm/[email protected]?api-version=1.0, then the token request.

What you see instead
The server on 8443 receives only the tenant discovery request. MSAL then sends GET https://localhost/common/userrealm/... to port 443. Depending on the machine, that is refused, hangs (WSL2 with mirrored networking), or reaches whatever else is listening on 443.

The MSAL Python version you are using
1.39.0, and dev at e86a1aa.

Additional context
The fix is to build the URL from the authority's netloc instead of instance. The same applies to the _domains_without_user_realm_discovery memo, so that a 404 from one port doesn't turn off User Realm Discovery for another. instance should stay as it is, because the token cache uses it as the account's environment. I have a PR with tests ready to open.

主要語言
Python
星號
971
分支
223
平均合併
3 天 2 小時
30 天內合併 PR
4

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

AzureAD/microsoft-authentication-library-for-python 的其他 Issue

查看 AzureAD/microsoft-authentication-library-for-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。