Skip to content

Commit

Permalink
Fix MatrixRTC sender key wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
hughns committed Oct 2, 2024
1 parent 9f40f32 commit 23ca391
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 6 deletions.
42 changes: 42 additions & 0 deletions spec/unit/matrixrtc/MatrixRTCSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,48 @@ describe("MatrixRTCSession", () => {
}
});

it("Wraps key index around to 0 when it reaches the maximum", async () => {
// this should give us keys with index [0...255, 0, 1]
const membersToTest = 258;
const members: CallMembershipData[] = [];
for (let i = 0; i < membersToTest; i++) {
members.push(Object.assign({}, membershipTemplate, { device_id: `DEVICE${i}` }));
}
jest.useFakeTimers();
try {
// start with a single member
const mockRoom = makeMockRoom(members.slice(0, 1));

for (let i = 0; i < membersToTest; i++) {
const keysSentPromise = new Promise<EncryptionKeysEventContent>((resolve) => {
sendEventMock.mockImplementation((_roomId, _evType, payload) => resolve(payload));
});

if (i === 0) {
// if first time around then set up the session
sess = MatrixRTCSession.roomSessionForRoom(client, mockRoom);
sess.joinRoomSession([mockFocus], mockFocus, { manageMediaKeys: true });
} else {
// otherwise update the state
mockRoom.getLiveTimeline().getState = jest
.fn()
.mockReturnValue(makeMockRoomState(members.slice(0, i + 1), mockRoom.roomId));
}

sess!.onMembershipUpdate();

// advance time to avoid key throttling
jest.advanceTimersByTime(10000);

const keysPayload = await keysSentPromise;
expect(keysPayload.keys).toHaveLength(1);
expect(keysPayload.keys[0].index).toEqual(i % 256);
}
} finally {
jest.useRealTimers();
}
});

it("Doesn't re-send key immediately", async () => {
const realSetTimeout = setTimeout;
jest.useFakeTimers();
Expand Down
11 changes: 5 additions & 6 deletions src/matrixrtc/MatrixRTCSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,13 +448,12 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
}

private getNewEncryptionKeyIndex(): number {
const userId = this.client.getUserId();
const deviceId = this.client.getDeviceId();

if (!userId) throw new Error("No userId!");
if (!deviceId) throw new Error("No deviceId!");
if (this.currentEncryptionKeyIndex === -1) {
return 0;
}

return (this.getKeysForParticipantInternal(userId, deviceId)?.length ?? 0) % 16;
// maximum key index is 255
return (this.currentEncryptionKeyIndex + 1) % 256;
}

/**
Expand Down

0 comments on commit 23ca391

Please sign in to comment.