Skip to content

MatrixRTC: Refactor | Introduce a new Encryption manager (used with experimental to device transport) #4799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
0f4e370
refactor: New encryption manager BasicEncryptionManager for todevice
BillCarsonFr Apr 10, 2025
b19c7a6
fix: ToDevice transport not setting the sent_ts
BillCarsonFr Apr 11, 2025
6411597
test: BasicEncryptionManager add statistics tests
BillCarsonFr Apr 11, 2025
7e0cf4d
code review
BillCarsonFr Apr 15, 2025
ecf3f82
Merge branch 'develop' into valere/rtc/simple_encryption_manager
BillCarsonFr Apr 16, 2025
c1b9e0f
feat: Encryption manager just reshare on new joiner
BillCarsonFr Apr 17, 2025
8ad79ef
refactor: Rename BasicEncryptionManger to RTCEncryptionManager
BillCarsonFr Apr 17, 2025
ec4c466
fixup: RTC experimental todevice should use new encryption mgr
BillCarsonFr Apr 17, 2025
80bd66d
fixup: use proper logger hierarchy
BillCarsonFr Apr 17, 2025
d79fc58
fixup: RTC rollout first key asap even if no members to send to
BillCarsonFr Apr 17, 2025
8e9af36
fixup: RTC add test for first key use
BillCarsonFr Apr 17, 2025
9b06920
Merge branch 'develop' into valere/rtc/simple_encryption_manager
BillCarsonFr Apr 17, 2025
a9413f9
fixup! emitting outbound key before anyone registered
BillCarsonFr Apr 17, 2025
be3c359
fix: quick patch for transport switch, need test
BillCarsonFr Apr 18, 2025
07af3d9
test: RTC encryption manager, add test for transport switch
BillCarsonFr Apr 22, 2025
5e7043f
Merge branch 'develop' into valere/rtc/simple_encryption_manager
BillCarsonFr May 13, 2025
cf05a8f
post rebase fix
BillCarsonFr May 13, 2025
64cbec1
Merge branch 'develop' into valere/rtc/simple_encryption_manager
BillCarsonFr May 19, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,6 @@
"outputDirectory": "coverage",
"outputName": "jest-sonar-report.xml",
"relativePaths": true
}
},
"packageManager": "[email protected]+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
Comment on lines +132 to 134
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like corepack is enabled.

70 changes: 70 additions & 0 deletions spec/unit/matrixrtc/KeyBuffer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2025 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { KeyBuffer } from "../../../src/matrixrtc/utils.ts";
import { type InboundEncryptionSession } from "../../../src/matrixrtc";

describe("KeyBuffer Test", () => {
it("Should buffer and disambiguate keys by timestamp", () => {
jest.useFakeTimers();

const buffer = new KeyBuffer(1000);

const aKey = fakeInboundSessionWithTimestamp(1000);
const olderKey = fakeInboundSessionWithTimestamp(300);
// Simulate receiving out of order keys

const init = buffer.disambiguate(aKey.participantId, aKey);
expect(init).toEqual(aKey);
// Some time pass
jest.advanceTimersByTime(600);
// Then we receive the most recent key out of order

const key = buffer.disambiguate(aKey.participantId, olderKey);
// this key is older and should be ignored even if received after
expect(key).toBe(null);
});

it("Should clear buffer after ttl", () => {
jest.useFakeTimers();

const buffer = new KeyBuffer(1000);

const aKey = fakeInboundSessionWithTimestamp(1000);
const olderKey = fakeInboundSessionWithTimestamp(300);
// Simulate receiving out of order keys

const init = buffer.disambiguate(aKey.participantId, aKey);
expect(init).toEqual(aKey);

// Similar to previous test but there is too much delay
// We don't want to keep key material for too long
jest.advanceTimersByTime(1200);

const key = buffer.disambiguate(aKey.participantId, olderKey);
// The buffer is cleared so should return this key
expect(key).toBe(olderKey);
});

function fakeInboundSessionWithTimestamp(ts: number): InboundEncryptionSession {
return {
keyIndex: 0,
creationTS: ts,
participantId: "@alice:localhost|ABCDE",
key: new Uint8Array(16),
};
}
});
25 changes: 23 additions & 2 deletions spec/unit/matrixrtc/MatrixRTCSession.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { MatrixRTCSession, MatrixRTCSessionEvent } from "../../../src/matrixrtc/
import { type EncryptionKeysEventContent } from "../../../src/matrixrtc/types";
import { secureRandomString } from "../../../src/randomstring";
import { makeMockEvent, makeMockRoom, makeMockRoomState, membershipTemplate, makeKey } from "./mocks";
import { RTCEncryptionManager } from "../../../src/matrixrtc/RTCEncryptionManager.ts";

const mockFocus = { type: "mock" };

Expand Down Expand Up @@ -878,11 +879,27 @@ describe("MatrixRTCSession", () => {
expect(sendKeySpy).toHaveBeenCalledTimes(1);
// check that we send the key with index 1 even though the send gets delayed when leaving.
// this makes sure we do not use an index that is one too old.
expect(sendKeySpy).toHaveBeenLastCalledWith(expect.any(String), 1, sess.memberships);
expect(sendKeySpy).toHaveBeenLastCalledWith(
expect.any(String),
1,
sess.memberships.map((m) => ({
userId: m.sender,
deviceId: m.deviceId,
membershipTs: m.createdTs(),
})),
);
// fake a condition in which we send another encryption key event.
// this could happen do to someone joining the call.
(sess as unknown as any).encryptionManager.sendEncryptionKeysEvent();
expect(sendKeySpy).toHaveBeenLastCalledWith(expect.any(String), 1, sess.memberships);
expect(sendKeySpy).toHaveBeenLastCalledWith(
expect.any(String),
1,
sess.memberships.map((m) => ({
userId: m.sender,
deviceId: m.deviceId,
membershipTs: m.createdTs(),
})),
);
jest.advanceTimersByTime(7000);

const secondKeysPayload = await keysSentPromise2;
Expand Down Expand Up @@ -996,10 +1013,14 @@ describe("MatrixRTCSession", () => {
useNewMembershipManager: true,
useExperimentalToDeviceTransport: true,
});
sess.onRTCSessionMemberUpdate();

await keySentPromise;

expect(sendToDeviceMock).toHaveBeenCalled();

// Access private to test
expect(sess["encryptionManager"]).toBeInstanceOf(RTCEncryptionManager);
} finally {
jest.useRealTimers();
}
Expand Down
Loading