Skip to content

[ACR] az acr: Validate the WWW-Authenticate realm before sending credentials to the registry token endpoint - #34301

Merged
mansoor sarfraz (msarfraz) merged 4 commits into
Azure:devfrom
jasminetMSFT:acr-validate-auth-realm
Nov 10, 2026
Merged

mansoor sarfraz (msarfraz) merged 4 commits into
Azure:devfrom
jasminetMSFT:acr-validate-auth-realm

Conversation

@jasminetMSFT

@jasminetMSFT Jasmine Tang (jasminetMSFT) commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

🤖 PR Validation — ️✔️ All clear

Breaking Changes Tests Suppressions
️✔️ None ️✔️ 130/130 ️✔️ None

Related command

az acr login, az acr check-health, and other az acr commands that get a registry token (for example az acr repository, az acr manifest)

Description

Harden the ACR token flow so registry credentials are only sent to the registry login server over HTTPS:

  • Check the realm in the registry's WWW-Authenticate challenge before requesting a token. It must use HTTPS and match the registry login server (case-insensitive, default port 443 ignored). Otherwise, getting the token fails with the new CONNECTIVITY_UNTRUSTED_REALM_ERROR before an AAD token is requested or any credential is posted.
  • If the realm is untrusted, the command stops. It doesn't fall back to admin credentials or a credential prompt.
  • Always build the /oauth2/exchange and /oauth2/token URLs from the login server instead of the challenge realm.
  • az acr check-health reports the new error.
  • Add unit tests for the realm check and the token flows.

No behavior change for Azure Container Registry, whose challenge realm is always https://<login-server>/oauth2/token.

Testing Guide

  1. Unit tests and static checks:
azdev test acr      # 146 passed, 13 skipped
azdev style acr     # pylint and flake8 passed
azdev linter acr    # passed

The new tests in test_acr_commands_mock.py cover accepted and rejected realms. They check that no AAD token is requested and nothing is posted for an untrusted realm, that an untrusted realm doesn't fall back to admin or prompted credentials, and that token URLs are built from the login server.

  1. Live tests against a standard registry and a DNL registry (<name>-<hash>.azurecr.io). All passed, and every token request went to https://<login-server>/oauth2/*:
az acr login -n <registry> --expose-token                      # AAD token exchange: refresh token returned
az acr repository list -n <registry>                           # AAD exchange + access token: repositories listed
az acr repository list -n <registry> -u <user> -p <password>   # username/password: repositories listed
az acr check-health -n <registry> -y --ignore-errors           # challenge, refresh token and access token checks: OK
  1. ARM lookup unavailable (the registry isn't in the selected subscription). az acr repository list gave the same results:
az acr login -n <registry> --expose-token --subscription <other>              # passed, used <registry>.azurecr.io
az acr login -n <name> --suffix <hash> --expose-token --subscription <other>  # DNL registry: passed, used <name>-<hash>.azurecr.io
az acr login -n <name> --expose-token --subscription <other>                  # DNL registry without --suffix: fails with "Could not connect to the registry login server", same as before
  1. Untrusted realm: a local fake registry returned an http realm or a realm on another host. Both the AAD and the username/password paths failed with CONNECTIVITY_UNTRUSTED_REALM_ERROR, and no credentials were sent.

  2. Docker login, --endpoint and Private Link, from a Linux VM with Docker in the private endpoint's virtual network. <registry> has regional endpoints enabled. Each command ran on Azure CLI 2.92.0 first and then with this change, with the same results:

az acr login -n <registry>                                          # docker login: Login Succeeded
az acr login -n <registry> --endpoint eastus2                       # docker login to <registry>.eastus2.geo.azurecr.io: Login Succeeded
az acr login -n <registry> --endpoint westus                        # expected failure: "Regional endpoint for 'westus' not found"
az acr login -n <private-link-registry> --expose-token              # through the private endpoint: refresh token returned
az acr login -n <private-link-registry>                             # docker login: Login Succeeded
az acr repository list -n <private-link-registry>                   # passed
az acr check-health -n <private-link-registry> -y --ignore-errors   # challenge, refresh token and access token checks: OK

History Notes

[ACR] az acr: Validate the token endpoint before sending credentials


This checklist is used to make sure that common guidelines for a pull request are followed.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the customer-reported Issues that are reported by GitHub users external to the Azure organization. label Oct 29, 2026
@microsoft-github-policy-service

Copy link
Copy Markdown
Contributor

Thank you for your contribution Jasmine Tang (@jasminetMSFT)! We will review the pull request and get back to you soon.

@yonzhan

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).

@jasminetMSFT
Jasmine Tang (jasminetMSFT) marked this pull request as ready for review October 29, 2026 23:15
Copilot AI balanced review requested due to automatic review settings October 29, 2026 23:15

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.

🟡 Changes recommended

The untrusted-realm rejection is caught as a CLIError, allowing login to proceed with admin credentials instead of stopping.

1 open issue
What changed in this PR

This PR hardens ACR authentication by validating challenge realms and deriving token endpoints from the registry login server.

Changes:

  • Require HTTPS realms matching the login server, allowing case differences and the default port.
  • Build token URLs from the login server and report untrusted realms in health checks.
  • Add tests for realm validation, credential flows, and diagnostics.
