Skip to content
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

Clear hash only if it contains known properties #4415

Merged
merged 4 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Clear hash only if it contains known response properties #4415",
"packageName": "@azure/msal-browser",
"email": "thomas.norling@microsoft.com",
"dependentChangeType": "patch"
}
7 changes: 4 additions & 3 deletions lib/msal-browser/src/interaction_client/RedirectClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export class RedirectClient extends StandardInteractionClient {
let state: string;
try {
state = this.validateAndExtractStateFromHash(responseHash, InteractionType.Redirect);
BrowserUtils.clearHash(window);
this.logger.verbose("State extracted from hash");
} catch (e) {
this.logger.info(`handleRedirectPromise was unable to extract state due to: ${e}`);
Expand Down Expand Up @@ -166,14 +165,16 @@ export class RedirectClient extends StandardInteractionClient {
this.logger.verbose("getRedirectResponseHash called");
// Get current location hash from window or cache.
const isResponseHash: boolean = UrlString.hashContainsKnownProperties(hash);
const cachedHash = this.browserStorage.getTemporaryCache(TemporaryCacheKeys.URL_HASH, true);
this.browserStorage.removeItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.URL_HASH));

if (isResponseHash) {
BrowserUtils.clearHash(window);
this.logger.verbose("Hash contains known properties, returning response hash");
return hash;
}

const cachedHash = this.browserStorage.getTemporaryCache(TemporaryCacheKeys.URL_HASH, true);
this.browserStorage.removeItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.URL_HASH));

this.logger.verbose("Hash does not contain known properties, returning cached hash");
return cachedHash;
}
Expand Down
18 changes: 18 additions & 0 deletions lib/msal-browser/test/interaction_client/RedirectClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,24 @@ describe("RedirectClient", () => {
});
});

it("Does not clear custom hash if response hash is retrieved from temporary cache", () => {
browserStorage.setInteractionInProgress(true);
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.ORIGIN_URI}`, window.location.href);
window.sessionStorage.setItem(`${Constants.CACHE_PREFIX}.${TEST_CONFIG.MSAL_CLIENT_ID}.${TemporaryCacheKeys.URL_HASH}`, TEST_HASHES.TEST_SUCCESS_CODE_HASH_REDIRECT);

window.location.hash = "testHash";
const clearHashSpy = sinon.spy(BrowserUtils, "clearHash");

sinon.stub(RedirectClient.prototype, <any>"handleHash").callsFake((responseHash) => {
expect(responseHash).toEqual(TEST_HASHES.TEST_SUCCESS_CODE_HASH_REDIRECT);
});

redirectClient.handleRedirectPromise().then(() => {
expect(clearHashSpy.notCalled).toBe(true);
expect(window.location.hash).toEqual("#testHash");
});
});

it("processes hash if navigateToLoginRequestUri is true and loginRequestUrl contains trailing slash", (done) => {
browserStorage.setInteractionInProgress(true);
const loginRequestUrl = window.location.href.endsWith("/") ? window.location.href.slice(0, -1) : window.location.href + "/";
Expand Down