From 97f21b6635e1b8faf5bb6308bc7de7aeb0108369 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 9 Mar 2023 09:38:55 +0000 Subject: [PATCH 1/2] Send the outgoing `m.room_key` messages returned by `shareRoomKey` I forgot this in https://github.com/matrix-org/matrix-js-sdk/pull/3122 :(. To be honest, I'm not sure how it ever worked. --- src/rust-crypto/RoomEncryptor.ts | 15 ++++++++++++++- src/rust-crypto/rust-crypto.ts | 8 +++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/rust-crypto/RoomEncryptor.ts b/src/rust-crypto/RoomEncryptor.ts index acd0e9ff033..381ea61786f 100644 --- a/src/rust-crypto/RoomEncryptor.ts +++ b/src/rust-crypto/RoomEncryptor.ts @@ -22,6 +22,7 @@ import { Room } from "../models/room"; import { logger, PrefixedLogger } from "../logger"; import { KeyClaimManager } from "./KeyClaimManager"; import { RoomMember } from "../models/room-member"; +import { OutgoingRequestProcessor } from "./OutgoingRequestProcessor"; /** * RoomEncryptor: responsible for encrypting messages to a given room @@ -38,6 +39,7 @@ export class RoomEncryptor { public constructor( private readonly olmMachine: OlmMachine, private readonly keyClaimManager: KeyClaimManager, + private readonly outgoingRequestProcessor: OutgoingRequestProcessor, private readonly room: Room, private encryptionSettings: IContent, ) { @@ -97,10 +99,21 @@ export class RoomEncryptor { const userList = members.map((u) => new UserId(u.userId)); await this.keyClaimManager.ensureSessionsForUsers(userList); + this.prefixedLogger.debug("Sessions for users are ready; now sharing room key"); + const rustEncryptionSettings = new EncryptionSettings(); /* FIXME historyVisibility, rotation, etc */ - await this.olmMachine.shareRoomKey(new RoomId(this.room.roomId), userList, rustEncryptionSettings); + const shareMessages = await this.olmMachine.shareRoomKey( + new RoomId(this.room.roomId), + userList, + rustEncryptionSettings, + ); + if (shareMessages) { + for (const m of shareMessages) { + await this.outgoingRequestProcessor.makeOutgoingRequest(m); + } + } } /** diff --git a/src/rust-crypto/rust-crypto.ts b/src/rust-crypto/rust-crypto.ts index 2377b8a2177..8b62e9e6c4a 100644 --- a/src/rust-crypto/rust-crypto.ts +++ b/src/rust-crypto/rust-crypto.ts @@ -204,7 +204,13 @@ export class RustCrypto implements CryptoBackend { if (existingEncryptor) { existingEncryptor.onCryptoEvent(config); } else { - this.roomEncryptors[room.roomId] = new RoomEncryptor(this.olmMachine, this.keyClaimManager, room, config); + this.roomEncryptors[room.roomId] = new RoomEncryptor( + this.olmMachine, + this.keyClaimManager, + this.outgoingRequestProcessor, + room, + config, + ); } // start tracking devices for any users already known to be in this room. From f4b83e1a277c25c8cb894b0739b0dec87d8c208d Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 9 Mar 2023 10:51:19 +0000 Subject: [PATCH 2/2] Stop a failed `/keys/claim` request getting stuck Putting the new request inside a `finally` block meant we would never actually transition the promise chain from failure to success. Sticking a no-op `catch` in the chain makes sure that we can recover from an error. --- src/rust-crypto/KeyClaimManager.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/rust-crypto/KeyClaimManager.ts b/src/rust-crypto/KeyClaimManager.ts index 0479bfa43c1..9df8f89daf9 100644 --- a/src/rust-crypto/KeyClaimManager.ts +++ b/src/rust-crypto/KeyClaimManager.ts @@ -54,7 +54,12 @@ export class KeyClaimManager { // The Rust-SDK requires that we only have one getMissingSessions process in flight at once. This little dance // ensures that, by only having one call to ensureSessionsForUsersInner active at once (and making them // queue up in order). - const prom = this.currentClaimPromise.finally(() => this.ensureSessionsForUsersInner(userList)); + const prom = this.currentClaimPromise + .catch(() => { + // any errors in the previous claim will have been reported already, so there is nothing to do here. + // we just throw away the error and start anew. + }) + .then(() => this.ensureSessionsForUsersInner(userList)); this.currentClaimPromise = prom; return prom; }