Skip to content

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

Merged
Peter Ombwa (peombwa) merged 3 commits into
mainfrom
fix/azure-auth-derive-scope-from-hostname
Jun 23, 2026
Merged

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

Conversation

@peombwa

Copy link
Copy Markdown
Contributor

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

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
Peter Ombwa (peombwa) marked this pull request as ready for review June 22, 2026 22:34
@peombwa
Peter Ombwa (peombwa) requested a review from a team as a code owner June 22, 2026 22:34
@github-project-automation github-project-automation Bot moved this to In Progress 🚧 in Kiota Jun 23, 2026
@peombwa
Peter Ombwa (peombwa) merged commit 7d80a66 into main Jun 23, 2026
53 checks passed
@peombwa
Peter Ombwa (peombwa) deleted the fix/azure-auth-derive-scope-from-hostname branch June 23, 2026 20:45
@github-project-automation github-project-automation Bot moved this from In Progress 🚧 to Done ✔️ in Kiota Jun 23, 2026
@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

Archived in project

Development

Successfully merging this pull request may close these issues.

4 participants