Skip to content

Validate region string format before use in URL construction#921

Merged
bgavrilMS merged 1 commit into
AzureAD:devfrom
bgavrilMS:fix/validate-region-string
Jun 16, 2026
Merged

Validate region string format before use in URL construction#921
bgavrilMS merged 1 commit into
AzureAD:devfrom
bgavrilMS:fix/validate-region-string

Conversation

@bgavrilMS

Copy link
Copy Markdown
Member

Fixes #920

Problem

_detect_region() returns the region string from either the REGION_NAME environment variable or the IMDS endpoint without validating its format. A user-provided azure_region parameter also flows directly into URL construction. If any of these contain unexpected characters (dots, slashes, etc.), the resulting authority URL could be malformed and lead to misdirected requests.

Solution

Added _validate_region(region, source) in msal/region.py which checks against ^[a-z][a-z0-9-]*$ (matching the Azure region naming convention, e.g. eastus, westus2, east-us-2). Invalid regions are logged and treated as None.

Validation is applied:

  • In _detect_region() for both the env var and IMDS paths
  • In _get_regional_authority() for user-provided azure_region values

Tests

Added tests/test_region.py covering valid regions, various invalid patterns (dots, slashes, uppercase, leading digits), and env var integration.

Reference

Add _validate_region() that checks region strings against ^[a-z][a-z0-9-]*$
to ensure they match the Azure region naming convention. Validation is
applied at discovery time (env var, IMDS) and before URL construction for
user-provided azure_region values.

Fixes AzureAD#920

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 25, 2026 13:47
@bgavrilMS bgavrilMS requested a review from a team as a code owner May 25, 2026 13:47

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.

Pull request overview

This PR adds server-side validation for Azure region strings before they are used to construct regional authority URLs, reducing the risk of malformed authority hosts when region values come from environment variables, IMDS, or user input.

Changes:

  • Added _validate_region() in msal/region.py and applied it to region detection paths (env var + IMDS).
  • Validated azure_region/configured region values in _get_regional_authority() before constructing regional hosts.
  • Added tests/test_region.py to cover valid/invalid region formats and env var integration.

Reviewed changes

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

File Description
msal/region.py Introduces _validate_region() and applies it to env/IMDS region discovery.
msal/application.py Validates configured region strings before using them to build regional authority hosts.
tests/test_region.py Adds unit tests for region validation and _detect_region() env var behavior.

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

Comment thread msal/region.py
Comment thread msal/application.py
Comment thread tests/test_region.py
Copilot AI added a commit that referenced this pull request Jun 16, 2026
…abel limit

Addresses review comment on PR #921: the original regex `^[a-z][a-z0-9-]*$`
allowed trailing hyphens (e.g. `eastus-`) and arbitrarily long strings, both of
which produce invalid DNS labels in `{region}.login.microsoft.com` URLs.

Changed to `^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$` which:
- Requires the region to end with an alphanumeric character
- Enforces the 63-character DNS label limit

Also applied the full set of region-validation changes:
- Added `_validate_region()` to msal/region.py
- Wired validation into `_detect_region` and `_detect_region_of_azure_vm`
- Imported and called `_validate_region` from msal/application.py
- Created tests/test_region.py including negative tests for trailing hyphen
  and overly long strings (> 63 chars)
@bgavrilMS bgavrilMS merged commit 422f9e5 into AzureAD:dev Jun 16, 2026
13 checks passed
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.

Validate region string format before use in URL construction

3 participants