// Copyright (c) 2026 Xessenger // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. package bridgekit import ( "context" "slices" "github.com/rs/zerolog" "maunium.net/go/mautrix/event" "maunium.net/go/mautrix/id" ) // refusedTypes are the event types bridgev2 forwards from Matrix to the network // (bridgev2/matrix.Connector.Init) that a switched-on user's app always sends encrypted in an // encrypted room. The rest pass unencrypted: member events (MSC4362 leaves them unencrypted, and a // user must be able to leave a room), redactions (Matrix cannot encrypt them) and // com.beeper.delete_chat and com.beeper.accept_message_request, which carry no content and which the // API sends for the user. Reactions are refused only with encryption.msc4392, under which the app // encrypts them. var refusedTypes = []event.Type{ event.EventMessage, event.EventSticker, event.EventUnstablePollStart, event.EventUnstablePollResponse, event.StatePowerLevels, event.StateRoomName, event.BeeperSendState, event.StateRoomAvatar, event.StateTopic, event.StateTombstone, event.StateBeeperDisappearingTimer, } // refuses reports whether refusePlaintext drops an unencrypted event of the type. func (k *Kit) refuses(eventType event.Type) bool { return slices.Contains(refusedTypes, eventType) || (eventType == event.EventReaction && k.main.Config.Encryption.MSC4392) } // installRefusal puts the extension's checks in front of bridgev2's Matrix event handlers, which the // event processor runs one after another on the same event. func (k *Kit) installRefusal() { types := refusedTypes if k.main.Config.Encryption.MSC4392 { types = append(types[:len(types):len(types)], event.EventReaction) } for _, eventType := range types { k.conn.EventProcessor.PrependHandler(eventType, k.refusePlaintext) } k.conn.EventProcessor.PrependHandler(event.StateMember, k.watchMembership) } // refusePlaintext drops an unencrypted event a switched-on user's account sent into an encrypted // room. The user's app encrypts everything it sends there; an unencrypted event comes from somebody // else holding the account's credentials, such as the server, and must not reach the network in the // user's name. Decrypted events (from the user's cross-signed devices, verification_levels.send) // arrive marked as encrypted and pass. // // bridgev2 ignores whatever the bridge bot sent, so the event is handed on as the bot's. func (k *Kit) refusePlaintext(ctx context.Context, evt *event.Event) { if evt.Mautrix.WasEncrypted || !k.active() || k.user(evt.Sender) == nil || !k.isEncrypted(ctx, evt.RoomID) { return } zerolog.Ctx(ctx).Warn(). Stringer("event_type", evt.Type). Stringer("room_id", evt.RoomID). Stringer("sender", evt.Sender). Stringer("event_id", evt.ID). Msg("Dropping unencrypted event a switched-on user's account sent into an encrypted room") evt.Sender = k.conn.Bot.UserID } // watchMembership notices a switched-on user arriving in one of their encrypted rooms, which is when // the room's metadata can be sent to them, and leaving one or being banned from it. A room the user // left has no members (ownerLeft), so it is brought in line then, on its own (syncLeftRoom), not with // the next sync: the members would read meanwhile what reaches the room, which bridgev2 does not leave // on the messenger. mautrix's state store has the membership before the handlers run. func (k *Kit) watchMembership(ctx context.Context, evt *event.Event) { if !k.active() || evt.StateKey == nil { return } target := id.UserID(*evt.StateKey) if k.user(target) == nil { return } switch evt.Content.AsMember().Membership { case event.MembershipJoin, event.MembershipInvite: if k.isEncrypted(ctx, evt.RoomID) { k.memberArrived(ctx, evt.RoomID, target) } case event.MembershipLeave, event.MembershipBan: if k.membersOf(target) != nil && k.isEncrypted(ctx, evt.RoomID) { go k.syncLeftRoom(k.backgroundContext(), target, evt.RoomID) } } }