@@ -16,7 +16,7 @@ limitations under the License.
1616
1717import { type Mocked } from "vitest" ;
1818
19- import { makeMockEvent } from "./mocks.ts" ;
19+ import { makeMatrixEvent } from "./mocks.ts" ;
2020import { ClientEvent , EventType , type MatrixClient } from "../../../src" ;
2121import { ToDeviceKeyTransport } from "../../../src/matrixrtc/ToDeviceKeyTransport.ts" ;
2222import { getMockClientWithEventEmitter } from "../../test-utils/client.ts" ;
@@ -122,24 +122,23 @@ describe("ToDeviceKeyTransport", () => {
122122 const testEncoded = "ABCDEDF" ;
123123 const testKeyIndex = 2 ;
124124
125- mockClient . emit (
126- ClientEvent . ToDeviceEvent ,
127- makeMockEvent ( EventType . CallEncryptionKeysPrefix , "@bob:example.org" , undefined , {
128- keys : {
129- index : testKeyIndex ,
130- key : testEncoded ,
131- } ,
132- member : {
133- claimed_device_id : "BOBDEVICE" ,
134- } ,
135- room_id : roomId ,
136- session : {
137- application : "m.call" ,
138- call_id : "" ,
139- scope : "m.room" ,
140- } ,
141- } ) ,
142- ) ;
125+ const mockEvent = makeMatrixEvent ( EventType . CallEncryptionKeysPrefix , "@bob:example.org" , undefined , {
126+ keys : {
127+ index : testKeyIndex ,
128+ key : testEncoded ,
129+ } ,
130+ member : {
131+ claimed_device_id : "BOBDEVICE" ,
132+ } ,
133+ room_id : roomId ,
134+ session : {
135+ application : "m.call" ,
136+ call_id : "" ,
137+ scope : "m.room" ,
138+ } ,
139+ } ) ;
140+ mockEvent . makeEncrypted ( EventType . RoomMessageEncrypted , { } , "" , "" ) ;
141+ mockClient . emit ( ClientEvent . ToDeviceEvent , mockEvent ) ;
143142
144143 const { userId, deviceId, keyBase64Encoded, index } = await receivedKeyResolvers . promise ;
145144 expect ( userId ) . toBe ( "@bob:example.org" ) ;
@@ -150,6 +149,41 @@ describe("ToDeviceKeyTransport", () => {
150149 expect ( statistics . counters . roomEventEncryptionKeysReceived ) . toBe ( 1 ) ;
151150 } ) ;
152151
152+ it ( "should drop non-encrypted/clear to-devic events" , ( ) => {
153+ const receivedKeyResolvers = vi . fn ( ) ;
154+ transport . on ( KeyTransportEvents . ReceivedKeys , ( membership , keyBase64Encoded , index , _timestamp ) => {
155+ receivedKeyResolvers ( ) ;
156+ } ) ;
157+ transport . start ( ) ;
158+
159+ const testEncoded = "ABCDEDF" ;
160+ const testKeyIndex = 2 ;
161+
162+ const clearEvent = makeMatrixEvent ( EventType . CallEncryptionKeysPrefix , "@bob:example.org" , undefined , {
163+ keys : {
164+ index : testKeyIndex ,
165+ key : testEncoded ,
166+ } ,
167+ member : {
168+ claimed_device_id : "BOBDEVICE" ,
169+ } ,
170+ room_id : roomId ,
171+ session : {
172+ application : "m.call" ,
173+ call_id : "" ,
174+ scope : "m.room" ,
175+ } ,
176+ } ) ;
177+ mockClient . emit ( ClientEvent . ToDeviceEvent , clearEvent ) ;
178+
179+ expect ( receivedKeyResolvers ) . toHaveBeenCalledTimes ( 0 ) ;
180+
181+ clearEvent . makeEncrypted ( EventType . RoomMessageEncrypted , { } , "" , "" ) ;
182+ mockClient . emit ( ClientEvent . ToDeviceEvent , clearEvent ) ;
183+
184+ expect ( receivedKeyResolvers ) . toHaveBeenCalledTimes ( 1 ) ;
185+ } ) ;
186+
153187 it ( "should not sent to ourself" , async ( ) => {
154188 const keyBase64Encoded = "ABCDEDF" ;
155189 const keyIndex = 2 ;
@@ -168,24 +202,24 @@ describe("ToDeviceKeyTransport", () => {
168202 const testEncoded = "ABCDEDF" ;
169203 const testKeyIndex = 2 ;
170204
171- mockClient . emit (
172- ClientEvent . ToDeviceEvent ,
173- makeMockEvent ( EventType . CallEncryptionKeysPrefix , "@bob:example.org" , undefined , {
174- keys : {
175- index : testKeyIndex ,
176- key : testEncoded ,
177- } ,
178- member : {
179- claimed_device_id : "BOBDEVICE " ,
180- } ,
181- room_id : "!anotherroom:id " ,
182- session : {
183- application : "m.call " ,
184- call_id : "" ,
185- scope : "m.room" ,
186- } ,
187- } ) ,
188- ) ;
205+ const keyEvent = makeMatrixEvent ( EventType . CallEncryptionKeysPrefix , "@bob:example.org" , undefined , {
206+ keys : {
207+ index : testKeyIndex ,
208+ key : testEncoded ,
209+ } ,
210+ member : {
211+ claimed_device_id : "BOBDEVICE" ,
212+ } ,
213+ room_id : "!anotherroom:id " ,
214+ session : {
215+ application : "m.call " ,
216+ call_id : "" ,
217+ scope : "m.room " ,
218+ } ,
219+ } ) ;
220+
221+ keyEvent . makeEncrypted ( EventType . RoomMessageEncrypted , { } , "" , "" ) ;
222+ mockClient . emit ( ClientEvent . ToDeviceEvent , keyEvent ) ;
189223
190224 expect ( mockLogger . warn ) . toHaveBeenCalledWith ( "Malformed Event: Mismatch roomId" ) ;
191225 expect ( statistics . counters . roomEventEncryptionKeysReceived ) . toBe ( 0 ) ;
@@ -240,7 +274,7 @@ describe("ToDeviceKeyTransport", () => {
240274
241275 mockClient . emit (
242276 ClientEvent . ToDeviceEvent ,
243- makeMockEvent ( EventType . CallEncryptionKeysPrefix , "@bob:example.org" , undefined , event ) ,
277+ makeMatrixEvent ( EventType . CallEncryptionKeysPrefix , "@bob:example.org" , undefined , event ) ,
244278 ) ;
245279
246280 expect ( mockLogger . warn ) . toHaveBeenCalled ( ) ;
0 commit comments