-
Notifications
You must be signed in to change notification settings - Fork 397
feat(sdk): Rotate session keys when a member leaves the room #6292
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
Open
kaylendog
wants to merge
5
commits into
main
Choose a base branch
from
kaylendog/history-sharing/respect-visibility
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bfe24d9
feat(sdk): Rotate session key when member leaves the room
kaylendog ba9e876
docs(sdk): Outline key rotation scenario on discard handler
kaylendog 635fdd3
refactor(crypto): Lock `GroupSessionCache::get` to `cfg(test)`
kaylendog 7a26db6
tests: Assert room key is rotated when a member leaves the room
kaylendog fd806a9
tests: Assert room key rotated on leave event after being offline
kaylendog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -65,7 +65,10 @@ use ruma::{ | |
| uiaa::{AuthData, AuthType, OAuthParams, UiaaInfo}, | ||
| }, | ||
| assign, | ||
| events::room::{MediaSource, ThumbnailInfo}, | ||
| events::room::{ | ||
| MediaSource, ThumbnailInfo, | ||
| member::{MembershipChange, OriginalSyncRoomMemberEvent}, | ||
| }, | ||
| }; | ||
| #[cfg(feature = "experimental-send-custom-to-device")] | ||
| use ruma::{events::AnyToDeviceEventContent, serde::Raw, to_device::DeviceIdOrAllDevices}; | ||
|
|
@@ -87,7 +90,7 @@ use self::{ | |
| verification::{SasVerification, Verification, VerificationRequest}, | ||
| }; | ||
| use crate::{ | ||
| Client, Error, HttpError, Result, RumaApiError, TransmissionProgress, | ||
| Client, Error, HttpError, Result, Room, RumaApiError, TransmissionProgress, | ||
| attachment::Thumbnail, | ||
| client::{ClientInner, WeakClient}, | ||
| cross_process_lock::CrossProcessLockGuard, | ||
|
|
@@ -1875,6 +1878,8 @@ impl Encryption { | |
| })); | ||
|
|
||
| tasks.receive_historic_room_key_bundles = bundle_receiver_task; | ||
|
|
||
| self.setup_room_membership_session_discard_handler(); | ||
| } | ||
|
|
||
| /// Waits for end-to-end encryption initialization tasks to finish, if any | ||
|
|
@@ -1948,6 +1953,60 @@ impl Encryption { | |
| } | ||
| } | ||
|
|
||
| /// Sets up a handler to rotate room keys when a user leaves a room. | ||
| /// | ||
| /// Previously, it was sufficient to check if we need to rotate the room key | ||
| /// prior to sending a message. However, the history sharing feature | ||
| /// ([MSC4268]) breaks this logic: | ||
| /// | ||
| /// 1. Alice sends a message M1 in room X; | ||
| /// 2. Bob invites Charlie, who joins and immediately leaves the room; | ||
| /// 3. Alice sends another message M2 in room X. | ||
| /// | ||
| /// Under the old logic, Alice would not rotate her key after Charlie | ||
| /// leaves, resulting in M2 being encrypted with the same session as M1. | ||
| /// This would allow Charlie to decrypt M2 if he ever gains access to | ||
| /// the event. | ||
| /// | ||
| /// This handler listens for changes to the room membership, and discards | ||
| /// the current room key if the event is a `leave` event. | ||
| /// | ||
| /// [MSC4268]: https://github.com/matrix-org/matrix-spec-proposals/pull/4268 | ||
| fn setup_room_membership_session_discard_handler(&self) { | ||
| let client = WeakClient::from_client(&self.client); | ||
| self.client.add_event_handler(|ev: OriginalSyncRoomMemberEvent, room: Room| async move { | ||
| let Some(client) = client.get() else { | ||
| // The main client has been dropped. | ||
| return; | ||
| }; | ||
| let Some(user_id) = client.user_id() else { | ||
| // We aren't logged in, so this shouldn't ever happen. | ||
| return; | ||
| }; | ||
|
Comment on lines
+1982
to
+1985
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm tempted to use |
||
| let olm = client.olm_machine().await; | ||
| let Some(olm) = olm.as_ref() else { | ||
| warn!("Cannot discard session - Olm machine is not available"); | ||
| return; | ||
| }; | ||
|
|
||
| if !matches!(ev.membership_change(), MembershipChange::Left) || ev.sender == user_id { | ||
| // We can ignore non-leave events and those that we sent. | ||
| return; | ||
| } | ||
|
|
||
| debug!(room_id = ?room.room_id(), member_id = ?ev.sender, "Discarding session as a user left the room"); | ||
|
|
||
| // Attempt to discard the current room key. This won't do anything if we don't have one, | ||
| // but that's fine since we will create a new room key whenever we try to send a message. | ||
| if let Err(e) = olm.discard_room_key(room.room_id()).await { | ||
| warn!( | ||
| room_id = ?room.room_id(), | ||
| "Error discarding room key after member leave: {e:?}" | ||
| ); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /// Encrypts then send the given content via the `/sendToDevice` end-point | ||
| /// using Olm encryption. | ||
| /// | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.