Skip to content

Commit 15e9cf8

Browse files
committed
feat(base): Add sync reponse processor for AnySyncState::RoomEncrypted
Signed-off-by: Skye Elliot <[email protected]>
1 parent 3d5d519 commit 15e9cf8

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

crates/matrix-sdk-base/src/response_processors/room/msc4186/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ pub async fn update_any_room(
118118
ambiguity_cache,
119119
&mut new_user_ids,
120120
state_store,
121+
#[cfg(feature = "e2e-encryption")]
122+
e2ee.clone(),
121123
)
122124
.await?;
123125

crates/matrix-sdk-base/src/response_processors/room/sync_v2.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ pub async fn update_joined_room(
7676
ambiguity_cache,
7777
&mut new_user_ids,
7878
state_store,
79+
#[cfg(feature = "e2e-encryption")]
80+
e2ee.clone(),
7981
)
8082
.await?;
8183

@@ -180,6 +182,8 @@ pub async fn update_left_room(
180182
ambiguity_cache,
181183
&mut (),
182184
state_store,
185+
#[cfg(feature = "e2e-encryption")]
186+
e2ee.clone(),
183187
)
184188
.await?;
185189

crates/matrix-sdk-base/src/response_processors/state_events.rs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use serde::Deserialize;
2626
use tracing::warn;
2727

2828
use super::Context;
29+
#[cfg(feature = "e2e-encryption")]
30+
use super::e2ee;
2931
use crate::store::BaseStateStore;
3032

3133
/// Collect [`AnySyncStateEvent`].
@@ -89,6 +91,7 @@ pub mod sync {
8991
ambiguity_cache: &mut AmbiguityCache,
9092
new_users: &mut U,
9193
state_store: &BaseStateStore,
94+
#[cfg(feature = "e2e-encryption")] e2ee: super::e2ee::E2EE<'_>,
9295
) -> StoreResult<()>
9396
where
9497
U: NewUsers,
@@ -142,6 +145,80 @@ pub mod sync {
142145
}
143146
}
144147

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+
145222
_ => {
146223
room_info.handle_state_event(event);
147224
}

0 commit comments

Comments
 (0)