Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore to-device messages from dehydrated devices #4569

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 34 additions & 28 deletions crates/matrix-sdk-crypto/src/machine/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use ruma::{
room_id,
serde::Raw,
uint, user_id, DeviceId, DeviceKeyAlgorithm, DeviceKeyId, MilliSecondsSinceUnixEpoch,
OneTimeKeyAlgorithm, TransactionId, UserId,
OneTimeKeyAlgorithm, RoomId, TransactionId, UserId,
};
use serde_json::json;
use vodozemac::{
Expand All @@ -48,7 +48,7 @@ use vodozemac::{

use super::CrossSigningBootstrapRequests;
use crate::{
error::EventError,
error::{EventError, OlmResult},
machine::{
test_helpers::{
get_machine_after_query_test_helper, get_machine_pair_with_session,
Expand All @@ -58,7 +58,7 @@ use crate::{
},
olm::{BackedUpRoomKey, ExportedRoomKey, SenderData, VerifyJson},
session_manager::CollectStrategy,
store::{BackupDecryptionKey, Changes, CryptoStore, MemoryStore},
store::{BackupDecryptionKey, Changes, CryptoStore, MemoryStore, RoomKeyInfo},
types::{
events::{
room::encrypted::{EncryptedToDeviceEvent, ToDeviceEncryptedEventContent},
Expand Down Expand Up @@ -388,33 +388,10 @@ async fn test_missing_sessions_calculation() {
#[async_test]
async fn test_room_key_sharing() {
let (alice, bob) = get_machine_pair_with_session(alice_id(), user_id(), false).await;

let room_id = room_id!("!test:example.org");

let to_device_requests = alice
.share_room_key(room_id, iter::once(bob.user_id()), EncryptionSettings::default())
.await
.unwrap();

let event = ToDeviceEvent::new(
alice.user_id().to_owned(),
to_device_requests_to_content(to_device_requests),
);
let event = json_convert(&event).unwrap();

let alice_session =
alice.inner.group_session_manager.get_outbound_group_session(room_id).unwrap();

let (decrypted, room_key_updates) = bob
.receive_sync_changes(EncryptionSyncChanges {
to_device_events: vec![event],
changed_devices: &Default::default(),
one_time_keys_counts: &Default::default(),
unused_fallback_keys: None,
next_batch_token: None,
})
.await
.unwrap();
let (decrypted, room_key_updates) =
send_room_key_to_device(&alice, &bob, room_id).await.unwrap();

let event = decrypted[0].deserialize().unwrap();

Expand All @@ -425,6 +402,9 @@ async fn test_room_key_sharing() {
panic!("expected RoomKeyEvent found {event:?}");
}

let alice_session =
alice.inner.group_session_manager.get_outbound_group_session(room_id).unwrap();

let session = bob.store().get_inbound_group_session(room_id, alice_session.session_id()).await;

assert!(session.unwrap().is_some());
Expand All @@ -434,6 +414,32 @@ async fn test_room_key_sharing() {
assert_eq!(room_key_updates[0].session_id, alice_session.session_id());
}

async fn send_room_key_to_device(
alice: &OlmMachine,
bob: &OlmMachine,
room_id: &RoomId,
) -> OlmResult<(Vec<Raw<AnyToDeviceEvent>>, Vec<RoomKeyInfo>)> {
Comment on lines +445 to +449
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be worth a bit of documentation saying this sends from alice to bob. (Alternatively, I guess you could use better names for the users.)

Could you also document what it returns?

let to_device_requests = alice
.share_room_key(room_id, iter::once(bob.user_id()), EncryptionSettings::default())
.await
.unwrap();

let event = ToDeviceEvent::new(
alice.user_id().to_owned(),
to_device_requests_to_content(to_device_requests),
);
let event = json_convert(&event).unwrap();

bob.receive_sync_changes(EncryptionSyncChanges {
to_device_events: vec![event],
changed_devices: &Default::default(),
one_time_keys_counts: &Default::default(),
unused_fallback_keys: None,
next_batch_token: None,
})
.await
}

#[async_test]
async fn test_request_missing_secrets() {
let (alice, _) = get_machine_pair_with_session(alice_id(), bob_id(), false).await;
Expand Down