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 6edf8c2 commit b5fa72e
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/e2ee/RustEngine.ts
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 b5fa72e

Please sign in to comment.