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

Conversation

andybalaam
Copy link
Member

Fixes #4466

@andybalaam andybalaam requested review from a team as code owners January 21, 2025 15:23
@andybalaam andybalaam requested review from Hywan and richvdh and removed request for a team January 21, 2025 15:23
// Handle the decrypted event, e.g. fetch out Megolm sessions out of
// the event.
self.handle_decrypted_to_device_event(transaction.cache(), &mut decrypted, changes)
.await?;
Copy link
Member Author

Choose a reason for hiding this comment

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

I could not find a way to test that the side effects of this call are suppressed when sending from a dehydrated device. I think the only observable side effects happen when we send a SecretSend to-device event, and when I looked at how to do that it looked like I would need an existing SecretRequest for anything to happen, and that looked complex to set up.

Copy link
Member Author

Choose a reason for hiding this comment

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

(To try to clarify what I mean: my tests would still pass if this if did not exist.)

// know better.
}

// Do we already know about this device?
Copy link
Member Author

Choose a reason for hiding this comment

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

My tests would still pass if the rest of this function did not exist.

I looked into how to construct a to-device message that was from a known dehydrated device, but did not itself contain device_keys and it just looked too complex. I even got part-way in to an implementation pairing with Rich, but it just seemed so much!

@andybalaam andybalaam force-pushed the andybalaam/drop-to-device-from-dehydrated branch from 69389e6 to c1c2b18 Compare January 21, 2025 15:28
@andybalaam
Copy link
Member Author

Any help with how to construct test cases that exercise the other parts of this code would be much appreciated! I have essentially given up because it seemed too hard.

Copy link

codecov bot commented Jan 21, 2025

Codecov Report

Attention: Patch coverage is 84.00000% with 4 lines in your changes missing coverage. Please review.

Project coverage is 85.41%. Comparing base (f2c9a8f) to head (c1c2b18).
Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
crates/matrix-sdk-crypto/src/machine/mod.rs 84.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4569      +/-   ##
==========================================
+ Coverage   85.39%   85.41%   +0.01%     
==========================================
  Files         285      285              
  Lines       32215    32235      +20     
==========================================
+ Hits        27510    27533      +23     
+ Misses       4705     4702       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

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

A few nits, generally looks about right to me

Comment on lines +417 to +421
async fn send_room_key_to_device(
alice: &OlmMachine,
bob: &OlmMachine,
room_id: &RoomId,
) -> OlmResult<(Vec<Raw<AnyToDeviceEvent>>, Vec<RoomKeyInfo>)> {
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?

warn!(
sender = ?e.sender,
session = ?decrypted.session,
"Received a to-device event from a dehydrated device. This is unexpected"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Received a to-device event from a dehydrated device. This is unexpected"
"Received a to-device event from a dehydrated device. This is unexpected: ignoring event"

Some(raw_event)
}

async fn event_is_from_dehydrated_device(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
async fn event_is_from_dehydrated_device(
async fn to_device_event_is_from_dehydrated_device(

Comment on lines +1365 to +1369
async fn event_is_from_dehydrated_device(
&self,
decrypted: &OlmDecryptionInfo,
sender_user_id: &UserId,
) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

it might come as a bit of a surprise that this accepts an OlmDecryptionInfo rather than, say, an AnyToDeviceEvent. Might be worth adding a doc-comment that explains that it works on decrypted to-device events.

Comment on lines +1394 to +1398
if let Some(device) = device {
device.is_dehydrated()
} else {
false
}
Copy link
Member

Choose a reason for hiding this comment

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

light suggestion, no strong feelings here

Suggested change
if let Some(device) = device {
device.is_dehydrated()
} else {
false
}
device.is_some_and(Device::is_dehydrated)

Comment on lines +1401 to +1406
// No - we received a to-device message from a device we don't know about. We
// continue to process it, but this is unexpected so we log an error.
warn!(
error = ?e,
"Received a to-device message from a device we don't recognise",
);
Copy link
Member

Choose a reason for hiding this comment

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

I think if we just don't recognise the device, get_device_from_curve_key will return Ok(None). This suggests a problem with the store.

It's probably still ok to return false here; but the comment and warning could do with a tweak.

}
Err(e) => {
// No - we received a to-device message from a device we don't know about. We
// continue to process it, but this is unexpected so we log an error.
Copy link
Member

Choose a reason for hiding this comment

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

we log an error

We are logging a warning, not an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Dehydrated devices: drop incoming to-device messages from dehydrated devices
2 participants