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.

fix(security): prevent cross-device data leak in chat message queries (#525)

Aldino Kemal177c3fc8cef8
diff --git a/src/domains/chatstorage/chatstorage.go b/src/domains/chatstorage/chatstorage.go
index 633d2ad1..c210165f 100644
--- a/src/domains/chatstorage/chatstorage.go
+++ b/src/domains/chatstorage/chatstorage.go
@@ -57,6 +57,7 @@ type DeviceRecord struct {
// MessageFilter represents query filters for messages
type MessageFilter struct {
+ DeviceID string
ChatJID string
Limit int
Offset int
diff --git a/src/domains/chatstorage/interfaces.go b/src/domains/chatstorage/interfaces.go
index e436de81..5141f4fe 100644
--- a/src/domains/chatstorage/interfaces.go
+++ b/src/domains/chatstorage/interfaces.go
@@ -23,7 +23,7 @@ type IChatStorageRepository interface {
StoreMessagesBatch(messages []*Message) error
GetMessageByID(id string) (*Message, error) // New method for efficient ID-only search
GetMessages(filter *MessageFilter) ([]*Message, error)
- SearchMessages(chatJID, searchText string, limit int) ([]*Message, error) // Database-level search
+ SearchMessages(deviceID, chatJID, searchText string, limit int) ([]*Message, error) // Database-level search with device isolation
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
diff --git a/src/infrastructure/chatstorage/device_repository.go b/src/infrastructure/chatstorage/device_repository.go
index 4c0332a3..ec00baed 100644
--- a/src/infrastructure/chatstorage/device_repository.go
+++ b/src/infrastructure/chatstorage/device_repository.go
@@ -2,7 +2,6 @@ package chatstorage
import (
"context"
- "strings"
"time"
domainChatStorage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage"
@@ -28,20 +27,6 @@ func (r *DeviceRepository) withDeviceChat(chat *domainChatStorage.Chat) *domainC
if chat == nil {
return nil
}
-
- // Prefer a JID already set on the chat.
- if chat.DeviceID != "" && strings.Contains(chat.DeviceID, "@") {
- r.deviceID = chat.DeviceID
- return chat
- }
-
- // Apply wrapper JID if already known.
- if strings.Contains(r.deviceID, "@") {
- chat.DeviceID = r.deviceID
- return chat
- }
-
- // Fallback to wrapper device ID when chat is empty.
if chat.DeviceID == "" {
chat.DeviceID = r.deviceID
}
@@ -66,15 +51,8 @@ func (r *DeviceRepository) GetChatByDevice(deviceID, jid string) (*domainChatSto
}
func (r *DeviceRepository) GetChats(filter *domainChatStorage.ChatFilter) ([]*domainChatStorage.Chat, error) {
- if filter != nil {
- switch {
- case filter.DeviceID != "" && strings.Contains(filter.DeviceID, "@"):
- r.deviceID = filter.DeviceID
- case strings.Contains(r.deviceID, "@"):
- filter.DeviceID = r.deviceID
- case filter.DeviceID == "":
- filter.DeviceID = r.deviceID
- }
+ if filter != nil && filter.DeviceID == "" {
+ filter.DeviceID = r.deviceID
}
return r.base.GetChats(filter)
}
@@ -100,11 +78,18 @@ func (r *DeviceRepository) GetMessageByID(id string) (*domainChatStorage.Message
}
func (r *DeviceRepository) GetMessages(filter *domainChatStorage.MessageFilter) ([]*domainChatStorage.Message, error) {
+ if filter != nil && filter.DeviceID == "" {
+ filter.DeviceID = r.deviceID
+ }
return r.base.GetMessages(filter)
}
-func (r *DeviceRepository) SearchMessages(chatJID, searchText string, limit int) ([]*domainChatStorage.Message, error) {
- return r.base.SearchMessages(chatJID, searchText, limit)
+func (r *DeviceRepository) SearchMessages(deviceID, chatJID, searchText string, limit int) ([]*domainChatStorage.Message, error) {
+ targetDeviceID := deviceID
+ if targetDeviceID == "" {
+ targetDeviceID = r.deviceID
+ }
+ return r.base.SearchMessages(targetDeviceID, chatJID, searchText, limit)
}
func (r *DeviceRepository) DeleteMessage(id, chatJID string) error {
diff --git a/src/infrastructure/chatstorage/sqlite_repository.go b/src/infrastructure/chatstorage/sqlite_repository.go
index d8e3b0a9..e0531233 100644
--- a/src/infrastructure/chatstorage/sqlite_repository.go
+++ b/src/infrastructure/chatstorage/sqlite_repository.go
@@ -326,12 +326,21 @@ func (r *SQLiteRepository) StoreMessagesBatch(messages []*domainChatStorage.Mess
// GetMessages retrieves messages with filtering
func (r *SQLiteRepository) GetMessages(filter *domainChatStorage.MessageFilter) ([]*domainChatStorage.Message, error) {
+ // Require device_id for data isolation - fail fast if missing
+ if filter.DeviceID == "" {
+ return nil, fmt.Errorf("device_id is required for message queries (data isolation)")
+ }
+
var conditions []string
var args []any
conditions = append(conditions, "chat_jid = ?")
args = append(args, filter.ChatJID)
+ // Filter by device_id to ensure data isolation between devices
+ conditions = append(conditions, "device_id = ?")
+ args = append(args, filter.DeviceID)
+
if filter.StartTime != nil {
conditions = append(conditions, "timestamp >= ?")
args = append(args, *filter.StartTime)
@@ -394,8 +403,12 @@ func (r *SQLiteRepository) GetMessages(filter *domainChatStorage.MessageFilter)
}
// SearchMessages performs database-level search for messages containing specific text
-func (r *SQLiteRepository) SearchMessages(chatJID, searchText string, limit int) ([]*domainChatStorage.Message, error) {
- // Return empty results for empty search text
+func (r *SQLiteRepository) SearchMessages(deviceID, chatJID, searchText string, limit int) ([]*domainChatStorage.Message, error) {
+ // Require device_id for data isolation - fail fast if missing
+ if deviceID == "" {
+ return nil, fmt.Errorf("device_id is required for message search (data isolation)")
+ }
+
if strings.TrimSpace(searchText) == "" {
return []*domainChatStorage.Message{}, nil
}
@@ -403,10 +416,12 @@ func (r *SQLiteRepository) SearchMessages(chatJID, searchText string, limit int)
var conditions []string
var args []any
- // Always filter by chat JID
conditions = append(conditions, "chat_jid = ?")
args = append(args, chatJID)
+ conditions = append(conditions, "device_id = ?")
+ args = append(args, deviceID)
+
// Add search condition using LIKE operator for case-insensitive search
conditions = append(conditions, "LOWER(content) LIKE ?")
args = append(args, "%"+strings.ToLower(searchText)+"%")
diff --git a/src/infrastructure/whatsapp/chatstorage_wrapper.go b/src/infrastructure/whatsapp/chatstorage_wrapper.go
index 6468afe8..efd8a7f4 100644
--- a/src/infrastructure/whatsapp/chatstorage_wrapper.go
+++ b/src/infrastructure/whatsapp/chatstorage_wrapper.go
@@ -2,7 +2,6 @@ package whatsapp
import (
"context"
- "strings"
"time"
domainChatStorage "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/chatstorage"
@@ -31,21 +30,6 @@ func (r *deviceChatStorage) withDeviceChat(chat *domainChatStorage.Chat) *domain
if chat == nil {
return nil
}
-
- // Prefer an explicit JID already set on the chat.
- if chat.DeviceID != "" && strings.Contains(chat.DeviceID, "@") {
- // Upgrade the wrapper device ID so future filters use the JID as well.
- r.deviceID = chat.DeviceID
- return chat
- }
-
- // If wrapper already holds a JID, apply it.
- if strings.Contains(r.deviceID, "@") {
- chat.DeviceID = r.deviceID
- return chat
- }
-
- // Fallback to existing value or wrapper device ID (likely a placeholder).
if chat.DeviceID == "" {
chat.DeviceID = r.deviceID
}
@@ -69,17 +53,8 @@ func (r *deviceChatStorage) GetChatByDevice(deviceID, jid string) (*domainChatSt
}
func (r *deviceChatStorage) GetChats(filter *domainChatStorage.ChatFilter) ([]*domainChatStorage.Chat, error) {
- if filter != nil {
- switch {
- case filter.DeviceID != "" && strings.Contains(filter.DeviceID, "@"):
- // Respect caller-provided JID and upgrade wrapper for future calls.
- r.deviceID = filter.DeviceID
- case strings.Contains(r.deviceID, "@"):
- filter.DeviceID = r.deviceID
- case filter.DeviceID == "":
- // Fall back to wrapper device ID (likely placeholder) if caller didn't set one.
- filter.DeviceID = r.deviceID
- }
+ if filter != nil && filter.DeviceID == "" {
+ filter.DeviceID = r.deviceID
}
return r.base.GetChats(filter)
}
@@ -105,11 +80,18 @@ func (r *deviceChatStorage) GetMessageByID(id string) (*domainChatStorage.Messag
}
func (r *deviceChatStorage) GetMessages(filter *domainChatStorage.MessageFilter) ([]*domainChatStorage.Message, error) {
+ if filter != nil && filter.DeviceID == "" {
+ filter.DeviceID = r.deviceID
+ }
return r.base.GetMessages(filter)
}
-func (r *deviceChatStorage) SearchMessages(chatJID, searchText string, limit int) ([]*domainChatStorage.Message, error) {
- return r.base.SearchMessages(chatJID, searchText, limit)
+func (r *deviceChatStorage) SearchMessages(deviceID, chatJID, searchText string, limit int) ([]*domainChatStorage.Message, error) {
+ targetDeviceID := deviceID
+ if targetDeviceID == "" {
+ targetDeviceID = r.deviceID
+ }
+ return r.base.SearchMessages(targetDeviceID, chatJID, searchText, limit)
}
func (r *deviceChatStorage) DeleteMessage(id, chatJID string) error {
diff --git a/src/usecase/chat.go b/src/usecase/chat.go
index 1234da48..2631b142 100644
--- a/src/usecase/chat.go
+++ b/src/usecase/chat.go
@@ -92,7 +92,11 @@ func (service serviceChat) GetChatMessages(ctx context.Context, request domainCh
return response, err
}
- // Get chat info first
+ deviceID := deviceIDFromContext(ctx)
+ if deviceID == "" {
+ return response, fmt.Errorf("device identification required")
+ }
+
chat, err := service.chatStorageRepo.GetChat(request.ChatJID)
if err != nil {
logrus.WithError(err).WithField("chat_jid", request.ChatJID).Error("Failed to get chat info")
@@ -132,13 +136,14 @@ func (service serviceChat) GetChatMessages(ctx context.Context, request domainCh
var messages []*domainChatStorage.Message
if request.Search != "" {
// Use search functionality if search query is provided
- messages, err = service.chatStorageRepo.SearchMessages(request.ChatJID, request.Search, request.Limit)
+ messages, err = service.chatStorageRepo.SearchMessages(deviceID, request.ChatJID, request.Search, request.Limit)
if err != nil {
logrus.WithError(err).WithField("chat_jid", request.ChatJID).Error("Failed to search messages")
return response, err
}
} else {
- // Use regular filter
+ // Use regular filter with device_id for data isolation
+ filter.DeviceID = deviceID
messages, err = service.chatStorageRepo.GetMessages(filter)
if err != nil {
logrus.WithError(err).WithField("chat_jid", request.ChatJID).Error("Failed to get messages")