Skip to content

Commit ce4daa5

Browse files
committed
store: prevent misuse of deleted devices
Fixes #1099
1 parent 234105b commit ce4daa5

3 files changed

Lines changed: 30 additions & 11 deletions

File tree

client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ func (cli *Client) connect(ctx context.Context) error {
503503
}
504504

505505
func (cli *Client) unlockedConnect(ctx context.Context) error {
506+
if cli.Store.Deleted {
507+
return store.ErrDeviceDeleted
508+
}
506509
if cli.socket != nil {
507510
if !cli.socket.IsConnected() {
508511
cli.unlockedDisconnect()

store/sqlstore/container.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,17 +265,7 @@ func (c *Container) PutDevice(ctx context.Context, device *store.Device) error {
265265

266266
func (c *Container) initializeDevice(device *store.Device) {
267267
innerStore := NewSQLStore(c, *device.ID)
268-
device.Identities = innerStore
269-
device.Sessions = innerStore
270-
device.PreKeys = innerStore
271-
device.SenderKeys = innerStore
272-
device.AppStateKeys = innerStore
273-
device.AppState = innerStore
274-
device.Contacts = innerStore
275-
device.ChatSettings = innerStore
276-
device.MsgSecrets = innerStore
277-
device.PrivacyTokens = innerStore
278-
device.EventBuffer = innerStore
268+
device.SetAllStores(innerStore)
279269
device.LIDs = c.LIDMap
280270
device.Container = c
281271
device.Initialized = true

store/store.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package store
99

1010
import (
1111
"context"
12+
"errors"
1213
"time"
1314

1415
"github.com/google/uuid"
@@ -226,6 +227,7 @@ type Device struct {
226227
FacebookUUID uuid.UUID
227228

228229
Initialized bool
230+
Deleted bool
229231
Identities IdentityStore
230232
Sessions SessionStore
231233
PreKeys PreKeyStore
@@ -259,20 +261,44 @@ func (device *Device) GetLID() types.JID {
259261
return device.LID
260262
}
261263

264+
var ErrDeviceDeleted = errors.New("invalid use of deleted device")
265+
262266
func (device *Device) Save(ctx context.Context) error {
267+
if device.Deleted {
268+
return ErrDeviceDeleted
269+
}
263270
return device.Container.PutDevice(ctx, device)
264271
}
265272

266273
func (device *Device) Delete(ctx context.Context) error {
274+
if device.Deleted {
275+
return nil
276+
}
267277
err := device.Container.DeleteDevice(ctx, device)
268278
if err != nil {
269279
return err
270280
}
271281
device.ID = nil
272282
device.LID = types.EmptyJID
283+
device.Deleted = true
284+
device.SetAllStores(&NoopStore{ErrDeviceDeleted})
273285
return nil
274286
}
275287

288+
func (device *Device) SetAllStores(store AllSessionSpecificStores) {
289+
device.Identities = store
290+
device.Sessions = store
291+
device.PreKeys = store
292+
device.SenderKeys = store
293+
device.AppStateKeys = store
294+
device.AppState = store
295+
device.Contacts = store
296+
device.ChatSettings = store
297+
device.MsgSecrets = store
298+
device.PrivacyTokens = store
299+
device.EventBuffer = store
300+
}
301+
276302
func (device *Device) GetAltJID(ctx context.Context, jid types.JID) (types.JID, error) {
277303
if device == nil {
278304
return types.EmptyJID, nil

0 commit comments

Comments
 (0)