Skip to content

Conversation

@hectormmg
Copy link
Member

@hectormmg hectormmg commented Dec 28, 2025

This pull request enhances authority/environment validation and telemetry in the MSAL libraries, improving error handling and diagnostics around authority mismatches and cache environment issues. It introduces stricter checks for empty or undefined environment values, ensures telemetry is recorded with normalized values, and adds comprehensive test coverage for these scenarios.

Authority/environment validation and error handling:

  • Updated AccountEntity and ResponseHandler to throw an invalid_cache_environment error if the environment is empty or consists only of whitespace, not just when undefined. This prevents invalid or ambiguous environment values from being used. [1] [2]
  • Added new unit tests in AccountEntity.spec.ts to cover error scenarios when the environment is empty, undefined, or null, and to verify correct behavior when a valid environment is provided.

Telemetry and instrumentation improvements:

  • Enhanced authority mismatch handling in BaseInteractionClient to record telemetry fields for discoveredAuthority and accountEnvironment, normalizing undefined or empty values for better diagnostics.
  • Extended the PerformanceEvent type to include the new telemetry fields discoveredAuthority and accountEnvironment.
  • Added new tests in BaseInteractionClient.spec.ts to verify that telemetry is correctly recorded with normalized values for various account environment edge cases (undefined, empty string, whitespace).

Changelog updates:

  • Added changelog entries for both @azure/msal-common and @azure/msal-browser documenting the authority mismatch instrumentation work. [1] [2]

Test maintenance:

  • Ensured proper cleanup of mocks in AccountEntity.spec.ts by restoring all mocks after each test, preventing test interference.

Copilot AI review requested due to automatic review settings December 28, 2025 05:16
@hectormmg hectormmg requested a review from a team as a code owner December 28, 2025 05:16
Copy link
Contributor

Copilot AI left a comment

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 enhances authority mismatch handling by improving environment value validation and adding telemetry instrumentation for diagnostic purposes. The changes ensure that both empty strings and undefined values are treated as invalid environments, and provide better visibility into authority mismatch scenarios through performance telemetry.

Key changes:

  • Updated validation in AccountEntity and ResponseHandler to explicitly reject empty string environment values
  • Added telemetry fields (discoveredAuthority and accountEnvironment) to track authority mismatch details
  • Implemented comprehensive test coverage for environment validation error scenarios

Reviewed changes

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

Show a summary per file
File Description
lib/msal-common/test/cache/entities/AccountEntity.spec.ts Adds comprehensive test suite for error scenarios when environment values are empty, undefined, or null
lib/msal-common/src/telemetry/performance/PerformanceEvent.ts Adds optional telemetry fields for discovered authority and account environment values
lib/msal-common/src/response/ResponseHandler.ts Updates validation to explicitly check for empty string environment values
lib/msal-common/src/cache/entities/AccountEntity.ts Updates validation to explicitly check for empty string environment values
lib/msal-browser/src/interaction_client/BaseInteractionClient.ts Adds telemetry instrumentation to log normalized authority values when mismatch occurs

Comment on lines 245 to 251
this.performanceClient.addFields(
{
discoveredAuthority: normalizeValue(discoveredAuthority.canonicalAuthority),
accountEnvironment: normalizeValue(account.environment),
},
this.correlationId
);
Copy link

Copilot AI Dec 28, 2025

Choose a reason for hiding this comment

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

The new telemetry instrumentation in BaseInteractionClient should have test coverage. The existing test file BaseInteractionClient.spec.ts has a test for the authority mismatch scenario (line 243), but it doesn't verify that the telemetry fields are correctly added when the mismatch occurs.

Consider adding assertions to verify that performanceClient.addFields is called with the correct discoveredAuthority and accountEnvironment values, including verification of the normalization behavior for empty strings and undefined values.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

hectormmg and others added 2 commits December 27, 2025 22:07
….json

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…e.json

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

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

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment on lines 966 to 986
it("throws invalidCacheEnvironment when authority.getPreferredCache() returns empty string and no environment provided", () => {
jest.spyOn(Authority.prototype, "getPreferredCache").mockReturnValue("");

const homeAccountId = AccountEntity.generateHomeAccountId(
TEST_DATA_CLIENT_INFO.TEST_RAW_CLIENT_INFO_GUIDS,
AuthorityType.Default,
logger,
cryptoInterface,
idTokenClaims
);

expect(() => {
AccountEntity.createAccount(
{
homeAccountId,
idTokenClaims: idTokenClaims,
},
authority
);
}).toThrow("invalid_cache_environment");
});
Copy link

Copilot AI Dec 30, 2025

Choose a reason for hiding this comment

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

Consider adding a test case for when authority.getPreferredCache() returns a whitespace-only string (e.g., " ") to match the validation logic in AccountEntity.ts which checks env.trim() === "". This would ensure comprehensive coverage of the new validation behavior.

Copilot uses AI. Check for mistakes.
hectormmg and others added 3 commits December 30, 2025 13:46
@hectormmg hectormmg closed this Jan 5, 2026
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.

2 participants