@@ -26,6 +26,8 @@ use serde::Deserialize;
26
26
use tracing:: warn;
27
27
28
28
use super :: Context ;
29
+ #[ cfg( feature = "e2e-encryption" ) ]
30
+ use super :: e2ee;
29
31
use crate :: store:: BaseStateStore ;
30
32
31
33
/// Collect [`AnySyncStateEvent`].
@@ -89,6 +91,7 @@ pub mod sync {
89
91
ambiguity_cache : & mut AmbiguityCache ,
90
92
new_users : & mut U ,
91
93
state_store : & BaseStateStore ,
94
+ #[ cfg( feature = "e2e-encryption" ) ] e2ee : super :: e2ee:: E2EE < ' _ > ,
92
95
) -> StoreResult < ( ) >
93
96
where
94
97
U : NewUsers ,
@@ -142,6 +145,80 @@ pub mod sync {
142
145
}
143
146
}
144
147
148
+ #[ cfg( feature = "e2e-encryption" ) ]
149
+ AnySyncStateEvent :: RoomEncrypted ( outer) => {
150
+ use matrix_sdk_crypto:: RoomEventDecryptionResult ;
151
+ use tracing:: { debug, warn} ;
152
+
153
+ debug ! ( event_id = ?outer. event_id( ) , "Received encrypted state event, attempting decryption..." ) ;
154
+
155
+ let Some ( olm_machine) = e2ee. olm_machine else {
156
+ panic ! ( ) ;
157
+ } ;
158
+
159
+ let decrypted_event = olm_machine
160
+ . try_decrypt_room_event (
161
+ raw_event. cast_ref_unchecked ( ) ,
162
+ & room_info. room_id ,
163
+ e2ee. decryption_settings ,
164
+ )
165
+ . await
166
+ . expect ( "OlmMachine was not started" ) ;
167
+
168
+ // Skip state events that failed to decrypt.
169
+ let RoomEventDecryptionResult :: Decrypted ( room_event) = decrypted_event else {
170
+ warn ! ( event_id = ?outer. event_id( ) , "Failed to decrypt state event" ) ;
171
+ continue ;
172
+ } ;
173
+
174
+ // Unpack event type and state key from outer, or discard if this fails.
175
+ let Some ( ( outer_event_type, outer_state_key) ) =
176
+ outer. state_key ( ) . split_once ( ":" )
177
+ else {
178
+ warn ! (
179
+ event_id = outer. event_id( ) . as_str( ) ,
180
+ state_key = event. state_key( ) ,
181
+ "Malformed state key"
182
+ ) ;
183
+ continue ;
184
+ } ;
185
+
186
+ let inner =
187
+ match room_event. event . deserialize_as_unchecked :: < AnySyncStateEvent > ( ) {
188
+ Ok ( inner) => inner,
189
+ Err ( e) => {
190
+ warn ! ( "Malformed event body: {e}" ) ;
191
+ continue ;
192
+ }
193
+ } ;
194
+
195
+ // Check event types match, discard if not.
196
+ let inner_event_type = inner. event_type ( ) . to_string ( ) ;
197
+ if outer_event_type != inner_event_type {
198
+ warn ! (
199
+ event_id = outer. event_id( ) . as_str( ) ,
200
+ expected = outer_event_type,
201
+ found = inner_event_type,
202
+ "Mismatched event type"
203
+ ) ;
204
+ continue ;
205
+ }
206
+
207
+ // Check state keys match, discard if not.
208
+ if outer_state_key != inner. state_key ( ) {
209
+ warn ! (
210
+ event_id = outer. event_id( ) . as_str( ) ,
211
+ expected = outer_state_key,
212
+ found = inner. state_key( ) ,
213
+ "Mismatched state key"
214
+ ) ;
215
+ continue ;
216
+ }
217
+
218
+ debug ! ( event_id = ?outer. event_id( ) , "Decrypted state event successfully." ) ;
219
+ room_info. handle_state_event ( & inner) ;
220
+ }
221
+
145
222
_ => {
146
223
room_info. handle_state_event ( event) ;
147
224
}
0 commit comments