Skip to content

Commit 241099b

Browse files
Gracefully handle old interaction format (#7731)
- Gracefully handle old interaction format
1 parent be3a23b commit 241099b

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "patch",
3+
"comment": "Gracefully handle old interaction format #7731",
4+
"packageName": "@azure/msal-browser",
5+
"email": "kshabelko@microsoft.com",
6+
"dependentChangeType": "patch"
7+
}

lib/msal-browser/src/cache/BrowserCacheManager.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import { CookieStorage } from "./CookieStorage.js";
6565
import { getAccountKeys, getTokenKeys } from "./CacheHelpers.js";
6666
import { EventType } from "../event/EventType.js";
6767
import { EventHandler } from "../event/EventHandler.js";
68+
import { clearHash } from "../utils/BrowserUtils.js";
6869

6970
/**
7071
* This class implements the cache storage interface for MSAL through browser local or session storage.
@@ -1203,7 +1204,18 @@ export class BrowserCacheManager extends CacheManager {
12031204
} | null {
12041205
const key = `${Constants.CACHE_PREFIX}.${TemporaryCacheKeys.INTERACTION_STATUS_KEY}`;
12051206
const value = this.getTemporaryCache(key, false);
1206-
return value ? JSON.parse(value) : null;
1207+
try {
1208+
return value ? JSON.parse(value) : null;
1209+
} catch (e) {
1210+
// Remove interaction and other temp keys if interaction status can't be parsed
1211+
this.logger.error(
1212+
`Cannot parse interaction status. Removing temporary cache items and clearing url hash. Retrying interaction should fix the error`
1213+
);
1214+
this.removeTemporaryItem(key);
1215+
this.resetRequestCache();
1216+
clearHash(window);
1217+
return null;
1218+
}
12071219
}
12081220

12091221
setInteractionInProgress(

lib/msal-browser/test/cache/BrowserCacheManager.spec.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
} from "@azure/msal-common";
4141
import {
4242
BrowserCacheLocation,
43+
INTERACTION_TYPE,
4344
InteractionType,
4445
TemporaryCacheKeys,
4546
} from "../../src/utils/BrowserConstants.js";
@@ -1467,6 +1468,90 @@ describe("BrowserCacheManager tests", () => {
14671468
);
14681469
});
14691470
});
1471+
1472+
describe("interactionInProgress", () => {
1473+
it("handles new format", () => {
1474+
const perfClient = new BrowserPerformanceClient({
1475+
auth: {
1476+
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
1477+
},
1478+
});
1479+
const cacheManager = new BrowserCacheManager(
1480+
TEST_CONFIG.MSAL_CLIENT_ID,
1481+
cacheConfig,
1482+
browserCrypto,
1483+
logger,
1484+
perfClient,
1485+
new EventHandler()
1486+
);
1487+
1488+
cacheManager.setInteractionInProgress(true);
1489+
expect(
1490+
cacheManager.getInteractionInProgress()?.clientId
1491+
).toEqual(TEST_CONFIG.MSAL_CLIENT_ID);
1492+
expect(
1493+
cacheManager.getInteractionInProgress()?.type
1494+
).toEqual(INTERACTION_TYPE.SIGNIN);
1495+
});
1496+
1497+
it("handles old format", () => {
1498+
const perfClient = new BrowserPerformanceClient({
1499+
auth: {
1500+
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
1501+
},
1502+
});
1503+
const cacheManager = new BrowserCacheManager(
1504+
TEST_CONFIG.MSAL_CLIENT_ID,
1505+
cacheConfig,
1506+
browserCrypto,
1507+
logger,
1508+
perfClient,
1509+
new EventHandler()
1510+
);
1511+
1512+
cacheManager.setTemporaryCache(
1513+
`${Constants.CACHE_PREFIX}.${TemporaryCacheKeys.INTERACTION_STATUS_KEY}`,
1514+
TEST_CONFIG.MSAL_CLIENT_ID
1515+
);
1516+
expect(cacheManager.getInteractionInProgress()).toBeNull();
1517+
});
1518+
1519+
it("handles old format and removes temporary artifacts", () => {
1520+
const perfClient = new BrowserPerformanceClient({
1521+
auth: {
1522+
clientId: TEST_CONFIG.MSAL_CLIENT_ID,
1523+
},
1524+
});
1525+
const cacheManager = new BrowserCacheManager(
1526+
TEST_CONFIG.MSAL_CLIENT_ID,
1527+
cacheConfig,
1528+
browserCrypto,
1529+
logger,
1530+
perfClient,
1531+
new EventHandler()
1532+
);
1533+
1534+
cacheManager.setTemporaryCache(
1535+
`${Constants.CACHE_PREFIX}.${TemporaryCacheKeys.INTERACTION_STATUS_KEY}`,
1536+
TEST_CONFIG.MSAL_CLIENT_ID
1537+
);
1538+
// @ts-ignore
1539+
const requestParamKey = cacheManager.generateCacheKey(
1540+
TemporaryCacheKeys.REQUEST_PARAMS
1541+
);
1542+
const requestParamPayload = JSON.stringify({
1543+
correlationId: "test-correlation-id",
1544+
});
1545+
cacheManager.setTemporaryCache(
1546+
requestParamKey,
1547+
requestParamPayload
1548+
);
1549+
expect(cacheManager.getInteractionInProgress()).toBeNull();
1550+
expect(
1551+
cacheManager.getTemporaryCache(requestParamKey)
1552+
).toBeNull();
1553+
});
1554+
});
14701555
});
14711556
});
14721557

0 commit comments

Comments
 (0)