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

fix(crypto): Redecrypt non-UTD messages to remove no-longer-relevant warning shields #4644

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

andybalaam
Copy link
Member

@andybalaam andybalaam commented Feb 7, 2025

Fixes element-hq/element-meta#2697

I'm sorry it's a big change. I've tried to break it into decent commits, and I did a couple of preparatory PRs to make it less painful, but it's still a bit to get your head around.

The basic idea is that when a session is updated and we call retry_event_decryption, we don't only look at UTDs any more - now we also look at decrypted events, and re-request their EncryptionInfo, in case it has improved.

@andybalaam andybalaam changed the title refactor(crypto): Store the session ID of a UTD in RemoteEventTimelineItem fix(crypto): Redecrypt non-UTD messages to remove no-longer-relevant warning shields Feb 7, 2025
@andybalaam andybalaam force-pushed the andybalaam/redecrypt-non-utds branch from 9a19fcf to 9cbfbfb Compare February 7, 2025 14:17
Copy link

codecov bot commented Feb 7, 2025

Codecov Report

Attention: Patch coverage is 85.22727% with 13 lines in your changes missing coverage. Please review.

Project coverage is 85.83%. Comparing base (9de6d28) to head (414f03d).

Files with missing lines Patch % Lines
...rates/matrix-sdk-ui/src/timeline/controller/mod.rs 85.18% 4 Missing ⚠️
crates/matrix-sdk/src/room/mod.rs 0.00% 4 Missing ⚠️
crates/matrix-sdk-ui/src/timeline/event_handler.rs 89.47% 2 Missing ⚠️
crates/matrix-sdk-ui/src/timeline/traits.rs 0.00% 2 Missing ⚠️
...tes/matrix-sdk-ui/src/timeline/controller/state.rs 95.23% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4644      +/-   ##
==========================================
+ Coverage   85.82%   85.83%   +0.01%     
==========================================
  Files         292      292              
  Lines       33761    33830      +69     
==========================================
+ Hits        28974    29037      +63     
- Misses       4787     4793       +6     

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

@andybalaam andybalaam force-pushed the andybalaam/redecrypt-non-utds branch 3 times, most recently from 741aa40 to d83e187 Compare February 14, 2025 11:45
…imelineItem

... meaning we attempt to redecrypt them even if they are not UTDs.
The redecryption doesn't actually work - that will be fixed in the next
commit.
…etry decryption

In later commits, we will make this cleverer, so it can calculate which
non-UTD events need updating too.
Future commits will add code to try non-UTDs too, in another function.
@andybalaam andybalaam force-pushed the andybalaam/redecrypt-non-utds branch from d83e187 to 414f03d Compare February 14, 2025 11:59
@andybalaam andybalaam marked this pull request as ready for review February 14, 2025 12:11
@andybalaam andybalaam requested review from a team as code owners February 14, 2025 12:11
@andybalaam andybalaam requested review from stefanceriu, richvdh and bnjbvr and removed request for a team and stefanceriu February 14, 2025 12:11
@andybalaam
Copy link
Member Author

I swapped the Rust team reviewer to @bnjbvr since I know he has questions about including session ID in the timeline item.

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.

seems generally good to me. A few comments.

@@ -121,6 +123,12 @@ pub(super) trait RoomDataProvider:
) -> impl Future<Output = Result<(), super::Error>> + SendOutsideWasm + 'a;

fn room_info(&self) -> Subscriber<RoomInfo>;

fn get_encryption_info(
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 be "obvious" but this could use a doc-comment?

@@ -441,6 +443,14 @@ impl<'a> TimelineStateTransaction<'a> {
}
}

pub(super) fn replace(
Copy link
Member

Choose a reason for hiding this comment

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

doc-comment?

@@ -368,6 +368,7 @@ impl<'a, 'o> TimelineEventHandler<'a, 'o> {
mut self,
date_divider_adjuster: &mut DateDividerAdjuster,
event_kind: TimelineEventKind,
session_id: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

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

generally: I think we need a better name, or better documentation, or both, for things called session_id. It's very unclear to a casual reader what it is supposed to be.

Comment on lines +185 to +187
// In the timeline, this is used to find events that need re-decrypting but in
// the message preview it is not used.
let session_id = None;
Copy link
Member

Choose a reason for hiding this comment

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

this seems to be baking in a bit of an assumption: is there no way we can get the session id here, to guard against future changes?

If not, we should document RemoteEventTimelineItem.session_id to note that it is not always populated.

Comment on lines 68 to 70

/// The megolm session ID used to send this event, if it is UTD.
pub session_id: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't feel right here -- in particular the fact that it overlaps with encryption_info.

Maybe we want to replace encryption_info with an enum field. But it sounds like Benji might have bigger plans anyway.

Comment on lines +1086 to +1090
/// `state` is the [`TimelineState`] state of the timeline.
/// `retry_indices` contains the indices of events to try within the
/// `state.items` `should_retry` checks that a session is included in
/// the list of updated sessions. `decryptor` does the actual work of
/// decrypting events.
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 there is a sort of informal convention of setting out parameter documentation as a markdown list.

Comment on lines +1466 to +1467
let mut retry_decryption_indices = Vec::new();
let mut retry_info_indices = Vec::new();
Copy link
Member

Choose a reason for hiding this comment

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

I don't have a strong opinion here, but in the past I've been asked to use partition_map for this sort of thing.

We could write this as a filter followed by a partition_map?

// Retry decrypting
self.retry_event_decryption_by_index(state, retry_indices, should_retry, decryptor).await;
// Retry decrypting UTDs
let state = self.state.clone().write_owned().await;
Copy link
Member

Choose a reason for hiding this comment

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

feels like this state.clone() should happen inside retry_event_decryption_by_index

@@ -225,6 +228,45 @@ impl TimelineState {
txn.commit();
}

/// Try to fetch [`EncryptionInfo`] for the events with the supplied
/// indices, and update them where we succeed.
pub(super) async fn retry_event_encryption_info<P: RoomDataProvider>(
Copy link
Member

Choose a reason for hiding this comment

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

I have to say that having this here but retry_event_decryption_by_index in the other place feels rather asymmetric. Is there a reason they need to be different?

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.

EX: Messages sent after an identity reset can sometimes be flagged as sent from insecure device
2 participants