Skip to content

feat(sdk): Rotate session keys when a member leaves the room#6292

Open
kaylendog wants to merge 10 commits intomainfrom
kaylendog/history-sharing/respect-visibility
Open

feat(sdk): Rotate session keys when a member leaves the room#6292
kaylendog wants to merge 10 commits intomainfrom
kaylendog/history-sharing/respect-visibility

Conversation

@kaylendog
Copy link
Contributor

@kaylendog kaylendog commented Mar 16, 2026

This PR adds an event handler on start that discards (and hence rotates) the current session whenever we see a leave membership event.

Part of element-hq/element-meta#3078

  • I've documented the public API Changes in the appropriate CHANGELOG.md files.
  • This PR was made with the help of AI.

Signed-off-by: Skye Elliot [email protected]

@kaylendog kaylendog self-assigned this Mar 16, 2026
@kaylendog kaylendog force-pushed the kaylendog/history-sharing/respect-visibility branch 2 times, most recently from d15f1b2 to 11a27af Compare March 16, 2026 11:25
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 16, 2026

Merging this PR will not alter performance

✅ 50 untouched benchmarks


Comparing kaylendog/history-sharing/respect-visibility (177f38a) with main (a4bb2f3)

Open in CodSpeed

@kaylendog kaylendog force-pushed the kaylendog/history-sharing/respect-visibility branch from 11a27af to f0c2d33 Compare March 16, 2026 11:46
Comment on lines +1964 to +1967
let Some(user_id) = client.user_id() else {
// We aren't logged in, so this shouldn't ever happen.
return;
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm tempted to use unwrap here, but I am wary that if any of these assumptions are wrong, this will crash the client entirely.

@kaylendog kaylendog force-pushed the kaylendog/history-sharing/respect-visibility branch from f0c2d33 to f0d12dd Compare March 16, 2026 12:30
@codecov
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

❌ Patch coverage is 65.21739% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.90%. Comparing base (d6c666a) to head (177f38a).
⚠️ Report is 30 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
crates/matrix-sdk/src/encryption/mod.rs 63.63% 6 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6292      +/-   ##
==========================================
- Coverage   89.94%   89.90%   -0.05%     
==========================================
  Files         374      374              
  Lines      102613   102635      +22     
  Branches   102613   102635      +22     
==========================================
- Hits        92299    92270      -29     
- Misses       6771     6799      +28     
- Partials     3543     3566      +23     

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

@kaylendog kaylendog marked this pull request as ready for review March 16, 2026 12:42
@kaylendog kaylendog requested a review from a team as a code owner March 16, 2026 12:42
@kaylendog kaylendog requested review from poljar and removed request for a team March 16, 2026 12:42
@richvdh
Copy link
Member

richvdh commented Mar 16, 2026

@kaylendog in the description of the PR, could you link the issue that explains why we need to do this?

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.

mostly lgtm, a couple of small comments

/// Sets up a handler to rotate room keys when a user leaves a room.
fn setup_room_membership_session_discard_handler(&self) {
let client = WeakClient::from_client(&self.client);
self.client.add_event_handler(move |ev: SyncRoomMemberEvent, room: Room| async move {
Copy link
Member

Choose a reason for hiding this comment

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

do things added by add_event_handler definitely get called when state changes outside the timeline section of the sync response?

It might be good to do a test which does something like:

  • Charlie leaves the room
  • Alice sends Message 1
  • Alice goes offline for a while
  • Bob invites Charlie
  • Charlie joins
  • Charlie leaves
  • Bob sends a bunch more messages
  • Alice comes back online
  • Alice sends Message 2

The point here is that, from Alice's point of view, Charlie is in the leave state at both Message 1 and Message 2, and she'll never even see the invite/join/leave stage unless she backfills. She should still discard the session because she'll see a different leave event for Bob, but it's a bit subtle.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This exact scenario did occur to me, but I want to avoid walking back through all the state changes up until we sent the last message... I'm hoping the handler fires for us here - I'll test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've implemented a test for this in 4a93def which should be passing, but I do think it's worth discussing the potential sliding sync a bit further.

@kaylendog kaylendog requested a review from a team as a code owner March 17, 2026 14:44
@kaylendog kaylendog force-pushed the kaylendog/history-sharing/respect-visibility branch from 02f3839 to 177f38a Compare March 17, 2026 15:17
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.

2 participants