From 0f1786370833ac8ffc51684b9534c5e70e440b6e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Mon, 5 Oct 2020 22:32:15 +0300 Subject: [PATCH] Add command to reset the megolm session in a room --- commands.go | 13 +++++++++++++ crypto.go | 7 +++++++ main.go | 1 + 3 files changed, 21 insertions(+) diff --git a/commands.go b/commands.go index 5d7cb00..5d4b72c 100644 --- a/commands.go +++ b/commands.go @@ -123,6 +123,8 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) { handler.CommandDeletePortal(ce) case "delete-all-portals": handler.CommandDeleteAllPortals(ce) + case "discard-megolm-session", "discard-session": + handler.CommandDiscardMegolmSession(ce) case "dev-test": handler.CommandDevTest(ce) case "set-pl": @@ -163,6 +165,17 @@ func (handler *CommandHandler) CommandMux(ce *CommandEvent) { } } +func (handler *CommandHandler) CommandDiscardMegolmSession(ce *CommandEvent) { + if handler.bridge.Crypto == nil { + ce.Reply("This bridge instance doesn't have end-to-bridge encryption enabled") + } else if !ce.User.Admin { + ce.Reply("Only the bridge admin can reset Megolm sessions") + } else { + handler.bridge.Crypto.ResetSession(ce.RoomID) + ce.Reply("Successfully reset Megolm session in this room. New decryption keys will be shared the next time a message is sent from WhatsApp.") + } +} + func (handler *CommandHandler) CommandRelaybot(ce *CommandEvent) { if handler.bridge.Relaybot == nil { ce.Reply("The relaybot is disabled") diff --git a/crypto.go b/crypto.go index ee05cc2..cae1ee9 100644 --- a/crypto.go +++ b/crypto.go @@ -189,6 +189,13 @@ func (helper *CryptoHelper) WaitForSession(roomID id.RoomID, senderKey id.Sender return helper.mach.WaitForSession(roomID, senderKey, sessionID, timeout) } +func (helper *CryptoHelper) ResetSession(roomID id.RoomID) { + err := helper.mach.CryptoStore.RemoveOutboundGroupSession(roomID) + if err != nil { + helper.log.Debugfln("Error manually removing outbound group session in %s: %v", roomID, err) + } +} + func (helper *CryptoHelper) HandleMemberEvent(evt *event.Event) { helper.mach.HandleMemberEvent(evt) } diff --git a/main.go b/main.go index a4582cf..6ea9f7f 100644 --- a/main.go +++ b/main.go @@ -154,6 +154,7 @@ type Crypto interface { Decrypt(*event.Event) (*event.Event, error) Encrypt(id.RoomID, event.Type, event.Content) (*event.EncryptedEventContent, error) WaitForSession(id.RoomID, id.SenderKey, id.SessionID, time.Duration) bool + ResetSession(id.RoomID) Init() error Start() Stop()