Skip to content

fix(authentication-azure): derive token scope from hostname, not netloc.#644

Open
peombwa wants to merge 3 commits into
mainfrom
fix/azure-auth-derive-scope-from-hostname
Open

fix(authentication-azure): derive token scope from hostname, not netloc.#644
peombwa wants to merge 3 commits into
mainfrom
fix/azure-auth-derive-scope-from-hostname

Conversation

@peombwa

@peombwa peombwa commented Jun 22, 2026

Copy link
Copy Markdown

Overview

Fixes a correctness bug in AzureIdentityAccessTokenProvider.get_authorization_token() where the default .default OAuth scope was derived from urlparse(uri).netloc instead of .hostname, and was cached on the provider instance.

Behaviors fixed

For a request URI passed to the auth provider:

Input URI Old derived scope New derived scope
https://graph.microsoft.com/v1.0/me https://graph.microsoft.com/.default https://graph.microsoft.com/.default
https://graph.microsoft.com:8443/v1.0/me https://graph.microsoft.com:8443/.default (rejected by Entra ID) https://graph.microsoft.com/.default
https://alice:secret@graph.microsoft.com/v1.0/me https://alice:secret@graph.microsoft.com/.default (creds sent to STS and written to OTel span) https://graph.microsoft.com/.default
Second call to https://graph.microsoft.us/... after a first call to https://graph.microsoft.com/... https://graph.microsoft.com/.default (sticky from first call) https://graph.microsoft.us/.default

Related Issue

Fixes # (issue)

Demo

Optional. Screenshots, curl examples, etc.

Notes

Related to MSRC case.

Testing Instructions

  • How to test this PR
  • Prefer bulleted description
  • Start after checking out this branch
  • Include any setup required, such as bundling scripts, restarting services, etc.
  • Include test case, and expected output

@peombwa peombwa self-assigned this Jun 22, 2026
@peombwa peombwa requested a review from Copilot June 22, 2026 21:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes how AzureIdentityAccessTokenProvider.get_authorization_token() derives the default /.default OAuth scope by basing it on the request host (not netloc) and by avoiding caching the derived scope on the provider instance, preventing incorrect audiences and leakage of userinfo/ports into scopes and telemetry.

Changes:

  • Derive the default scope from urlparse(uri).hostname per call (instead of netloc and caching into self._scopes).
  • Add tests to ensure userinfo/port are stripped and that derived scopes are not reused across different hosts.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
packages/authentication/azure/kiota_authentication_azure/azure_identity_access_token_provider.py Updates default scope derivation to use per-call hostname-based scope instead of cached netloc-derived scope.
packages/authentication/azure/tests/test_azure_identity_access_token_provider.py Adds regression tests for stripping userinfo/port and ensuring derived scopes are not cached across hosts.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread packages/authentication/azure/tests/test_azure_identity_access_token_provider.py Outdated
@peombwa peombwa marked this pull request as ready for review June 22, 2026 22:34
@peombwa peombwa requested a review from a team as a code owner June 22, 2026 22:34
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants