Skip to content

Commit

Permalink
Query & claim needed keys before encrypting (#270)
Browse files Browse the repository at this point in the history
* Query & claim needed keys before encrypting

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 <[email protected]>

* Change public API signature for RustEngine

* Move KeysQuery into lock for safety

Signed-off-by: Andrew Ferrazzutti <[email protected]>
Co-authored-by: Travis Ralston <[email protected]>
  • Loading branch information
AndrewFerr and turt2live authored Dec 19, 2022
1 parent 38d4cc4 commit c3bf77c
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/e2ee/RustEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@ export class RustEngine {
}

public async run() {
await this.runOnly(); // run everything, but with syntactic sugar
}

private async runOnly(...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 +111,14 @@ export class RustEngine {
settings.rotationPeriod = BigInt(encEv.rotationPeriodMs);
settings.rotationPeriodMessages = BigInt(encEv.rotationPeriodMessages);

await this.lock.acquire(SYNC_LOCK_NAME, async () => {
await this.runOnly(RequestType.KeysQuery);
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 c3bf77c

Please sign in to comment.