Skip to content

Commit

Permalink
Returned MatrixRTC encryption key is undefined is not known
Browse files Browse the repository at this point in the history
This changes the return type of some public functions
  • Loading branch information
hughns committed Sep 23, 2024
1 parent dbd7d26 commit 4ae5471
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
3 changes: 3 additions & 0 deletions spec/unit/matrixrtc/MatrixRTCSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,9 @@ describe("MatrixRTCSession", () => {

bobKeys = sess.getKeysForParticipant("@bob:example.org", "bobsphone")!;
expect(bobKeys).toHaveLength(5);
expect(bobKeys[1]).toBeUndefined();
expect(bobKeys[2]).toBeUndefined();
expect(bobKeys[3]).toBeUndefined();
expect(bobKeys[4]).toEqual(Buffer.from("this is the key", "utf-8"));
expect(sess!.statistics.counters.roomEventEncryptionKeysReceived).toEqual(2);
});
Expand Down
19 changes: 14 additions & 5 deletions src/matrixrtc/MatrixRTCSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
private manageMediaKeys = false;
private useLegacyMemberEvents = true;
// userId:deviceId => array of (key, timestamp)
private encryptionKeys = new Map<string, Array<{ key: Uint8Array; timestamp: number }>>();
private encryptionKeys = new Map<string, Array<{ key: Uint8Array; timestamp: number } | undefined>>();
private lastEncryptionKeyUpdateRequest?: number;

// We use this to store the last membership fingerprints we saw, so we can proactively re-send encryption keys
Expand Down Expand Up @@ -412,18 +412,21 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
* @param deviceId the device ID of the participant
* @returns The encryption keys for the given participant, or undefined if they are not known.
*/
public getKeysForParticipant(userId: string, deviceId: string): Array<Uint8Array> | undefined {
return this.encryptionKeys.get(getParticipantId(userId, deviceId))?.map((entry) => entry.key);
public getKeysForParticipant(userId: string, deviceId: string): Array<Uint8Array | undefined> | undefined {
return this.encryptionKeys.get(getParticipantId(userId, deviceId))?.map((entry) => entry?.key);
}

/**
* A map of keys used to encrypt and decrypt (we are using a symmetric
* cipher) given participant's media. This also includes our own key
*/
public getEncryptionKeys(): IterableIterator<[string, Array<Uint8Array>]> {
public getEncryptionKeys(): IterableIterator<[string, Array<Uint8Array | undefined>]> {
// the returned array doesn't contain the timestamps
return Array.from(this.encryptionKeys.entries())
.map(([participantId, keys]): [string, Uint8Array[]] => [participantId, keys.map((k) => k.key)])
.map(([participantId, keys]): [string, (Uint8Array | undefined)[]] => [
participantId,
keys.map((k) => k?.key),
])
.values();
}

Expand Down Expand Up @@ -483,6 +486,7 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
}
}

// n.b. this will extend the array if necessary and fill in the gaps with undefined
participantKeys[encryptionKeyIndex] = {
key: keyBin,
timestamp,
Expand Down Expand Up @@ -582,6 +586,11 @@ export class MatrixRTCSession extends TypedEventEmitter<MatrixRTCSessionEvent, M
const keyIndexToSend = indexToSend ?? this.currentEncryptionKeyIndex;
const keyToSend = myKeys[keyIndexToSend];

if (!keyToSend) {
logger.warn(`Tried to send encryption key at index ${indexToSend} but no key found`);
return;
}

try {
const content: EncryptionKeysEventContent = {
keys: [
Expand Down

0 comments on commit 4ae5471

Please sign in to comment.