GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
fix(device): compare purge slot id verbatim and surface shared-JID lookup failures
Aldino Kemala69b1fb085c5
diff --git a/src/infrastructure/whatsapp/device_logout_keepslot_test.go b/src/infrastructure/whatsapp/device_logout_keepslot_test.goindex 96aaed99..3d224a23 100644--- a/src/infrastructure/whatsapp/device_logout_keepslot_test.go+++ b/src/infrastructure/whatsapp/device_logout_keepslot_test.go@@ -3,6 +3,7 @@ package whatsappimport ("context""errors"+ "slices""strings""testing""time"@@ -23,6 +24,7 @@ type keepSlotStubStorage struct {domainChatStorage.IChatStorageRepositorysaveErr errordeleteDataErr error+ listRecordsErr errorsavedRecords []*domainChatStorage.DeviceRecorddeviceRecords []*domainChatStorage.DeviceRecorddeletedData []string@@ -52,7 +54,7 @@ func (s *keepSlotStubStorage) GetChatwootDeviceConfig(string) (*domainChatStorag}func (s *keepSlotStubStorage) ListDeviceRecords() ([]*domainChatStorage.DeviceRecord, error) {- return s.deviceRecords, nil+ return s.deviceRecords, s.listRecordsErr}// assertStoreLacksJID fails if any device row in the container still matches the given@@ -487,3 +489,49 @@ func TestRemoteLogoutCallback_KeepsSlot(t *testing.T) {assertStoreLacksJID(t, ctx, primaryStore, nonAD)assertStoreLacksJID(t, ctx, keysStore, nonAD)}++// A slot id is stored exactly as supplied, padding included. The shared-JID guard must+// therefore compare it verbatim: trimming it would stop " sales " from matching its own+// record, the slot would read as another claim on its own number, and its chat partition+// would survive a purge that reported success.+func TestPurgeDevice_PaddedSlotIDStillDeletesItsOwnJIDPartition(t *testing.T) {+ ctx := context.Background()+ storage := &keepSlotStubStorage{}+ manager := NewDeviceManager(nil, nil, storage)++ const slotID = " sales "+ nonAD := "[email protected]"+ manager.devices[slotID] = &DeviceInstance{id: slotID, jid: nonAD, createdAt: time.Now()}+ storage.deviceRecords = []*domainChatStorage.DeviceRecord{{DeviceID: slotID, JID: nonAD}}++ if err := manager.PurgeDevice(ctx, slotID); err != nil {+ t.Fatalf("PurgeDevice returned error: %v", err)+ }+ if !slices.Contains(storage.deletedData, nonAD) {+ t.Fatalf("expected JID partition %q to be deleted for sole slot %q, got deletes %v", nonAD, slotID, storage.deletedData)+ }+}++// A failed lookup is not evidence that another slot claims the number. Skipping the+// partition is the safe response, but it must surface as an error so the slot stays+// registered and DELETE can be retried rather than reporting a success that lost data.+func TestPurgeDevice_SurfacesSharedJIDLookupFailureAndKeepsSlot(t *testing.T) {+ ctx := context.Background()+ storage := &keepSlotStubStorage{listRecordsErr: errors.New("database is locked")}+ manager := NewDeviceManager(nil, nil, storage)++ const slotID = "solo"+ nonAD := "[email protected]"+ manager.devices[slotID] = &DeviceInstance{id: slotID, jid: nonAD, createdAt: time.Now()}++ err := manager.PurgeDevice(ctx, slotID)+ if err == nil {+ t.Fatal("expected PurgeDevice to surface the shared-JID lookup failure, got nil")+ }+ if slices.Contains(storage.deletedData, nonAD) {+ t.Fatalf("expected JID partition %q to be skipped when the claim check failed, got deletes %v", nonAD, storage.deletedData)+ }+ if _, ok := manager.GetDevice(slotID); !ok {+ t.Fatal("expected slot to remain registered so the purge can be retried")+ }+}diff --git a/src/infrastructure/whatsapp/device_manager.go b/src/infrastructure/whatsapp/device_manager.goindex 8629f35f..4b580244 100644--- a/src/infrastructure/whatsapp/device_manager.go+++ b/src/infrastructure/whatsapp/device_manager.go@@ -208,24 +208,29 @@ func (m *DeviceManager) resolveDeviceForPurge(deviceID string) (*DeviceInstance,// nonADJIDClaimedByOtherSlot reports whether another registered slot still uses the// same bare-number JID for chat-storage partitioning (companion slots on one number).-func (m *DeviceManager) nonADJIDClaimedByOtherSlot(excludeSlotID, nonADJID string) bool {+//+// excludeSlotID is compared verbatim: slots are stored under the id as supplied, so+// trimming it here would stop a padded id such as " sales " from matching its own+// record, and the slot would then read as somebody else's claim on its own number.+//+// A lookup failure is returned rather than swallowed. The caller cannot tell "claimed"+// from "could not tell" otherwise, and would drop the partition while reporting success.+func (m *DeviceManager) nonADJIDClaimedByOtherSlot(excludeSlotID, nonADJID string) (bool, error) {nonADJID = strings.TrimSpace(nonADJID)- excludeSlotID = strings.TrimSpace(excludeSlotID)if nonADJID == "" {- return false+ return false, nil}if m.storage != nil {records, err := m.storage.ListDeviceRecords()if err != nil {- logrus.WithError(err).Warn("[DEVICE_MANAGER] failed to list device records while checking shared JID partitions")- return true+ return true, err}for _, rec := range records {if rec == nil || rec.DeviceID == excludeSlotID {continue}if strings.TrimSpace(rec.JID) == nonADJID {- return true+ return true, nil}}}@@ -236,10 +241,10 @@ func (m *DeviceManager) nonADJIDClaimedByOtherSlot(excludeSlotID, nonADJID strincontinue}if strings.TrimSpace(inst.JID()) == nonADJID {- return true+ return true, nil}}- return false+ return false, nil}func (m *DeviceManager) RemoveDevice(id string) {@@ -354,10 +359,15 @@ func (m *DeviceManager) PurgeDevice(ctx context.Context, deviceID string) errorif m.storage != nil {partitionKeys := []string{resolvedID}if jidKey := strings.TrimSpace(inst.JID()); jidKey != "" && jidKey != resolvedID {- if !m.nonADJIDClaimedByOtherSlot(resolvedID, jidKey) {- partitionKeys = append(partitionKeys, jidKey)- } else {+ claimed, err := m.nonADJIDClaimedByOtherSlot(resolvedID, jidKey)+ switch {+ case err != nil:+ logrus.WithError(err).Warnf("[DEVICE_MANAGER] failed to check shared chatstorage partition %s for slot %s", jidKey, resolvedID)+ recordErr(err)+ case claimed:logrus.Warnf("[DEVICE_MANAGER] skipping shared chatstorage partition %s for slot %s (another slot still claims this number)", jidKey, resolvedID)+ default:+ partitionKeys = append(partitionKeys, jidKey)}}for _, key := range partitionKeys {