Skip to content

Commit

Permalink
Merge pull request #135 from mautrix/max/be-12207
Browse files Browse the repository at this point in the history
Add option to delete outdated inbound keys
  • Loading branch information
tulir committed Jun 23, 2023
2 parents 1d9b6a4 + 6728ac5 commit 33c91ff
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions bridge/bridgeconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ type EncryptionConfig struct {
DeletePrevOnNewSession bool `yaml:"delete_prev_on_new_session"`
DeleteOnDeviceDelete bool `yaml:"delete_on_device_delete"`
PeriodicallyDeleteExpired bool `yaml:"periodically_delete_expired"`
DeleteOutdatedInbound bool `yaml:"delete_outdated_inbound"`
} `yaml:"delete_keys"`

VerificationLevels struct {
Expand Down
10 changes: 10 additions & 0 deletions bridge/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ func (helper *CryptoHelper) Init() error {
go helper.mach.ExpiredKeyDeleteLoop(ctx)
}

if encryptionConfig.DeleteKeys.DeleteOutdatedInbound {
deleted, err := helper.store.RedactOutdatedGroupSessions()
if err != nil {
return err
}
if len(deleted) > 0 {
helper.log.Debug().Int("deleted", len(deleted)).Msg("Deleted inbound keys which lacked expiration metadata")
}
}

helper.client.Syncer = &cryptoSyncer{helper.mach}
helper.client.Store = helper.store

Expand Down
16 changes: 16 additions & 0 deletions crypto/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,22 @@ func (store *SQLCryptoStore) RedactExpiredGroupSessions() ([]id.SessionID, error
return sessionIDs, err
}

func (store *SQLCryptoStore) RedactOutdatedGroupSessions() ([]id.SessionID, error) {
res, err := store.DB.Query(`
UPDATE crypto_megolm_inbound_session
SET withheld_code=$1, withheld_reason=$2, session=NULL, forwarding_chains=NULL
WHERE account_id=$3 AND session IS NOT NULL AND received_at IS NULL
RETURNING session_id
`, event.RoomKeyWithheldBeeperRedacted, "Session redacted: outdated", store.AccountID)
var sessionIDs []id.SessionID
for res.Next() {
var sessionID id.SessionID
_ = res.Scan(&sessionID)
sessionIDs = append(sessionIDs, sessionID)
}
return sessionIDs, err
}

func (store *SQLCryptoStore) PutWithheldGroupSession(content event.RoomKeyWithheldEventContent) error {
_, err := store.DB.Exec("INSERT INTO crypto_megolm_inbound_session (session_id, sender_key, room_id, withheld_code, withheld_reason, received_at, account_id) VALUES ($1, $2, $3, $4, $5, $6, $7)",
content.SessionID, content.SenderKey, content.RoomID, content.Code, content.Reason, time.Now().UTC(), store.AccountID)
Expand Down
6 changes: 6 additions & 0 deletions crypto/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type Store interface {
RedactGroupSessions(id.RoomID, id.SenderKey, string) ([]id.SessionID, error)
// RedactExpiredGroupSessions removes the session data for all inbound Megolm sessions that have expired.
RedactExpiredGroupSessions() ([]id.SessionID, error)
// RedactOutdatedGroupSessions removes the session data for all inbound Megolm sessions that are lacking the expiration metadata.
RedactOutdatedGroupSessions() ([]id.SessionID, error)
// PutWithheldGroupSession tells the store that a specific Megolm session was withheld.
PutWithheldGroupSession(event.RoomKeyWithheldEventContent) error
// GetWithheldGroupSession gets the event content that was previously inserted with PutWithheldGroupSession.
Expand Down Expand Up @@ -317,6 +319,10 @@ func (gs *MemoryStore) RedactExpiredGroupSessions() ([]id.SessionID, error) {
return nil, fmt.Errorf("not implemented")
}

func (gs *MemoryStore) RedactOutdatedGroupSessions() ([]id.SessionID, error) {
return nil, fmt.Errorf("not implemented")
}

func (gs *MemoryStore) getWithheldGroupSessions(roomID id.RoomID, senderKey id.SenderKey) map[id.SessionID]*event.RoomKeyWithheldEventContent {
room, ok := gs.WithheldGroupSessions[roomID]
if !ok {
Expand Down

0 comments on commit 33c91ff

Please sign in to comment.