File Description
src/​azure-cli/​azure/​cli/​command_modules/​acr/​tests/​latest/​test_acr_commands_mock.py Adds realm validation and authentication regression tests.
src/​azure-cli/​azure/​cli/​command_modules/​acr/​check_health.py Handles untrusted-realm diagnostics.
src/​azure-cli/​azure/​cli/​command_modules/​acr/​_errors.py Defines the untrusted token endpoint error.
src/​azure-cli/​azure/​cli/​command_modules/​acr/​_docker_utils.py Validates realms and constructs registry-local token URLs.

Review effort: Balanced

…edentials to the registry token endpoint

Only follow a realm that uses HTTPS and whose host matches the registry login
server, and build the /oauth2/exchange and /oauth2/token URLs from the login
server. Otherwise fail with CONNECTIVITY_UNTRUSTED_REALM_ERROR before any
credential is requested or sent. `az acr check-health` reports the new error.

@lizMSFT Zoey Li (lizMSFT) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you validate that this change preserves login behavior for DNL registries, regional endpoints (--endpoint), Private Link, and the fallback path when ARM lookup is unavailable (including --suffix where applicable)? Please cover both successful logins and expected failures, including rejection of untrusted realms.

Also could you document the scenarios tested, commands used, and results in the PR description, and note any scenarios that couldn’t be validated. Please redact credentials or tokens from the output

@yonzhan

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).

@msarfraz

Copy link
Copy Markdown
Contributor

The fix is sound: the realm is now validated, token URLs are built from the login server, and the admin-fallback leak Copilot raised is closed (d63da94). Two follow-ups before merge:

  1. Apply lizMSFT's two wording suggestions in docker_utils.py (drop the user-info example) and errors.py (the new message text).
  2. Reply on lizMSFT's validation request: list which of these you tested (standard, DNL, Private Link, --suffix) and which you could not, with redacted output. From the code, the CLI always takes the login server from ARM or the --suffix, so these should behave the same, but please confirm.

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.

Jasmine Tang (@jasminetMSFT) please resolve the followup questions so it can be merged

@jasminetMSFT

Copy link
Copy Markdown
Contributor Author

Hi Zoey Li (@lizMSFT) mansoor sarfraz (@msarfraz), thanks for the review. I updated the Testing Guide in the PR description with the commands and results.

I tested all the scenarios in your lists:

  • Standard registry: az acr login (with Docker and with --expose-token), az acr repository list (AAD and admin user) and az acr check-health passed.
  • DNL registry (<name>-<hash>.azurecr.io): the same commands passed.
  • --suffix and the ARM lookup fallback: passed for both registries. The DNL registry needs --suffix <hash>. Without it, the CLI fails with "Could not connect to the registry login server", the same as before.
  • Private Link: from a VM in the private endpoint's virtual network, az acr login (with Docker and with --expose-token), az acr repository list and az acr check-health passed.
  • --endpoint: on a registry with regional endpoints enabled, az acr login --endpoint eastus2 logged in to <registry>.eastus2.geo.azurecr.io. A region without a regional endpoint failed with "Regional endpoint for 'westus' not found", the same as before.
  • Untrusted realm: rejected with CONNECTIVITY_UNTRUSTED_REALM_ERROR, and no credentials were sent. I used a local fake registry for this, because ACR always returns its login server as the realm.

The Docker, Private Link and --endpoint tests ran on Azure CLI 2.92.0 first and then with this change, and the results were the same.

Redacted output:

> az acr login -n <private-link-registry> --debug    (VM in the private endpoint's virtual network)
Sending a HTTP Get request to https://<private-link-registry>.azurecr.io/v2/
Sending a HTTP Post request to https://<private-link-registry>.azurecr.io/oauth2/exchange
Invoking 'docker login --username 00000000-0000-0000-0000-000000000000 --password-stdin <private-link-registry>.azurecr.io'
Login Succeeded

> az acr login -n <registry> --endpoint eastus2 --debug
Sending a HTTP Get request to https://<registry>.azurecr.io/v2/
Sending a HTTP Post request to https://<registry>.azurecr.io/oauth2/exchange
Logging in to regional endpoint: <registry>.eastus2.geo.azurecr.io
Login Succeeded

> az acr check-health -n <dnl-registry> -y --ignore-errors
Challenge endpoint https://<name>-<hash>.azurecr.io/v2/ : OK
Fetch refresh token for registry '<name>-<hash>.azurecr.io' : OK
Fetch access token for registry '<name>-<hash>.azurecr.io' : OK

Could you help take another look if everything looks good?

@lizMSFT

Copy link
Copy Markdown
Member

Jasmine Tang (@jasminetMSFT) Have you verified that this change addresses the scenario described in MSRC case?

@jasminetMSFT

Copy link
Copy Markdown
Contributor Author

Jasmine Tang (Jasmine Tang (@jasminetMSFT)) Have you verified that this change addresses the scenario described in MSRC case?

Yes Zoey Li (@lizMSFT) , I verified it end to end with the CLI and a local test registry, through the AAD, username/password and admin fallback paths. With this change, the CLI stops with CONNECTIVITY_UNTRUSTED_REALM_ERROR and doesn't send any credentials. I added the details to the case.

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.

LGTM

@msarfraz

mansoor sarfraz (msarfraz) commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Zoey Li (@lizMSFT) Please let us know if you have any questions that need to be addressed before merging this PR?

@msarfraz
mansoor sarfraz (msarfraz) merged commit 0c3e716 into Azure:dev Nov 10, 2026
52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AAD act-identity-squad customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants