Skip to content

Commit 94b28a1

Browse files
committed
refactor(crypto): Extract a method to check for to-device events from dehydrated devices
1 parent 97ca9ee commit 94b28a1

File tree

1 file changed

+25
-20
lines changed
  • crates/matrix-sdk-crypto/src/machine

1 file changed

+25
-20
lines changed

crates/matrix-sdk-crypto/src/machine/mod.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -868,27 +868,13 @@ impl OlmMachine {
868868
let mut decrypted =
869869
transaction.account().await?.decrypt_to_device_event(&self.inner.store, event).await?;
870870

871-
let from_dehydrated_device =
872-
self.to_device_event_is_from_dehydrated_device(&decrypted, &event.sender).await?;
873-
874-
// Check whether this event is from a dehydrated device - if so, return Ok(None)
875-
// to skip it because we don't expect ever to receive an event from a
876-
// dehydrated device.
877-
if from_dehydrated_device {
878-
// Device is dehydrated: ignore this event
879-
warn!(
880-
sender = ?event.sender,
881-
session = ?decrypted.session,
882-
"Received a to-device event from a dehydrated device. This is unexpected: ignoring event"
883-
);
884-
Err(DecryptToDeviceError::FromDehydratedDevice)
885-
} else {
886-
// Device is not dehydrated: handle it as normal e.g. create a Megolm session
887-
self.handle_decrypted_to_device_event(transaction.cache(), &mut decrypted, changes)
888-
.await?;
871+
// Return early if the sending device is decrypted
872+
self.check_to_device_event_is_not_from_dehydrated_device(&decrypted, &event.sender).await?;
889873

890-
Ok(decrypted)
891-
}
874+
// Device is not dehydrated: handle it as normal e.g. create a Megolm session
875+
self.handle_decrypted_to_device_event(transaction.cache(), &mut decrypted, changes).await?;
876+
877+
Ok(decrypted)
892878
}
893879

894880
#[instrument(
@@ -1507,6 +1493,25 @@ impl OlmMachine {
15071493
})
15081494
}
15091495

1496+
/// Return an error if the supplied to-device event was sent from a
1497+
/// dehydrated device.
1498+
async fn check_to_device_event_is_not_from_dehydrated_device(
1499+
&self,
1500+
decrypted: &OlmDecryptionInfo,
1501+
sender_user_id: &UserId,
1502+
) -> Result<(), DecryptToDeviceError> {
1503+
if self.to_device_event_is_from_dehydrated_device(decrypted, sender_user_id).await? {
1504+
warn!(
1505+
sender = ?sender_user_id,
1506+
session = ?decrypted.session,
1507+
"Received a to-device event from a dehydrated device. This is unexpected: ignoring event"
1508+
);
1509+
Err(DecryptToDeviceError::FromDehydratedDevice)
1510+
} else {
1511+
Ok(())
1512+
}
1513+
}
1514+
15101515
/// Decide whether a decrypted to-device event was sent from a dehydrated
15111516
/// device.
15121517
///

0 commit comments

Comments
 (0)