Skip to content

Commit

Permalink
Use non-deprecated method of re-processing MatrixRTCSession keys (#2646)
Browse files Browse the repository at this point in the history
* Handle case of encryption key for an index to be undefined

As per matrix-org/matrix-js-sdk#4423 the key can be undefined and so we should handle this rather than waiting for SubtleCrypto.importKey() to fail.

* Use release version of matrix-js-sdk

Diff is matrix-org/matrix-js-sdk@baa6d13...v34.7.0

* Use RTCSession. reemitEncryptionKeys()

* Add some test coverage whilst we are here

* Add some test coverage whilst we are here

* Lint
  • Loading branch information
hughns authored Oct 11, 2024
1 parent 8272c54 commit 8a84c6c
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"livekit-client": "^2.5.7",
"lodash": "^4.17.21",
"loglevel": "^1.9.1",
"matrix-js-sdk": "matrix-org/matrix-js-sdk#baa6d135065637c9769c61325c69709d3618f5f1",
"matrix-js-sdk": "^34.7.0",
"matrix-widget-api": "^1.8.2",
"normalize.css": "^8.0.1",
"observable-hooks": "^4.2.3",
Expand Down
72 changes: 72 additions & 0 deletions src/e2ee/matrixKeyProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
*/

import { describe, expect, test, vi } from "vitest";
import {
MatrixRTCSession,
MatrixRTCSessionEvent,
} from "matrix-js-sdk/src/matrixrtc";
import { KeyProviderEvent } from "livekit-client";

import { MatrixKeyProvider } from "./matrixKeyProvider";

function mockRTCSession(): MatrixRTCSession {
return {
on: vi.fn(),
off: vi.fn(),
reemitEncryptionKeys: vi.fn(),
} as unknown as MatrixRTCSession;
}

describe("matrixKeyProvider", () => {
test("initializes", () => {
const keyProvider = new MatrixKeyProvider();
expect(keyProvider).toBeTruthy();
});

test("listens for key requests and emits existing keys", () => {
const keyProvider = new MatrixKeyProvider();

const session = mockRTCSession();

keyProvider.setRTCSession(session);

expect(session.on).toHaveBeenCalledWith(
MatrixRTCSessionEvent.EncryptionKeyChanged,
expect.any(Function),
);
expect(session.off).not.toHaveBeenCalled();
});

test("stops listening when session changes", () => {
const keyProvider = new MatrixKeyProvider();

const session1 = mockRTCSession();
const session2 = mockRTCSession();

keyProvider.setRTCSession(session1);
expect(session1.off).not.toHaveBeenCalled();

keyProvider.setRTCSession(session2);
expect(session1.off).toHaveBeenCalledWith(
MatrixRTCSessionEvent.EncryptionKeyChanged,
expect.any(Function),
);
});

test("emits existing keys", () => {
const keyProvider = new MatrixKeyProvider();
const setKeyListener = vi.fn();
keyProvider.on(KeyProviderEvent.SetKey, setKeyListener);

const session = mockRTCSession();

keyProvider.setRTCSession(session);

expect(session.reemitEncryptionKeys).toHaveBeenCalled();
});
});
11 changes: 2 additions & 9 deletions src/e2ee/matrixKeyProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,8 @@ export class MatrixKeyProvider extends BaseKeyProvider {
);

// The new session could be aware of keys of which the old session wasn't,
// so emit a key changed event.
for (const [
participant,
encryptionKeys,
] of this.rtcSession.getEncryptionKeys()) {
for (const [index, encryptionKey] of encryptionKeys.entries()) {
this.onEncryptionKeyChanged(encryptionKey, index, participant);
}
}
// so emit key changed events
this.rtcSession.reemitEncryptionKeys();
}

private onEncryptionKeyChanged = (
Expand Down
7 changes: 4 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5925,9 +5925,10 @@ [email protected]:
resolved "https://registry.yarnpkg.com/matrix-events-sdk/-/matrix-events-sdk-0.0.1.tgz#c8c38911e2cb29023b0bbac8d6f32e0de2c957dd"
integrity sha512-1QEOsXO+bhyCroIe2/A5OwaxHvBm7EsSQ46DEDn8RBIfQwN5HWBpFvyWWR4QY0KHPPnnJdI99wgRiAl7Ad5qaA==

matrix-js-sdk@matrix-org/matrix-js-sdk#baa6d135065637c9769c61325c69709d3618f5f1:
version "34.6.0"
resolved "https://codeload.github.com/matrix-org/matrix-js-sdk/tar.gz/baa6d135065637c9769c61325c69709d3618f5f1"
matrix-js-sdk@^34.7.0:
version "34.7.0"
resolved "https://registry.yarnpkg.com/matrix-js-sdk/-/matrix-js-sdk-34.7.0.tgz#243e4eacbedd98a1096135a75765756cda910b7b"
integrity sha512-epauE/ZwksDyadm+0vg+g1keRUo600H/b1MzDZbaIrCY9fELzq3fIWctq9IxMQE/EEPe9jjLiNDooGJT5JJ2Ag==
dependencies:
"@babel/runtime" "^7.12.5"
"@matrix-org/matrix-sdk-crypto-wasm" "^9.0.0"
Expand Down

0 comments on commit 8a84c6c

Please sign in to comment.