Skip to content

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

Open
hectormmg wants to merge 4 commits intodevfrom
fix/stable-sort-account-priority
Open

fix(msal-common): use proper 2-arg comparator in getAccountInfoFilteredBy sort#8522
hectormmg wants to merge 4 commits intodevfrom
fix/stable-sort-account-priority

Conversation

@hectormmg
Copy link
Copy Markdown
Member

Re-submission of #8477 (originally authored by @rickygao). CI could not run on the forked PR, so this cherry-picks the same commit from the fork onto a branch in the AzureAD org.

Original PR: #8477
Original commit author: Ruijun Gao (@rickygao)


Summary

Fixes the comparator function passed to Array.prototype.sort() in getAccountInfoFilteredBy. JavaScript's sort() requires a comparator that returns a negative number, zero, or positive number — but the existing code returned a boolean. This caused inconsistent ordering across different JS engines.

Changes

  • lib/msal-common/src/cache/CacheManager.ts: Fix comparator to use proper subtraction-based comparison
  • lib/msal-common/test/cache/CacheManager.spec.ts: Add tests covering sort stability
  • samples/: Minor associated sample update

…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.
@hectormmg hectormmg requested a review from a team as a code owner April 9, 2026 20:20
Copilot AI review requested due to automatic review settings April 9, 2026 20:20
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 incorrect Array.prototype.sort() comparator usage (returning boolean/1-arg comparator) to ensure deterministic/stable ordering when selecting an account in multi-tenant scenarios.

Changes:

  • Update getAccountInfoFilteredBy to use a proper 2-argument comparator that returns negative/zero/positive values.
  • Add a unit test verifying the stable ordering behavior when multiple matching accounts have idTokenClaims.
  • Update the associated sample’s account selection sort logic to use a proper 2-argument comparator.

Reviewed changes

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

File Description
samples/msal-browser-samples/VanillaJSTestApp2.0/app/customizable-e2e-test/auth.js Fixes account prioritization sort comparator to be a valid 2-arg comparator.
lib/msal-common/test/cache/CacheManager.spec.ts Adds regression test to ensure stable selection when multiple accounts have idTokenClaims.
lib/msal-common/src/cache/CacheManager.ts Fixes comparator in getAccountInfoFilteredBy to avoid unstable ordering across JS engines.
change/@azure-msal-common-b843b148-a865-4af5-adbe-be8d29b8f313.json Adds required beachball change file (patch) describing the fix.

hectormmg and others added 3 commits April 9, 2026 13:25
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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