Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query & claim needed keys before encrypting #270

Merged
merged 3 commits into from
Dec 19, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to cause a query request to happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, because by the time this is hit, the CryptoClient has updated the list of tracked users in the room, which queues outgoing key query requests in the crypto state machine.

Without this line, the first message a bot sends in an encrypted room with a user it hasn't seen before will be unencryptable by that user.

Otherwise, the SDK calls RustEngine.run (and processes all outgoing requests, including key queries) after having already sent its first (encrypted) message, meaning it won't have the device key of new users & can't send them the to-device message containing the room key.

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