Skip to content

Commit

Permalink
Track users when joining encrypted rooms (#251)
Browse files Browse the repository at this point in the history
* Track users when joining encrypted rooms

Users are only tracked when the bot user is already in the room when
encryption is enabled or if the member status of the user changes in an
already encrypted room.
This leads to megolm sessions which don't include all devices when a bot
joins an encrypted room and existing users can't decrypt messages by the
bot.
Before this change, only in rooms created by the bot could all other
users decrypt messages from the bot.
Now already present users will also be tracked and receive the session
keys.

Signed-off-by: Fabian Möller <[email protected]>

* Add test & adjust logic slightly

Signed-off-by: Fabian Möller <[email protected]>
Co-authored-by: Travis Ralston <[email protected]>
Co-authored-by: Travis Ralston <[email protected]>
  • Loading branch information
3 people authored Sep 21, 2022
1 parent c92382f commit 9e744cb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/e2ee/CryptoClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ export class CryptoClient {
*/
public async onRoomJoin(roomId: string) {
await this.roomTracker.onRoomJoin(roomId);
if (await this.isRoomEncrypted(roomId)) {
const members = await this.client.getRoomMembers(roomId, null, ['join', 'invite']);
await this.engine.addTrackedUsers(members.map(e => e.membershipFor));
}
}

/**
Expand Down
40 changes: 40 additions & 0 deletions test/encryption/CryptoClientTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,5 +555,45 @@ describe('CryptoClient', () => {
// end up not running fast enough for our callCount checks.
await Promise.all([prom1, prom2]);
});

it('should update the tracked users when joining a new room', async () => {
// Stub the room tracker
(client.crypto as any).roomTracker.onRoomJoin = () => {};

const targetUserIds = ["@bob:example.org", "@charlie:example.org"];
const prom1 = new Promise<void>(extResolve => {
(client.crypto as any).engine.addTrackedUsers = simple.mock().callFn((uids) => {
expect(uids).toEqual(targetUserIds);
extResolve();
return Promise.resolve();
});
});

const roomId = "!room:example.org";
const prom2 = new Promise<void>(extResolve => {
client.getRoomMembers = simple.mock().callFn((rid, token, memberships) => {
expect(rid).toEqual(roomId);
expect(token).toBeFalsy();
expect(memberships).toEqual(["join", "invite"]);
extResolve();
return Promise.resolve(targetUserIds.map(u => new MembershipEvent({
type: "m.room.member",
state_key: u,
content: { membership: "join" },
sender: u,
})));
});
});

client.crypto.isRoomEncrypted = async (rid) => {
expect(rid).toEqual(roomId);
return true;
};
client.emit("room.join", roomId);

// We do weird promise things because `emit()` is sync and we're using async code, so it can
// end up not running fast enough for our callCount checks.
await Promise.all([prom1, prom2]);
});
});
});

0 comments on commit 9e744cb

Please sign in to comment.