From 895116a5c288ccb8326fdc1cf1b62ec3c65dbd03 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Thu, 8 Dec 2022 00:10:07 -0500 Subject: [PATCH] Query & claim needed keys before encrypting (#270) Ensure that the bot has all keys needed for sharing a room key with recipients before encryping an event in that room. Signed-off-by: Andrew Ferrazzutti --- src/e2ee/RustEngine.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/e2ee/RustEngine.ts b/src/e2ee/RustEngine.ts index 7cbed950..8ab7fecd 100644 --- a/src/e2ee/RustEngine.ts +++ b/src/e2ee/RustEngine.ts @@ -32,10 +32,11 @@ export class RustEngine { public constructor(public readonly machine: OlmMachine, private client: MatrixClient) { } - public async run() { + public async run(...types: RequestType[]) { // Note: we should not be running this until it runs out, so cache the value into a variable const requests = await this.machine.outgoingRequests(); for (const request of requests) { + if (types.length && !types.includes(request.type)) continue; switch (request.type) { case RequestType.KeysUpload: await this.processKeysUploadRequest(request); @@ -106,6 +107,14 @@ export class RustEngine { settings.rotationPeriod = BigInt(encEv.rotationPeriodMs); settings.rotationPeriodMessages = BigInt(encEv.rotationPeriodMessages); + await this.run(RequestType.KeysQuery); + await this.lock.acquire(SYNC_LOCK_NAME, async () => { + const keysClaim = await this.machine.getMissingSessions(members); + if (keysClaim) { + await this.processKeysClaimRequest(keysClaim); + } + }); + await this.lock.acquire(roomId, async () => { const requests = JSON.parse(await this.machine.shareRoomKey(new RoomId(roomId), members, settings)); for (const req of requests) {