-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Race condition in cache initialization causes intermittent token claim loss under slower conditions after migration to v4 #7561
Comments
As per our initialization docs: "The initialize function is asynchronous and must resolve before invoking other MSAL.js APIs." Additionally the docs for the useMsal hook specifically mention not relying on claims returned from this object: "Note: The accounts value returned by useMsal will only update when accounts are added or removed, and will not update when claims are updated. If you need access to updated claims for the current user, use the useAccount hook or call acquireTokenSilent instead." Please use the useAccount hook for account info and ensure the inProgress state is "None" prior to invoking any MSAL APIs. |
Thanks for the quick response @tnorling. I adapted my linked github example accordingly: import {useAccount} from "@azure/msal-react";
import {handleLogout, handleMsalSilentLogin} from "@/auth/msal";
export default function RootPage() {
const account = useAccount();
const handleFetchTokenSilently = async () => {
const result = await handleMsalSilentLogin();
console.log('##### Result of silent login', result);
}
return (
<div className="items-center gap-y-2">
<h2>Active Account</h2>
<div className="justify-center">
<span>{JSON.stringify(account, null, 2)}</span>
</div>
<div className="flex flex-col items-center gap-y-2">
<button className="btn btn-primary" onClick={() => handleLogout()}>Logout</button>
<button className="btn btn-primary" onClick={() => handleFetchTokenSilently()}>Fetch token silently</button>
</div>
</div>
);
} import {Inter} from "next/font/google";
import {MsalAuthenticationTemplate, useMsal} from "@azure/msal-react";
import {InteractionStatus, InteractionType} from "@azure/msal-browser";
import {msalRedirectRequest} from "@/auth/msal";
import RootPage from "@/components/root";
const Loading = () => <div>Loading...</div>
const Error = () => <div>Error...</div>
function Home() {
const redirectRequest = msalRedirectRequest();
const {inProgress} = useMsal();
if (inProgress !== InteractionStatus.None) {
return <div>Loading... current status {inProgress}</div>;
}
return (
<main className="container mx-auto w-full">
<MsalAuthenticationTemplate
interactionType={InteractionType.Redirect}
authenticationRequest={redirectRequest}
loadingComponent={Loading}
errorComponent={Error}
>
<RootPage />
</MsalAuthenticationTemplate>
</main>
);
}
Home.getInitialProps = () => {
return {}
}
export default Home; The
Still the same steps to reproduce as before + click the I also noticed that the event callback for the |
As there is still the Needs: Author Feedback label assigned, is there anything you need from my side ? In the meantime i was playing around a bit more and was even able to reproduce it with the msal-react nextjs sample provided in this repo.
From here msal is unable to recover on it's own. Removing the CPU slowdown refreshing the page or even using Based on that i am pretty sure that not the way we integrated msal cause the issues but a problem directly emerging from the encryption introduced with msal v4. In case you need anything else, feel free to reach out. I am happy to help wherever i can. |
Core Library
MSAL.js (@azure/msal-browser)
Core Library Version
4.2.0
Wrapper Library
MSAL React (@azure/msal-react)
Wrapper Library Version
3.0.4
Public or Confidential Client?
Public
Description
I may have identified a significant race condition in MSAL's cache initialization process that leads to intermittent loss of token claims, particularly under slower processing conditions. The issue manifests when the cache access attempts occur before the initialization process (including decryption) completes fully. This timing-sensitive behavior causes authentication problems that are especially noticeable in Firefox and under constrained CPU conditions.
Error Message
No response
MSAL Logs
Trace Level Logs:
chrome-msal-issue.log
chrome-for-testing-4x-throttle-issue.log
firefox-msal-issue.txt
Network Trace (Preferrably Fiddler)
MSAL Configuration
Relevant Code Snippets
Reproduction Steps
Next.js 14 Sample
Steps to reproduce are listed as part of its
README.md
Expected Behavior
Identity Provider
Entra ID (formerly Azure AD) / MSA
Browsers Affected (Select all that apply)
Firefox, Chrome
Regression
@azure/[email protected] @azure/[email protected]
The text was updated successfully, but these errors were encountered: