fix(msal-common): use proper 2-arg comparator in getAccountInfoFilteredBy sort#8477
Open
rickygao wants to merge 4 commits intoAzureAD:devfrom
Open
fix(msal-common): use proper 2-arg comparator in getAccountInfoFilteredBy sort#8477rickygao wants to merge 4 commits intoAzureAD:devfrom
rickygao wants to merge 4 commits intoAzureAD:devfrom
Conversation
Contributor
There was a problem hiding this comment.
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.getAccountInfoFilteredByto use a proper 2-arg comparator when prioritizing accounts withidTokenClaims. - Add a unit test asserting insertion order is preserved when multiple matching accounts have
idTokenClaims. - Fix the same 1-arg
sortcomparator 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. |
change/@azure-msal-common-b843b148-a865-4af5-adbe-be8d29b8f313.json
Outdated
Show resolved
Hide resolved
23953b2 to
b7de07a
Compare
Author
|
Hey @hectormmg and @tnorling, this issue is misuse of |
b7de07a to
04faa9f
Compare
tnorling
approved these changes
Mar 27, 2026
…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.
04faa9f to
3c8966a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
getAccountInfoFilteredBy()and the e2e test sample useArray.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 returns0, 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
0for equal priority, preserving insertion order.Changes
lib/msal-common/src/cache/CacheManager.tsgetAccountInfoFilteredBylib/msal-common/test/cache/CacheManager.spec.tssamples/.../customizable-e2e-test/auth.js