Skip to content

Commit 3dd55e4

Browse files
committed
fix indexing take two
- use correct value for: `onEncryptionKeysChanged` - only update `latestGeneratedKeyIndex` for "this user" key
1 parent 598a31c commit 3dd55e4

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/matrixrtc/EncryptionManager.ts

+20-3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ export class EncryptionManager implements IEncryptionManager {
8080
// if it looks like a membership has been updated.
8181
private lastMembershipFingerprints: Set<string> | undefined;
8282

83+
// TODO: remove this value since I think this is not helpful to use
84+
// it represents what index we actually sent over to ElementCall (which we do in a delayed manner)
85+
// but it has no releavnt information for the encryption manager.
8386
private mediaTrailerKeyIndexInUse = -1;
8487
private latestGeneratedKeyIndex = -1;
8588
private joinConfig: EncryptionConfig | undefined;
@@ -267,6 +270,12 @@ export class EncryptionManager implements IEncryptionManager {
267270
}
268271

269272
const keyIndexToSend = indexToSend ?? this.latestGeneratedKeyIndex;
273+
// TODO remove this debug log. it just shows then when sending mediaTrailerKeyIndexInUse contained the wrong index.
274+
logger.debug(
275+
`Compare key in use to last generated key\n`,
276+
`latestGeneratedKeyIndex: ${this.latestGeneratedKeyIndex}\n`,
277+
`mediaTrailerKeyIndexInUse: ${this.mediaTrailerKeyIndexInUse}`,
278+
);
270279
logger.info(
271280
`Try sending encryption keys event. keyIndexToSend=${keyIndexToSend} (method parameter: ${indexToSend})`,
272281
);
@@ -359,7 +368,15 @@ export class EncryptionManager implements IEncryptionManager {
359368
}
360369
}
361370

362-
this.latestGeneratedKeyIndex = encryptionKeyIndex;
371+
if (userId === this.userId && deviceId === this.deviceId) {
372+
// It is important to already update the latestGeneratedKeyIndex here
373+
// NOT IN THE `delayBeforeUse` `setTimeout`.
374+
// Even though this is where we call onEncryptionKeysChanged and set the key in EC (and livekit).
375+
// It needs to happen here because we will send the key before the timeout has passed and sending
376+
// the key will use latestGeneratedKeyIndex as the index. if we update it in the `setTimeout` callback
377+
// it will use the wrong index (index - 1)!
378+
this.latestGeneratedKeyIndex = encryptionKeyIndex;
379+
}
363380
participantKeys[encryptionKeyIndex] = {
364381
key: keyBin,
365382
timestamp,
@@ -372,14 +389,14 @@ export class EncryptionManager implements IEncryptionManager {
372389
if (userId === this.userId && deviceId === this.deviceId) {
373390
this.mediaTrailerKeyIndexInUse = encryptionKeyIndex;
374391
}
375-
this.onEncryptionKeysChanged(keyBin, this.mediaTrailerKeyIndexInUse, participantId);
392+
this.onEncryptionKeysChanged(keyBin, encryptionKeyIndex, participantId);
376393
}, this.useKeyDelay);
377394
this.setNewKeyTimeouts.add(useKeyTimeout);
378395
} else {
379396
if (userId === this.userId && deviceId === this.deviceId) {
380397
this.mediaTrailerKeyIndexInUse = encryptionKeyIndex;
381398
}
382-
this.onEncryptionKeysChanged(keyBin, this.mediaTrailerKeyIndexInUse, participantId);
399+
this.onEncryptionKeysChanged(keyBin, encryptionKeyIndex, participantId);
383400
}
384401
}
385402

0 commit comments

Comments
 (0)