@@ -737,6 +737,8 @@ async fn test_megolm_encryption() {
737
737
async fn test_megolm_state_encryption ( ) {
738
738
use ruma:: events:: { AnyStateEvent , EmptyStateKey } ;
739
739
740
+ use crate :: types:: events:: room:: { encrypted:: RoomEncryptedEventContent , Event } ;
741
+
740
742
let ( alice, bob) =
741
743
get_machine_pair_with_setup_sessions_test_helper ( alice_id ( ) , user_id ( ) , false ) . await ;
742
744
let room_id = room_id ! ( "!test:example.org" ) ;
@@ -790,14 +792,61 @@ async fn test_megolm_state_encryption() {
790
792
"content" : encrypted_content,
791
793
} ) ;
792
794
795
+ // Malformed events
796
+ let bad_type_event = json ! ( {
797
+ "event_id" : "$xxxxx:example.org" ,
798
+ "origin_server_ts" : MilliSecondsSinceUnixEpoch :: now( ) ,
799
+ "sender" : alice. user_id( ) ,
800
+ "type" : "m.room.encrypted" ,
801
+ "state_key" : "m.room.malformed:" ,
802
+ "content" : encrypted_content,
803
+ } ) ;
804
+ let bad_state_key_event = json ! ( {
805
+ "event_id" : "$xxxxx:example.org" ,
806
+ "origin_server_ts" : MilliSecondsSinceUnixEpoch :: now( ) ,
807
+ "sender" : alice. user_id( ) ,
808
+ "type" : "m.room.encrypted" ,
809
+ "state_key" : "m.room.malformed:" ,
810
+ "content" : encrypted_content,
811
+ } ) ;
812
+
793
813
let event = json_convert ( & event) . unwrap ( ) ;
794
814
815
+ let bad_type_event: Raw < Event < RoomEncryptedEventContent > > =
816
+ json_convert ( & bad_type_event) . unwrap ( ) ;
817
+ let bad_state_key_event = json_convert ( & bad_state_key_event) . unwrap ( ) ;
818
+
795
819
let decryption_settings =
796
820
DecryptionSettings { sender_device_trust_requirement : TrustRequirement :: Untrusted } ;
797
821
798
822
let decryption_result =
799
823
bob. try_decrypt_room_event ( & event, room_id, & decryption_settings) . await . unwrap ( ) ;
824
+
825
+ let bad_type_decryption_result =
826
+ bob. try_decrypt_room_event ( & bad_type_event, room_id, & decryption_settings) . await . unwrap ( ) ;
827
+ let bad_state_key_decryption_result = bob
828
+ . try_decrypt_room_event ( & bad_state_key_event, room_id, & decryption_settings)
829
+ . await
830
+ . unwrap ( ) ;
831
+
800
832
assert_let ! ( RoomEventDecryptionResult :: Decrypted ( decrypted_event) = decryption_result) ;
833
+
834
+ // Require malformed events fail verification
835
+ assert_matches ! (
836
+ bad_type_decryption_result,
837
+ RoomEventDecryptionResult :: UnableToDecrypt ( UnableToDecryptInfo {
838
+ reason: UnableToDecryptReason :: StateKeyVerificationFailed ,
839
+ ..
840
+ } )
841
+ ) ;
842
+ assert_matches ! (
843
+ bad_state_key_decryption_result,
844
+ RoomEventDecryptionResult :: UnableToDecrypt ( UnableToDecryptInfo {
845
+ reason: UnableToDecryptReason :: StateKeyVerificationFailed ,
846
+ ..
847
+ } )
848
+ ) ;
849
+
801
850
let decrypted_event = decrypted_event. event . deserialize ( ) . unwrap ( ) ;
802
851
803
852
if let AnyTimelineEvent :: State ( AnyStateEvent :: RoomTopic ( StateEvent :: Original (
0 commit comments