microsoft-authentication-library-for-js icon indicating copy to clipboard operation
microsoft-authentication-library-for-js copied to clipboard

Unable to use Msal localStorage option on Azure Frontdoor (with WAF) hosted website

Open yurnih opened this issue 1 year ago • 13 comments

Core Library

MSAL.js (@azure/msal-browser)

Core Library Version

4.0.2

Wrapper Library

MSAL Angular (@azure/msal-angular)

Wrapper Library Version

4.0.2

Public or Confidential Client?

Public

Description

When upgrading to MSAL v4 and using localStorage as the backend for caching, MSAL creates a session cookie msal.cache.encryption as described in docs. This cookie stores a JSON string with the following format: {"id":"...uuid...","key":"qQfZ7...oWmXE"}.The contents of this cookie triggers web application firewall rules for sql-injections.

Error Message

No response

MSAL Logs

No response

Network Trace (Preferrably Fiddler)

  • [ ] Sent
  • [ ] Pending

MSAL Configuration

{
  auth: {
    clientId: msal.clientId,
    authority: `https://login.microsoftonline.com/${msal.tenantId}`,
    redirectUri: '/account/login',
    navigateToLoginRequestUrl: true,
  },
  cache: {
    cacheLocation: BrowserCacheLocation.LocalStorage,
  }
}

Relevant Code Snippets

private tryMsalLogin(allowRedirect: boolean = true, background: boolean = false) {
  if (this.msal.instance.getAllAccounts().length === 0) {
    if (!allowRedirect) return;
    return this.loginRedirect();
  }
  this.msal
    .acquireTokenSilent({
      scopes: this.env.appScope,
      account: this.msal.instance.getAllAccounts()[0],
    })
    .subscribe({
      next: (value) => this.setToken(value.accessToken, background),
      error: (_) => {
        if (!allowRedirect) return;
        this.loginRedirect();
      },
    });
}

private loginRedirect(): void {
  this.msal.loginRedirect({
    scopes: ['openid', 'email', 'offline_access', ...this.env.appScope],
  });
}

Reproduction Steps

  1. Create an application with MSAL v4
  2. Deploy behind Azure Front Door with web application firewall rules turned on
  3. Login in application
  4. After login is finish and local storage / cookies are set all following requests on the domain are blocked (Detects MySQL comment-/space-obfuscated injections and backtick termination / Detects basic SQL authentication bypass attempts / Detects classic SQL injection probings)

Expected Behavior

Similar as MSAL v3

Identity Provider

Entra ID (formerly Azure AD) / MSA

Browsers Affected (Select all that apply)

Chrome, Firefox, Edge, Safari, Other

Regression

@azure/msal-browser ^3

yurnih avatar Jan 28 '25 15:01 yurnih

Hello, I can confirm that the issue also occurs on our end. Our SPA applications are hosted behind Azure Front Door, and SQL rules 942370, 942200, and 942000 are being triggered, causing the site to stop serving content.

Image

We temporarily disabled the prevention mode in our WAF policies to better understand how to create a proper exclusion rule. Ideally, this should be addressed within the library itself to prevent these false positives from occurring.

We appreciate your support in resolving this. Can maybe @tnorling help us out?

@yurnih: Have you already implemented a workaround for this?

Best regards, Patrick

plamber avatar Feb 13 '25 04:02 plamber

I'm getting blocked too if I use LocalStorage, any news on this?

adrianmartinm avatar Feb 27 '25 16:02 adrianmartinm

Yep, we are facing the same problem too :(

mitch098 avatar Mar 03 '25 00:03 mitch098

Also happening here with SessionStorage.

praiaLuisPohren avatar Mar 05 '25 22:03 praiaLuisPohren

Create WAF exclusion rule for msal.cache.encryption cookie name.

Hopefully library will be updated in the future to handle this. I'm guessing since there's a workaround it won't be as high of a priority.

alexkolt avatar Mar 09 '25 16:03 alexkolt

We can also confirm that this is an issue when using LocalStorage and MSAL 4.x Switching to SessionStorage fixes the issue

magicintheair avatar Mar 31 '25 12:03 magicintheair

Can confirm this issue using Azure Application Gateway. A bit sad two microsoft products are fighting eachother like this.

dbpieter avatar May 23 '25 09:05 dbpieter

We are facing the same problem too, any proper fix for this?

IbrahimElkhatib avatar Jun 03 '25 14:06 IbrahimElkhatib

Same problem, the issue is quite serious. Creating a WAF exclusion rule is a no go given the amount of apps/environments affected. At the moment we rolled back to MSAL 3.x

An idea for a simple fix in the library: if the value of msal.cache.encryption would be internally base64 encoded before being url encoded, the default WAF rules would have no issue with it.

mezei avatar Jun 04 '25 17:06 mezei

This issue is affecting a huge number of enterprise apps that I am supporting. All of them are using this library and now we see them failing one by one on a daily basis after the update is done.

@tnorling @jo-arroyo any plans to look into it? I can imagine this will affect many enterprise customers very soon!

jeff1985 avatar Jul 25 '25 10:07 jeff1985

I understand the frustration with this WAF conflict between two Microsoft products.

I recommend you switch to sessionStorage as your primary fix. This resolves the WAF conflicts and actually improves your security posture by not persisting tokens across browser sessions. The trade-off is users need to re-authenticate when opening new tabs or after browser restart.

Why this approach works best:

  • More secure than localStorage
  • No WAF policy changes required initially
  • Demonstrates security awareness to your security team

Parallel track: Use this as an opportunity to start conversations with your security team about a controlled WAF exclusion for the msal.cache.encryption cookie. Coming to them with sessionStorage already implemented shows you prioritize security, making future negotiations much easier. If they are against broad cookie exclusion explore separate WAF policy route. It’s a win-win: better security posture plus stronger cross-team relationships for future issues.​​​​​​​​​​​​​​​

alexkolt avatar Jul 25 '25 14:07 alexkolt

This bug also occurs with React applications

brense avatar Oct 23 '25 11:10 brense

And in response to @alexkolt. Switching to sessionStorage does not improve security posture. There is absolutely no difference in vulnerability between sessionStorage and localStorage, since neither changes the expiry time of the auth token. Whether an attacker obtains the token from sessionStorage or localStorage makes no difference.

brense avatar Oct 23 '25 11:10 brense

+1 for a fix that doesn't need WAF exclusions

mikewtw avatar Nov 24 '25 12:11 mikewtw