-
-
Notifications
You must be signed in to change notification settings - Fork 624
MatrixRTC: ToDevice distribution for media stream keys #4785
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0a587e9
MatrixRTC: ToDevice distribution for media stream keys
BillCarsonFr 233fd85
test: Add RTC to device transport test
BillCarsonFr b3b5347
lint
BillCarsonFr 598a31c
fix key indexing
toger5 3dd55e4
fix indexing take two
toger5 96382ca
test: add test for join config `useExperimentalToDeviceTransport`
BillCarsonFr 5c01a2d
update test to fail without the fixed encryption key index
toger5 778db8c
review
toger5 a9aa68d
review (dave)
toger5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
/* | ||
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 { type Mocked } from "jest-mock"; | ||
|
||
import { makeMockEvent, membershipTemplate, mockCallMembership } from "./mocks"; | ||
import { ClientEvent, EventType, type MatrixClient } from "../../../src"; | ||
import { ToDeviceKeyTransport } from "../../../src/matrixrtc/ToDeviceKeyTransport.ts"; | ||
import { getMockClientWithEventEmitter } from "../../test-utils/client.ts"; | ||
import { type Statistics } from "../../../src/matrixrtc"; | ||
import { KeyTransportEvents } from "../../../src/matrixrtc/IKeyTransport.ts"; | ||
import { defer } from "../../../src/utils.ts"; | ||
import { type Logger } from "../../../src/logger.ts"; | ||
|
||
describe("ToDeviceKeyTransport", () => { | ||
const roomId = "!room:id"; | ||
|
||
let mockClient: Mocked<MatrixClient>; | ||
let statistics: Statistics; | ||
let mockLogger: Mocked<Logger>; | ||
let transport: ToDeviceKeyTransport; | ||
|
||
function setup() { | ||
toger5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mockClient = getMockClientWithEventEmitter({ | ||
encryptAndSendToDevice: jest.fn(), | ||
}); | ||
mockLogger = { | ||
debug: jest.fn(), | ||
warn: jest.fn(), | ||
} as unknown as Mocked<Logger>; | ||
statistics = { | ||
counters: { | ||
roomEventEncryptionKeysSent: 0, | ||
roomEventEncryptionKeysReceived: 0, | ||
}, | ||
totals: { | ||
roomEventEncryptionKeysReceivedTotalAge: 0, | ||
}, | ||
}; | ||
|
||
transport = new ToDeviceKeyTransport("@alice:example.org", "MYDEVICE", roomId, mockClient, statistics, { | ||
getChild: jest.fn().mockReturnValue(mockLogger), | ||
} as unknown as Mocked<Logger>); | ||
} | ||
|
||
it("should send my keys on via to device", async () => { | ||
setup(); | ||
|
||
transport.start(); | ||
|
||
const keyBase64Encoded = "ABCDEDF"; | ||
const keyIndex = 2; | ||
await transport.sendKey(keyBase64Encoded, keyIndex, [ | ||
mockCallMembership( | ||
Object.assign({}, membershipTemplate, { device_id: "BOBDEVICE" }), | ||
roomId, | ||
"@bob:example.org", | ||
), | ||
mockCallMembership( | ||
Object.assign({}, membershipTemplate, { device_id: "CARLDEVICE" }), | ||
roomId, | ||
"@carl:example.org", | ||
), | ||
mockCallMembership( | ||
Object.assign({}, membershipTemplate, { device_id: "MATDEVICE" }), | ||
roomId, | ||
"@mat:example.org", | ||
), | ||
]); | ||
|
||
expect(mockClient.encryptAndSendToDevice).toHaveBeenCalledTimes(1); | ||
expect(mockClient.encryptAndSendToDevice).toHaveBeenCalledWith( | ||
"io.element.call.encryption_keys", | ||
[ | ||
{ userId: "@bob:example.org", deviceId: "BOBDEVICE" }, | ||
{ userId: "@carl:example.org", deviceId: "CARLDEVICE" }, | ||
{ userId: "@mat:example.org", deviceId: "MATDEVICE" }, | ||
], | ||
{ | ||
keys: { | ||
index: keyIndex, | ||
key: keyBase64Encoded, | ||
}, | ||
member: { | ||
claimed_device_id: "MYDEVICE", | ||
}, | ||
room_id: roomId, | ||
session: { | ||
application: "m.call", | ||
call_id: "", | ||
scope: "m.room", | ||
}, | ||
}, | ||
); | ||
|
||
expect(statistics.counters.roomEventEncryptionKeysSent).toBe(1); | ||
}); | ||
|
||
it("should emit when a key is received", async () => { | ||
setup(); | ||
|
||
const deferred = defer<{ userId: string; deviceId: string; keyBase64Encoded: string; index: number }>(); | ||
transport.on(KeyTransportEvents.ReceivedKeys, (userId, deviceId, keyBase64Encoded, index, timestamp) => { | ||
deferred.resolve({ userId, deviceId, keyBase64Encoded, index }); | ||
}); | ||
transport.start(); | ||
|
||
const testEncoded = "ABCDEDF"; | ||
const testKeyIndex = 2; | ||
|
||
mockClient.emit( | ||
ClientEvent.ToDeviceEvent, | ||
makeMockEvent(EventType.CallEncryptionKeysPrefix, "@bob:example.org", undefined, { | ||
keys: { | ||
index: testKeyIndex, | ||
key: testEncoded, | ||
}, | ||
member: { | ||
claimed_device_id: "BOBDEVICE", | ||
}, | ||
room_id: roomId, | ||
session: { | ||
application: "m.call", | ||
call_id: "", | ||
scope: "m.room", | ||
}, | ||
}), | ||
); | ||
|
||
const { userId, deviceId, keyBase64Encoded, index } = await deferred.promise; | ||
expect(userId).toBe("@bob:example.org"); | ||
expect(deviceId).toBe("BOBDEVICE"); | ||
expect(keyBase64Encoded).toBe(testEncoded); | ||
expect(index).toBe(testKeyIndex); | ||
|
||
expect(statistics.counters.roomEventEncryptionKeysReceived).toBe(1); | ||
}); | ||
|
||
it("should not sent to ourself", async () => { | ||
setup(); | ||
|
||
const keyBase64Encoded = "ABCDEDF"; | ||
const keyIndex = 2; | ||
await transport.sendKey(keyBase64Encoded, keyIndex, [ | ||
mockCallMembership( | ||
Object.assign({}, membershipTemplate, { device_id: "MYDEVICE" }), | ||
roomId, | ||
"@alice:example.org", | ||
), | ||
]); | ||
|
||
transport.start(); | ||
|
||
expect(mockClient.encryptAndSendToDevice).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it("should warn when there is a room mismatch", () => { | ||
setup(); | ||
|
||
transport.start(); | ||
|
||
const testEncoded = "ABCDEDF"; | ||
const testKeyIndex = 2; | ||
|
||
mockClient.emit( | ||
ClientEvent.ToDeviceEvent, | ||
makeMockEvent(EventType.CallEncryptionKeysPrefix, "@bob:example.org", undefined, { | ||
keys: { | ||
index: testKeyIndex, | ||
key: testEncoded, | ||
}, | ||
member: { | ||
claimed_device_id: "BOBDEVICE", | ||
}, | ||
room_id: "!anotherroom:id", | ||
session: { | ||
application: "m.call", | ||
call_id: "", | ||
scope: "m.room", | ||
}, | ||
}), | ||
); | ||
|
||
expect(mockLogger.warn).toHaveBeenCalledWith("Malformed Event: Mismatch roomId"); | ||
expect(statistics.counters.roomEventEncryptionKeysReceived).toBe(0); | ||
}); | ||
|
||
describe("malformed events", () => { | ||
const MALFORMED_EVENT = [ | ||
{ | ||
keys: {}, | ||
member: { claimed_device_id: "MYDEVICE" }, | ||
room_id: "!room:id", | ||
session: { application: "m.call", call_id: "", scope: "m.room" }, | ||
}, | ||
{ | ||
keys: { index: 0 }, | ||
member: { claimed_device_id: "MYDEVICE" }, | ||
room_id: "!room:id", | ||
session: { application: "m.call", call_id: "", scope: "m.room" }, | ||
}, | ||
{ | ||
keys: { keys: "ABCDEF" }, | ||
member: { claimed_device_id: "MYDEVICE" }, | ||
room_id: "!room:id", | ||
session: { application: "m.call", call_id: "", scope: "m.room" }, | ||
}, | ||
{ | ||
keys: { keys: "ABCDEF", index: 2 }, | ||
room_id: "!room:id", | ||
session: { application: "m.call", call_id: "", scope: "m.room" }, | ||
}, | ||
{ | ||
keys: { keys: "ABCDEF", index: 2 }, | ||
member: {}, | ||
room_id: "!room:id", | ||
session: { application: "m.call", call_id: "", scope: "m.room" }, | ||
}, | ||
{ | ||
keys: { keys: "ABCDEF", index: 2 }, | ||
member: { claimed_device_id: "MYDEVICE" }, | ||
session: { application: "m.call", call_id: "", scope: "m.room" }, | ||
}, | ||
{ | ||
keys: { keys: "ABCDEF", index: 2 }, | ||
member: { claimed_device_id: "MYDEVICE" }, | ||
room_id: "!room:id", | ||
session: { application: "m.call", call_id: "", scope: "m.room" }, | ||
}, | ||
]; | ||
|
||
test.each(MALFORMED_EVENT)("should warn on malformed event %j", (event) => { | ||
setup(); | ||
|
||
transport.start(); | ||
|
||
mockClient.emit( | ||
ClientEvent.ToDeviceEvent, | ||
makeMockEvent(EventType.CallEncryptionKeysPrefix, "@bob:example.org", undefined, event), | ||
); | ||
|
||
expect(mockLogger.warn).toHaveBeenCalled(); | ||
expect(statistics.counters.roomEventEncryptionKeysReceived).toBe(0); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.