Skip to content

fix(msal-common): use proper 2-arg comparator in getAccountInfoFilteredBy sort#8477

Open
rickygao wants to merge 4 commits intoAzureAD:devfrom
rickygao:fix/stable-sort-account-priority
Open

fix(msal-common): use proper 2-arg comparator in getAccountInfoFilteredBy sort#8477
rickygao wants to merge 4 commits intoAzureAD:devfrom
rickygao:fix/stable-sort-account-priority

Conversation

@rickygao
Copy link
Copy Markdown

Problem

getAccountInfoFilteredBy() and the e2e test sample use Array.prototype.sort() with a single-argument comparator, which is incorrect. sort() expects two arguments and a return value of negative/zero/positive. The 1-arg form never returns 0, breaking stable sort guarantees.

Impact: After cross-tenant acquireTokenSilent, both home and guest tenant profiles have cached ID tokens. getAccount({ homeAccountId }) may return the guest profile instead of the expected home profile.

Fix

Replace 1-arg comparators with proper 2-arg comparators that return 0 for equal priority, preserving insertion order.

Changes

File Change
lib/msal-common/src/cache/CacheManager.ts Fix sort comparator in getAccountInfoFilteredBy
lib/msal-common/test/cache/CacheManager.spec.ts Add test verifying stable sort when both accounts have idTokenClaims
samples/.../customizable-e2e-test/auth.js Fix same 1-arg sort pattern for home tenant priority

@rickygao rickygao requested a review from a team as a code owner March 26, 2026 10:09
Copilot AI review requested due to automatic review settings March 26, 2026 10:09
Copy link
Copy Markdown
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

Fixes account selection in msal-common when multiple tenant profiles exist by correcting Array.prototype.sort comparator usage so equal-priority items return 0 and preserve insertion order (stable sort behavior).

Changes:

  • Update CacheManager.getAccountInfoFilteredBy to use a proper 2-arg comparator when prioritizing accounts with idTokenClaims.
  • Add a unit test asserting insertion order is preserved when multiple matching accounts have idTokenClaims.
  • Fix the same 1-arg sort comparator pattern in a Vanilla JS E2E sample to prefer the home tenant deterministically.

Reviewed changes

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

File Description
samples/msal-browser-samples/VanillaJSTestApp2.0/app/customizable-e2e-test/auth.js Fixes incorrect 1-arg comparator so home-tenant prioritization is deterministic and stable.
lib/msal-common/test/cache/CacheManager.spec.ts Adds coverage for stable ordering when multiple accounts have idTokenClaims.
lib/msal-common/src/cache/CacheManager.ts Fixes comparator in getAccountInfoFilteredBy to correctly return negative/zero/positive with 2 args.
change/@azure-msal-common-b843b148-a865-4af5-adbe-be8d29b8f313.json Adds a changefile for the msal-common patch release.

@rickygao rickygao force-pushed the fix/stable-sort-account-priority branch 2 times, most recently from 23953b2 to b7de07a Compare March 26, 2026 10:42
@rickygao
Copy link
Copy Markdown
Author

rickygao commented Mar 27, 2026

Hey @hectormmg and @tnorling, this issue is misuse of Array.prototype.sort, also causes unexpected behaviors in multi-tenant scenario, can you help review? Thanks!

@rickygao rickygao force-pushed the fix/stable-sort-account-priority branch from b7de07a to 04faa9f Compare March 27, 2026 08:54
Copy link
Copy Markdown
Member

@hectormmg hectormmg left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

…edBy sort

The previous sort used a single-argument comparator ((account) => ...),
which is incorrect for Array.prototype.sort(). A comparator must take two
arguments and return negative, zero, or positive. The 1-arg version never
returns 0, breaking stable sort guarantees and causing unpredictable
account priority when multiple accounts share the same idTokenClaims
presence (e.g. after cross-tenant token acquisition both home and guest
profiles have cached ID tokens).

Replace with a proper 2-arg comparator that returns 0 when both accounts
have (or both lack) idTokenClaims, preserving insertion order.
@rickygao rickygao force-pushed the fix/stable-sort-account-priority branch from 04faa9f to 3c8966a Compare March 28, 2026 01:35
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