Skip to content

Commit dbe441d

Browse files
authored
Exclude cancelled requests from in-progress lists (#5016)
Fixes element-hq/element-web#29882 When we ask for the in-progress verification requests, exclude requests that have been cancelled. This means that we don't erroneously tell the user that the new request they are about to create has been cancelled.
1 parent 9f3ca71 commit dbe441d

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

spec/integ/crypto/verification.spec.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,35 @@ describe("verification", () => {
735735
expect(request.cancellingUserId).toEqual("@alice:localhost");
736736
});
737737

738+
it("does not include cancelled requests in the list of requests", async () => {
739+
// Given Alice started a verification request
740+
const [, request] = await Promise.all([
741+
expectSendToDeviceMessage("m.key.verification.request"),
742+
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
743+
]);
744+
const transactionId = request.transactionId!;
745+
746+
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.sas.v1"]));
747+
await waitForVerificationRequestChanged(request);
748+
749+
// Sanity: the request is listed
750+
const requestsBeforeCancel = aliceClient
751+
.getCrypto()!
752+
.getVerificationRequestsToDeviceInProgress(TEST_USER_ID);
753+
754+
expect(requestsBeforeCancel).toHaveLength(1);
755+
756+
// When Alice cancels it
757+
await Promise.all([expectSendToDeviceMessage("m.key.verification.cancel"), request.cancel()]);
758+
759+
// Then it is no longer listed as in progress
760+
const requestsAfterCancel = aliceClient
761+
.getCrypto()!
762+
.getVerificationRequestsToDeviceInProgress(TEST_USER_ID);
763+
764+
expect(requestsAfterCancel).toHaveLength(0);
765+
});
766+
738767
it("can cancel during the SAS phase", async () => {
739768
// have alice initiate a verification. She should send a m.key.verification.request
740769
const [, request] = await Promise.all([
@@ -1072,6 +1101,29 @@ describe("verification", () => {
10721101
).not.toBeDefined();
10731102
});
10741103

1104+
it("ignores cancelled verification requests", async () => {
1105+
// Given a verification request exists
1106+
const event = createVerificationRequestEvent();
1107+
returnRoomMessageFromSync(TEST_ROOM_ID, event);
1108+
1109+
// Wait for the request to be received
1110+
await emitPromise(aliceClient, CryptoEvent.VerificationRequestReceived);
1111+
1112+
const request = aliceClient.getCrypto()!.findVerificationRequestDMInProgress(TEST_ROOM_ID, "@bob:xyz");
1113+
1114+
// When I cancel it
1115+
fetchMock.put("express:/_matrix/client/v3/rooms/:roomId/send/m.key.verification.cancel/:id", {
1116+
event_id: event.event_id,
1117+
});
1118+
await request!.cancel();
1119+
expect(request!.phase).toEqual(VerificationPhase.Cancelled);
1120+
1121+
// Then it is no longer found
1122+
expect(
1123+
aliceClient.getCrypto()!.findVerificationRequestDMInProgress(TEST_ROOM_ID, "@bob:xyz"),
1124+
).not.toBeDefined();
1125+
});
1126+
10751127
it("Plaintext verification request from Bob to Alice", async () => {
10761128
// Add verification request from Bob to Alice in the DM between them
10771129
returnRoomMessageFromSync(TEST_ROOM_ID, createVerificationRequestEvent());

src/rust-crypto/rust-crypto.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
10401040
new RustSdkCryptoJs.UserId(userId),
10411041
);
10421042
return requests
1043-
.filter((request) => request.roomId === undefined)
1043+
.filter((request) => request.roomId === undefined && !request.isCancelled())
10441044
.map((request) => this.makeVerificationRequest(request));
10451045
}
10461046

@@ -1063,7 +1063,7 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, CryptoEventH
10631063
);
10641064

10651065
// Search for the verification request for the given room id
1066-
const request = requests.find((request) => request.roomId?.toString() === roomId);
1066+
const request = requests.find((request) => request.roomId?.toString() === roomId && !request.isCancelled());
10671067

10681068
if (request) {
10691069
return this.makeVerificationRequest(request);

0 commit comments

Comments
 (0)