Skip to content

Commit 93f5562

Browse files
authored
Only send verifications requests to devices that are cross-signed (#1884)
Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
1 parent 18971e8 commit 93f5562

File tree

2 files changed

+50
-11
lines changed
  • crates
    • matrix-sdk-crypto/src/identities
    • matrix-sdk/src/encryption/identities

2 files changed

+50
-11
lines changed

crates/matrix-sdk-crypto/src/identities/user.rs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
use std::{
16+
collections::HashMap,
1617
ops::Deref,
1718
sync::{
1819
atomic::{AtomicBool, Ordering},
@@ -25,7 +26,7 @@ use ruma::{
2526
events::{
2627
key::verification::VerificationMethod, room::message::KeyVerificationRequestEventContent,
2728
},
28-
EventId, OwnedDeviceId, OwnedUserId, RoomId, UserId,
29+
DeviceId, EventId, OwnedDeviceId, OwnedUserId, RoomId, UserId,
2930
};
3031
use serde::{Deserialize, Serialize};
3132
use tracing::error;
@@ -161,14 +162,10 @@ impl OwnUserIdentity {
161162
&self,
162163
methods: Option<Vec<VerificationMethod>>,
163164
) -> Result<(VerificationRequest, OutgoingVerificationRequest), CryptoStoreError> {
164-
let devices: Vec<OwnedDeviceId> = self
165-
.verification_machine
166-
.store
167-
.get_user_devices(self.user_id())
168-
.await?
169-
.into_keys()
170-
.filter(|d| &**d != self.verification_machine.own_device_id())
171-
.collect();
165+
let all_devices = self.verification_machine.store.get_user_devices(self.user_id()).await?;
166+
let devices = self
167+
.inner
168+
.filter_devices_to_request(all_devices, self.verification_machine.own_device_id());
172169

173170
Ok(self
174171
.verification_machine
@@ -622,6 +619,20 @@ impl ReadOnlyOwnUserIdentity {
622619

623620
Ok(())
624621
}
622+
623+
fn filter_devices_to_request(
624+
&self,
625+
devices: HashMap<OwnedDeviceId, ReadOnlyDevice>,
626+
own_device_id: &DeviceId,
627+
) -> Vec<OwnedDeviceId> {
628+
devices
629+
.into_iter()
630+
.filter_map(|(device_id, device)| {
631+
(device_id != own_device_id && self.is_device_signed(&device).is_ok())
632+
.then_some(device_id)
633+
})
634+
.collect()
635+
}
625636
}
626637

627638
#[cfg(any(test, feature = "testing"))]
@@ -699,11 +710,11 @@ pub(crate) mod testing {
699710

700711
#[cfg(test)]
701712
pub(crate) mod tests {
702-
use std::sync::Arc;
713+
use std::{collections::HashMap, sync::Arc};
703714

704715
use assert_matches::assert_matches;
705716
use matrix_sdk_test::async_test;
706-
use ruma::user_id;
717+
use ruma::{device_id, user_id};
707718
use serde_json::{json, Value};
708719
use tokio::sync::Mutex;
709720

@@ -875,4 +886,27 @@ pub(crate) mod tests {
875886
Err(_)
876887
);
877888
}
889+
890+
#[test]
891+
fn filter_devices_to_request() {
892+
let response = own_key_query();
893+
let identity = get_own_identity();
894+
let (first, second) = device(&response);
895+
896+
let second_device_id = second.device_id().to_owned();
897+
let unknown_device_id = device_id!("UNKNOWN");
898+
899+
let devices = HashMap::from([
900+
(first.device_id().to_owned(), first),
901+
(second.device_id().to_owned(), second),
902+
]);
903+
904+
// Own device and devices not verified are filtered out.
905+
assert_eq!(identity.filter_devices_to_request(devices.clone(), &second_device_id).len(), 0);
906+
// Signed devices that are not our own are kept.
907+
assert_eq!(
908+
identity.filter_devices_to_request(devices, unknown_device_id),
909+
[second_device_id]
910+
);
911+
}
878912
}

crates/matrix-sdk/src/encryption/identities/devices.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,11 @@ impl Device {
513513
pub async fn set_local_trust(&self, trust_state: LocalTrust) -> Result<(), CryptoStoreError> {
514514
self.inner.set_local_trust(trust_state).await
515515
}
516+
517+
/// Is the device cross-signed by its own user.
518+
pub fn is_cross_signed_by_owner(&self) -> bool {
519+
self.inner.is_cross_signed_by_owner()
520+
}
516521
}
517522

518523
/// The collection of all the [`Device`]s a user has.

0 commit comments

Comments
 (0)