Skip to content

Sanitize authorization code response payloads consistently across Node clients - #8959

Merged
Konstantin (konstantin-msft) merged 12 commits into
devfrom
sanitize_auth_payload
Nov 11, 2026
Merged

Konstantin (konstantin-msft) merged 12 commits into
devfrom
sanitize_auth_payload

Conversation

@konstantin-msft

Copy link
Copy Markdown
Collaborator

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:

  • Added strict validation for region names to ensure they are single DNS labels (≤63 characters, starting with a letter, only letters/digits/hyphens, not ending with a hyphen); invalid regions are now rejected and logged, preventing their use in endpoint resolution and token acquisition. (RegionDiscovery.ts, Authority.ts, regional-authorities.md) [1] [2] [3] [4] [5]
  • Improved authority URL parsing and validation to reject ambiguous or unsafe authorities (e.g., those containing control characters, backslashes, or invalid hostnames) before extracting host information. (UrlString.ts) [1] [2]

Authorization Code Handling:

  • Authorization code response payloads are now sanitized consistently across all Node clients, ensuring that only valid and expected data is processed. (@azure-msal-node, request.md) [1] [2]
  • The use of cloud_instance_host_name during authorization code redemption is now gated by the instanceAware configuration, preventing unintended authority changes. (AuthorizationCodeClient.ts)

Testing Improvements:

  • Added and updated unit tests to verify region and authority validation, including cases with invalid/ambiguous inputs and to ensure non-authority URL content is handled correctly. (Authority.spec.ts, AuthorizationCodeClient.spec.ts, UrlString.spec.ts, PopTokenGenerator.spec.ts) [1] [2] [3] [4] [5] [6] [7]

Documentation Updates:

  • Updated documentation to clarify region name requirements and the behavior when invalid regions are configured or discovered. (regional-authorities.md)

Change tracking:

  • Added change files for tracking the above updates in the @azure/msal-common and @azure/msal-node packages. [1] [2]

Copilot AI balanced review requested due to automatic review settings November 2, 2026 20:57

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.

🟡 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

Comment thread lib/msal-common/src/url/UrlString.ts Resolved
Enhance host matching logic to include IPv6 support.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

@github-actions github-actions Bot 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.

⚠️ 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.

CC: Bogdan Gavril (@bgavrilMS) Robbie-Microsoft

@konstantin-msft

Copy link
Copy Markdown
Collaborator Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 4 pipeline(s).

export function isValidRegion(region: string): boolean {
return (
typeof region === "string" &&
region.length <= 63 &&

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.

Why 63? Seems kind of random and arbitrary, a comment explaining this would help

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Comment thread lib/msal-common/src/url/UrlString.ts Outdated
const hostMatches =
host === parsedUrl.host ||
host === `${parsedUrl.host}${defaultPort}` ||
(/^\[[0-9a-f:.]+\](?::[0-9]+)?$/i.test(host ?? "") &&

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.

Here as well

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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

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.

🟡 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>
auto-merge was automatically disabled November 10, 2026 18:43

Head branch was pushed to by a user without write access

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.

🟡 Changes recommended

Token redemption uses only the global instance-aware flag, ignoring per-request overrides and causing cloud routing to conflict with authorization URL generation and authority discovery.

2 open issues

Review effort: Balanced

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

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.

🟡 Changes recommended

Redirect redemption drops per-request instance_aware overrides when recreating the code client, potentially routing code redemption to the wrong authority.

2 open issues
1 resolved since last review

Review effort: Balanced

Co-authored-by: konstantin-msft <119362382+konstantin-msft@users.noreply.github.com>

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.

🟢 Approval recommended

The authority-validation, response-sanitization, and instance-aware routing changes have no active review issues.

1 open issue
1 resolved since last review

Review effort: Balanced

@konstantin-msft
Konstantin (konstantin-msft) merged commit 8999653 into dev Nov 11, 2026
85 checks passed
@konstantin-msft
Konstantin (konstantin-msft) deleted the sanitize_auth_payload branch November 11, 2026 18:00
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.

4 participants