Skip to content

Commit

Permalink
Query & claim needed keys before encrypting (turt2live#270)
Browse files Browse the repository at this point in the history
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 <andrewf@element.io>
  • Loading branch information
AndrewFerr committed Dec 9, 2022
1 parent f10d39f commit 5d62511
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/e2ee/RustEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5d62511

Please sign in to comment.