Repository navigation
Sanitize authorization code response payloads consistently across Node clients - #8959
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The host comparison incorrectly rejects valid expanded IPv6 URLs after normalization, causing url_parse_error and affecting PoP resource signing.
1 open issue
What changed in this PR
This PR hardens authentication input handling in MSAL Common and makes authorization code response sanitization consistent across MSAL Node clients.
Changes:
- Validates authority URLs and region names before endpoint resolution.
- Uses request-owned code and nonce values in Node clients and gates cloud-instance routing on
instanceAware. - Adds regression tests, documents the behavior, and supplies package changefiles.
| File | Description |
|---|---|
lib/msal-node/test/client/PublicClientApplication.spec.ts |
Tests response sanitization and network routing. |
lib/msal-node/test/client/ConfidentialClientApplication.spec.ts |
Tests rejection of invalid configured regions. |
lib/msal-node/src/client/ClientApplication.ts |
Sanitizes authorization code payloads centrally. |
lib/msal-node/docs/request.md |
Clarifies payload handling across Node clients. |
lib/msal-node/docs/regional-authorities.md |
Documents region validation and fallback behavior. |
lib/msal-common/test/url/UrlString.spec.ts |
Tests authority validation and URL preservation. |
lib/msal-common/test/crypto/PopTokenGenerator.spec.ts |
Tests PoP URLs with non-authority backslashes. |
lib/msal-common/test/client/AuthorizationCodeClient.spec.ts |
Tests instance-aware routing behavior. |
lib/msal-common/test/authority/Authority.spec.ts |
Tests configured and discovered region validation. |
lib/msal-common/src/url/UrlString.ts |
Rejects ambiguous authority components. |
lib/msal-common/src/client/AuthorizationCodeClient.ts |
Gates cloud-instance routing on configuration. |
lib/msal-common/src/authority/RegionDiscovery.ts |
Validates discovered region names. |
lib/msal-common/src/authority/Authority.ts |
Validates regions before building endpoints. |
change/@azure-msal-node-sanitize-code-response.json |
Records the Node sanitization patch. |
change/@azure-msal-common-validate-authority-inputs.json |
Records the Common validation patch. |
Review effort: Balanced
Enhance host matching logic to include IPv6 support. Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark 'msal-node client-credential Regression Test'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.10.
| Benchmark suite | Current: 6a84164 | Previous: 5fe6737 | Ratio |
|---|---|---|---|
ConfidentialClientApplication#acquireTokenByClientCredential-fromCache-resourceIsFirstItemInTheCache |
393482 ops/sec (±0.83%) |
479063 ops/sec (±0.75%) |
1.22 |
ConfidentialClientApplication#acquireTokenByClientCredential-fromCache-resourceIsLastItemInTheCache |
388747 ops/sec (±0.73%) |
473547 ops/sec (±0.89%) |
1.22 |
This comment was automatically generated by workflow using github-action-benchmark.
|
/azp run |
|
Azure Pipelines: Successfully started running 4 pipeline(s). |
| export function isValidRegion(region: string): boolean { | ||
| return ( | ||
| typeof region === "string" && | ||
| region.length <= 63 && |
There was a problem hiding this comment.
Why 63? Seems kind of random and arbitrary, a comment explaining this would help
There was a problem hiding this comment.
The region becomes a DNS label, whose maximum length is 63 octets under RFC 1035 §2.3.4. Since we allow only ASCII, that equals 63 characters. Added a named constant and a comment explaining the limit.
| const hostMatches = | ||
| host === parsedUrl.host || | ||
| host === `${parsedUrl.host}${defaultPort}` || | ||
| (/^\[[0-9a-f:.]+\](?::[0-9]+)?$/i.test(host ?? "") && |
There was a problem hiding this comment.
Here as well
There was a problem hiding this comment.
Removed this regex instead. The earlier new URL() call already validates IPv6 syntax, so checking for an opening bracket is sufficient to select the IPv6 normalization branch. Added a comment that explains that
There was a problem hiding this comment.
🟡 Changes recommended
The browser’s instance-aware setting is not forwarded to the token client, causing cloud-specific authorization codes to be redeemed against the original authority instead of the routed cloud authority.
1 open issue
1 resolved since last review
Review effort: Balanced
Co-authored-by: konstantin-msft <119362382+konstantin-msft@users.noreply.github.com>
Head branch was pushed to by a user without write access
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: konstantin-msft <119362382+konstantin-msft@users.noreply.github.com>

This pull request introduces comprehensive validation and sanitization improvements to authority and region handling in the MSAL libraries, enhancing security and reliability. The main changes include stricter validation of region names and authority URLs, consistent sanitization of authorization code responses, and improved handling of instance-aware configuration during token acquisition. Extensive new and updated tests ensure these validations are robust, and documentation has been updated to clarify the new requirements.
Validation and Security Enhancements:
RegionDiscovery.ts,Authority.ts,regional-authorities.md) [1] [2] [3] [4] [5]UrlString.ts) [1] [2]Authorization Code Handling:
@azure-msal-node,request.md) [1] [2]cloud_instance_host_nameduring authorization code redemption is now gated by theinstanceAwareconfiguration, preventing unintended authority changes. (AuthorizationCodeClient.ts)Testing Improvements:
Authority.spec.ts,AuthorizationCodeClient.spec.ts,UrlString.spec.ts,PopTokenGenerator.spec.ts) [1] [2] [3] [4] [5] [6] [7]Documentation Updates:
regional-authorities.md)Change tracking:
@azure/msal-commonand@azure/msal-nodepackages. [1] [2]