Skip to content

GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.

feat: data leak issue #513

Aldino Kemalb48e387d577a
diff --git a/AGENTS.md b/CLAUDE.md
similarity index 100%
rename from AGENTS.md
rename to CLAUDE.md
diff --git a/src/domains/chatstorage/interfaces.go b/src/domains/chatstorage/interfaces.go
index c3183682..e436de81 100644
--- a/src/domains/chatstorage/interfaces.go
+++ b/src/domains/chatstorage/interfaces.go
@@ -13,8 +13,10 @@ type IChatStorageRepository interface {
CreateMessage(ctx context.Context, evt *events.Message) error
StoreChat(chat *Chat) error
GetChat(jid string) (*Chat, error)
+ GetChatByDevice(deviceID, jid string) (*Chat, error)
GetChats(filter *ChatFilter) ([]*Chat, error)
DeleteChat(jid string) error
+ DeleteChatByDevice(deviceID, jid string) error
// Message operations
StoreMessage(message *Message) error
@@ -23,13 +25,16 @@ type IChatStorageRepository interface {
GetMessages(filter *MessageFilter) ([]*Message, error)
SearchMessages(chatJID, searchText string, limit int) ([]*Message, error) // Database-level search
DeleteMessage(id, chatJID string) error
+ DeleteMessageByDevice(deviceID, id, chatJID string) error
StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time) error
// Statistics
GetChatMessageCount(chatJID string) (int64, error)
+ GetChatMessageCountByDevice(deviceID, chatJID string) (int64, error)
GetTotalMessageCount() (int64, error)
GetTotalChatCount() (int64, error)
GetChatNameWithPushName(jid types.JID, chatJID string, senderUser string, pushName string) string
+ GetChatNameWithPushNameByDevice(deviceID string, jid types.JID, chatJID string, senderUser string, pushName string) string
GetStorageStatistics() (chatCount int64, messageCount int64, err error)
// Cleanup operations
diff --git a/src/infrastructure/chatstorage/device_repository.go b/src/infrastructure/chatstorage/device_repository.go
index 25b5cf9b..4c0332a3 100644
--- a/src/infrastructure/chatstorage/device_repository.go
+++ b/src/infrastructure/chatstorage/device_repository.go
@@ -58,7 +58,11 @@ func (r *DeviceRepository) StoreChat(chat *domainChatStorage.Chat) error {
}
func (r *DeviceRepository) GetChat(jid string) (*domainChatStorage.Chat, error) {
- return r.base.GetChat(jid)
+ return r.base.GetChatByDevice(r.deviceID, jid)
+}
+
+func (r *DeviceRepository) GetChatByDevice(deviceID, jid string) (*domainChatStorage.Chat, error) {
+ return r.base.GetChatByDevice(deviceID, jid)
}
func (r *DeviceRepository) GetChats(filter *domainChatStorage.ChatFilter) ([]*domainChatStorage.Chat, error) {
@@ -76,7 +80,11 @@ func (r *DeviceRepository) GetChats(filter *domainChatStorage.ChatFilter) ([]*do
}
func (r *DeviceRepository) DeleteChat(jid string) error {
- return r.base.DeleteChat(jid)
+ return r.base.DeleteChatByDevice(r.deviceID, jid)
+}
+
+func (r *DeviceRepository) DeleteChatByDevice(deviceID, jid string) error {
+ return r.base.DeleteChatByDevice(deviceID, jid)
}
func (r *DeviceRepository) StoreMessage(message *domainChatStorage.Message) error {
@@ -100,7 +108,11 @@ func (r *DeviceRepository) SearchMessages(chatJID, searchText string, limit int)
}
func (r *DeviceRepository) DeleteMessage(id, chatJID string) error {
- return r.base.DeleteMessage(id, chatJID)
+ return r.base.DeleteMessageByDevice(r.deviceID, id, chatJID)
+}
+
+func (r *DeviceRepository) DeleteMessageByDevice(deviceID, id, chatJID string) error {
+ return r.base.DeleteMessageByDevice(deviceID, id, chatJID)
}
func (r *DeviceRepository) StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time) error {
@@ -108,7 +120,11 @@ func (r *DeviceRepository) StoreSentMessageWithContext(ctx context.Context, mess
}
func (r *DeviceRepository) GetChatMessageCount(chatJID string) (int64, error) {
- return r.base.GetChatMessageCount(chatJID)
+ return r.base.GetChatMessageCountByDevice(r.deviceID, chatJID)
+}
+
+func (r *DeviceRepository) GetChatMessageCountByDevice(deviceID, chatJID string) (int64, error) {
+ return r.base.GetChatMessageCountByDevice(deviceID, chatJID)
}
func (r *DeviceRepository) GetTotalMessageCount() (int64, error) {
@@ -120,7 +136,11 @@ func (r *DeviceRepository) GetTotalChatCount() (int64, error) {
}
func (r *DeviceRepository) GetChatNameWithPushName(jid types.JID, chatJID string, senderUser string, pushName string) string {
- return r.base.GetChatNameWithPushName(jid, chatJID, senderUser, pushName)
+ return r.base.GetChatNameWithPushNameByDevice(r.deviceID, jid, chatJID, senderUser, pushName)
+}
+
+func (r *DeviceRepository) GetChatNameWithPushNameByDevice(deviceID string, jid types.JID, chatJID string, senderUser string, pushName string) string {
+ return r.base.GetChatNameWithPushNameByDevice(deviceID, jid, chatJID, senderUser, pushName)
}
func (r *DeviceRepository) GetStorageStatistics() (chatCount int64, messageCount int64, err error) {
diff --git a/src/infrastructure/chatstorage/sqlite_repository.go b/src/infrastructure/chatstorage/sqlite_repository.go
index 65ab49cb..b0afc59d 100644
--- a/src/infrastructure/chatstorage/sqlite_repository.go
+++ b/src/infrastructure/chatstorage/sqlite_repository.go
@@ -65,6 +65,22 @@ func (r *SQLiteRepository) GetChat(jid string) (*domainChatStorage.Chat, error)
return chat, err
}
+// GetChatByDevice retrieves a chat by JID for a specific device
+func (r *SQLiteRepository) GetChatByDevice(deviceID, jid string) (*domainChatStorage.Chat, error) {
+ query := `
+ SELECT device_id, jid, name, last_message_time, ephemeral_expiration, created_at, updated_at
+ FROM chats
+ WHERE jid = ? AND device_id = ?
+ `
+
+ chat, err := r.scanChat(r.db.QueryRow(query, jid, deviceID))
+ if err == sql.ErrNoRows {
+ return nil, nil
+ }
+
+ return chat, err
+}
+
// GetMessageByID retrieves a message by its ID from any chat
// This is more efficient than searching through all chats
func (r *SQLiteRepository) GetMessageByID(id string) (*domainChatStorage.Message, error) {
@@ -172,6 +188,29 @@ func (r *SQLiteRepository) DeleteChat(jid string) error {
return tx.Commit()
}
+// DeleteChatByDevice deletes a chat and all its messages for a specific device
+func (r *SQLiteRepository) DeleteChatByDevice(deviceID, jid string) error {
+ tx, err := r.db.Begin()
+ if err != nil {
+ return err
+ }
+ defer tx.Rollback()
+
+ // Delete messages first (foreign key constraint)
+ _, err = tx.Exec("DELETE FROM messages WHERE chat_jid = ? AND device_id = ?", jid, deviceID)
+ if err != nil {
+ return err
+ }
+
+ // Delete chat
+ _, err = tx.Exec("DELETE FROM chats WHERE jid = ? AND device_id = ?", jid, deviceID)
+ if err != nil {
+ return err
+ }
+
+ return tx.Commit()
+}
+
// StoreMessage creates or updates a message
func (r *SQLiteRepository) StoreMessage(message *domainChatStorage.Message) error {
now := time.Now()
@@ -419,6 +458,12 @@ func (r *SQLiteRepository) DeleteMessage(id, chatJID string) error {
return err
}
+// DeleteMessageByDevice deletes a specific message for a specific device
+func (r *SQLiteRepository) DeleteMessageByDevice(deviceID, id, chatJID string) error {
+ _, err := r.db.Exec("DELETE FROM messages WHERE id = ? AND chat_jid = ? AND device_id = ?", id, chatJID, deviceID)
+ return err
+}
+
// getCount is a private helper for count queries
func (r *SQLiteRepository) getCount(query string, args ...any) (int64, error) {
var count int64
@@ -453,6 +498,11 @@ func (r *SQLiteRepository) GetChatMessageCount(chatJID string) (int64, error) {
return r.getCount("SELECT COUNT(*) FROM messages WHERE chat_jid = ?", chatJID)
}
+// GetChatMessageCountByDevice returns the number of messages in a chat for a specific device
+func (r *SQLiteRepository) GetChatMessageCountByDevice(deviceID, chatJID string) (int64, error) {
+ return r.getCount("SELECT COUNT(*) FROM messages WHERE chat_jid = ? AND device_id = ?", chatJID, deviceID)
+}
+
// GetTotalMessageCount returns the total number of messages
func (r *SQLiteRepository) GetTotalMessageCount() (int64, error) {
return r.getCount("SELECT COUNT(*) FROM messages")
@@ -636,6 +686,49 @@ func (r *SQLiteRepository) GetChatNameWithPushName(jid types.JID, chatJID string
return name
}
+// GetChatNameWithPushNameByDevice determines the appropriate name for a chat with pushname support (device-scoped)
+func (r *SQLiteRepository) GetChatNameWithPushNameByDevice(deviceID string, jid types.JID, chatJID string, senderUser string, pushName string) string {
+ // Special handling for status@broadcast - always return "Status"
+ if chatJID == "status@broadcast" || jid.String() == "status@broadcast" {
+ return "Status"
+ }
+
+ // First, check if chat already exists with a name (device-scoped!)
+ existingChat, err := r.GetChatByDevice(deviceID, chatJID)
+ if err == nil && existingChat != nil && existingChat.Name != "" {
+ // If we have a pushname and the existing name is just a phone number/JID user, update it
+ if pushName != "" && (existingChat.Name == jid.User || existingChat.Name == senderUser) {
+ return pushName
+ }
+ return existingChat.Name
+ }
+
+ // Determine chat type and name
+ var name string
+
+ switch jid.Server {
+ case "g.us":
+ // This is a group chat
+ // For now, use a generic name - this can be enhanced later with group info
+ name = fmt.Sprintf("Group %s", jid.User)
+ case "newsletter":
+ // This is a newsletter/channel
+ name = fmt.Sprintf("Newsletter %s", jid.User)
+ default:
+ // This is an individual contact
+ // Priority: pushName > senderUser > JID user
+ if pushName != "" && pushName != senderUser && pushName != jid.User {
+ name = pushName
+ } else if senderUser != "" {
+ name = senderUser
+ } else {
+ name = jid.User
+ }
+ }
+
+ return name
+}
+
func (r *SQLiteRepository) CreateMessage(ctx context.Context, evt *events.Message) error {
if evt == nil || evt.Message == nil {
return nil
diff --git a/src/infrastructure/whatsapp/chatstorage_wrapper.go b/src/infrastructure/whatsapp/chatstorage_wrapper.go
index 3c2112ec..6468afe8 100644
--- a/src/infrastructure/whatsapp/chatstorage_wrapper.go
+++ b/src/infrastructure/whatsapp/chatstorage_wrapper.go
@@ -61,7 +61,11 @@ func (r *deviceChatStorage) StoreChat(chat *domainChatStorage.Chat) error {
}
func (r *deviceChatStorage) GetChat(jid string) (*domainChatStorage.Chat, error) {
- return r.base.GetChat(jid)
+ return r.base.GetChatByDevice(r.deviceID, jid)
+}
+
+func (r *deviceChatStorage) GetChatByDevice(deviceID, jid string) (*domainChatStorage.Chat, error) {
+ return r.base.GetChatByDevice(deviceID, jid)
}
func (r *deviceChatStorage) GetChats(filter *domainChatStorage.ChatFilter) ([]*domainChatStorage.Chat, error) {
@@ -81,7 +85,11 @@ func (r *deviceChatStorage) GetChats(filter *domainChatStorage.ChatFilter) ([]*d
}
func (r *deviceChatStorage) DeleteChat(jid string) error {
- return r.base.DeleteChat(jid)
+ return r.base.DeleteChatByDevice(r.deviceID, jid)
+}
+
+func (r *deviceChatStorage) DeleteChatByDevice(deviceID, jid string) error {
+ return r.base.DeleteChatByDevice(deviceID, jid)
}
func (r *deviceChatStorage) StoreMessage(message *domainChatStorage.Message) error {
@@ -105,7 +113,11 @@ func (r *deviceChatStorage) SearchMessages(chatJID, searchText string, limit int
}
func (r *deviceChatStorage) DeleteMessage(id, chatJID string) error {
- return r.base.DeleteMessage(id, chatJID)
+ return r.base.DeleteMessageByDevice(r.deviceID, id, chatJID)
+}
+
+func (r *deviceChatStorage) DeleteMessageByDevice(deviceID, id, chatJID string) error {
+ return r.base.DeleteMessageByDevice(deviceID, id, chatJID)
}
func (r *deviceChatStorage) StoreSentMessageWithContext(ctx context.Context, messageID string, senderJID string, recipientJID string, content string, timestamp time.Time) error {
@@ -113,7 +125,11 @@ func (r *deviceChatStorage) StoreSentMessageWithContext(ctx context.Context, mes
}
func (r *deviceChatStorage) GetChatMessageCount(chatJID string) (int64, error) {
- return r.base.GetChatMessageCount(chatJID)
+ return r.base.GetChatMessageCountByDevice(r.deviceID, chatJID)
+}
+
+func (r *deviceChatStorage) GetChatMessageCountByDevice(deviceID, chatJID string) (int64, error) {
+ return r.base.GetChatMessageCountByDevice(deviceID, chatJID)
}
func (r *deviceChatStorage) GetTotalMessageCount() (int64, error) {
@@ -125,7 +141,11 @@ func (r *deviceChatStorage) GetTotalChatCount() (int64, error) {
}
func (r *deviceChatStorage) GetChatNameWithPushName(jid types.JID, chatJID string, senderUser string, pushName string) string {
- return r.base.GetChatNameWithPushName(jid, chatJID, senderUser, pushName)
+ return r.base.GetChatNameWithPushNameByDevice(r.deviceID, jid, chatJID, senderUser, pushName)
+}
+
+func (r *deviceChatStorage) GetChatNameWithPushNameByDevice(deviceID string, jid types.JID, chatJID string, senderUser string, pushName string) string {
+ return r.base.GetChatNameWithPushNameByDevice(deviceID, jid, chatJID, senderUser, pushName)
}
func (r *deviceChatStorage) GetStorageStatistics() (chatCount int64, messageCount int64, err error) {
diff --git a/src/infrastructure/whatsapp/history_sync.go b/src/infrastructure/whatsapp/history_sync.go
index 631d7f97..c98e78ac 100644
--- a/src/infrastructure/whatsapp/history_sync.go
+++ b/src/infrastructure/whatsapp/history_sync.go
@@ -255,6 +255,18 @@ func processPushNames(ctx context.Context, data *waHistorySync.HistorySync, chat
pushnames := data.GetPushnames()
log.Infof("Processing %d push names from history sync", len(pushnames))
+ // Extract device ID from context (same pattern as processConversationMessages)
+ deviceID := ""
+ if inst, ok := DeviceFromContext(ctx); ok && inst != nil {
+ deviceID = inst.JID()
+ if deviceID == "" {
+ deviceID = inst.ID()
+ }
+ }
+ if deviceID == "" && client != nil && client.Store != nil && client.Store.ID != nil {
+ deviceID = client.Store.ID.ToNonAD().String()
+ }
+
for _, pushname := range pushnames {
rawJIDStr := pushname.GetID()
name := pushname.GetPushname()
@@ -272,8 +284,8 @@ func processPushNames(ctx context.Context, data *waHistorySync.HistorySync, chat
jid = NormalizeJIDFromLID(ctx, jid, client)
jidStr := jid.String()
- // Check if chat exists
- existingChat, err := chatStorageRepo.GetChat(jidStr)
+ // Check if chat exists (device-scoped to avoid cross-device data leak)
+ existingChat, err := chatStorageRepo.GetChatByDevice(deviceID, jidStr)
if err != nil || existingChat == nil {
// Chat doesn't exist yet, skip
continue
diff --git a/src/usecase/group.go b/src/usecase/group.go
index f4f6ac91..d02b1151 100644
--- a/src/usecase/group.go
+++ b/src/usecase/group.go
@@ -189,12 +189,43 @@ func (service serviceGroup) GetGroupParticipants(ctx context.Context, request do
if groupInfo != nil {
response.Name = groupInfo.GroupName.Name
response.Participants = make([]domainGroup.GroupParticipant, 0, len(groupInfo.Participants))
+
+ // Collect JIDs for batch GetUserInfo call
+ jids := make([]types.JID, 0, len(groupInfo.Participants))
+ for _, p := range groupInfo.Participants {
+ jids = append(jids, p.JID)
+ }
+
+ // Fetch user info for verified business names (ignore errors)
+ userInfoMap := make(map[types.JID]types.UserInfo)
+ if len(jids) > 0 {
+ userInfoMap, _ = client.GetUserInfo(ctx, jids)
+ }
+
for _, participant := range groupInfo.Participants {
+ displayName := participant.DisplayName
+
+ // Try contact store first (for known contacts)
+ if displayName == "" {
+ if contact, err := client.Store.Contacts.GetContact(ctx, participant.JID); err == nil && contact.FullName != "" {
+ displayName = contact.FullName
+ } else if info, ok := userInfoMap[participant.JID]; ok && info.VerifiedName != nil && info.VerifiedName.Details != nil {
+ // Fall back to verified business name
+ displayName = info.VerifiedName.Details.GetVerifiedName()
+ }
+ }
+
+ // Use JID.User for clean phone number, fallback to PhoneNumber.String()
+ phoneNumber := participant.JID.User
+ if phoneNumber == "" {
+ phoneNumber = participant.PhoneNumber.String()
+ }
+
participantData := domainGroup.GroupParticipant{
JID: participant.JID.String(),
- PhoneNumber: participant.PhoneNumber.String(),
+ PhoneNumber: phoneNumber,
LID: participant.LID.String(),
- DisplayName: participant.DisplayName,
+ DisplayName: displayName,
IsAdmin: participant.IsAdmin,
IsSuperAdmin: participant.IsSuperAdmin,
}