Skip to content

Commit fbe146a

Browse files
committed
test(crypto): Regresion test for #5613
Add a test to ensure that history-sharing still works when "exclude insecure devices" is enabled.
1 parent 28beb91 commit fbe146a

File tree

3 files changed

+60
-1
lines changed

3 files changed

+60
-1
lines changed

crates/matrix-sdk/src/room/shared_room_history.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ pub(crate) async fn maybe_accept_key_bundle(room: &Room, inviter: &UserId) -> Re
132132

133133
// Ensure that we get a fresh list of devices for the inviter, in case we need
134134
// to recalculate the `SenderData`.
135+
// XXX: is this necessary, given (with exclude-insecure-devices), we should have
136+
// checked that the inviter device was cross-signed when we received the
137+
// to-device message?
135138
let (req_id, request) =
136139
olm_machine.query_keys_for_users(iter::once(bundle_info.sender_user.as_ref()));
137140

testing/matrix-sdk-integration-testing/src/helpers.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use assign::assign;
1212
use matrix_sdk::{
1313
Client, ClientBuilder, Room,
1414
config::{RequestConfig, SyncSettings},
15+
crypto::{CollectStrategy, DecryptionSettings},
1516
encryption::EncryptionSettings,
1617
ruma::{
1718
RoomId,
@@ -22,6 +23,7 @@ use matrix_sdk::{
2223
sync::SyncResponse,
2324
timeout::ElapsedError,
2425
};
26+
use matrix_sdk_base::crypto::TrustRequirement;
2527
use once_cell::sync::Lazy;
2628
use rand::Rng as _;
2729
use tempfile::{TempDir, tempdir};
@@ -39,6 +41,8 @@ enum SqlitePath {
3941
pub struct TestClientBuilder {
4042
username: String,
4143
use_sqlite_dir: Option<SqlitePath>,
44+
decryption_settings: Option<DecryptionSettings>,
45+
room_key_recipient_strategy: CollectStrategy,
4246
encryption_settings: EncryptionSettings,
4347
enable_share_history_on_invite: bool,
4448
http_proxy: Option<String>,
@@ -56,7 +60,9 @@ impl TestClientBuilder {
5660
Self {
5761
username,
5862
use_sqlite_dir: None,
63+
decryption_settings: None,
5964
encryption_settings: Default::default(),
65+
room_key_recipient_strategy: Default::default(),
6066
enable_share_history_on_invite: false,
6167
http_proxy: None,
6268
cross_process_store_locks_holder_name: None,
@@ -87,6 +93,21 @@ impl TestClientBuilder {
8793
self
8894
}
8995

96+
/// Simulate the behaviour of the clients when the "exclude insecure
97+
/// devices" (MSC4153) labs flag is enabled.
98+
pub fn exclude_insecure_devices(mut self, exclude_insecure_devices: bool) -> Self {
99+
let (sender_device_trust_requirement, room_key_recipient_strategy) =
100+
if exclude_insecure_devices {
101+
(TrustRequirement::CrossSignedOrLegacy, CollectStrategy::IdentityBasedStrategy)
102+
} else {
103+
(TrustRequirement::Untrusted, CollectStrategy::AllDevices)
104+
};
105+
self.decryption_settings = Some(DecryptionSettings { sender_device_trust_requirement });
106+
self.room_key_recipient_strategy = room_key_recipient_strategy;
107+
108+
self
109+
}
110+
90111
pub fn http_proxy(mut self, url: String) -> Self {
91112
self.http_proxy = Some(url);
92113
self
@@ -106,9 +127,14 @@ impl TestClientBuilder {
106127
.homeserver_url(homeserver_url)
107128
.sliding_sync_version_builder(VersionBuilder::Native)
108129
.with_encryption_settings(self.encryption_settings)
130+
.with_room_key_recipient_strategy(self.room_key_recipient_strategy.clone())
109131
.with_enable_share_history_on_invite(self.enable_share_history_on_invite)
110132
.request_config(RequestConfig::short_retry());
111133

134+
if let Some(decryption_settings) = &self.decryption_settings {
135+
client_builder = client_builder.with_decryption_settings(decryption_settings.clone())
136+
}
137+
112138
if let Some(holder_name) = &self.cross_process_store_locks_holder_name {
113139
client_builder =
114140
client_builder.cross_process_store_locks_holder_name(holder_name.clone());

testing/matrix-sdk-integration-testing/src/tests/e2ee/shared_history.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,26 @@ use crate::helpers::{SyncTokenAwareClient, TestClientBuilder, wait_for_room};
2626

2727
/// When we invite another user to a room with "joined" history visibility, we
2828
/// share the encryption history.
29+
///
30+
/// Pre-"exclude insecure devices" test variant.
2931
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
3032
async fn test_history_share_on_invite() -> Result<()> {
33+
test_history_share_on_invite_helper(false).await
34+
}
35+
36+
/// When we invite another user to a room with "joined" history visibility, we
37+
/// share the encryption history, even when "exclude insecure devices" is
38+
/// enabled.
39+
///
40+
/// Regression test for https://github.com/matrix-org/matrix-rust-sdk/issues/5613
41+
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
42+
async fn test_history_share_on_invite_exclude_insecure_devices() -> Result<()> {
43+
test_history_share_on_invite_helper(true).await
44+
}
45+
46+
/// Common implementation for [`test_history_share_on_invite`] and
47+
/// [`test_history_share_on_invite_exclude_insecure_devices].
48+
async fn test_history_share_on_invite_helper(exclude_insecure_devices: bool) -> Result<()> {
3149
let alice_span = tracing::info_span!("alice");
3250
let bob_span = tracing::info_span!("bob");
3351

@@ -38,6 +56,7 @@ async fn test_history_share_on_invite() -> Result<()> {
3856
.use_sqlite()
3957
.encryption_settings(encryption_settings)
4058
.enable_share_history_on_invite(true)
59+
.exclude_insecure_devices(exclude_insecure_devices)
4160
.build()
4261
.await?;
4362

@@ -55,6 +74,7 @@ async fn test_history_share_on_invite() -> Result<()> {
5574
TestClientBuilder::new("bob")
5675
.encryption_settings(encryption_settings)
5776
.enable_share_history_on_invite(true)
77+
.exclude_insecure_devices(exclude_insecure_devices)
5878
.build()
5979
.await?,
6080
);
@@ -86,8 +106,18 @@ async fn test_history_share_on_invite() -> Result<()> {
86106
alice_room.invite_user_by_id(bob.user_id().unwrap()).await?;
87107

88108
// Alice is done. Bob has been invited and the room key bundle should have been
89-
// sent out. Let's stop syncing so the logs contain less noise.
109+
// sent out. Let's log her out, so we know that this feature works even when the
110+
// sender device has been deleted (and to reduce the amount of noise in the
111+
// logs).
90112
alice_sync_service.stop().await;
113+
alice.logout().instrument(alice_span.clone()).await?;
114+
115+
// Workaround for https://github.com/matrix-org/matrix-rust-sdk/issues/5770: Bob needs a copy of
116+
// Alice's identity.
117+
bob.encryption()
118+
.request_user_identity(alice.user_id().unwrap())
119+
.instrument(bob_span.clone())
120+
.await?;
91121

92122
let bob_response = bob.sync_once().instrument(bob_span.clone()).await?;
93123

0 commit comments

Comments
 (0)