Skip to content

Commit

Permalink
Catch errors thrown by "decodeURIComponent" (#6226)
Browse files Browse the repository at this point in the history
- Catch errors thrown by "decodeURIComponent"
- Negate state condition check for better readability.
  • Loading branch information
konstantin-msft authored Jul 10, 2023
1 parent 1045be1 commit 251b39a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Catch errors thrown by \"decodeURIComponent\" #6226",
"packageName": "@azure/msal-common",
"email": "[email protected]",
"dependentChangeType": "patch"
}
32 changes: 25 additions & 7 deletions lib/msal-common/src/response/ResponseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,33 @@ export class ResponseHandler {
cryptoObj: ICrypto
): void {
if (!serverResponseHash.state || !cachedState) {
throw !serverResponseHash.state
? ClientAuthError.createStateNotFoundError("Server State")
: ClientAuthError.createStateNotFoundError("Cached State");
throw serverResponseHash.state
? ClientAuthError.createStateNotFoundError("Cached State")
: ClientAuthError.createStateNotFoundError("Server State");
}

if (
decodeURIComponent(serverResponseHash.state) !==
decodeURIComponent(cachedState)
) {
let decodedServerResponseHash: string;
let decodedCachedState: string;

try {
decodedServerResponseHash = decodeURIComponent(serverResponseHash.state);
} catch (e) {
throw ClientAuthError.createInvalidStateError(
serverResponseHash.state,
`Server response hash URI could not be decoded`
);
}

try {
decodedCachedState = decodeURIComponent(cachedState);
} catch (e) {
throw ClientAuthError.createInvalidStateError(
serverResponseHash.state,
`Cached state URI could not be decoded`
);
}

if (decodedServerResponseHash !== decodedCachedState) {
throw ClientAuthError.createStateMismatchError();
}

Expand Down
30 changes: 30 additions & 0 deletions lib/msal-common/test/response/ResponseHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -847,5 +847,35 @@ describe("ResponseHandler.ts", () => {
);
expect(buildClientInfoSpy.notCalled).toBe(true);
});

it("throws invalid state error", (done) => {
const testServerCodeResponse: ServerAuthorizationCodeResponse = {
code: "testCode",
client_info: TEST_DATA_CLIENT_INFO.TEST_RAW_CLIENT_INFO,
state: TEST_STATE_VALUES.URI_ENCODED_LIB_STATE,
};

const responseHandler = new ResponseHandler(
"this-is-a-client-id",
testCacheManager,
cryptoInterface,
logger,
null,
null
);

try {
responseHandler.validateServerAuthorizationCodeResponse(
testServerCodeResponse,
'dummy-state-%20%%%30%%%%%40',
cryptoInterface
);
} catch (e) {
expect(e).toBeInstanceOf(ClientAuthError);
const err = e as ClientAuthError;
expect(err.message).toContain(`Cached state URI could not be decoded`);
done();
}
});
});
});

0 comments on commit 251b39a

Please sign in to comment.