Tighten region regex: reject trailing hyphens and enforce 63-char DNS label limit#928
Closed
Copilot wants to merge 2 commits into
Closed
Tighten region regex: reject trailing hyphens and enforce 63-char DNS label limit#928Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
…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)
Copilot
AI
changed the title
[WIP] Fix code as per review comment
Tighten region regex: reject trailing hyphens and enforce 63-char DNS label limit
Jun 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The region validation regex
^[a-z][a-z0-9-]*$accepted values that produce malformed DNS labels in{region}.login.microsoft.com— specifically, trailing hyphens (e.g.eastus-) and strings exceeding 63 characters.Changes
msal/region.py: Tightens_VALID_REGION_REto require an alphanumeric ending character and cap total length at 63 chars; adds_validate_region()helper used in_detect_regionand_detect_region_of_azure_vmmsal/application.py: Calls_validate_region()onregion_to_usebefore constructing the regional authority URLtests/test_region.py: New test module covering valid/invalid cases, including the previously-unguarded trailing hyphen and >63-char inputs