Skip to content

Commit 6a761af

Browse files
authored
Element-R: emit VerificationRequestReceived on incoming request (#3762)
1 parent 6eec2ce commit 6a761af

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

spec/integ/crypto/verification.spec.ts

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -980,12 +980,43 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
980980
});
981981
}
982982

983+
it("Verification request not found", async () => {
984+
// Expect to not find any verification request
985+
const request = aliceClient.getCrypto()!.findVerificationRequestDMInProgress(TEST_ROOM_ID, "@bob:xyz");
986+
expect(request).toBeUndefined();
987+
});
988+
989+
it("ignores old verification requests", async () => {
990+
const eventHandler = jest.fn();
991+
aliceClient.on(CryptoEvent.VerificationRequestReceived, eventHandler);
992+
993+
const verificationRequestEvent = createVerificationRequestEvent();
994+
verificationRequestEvent.origin_server_ts -= 1000000;
995+
returnRoomMessageFromSync(TEST_ROOM_ID, verificationRequestEvent);
996+
997+
await syncPromise(aliceClient);
998+
999+
// make sure the event has arrived
1000+
const room = aliceClient.getRoom(TEST_ROOM_ID)!;
1001+
const matrixEvent = room.getLiveTimeline().getEvents()[0];
1002+
expect(matrixEvent.getId()).toEqual(verificationRequestEvent.event_id);
1003+
1004+
// check that an event has not been raised, and that the request is not found
1005+
expect(eventHandler).not.toHaveBeenCalled();
1006+
expect(
1007+
aliceClient.getCrypto()!.findVerificationRequestDMInProgress(TEST_ROOM_ID, "@bob:xyz"),
1008+
).not.toBeDefined();
1009+
});
1010+
9831011
it("Plaintext verification request from Bob to Alice", async () => {
9841012
// Add verification request from Bob to Alice in the DM between them
9851013
returnRoomMessageFromSync(TEST_ROOM_ID, createVerificationRequestEvent());
9861014

987-
// Wait for the sync response to be processed
988-
await syncPromise(aliceClient);
1015+
// Wait for the request to be received
1016+
const request1 = await emitPromise(aliceClient, CryptoEvent.VerificationRequestReceived);
1017+
expect(request1.roomId).toBe(TEST_ROOM_ID);
1018+
expect(request1.isSelfVerification).toBe(false);
1019+
expect(request1.otherUserId).toBe("@bob:xyz");
9891020

9901021
const request = aliceClient.getCrypto()!.findVerificationRequestDMInProgress(TEST_ROOM_ID, "@bob:xyz");
9911022
// Expect to find the verification request received during the sync
@@ -994,12 +1025,6 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
9941025
expect(request?.otherUserId).toBe("@bob:xyz");
9951026
});
9961027

997-
it("Verification request not found", async () => {
998-
// Expect to not find any verification request
999-
const request = aliceClient.getCrypto()!.findVerificationRequestDMInProgress(TEST_ROOM_ID, "@bob:xyz");
1000-
expect(request).not.toBeDefined();
1001-
});
1002-
10031028
it("Encrypted verification request from Bob to Alice", async () => {
10041029
const p2pSession = await createOlmSession(testOlmAccount, e2eKeyReceiver);
10051030
const groupSession = new Olm.OutboundGroupSession();
@@ -1021,14 +1046,19 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
10211046
await awaitDecryption(matrixEvent);
10221047
expect(matrixEvent.getContent().msgtype).toEqual("m.bad.encrypted");
10231048

1049+
const requestEventPromise = emitPromise(aliceClient, CryptoEvent.VerificationRequestReceived);
1050+
10241051
// Send Bob the room keys
10251052
returnToDeviceMessageFromSync(toDeviceEvent);
10261053

10271054
// advance the clock, because the devicelist likes to sleep for 5ms during key downloads
10281055
await jest.advanceTimersByTimeAsync(10);
10291056

1030-
// Wait for the message to be decrypted
1031-
await awaitDecryption(matrixEvent, { waitOnDecryptionFailure: true });
1057+
// Wait for the request to be decrypted
1058+
const request1 = await requestEventPromise;
1059+
expect(request1.roomId).toBe(TEST_ROOM_ID);
1060+
expect(request1.isSelfVerification).toBe(false);
1061+
expect(request1.otherUserId).toBe("@bob:xyz");
10321062

10331063
const request = aliceClient.getCrypto()!.findVerificationRequestDMInProgress(TEST_ROOM_ID, "@bob:xyz");
10341064
// Expect to find the verification request received during the sync

src/rust-crypto/rust-crypto.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import { keyFromPassphrase } from "../crypto/key_passphrase";
6262
import { encodeRecoveryKey } from "../crypto/recoverykey";
6363
import { crypto } from "../crypto/crypto";
6464
import { isVerificationEvent, RustVerificationRequest, verificationMethodIdentifierToMethod } from "./verification";
65-
import { EventType } from "../@types/event";
65+
import { EventType, MsgType } from "../@types/event";
6666
import { CryptoEvent } from "../crypto";
6767
import { TypedEventEmitter } from "../models/typed-event-emitter";
6868
import { RustBackupCryptoEventMap, RustBackupCryptoEvents, RustBackupDecryptor, RustBackupManager } from "./backup";
@@ -1407,6 +1407,32 @@ export class RustCrypto extends TypedEventEmitter<RustCryptoEvents, RustCryptoEv
14071407
new RustSdkCryptoJs.RoomId(roomId),
14081408
);
14091409

1410+
if (
1411+
event.getType() === EventType.RoomMessage &&
1412+
event.getContent().msgtype === MsgType.KeyVerificationRequest
1413+
) {
1414+
const request: RustSdkCryptoJs.VerificationRequest | undefined = this.olmMachine.getVerificationRequest(
1415+
new RustSdkCryptoJs.UserId(event.getSender()!),
1416+
event.getId()!,
1417+
);
1418+
1419+
if (!request) {
1420+
// There are multiple reasons this can happen; probably the most likely is that the event is too old.
1421+
logger.info(
1422+
`Ignoring just-received verification request ${event.getId()} which did not start a rust-side verification`,
1423+
);
1424+
} else {
1425+
this.emit(
1426+
CryptoEvent.VerificationRequestReceived,
1427+
new RustVerificationRequest(
1428+
request,
1429+
this.outgoingRequestProcessor,
1430+
this._supportedVerificationMethods,
1431+
),
1432+
);
1433+
}
1434+
}
1435+
14101436
// that may have caused us to queue up outgoing requests, so make sure we send them.
14111437
this.outgoingRequestLoop();
14121438
}

0 commit comments

Comments
 (0)