From 75a508cc279526b7463072836f610cac67ea4e06 Mon Sep 17 00:00:00 2001 From: Tak Wai Wong <64229756+tak-hntlabs@users.noreply.github.com> Date: Wed, 2 Nov 2022 03:02:23 -0700 Subject: [PATCH] Fix issue where a member is forced to leave a room when the invite is marked deleted (#2839) Proposed fix for issue: https://github.com/matrix-org/dendrite/issues/2838 Suppose bob received invites to spaceA and spaceB. When Bob joins spaceA, we add an OutputEvent event to retire the invite. This sets the invite to "deleted" in the database. This makes sense. The bug is in stream_invites.go. Triggered when bob received a new invite for spaceB, and does a client sync. In the block (line 76) `for roomID := range retiredInvites if _, ok := req.Response.Rooms.Invite[roomID]; ok { continue } if _, ok := req.Response.Rooms.Join[roomID]; ok { continue } ... ` Bob is not in either maps even though he had just accepted the invite for spaceA. Consequently, the spaceA invite is treated as a retired invite, and a membership Leave event is generated. What bob sees is that after accepting the invite to spaceB, he lose access to spaceA. ### Pull Request Checklist * [ ] I have added tests for PR _or_ I have justified why this PR doesn't need tests. * [x ] Pull request includes a [sign off below using a legally identifiable name](https://matrix-org.github.io/dendrite/development/contributing#sign-off) _or_ I have already signed off privately Signed-off-by: `Tak Wai Wong ` Co-authored-by: Neil Alexander --- syncapi/streams/stream_invite.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/syncapi/streams/stream_invite.go b/syncapi/streams/stream_invite.go index 700f25c1..e4de30e1 100644 --- a/syncapi/streams/stream_invite.go +++ b/syncapi/streams/stream_invite.go @@ -4,6 +4,7 @@ import ( "context" "crypto/sha256" "encoding/base64" + "math" "strconv" "time" @@ -74,12 +75,14 @@ func (p *InviteStreamProvider) IncrementalSync( return to } for roomID := range retiredInvites { - if _, ok := req.Response.Rooms.Invite[roomID]; ok { - continue - } - if _, ok := req.Response.Rooms.Join[roomID]; ok { + membership, _, err := snapshot.SelectMembershipForUser(ctx, roomID, req.Device.UserID, math.MaxInt64) + // Skip if the user is an existing member of the room. + // Otherwise, the NewLeaveResponse will eject the user from the room unintentionally + if membership == gomatrixserverlib.Join || + err != nil { continue } + lr := types.NewLeaveResponse() h := sha256.Sum256(append([]byte(roomID), []byte(strconv.FormatInt(int64(to), 10))...)) lr.Timeline.Events = append(lr.Timeline.Events, gomatrixserverlib.ClientEvent{ @@ -93,7 +96,6 @@ func (p *InviteStreamProvider) IncrementalSync( Content: gomatrixserverlib.RawJSON(`{"membership":"leave"}`), }) req.Response.Rooms.Leave[roomID] = lr - } return maxID