Repository navigation
fix(msal): stop re-probing the secure store on every launch - #3166
Merged
kazo0 merged 1 commit intoAug 26, 2026
Merged
Conversation
MsalCacheHelper.VerifyPersistence() ran on every storage setup, costing a
write, a read and a delete against the platform's secure store per app start.
On macOS it could never be granted once and for all. MSAL's validation
accessor builds its probe entry with a fresh Guid in the keychain service
name:
// MacKeychainAccessor.CreateForPersistenceValidation()
new MacKeychainAccessor(_cacheFilePath + ".test",
_service + Guid.NewGuid(), _account, _logger)
macOS gates keychain access with a per-item ACL, so an entry that never has
the same name twice is a first-ever access every time: the user is prompted,
answers "Always Allow", the entry is deleted at the end of the check, and the
next launch asks again. Three prompts on every start, with no way to make
them stop.
The call could not simply be dropped. It was the only thing reporting a
broken store, because MsalCacheHelper handles the two directions
differently: Storage.ReadData() rethrows, but AfterAccessNotification catches
and logs "Could not write the token cache. Ignoring." Removing the check
would have traded a startup error for silent loss of the refresh token.
Two properties of the accessors make a cheaper equivalent possible. Every
ICacheAccessor.Write ends in FileIOWithRetries.TouchFile(_cacheFilePath), so
the cache file tracks whether a write landed - on macOS too, where the
payload lives in the keychain. And TokenCache.OnAfterAccessAsync invokes the
synchronous callback and then the asynchronous one, so the provider can
attach SetAfterAccessAsync without displacing RegisterCache.
So:
- MsalConfiguration.VerifyCachePersistence (Auto default, Always, Never),
with the decision in MsalStorageDefaults.ShouldVerifyPersistence as a pure
function; the provider supplies cacheAlreadyPersisted from File.Exists.
- ArmWriteVerification / OnAfterCacheAccessAsync confirm the first
state-changing write reached the store and, when it did not, log and reset
_setupStorageTask so the next setup rebuilds - probing even under Auto,
since the file is missing, and taking the
AllowUnprotectedTokenCacheFallback path if the store is genuinely broken.
- The unprotected-file fallback still verifies unconditionally: it only runs
because the protected store already failed, and a plain file carries none
of the cost that justifies skipping.
Steady-state macOS launches now make zero extra secure-store round-trips.
Verified on the net10.0-desktop head of Authentication.MsalExtensionsDemo:
three keychain prompts at startup before, none after, with the keychain
item's mdat still advancing so the cache is demonstrably read and written.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01RhtkcHHpbm6XiDagFY6VaS
|
Tick the box to add this pull request to the merge queue (same as
|
4 of 7 tasks
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.
GitHub Issue (If applicable): closes #
Stacked on
dev/sb/auth-providers-fixes— this fix builds onMsalStorageDefaultsandAllowUnprotectedTokenCacheFallback, neither of which is onmainyet.PR Type
What is the current behavior?
MsalAuthenticationProvider.SetupStorageCorecallsMsalCacheHelper.VerifyPersistence()on every storage setup. The check writes, reads and deletes a probe entry in the platform's secure store, so it costs three round-trips per app start.On macOS it can never be granted once and for all. MSAL's validation accessor builds its probe entry with a fresh
Guidin the keychain service name:macOS gates keychain access with a per-item ACL, so an entry that never has the same name twice is a first-ever access every time. The user is prompted, answers Always Allow, the entry is deleted at the end of the check, and the next launch asks again — three prompts on every single start, with no way to make them stop.
What is the new behavior?
The check runs once per cache location instead of once per launch, and the guarantee it was providing is preserved by a cheaper mechanism.
It could not simply be deleted: it was the only thing reporting a broken store, because
MsalCacheHelperhandles the two directions differently.Storage.ReadData()rethrows, butAfterAccessNotificationcatches and logs"Could not write the token cache. Ignoring."Dropping the check would have traded a startup error for silent loss of the refresh token.Two properties of the accessors make the replacement possible:
ICacheAccessor.Writeends inFileIOWithRetries.TouchFile(_cacheFilePath), so the cache file tracks whether a write landed — on macOS too, where the payload lives in the keychain.TokenCache.OnAfterAccessAsyncinvokes the synchronousTokenCacheCallbackand then the asynchronous one.RegisterCacheuses only the synchronous slots, so the provider can attachSetAfterAccessAsyncwithout displacing it.Hence:
MsalConfiguration.VerifyCachePersistence—Auto(default),Always,Never. The decision lives inMsalStorageDefaults.ShouldVerifyPersistence(mode, cacheAlreadyPersisted)as a pure function; the provider suppliescacheAlreadyPersistedfromFile.Exists.ArmWriteVerification/OnAfterCacheAccessAsync— confirm the first state-changing write reached the store. If it did not, log an error and null_setupStorageTaskso the nextSetupStoragerebuilds; that rebuild sees no cache file, so it probes even underAutoand either recovers or takes theAllowUnprotectedTokenCacheFallbackpath.Steady-state macOS launches now make zero extra secure-store round-trips.
specs/015-msal-cache-persistence-check/spec.mdrecords the design, the IL evidence behind each claim, and the risks (chiefly thatTouchFile-on-write is an MSAL implementation detail rather than a contract; if it ever goes away,Autodegrades to "never probe" and the write watchdog fires loudly rather than silently).Verification
Measured on the
net10.0-desktophead ofUno.Samples/UI/Authentication.MsalExtensionsDemoon macOS 26.5.2:mdatadvancesSecurityAgent(macOS prompt UI) spawnedAlso confirms the change is inert where it should be: the packed
lib/net9.0-browserwasm1.0assembly contains only the new enum, not the provider methods, so the#if !UNO_EXT_MSAL_NOSTORAGEguard keeps it out of the WebAssembly path.PR Checklist
Screenshots Compare Test Runresults.Notes on the checklist:
Given_MsalStorageDefaultsgains 5 cases covering the decision table (Autoboth ways,Always,Never) and pinningAutoas both the configuration default anddefault(MsalCachePersistenceCheck).dotnet testonUno.Extensions.Authentication.MSAL.Tests: 44 passed, 0 failed.MsalCachePersistenceCheck.csis linked into that project alongside the other MSAL sources.HowTo-MsalAuthentication.mddocuments the setting and its table;UpdatingExtensions.mdcarries the behavior-change entry alongside the other MSAL 7.0 changes.Alwaysrestores the previous behavior exactly.🤖 Generated with Claude Code
https://claude.ai/code/session_01RhtkcHHpbm6XiDagFY6VaS