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(webhook): skip broadcast messages and return no rows for missing devices

Aldino Kemale4c62f89bd1f
diff --git a/src/infrastructure/chatstorage/sqlite_repository.go b/src/infrastructure/chatstorage/sqlite_repository.go
index 5c0e6c40..b39b14b4 100644
--- a/src/infrastructure/chatstorage/sqlite_repository.go
+++ b/src/infrastructure/chatstorage/sqlite_repository.go
@@ -1266,7 +1266,7 @@ func (r *SQLiteRepository) SetDeviceWebhookConfig(deviceID string, config *domai
webhookURL = config.WebhookURL
}
- _, err := r.db.Exec(`
+ result, err := r.db.Exec(`
UPDATE devices
SET webhook_url = ?, webhook_secret = ?, webhook_events = ?, webhook_insecure_skip_verify = ?, updated_at = ?
WHERE device_id = ?
@@ -1274,6 +1274,13 @@ func (r *SQLiteRepository) SetDeviceWebhookConfig(deviceID string, config *domai
if err != nil {
return err
}
+ rowsAffected, err := result.RowsAffected()
+ if err != nil {
+ return err
+ }
+ if rowsAffected == 0 {
+ return sql.ErrNoRows
+ }
return nil
}
diff --git a/src/infrastructure/chatstorage/sqlite_repository_test.go b/src/infrastructure/chatstorage/sqlite_repository_test.go
index 746baae4..d89cea72 100644
--- a/src/infrastructure/chatstorage/sqlite_repository_test.go
+++ b/src/infrastructure/chatstorage/sqlite_repository_test.go
@@ -90,6 +90,18 @@ func TestSQLiteRepositoryGetsDeviceWebhookConfigByJID(t *testing.T) {
}
}
+func TestSQLiteRepositorySetDeviceWebhookConfigReturnsErrNoRowsForMissingDevice(t *testing.T) {
+ repo := newTestSQLiteRepository(t)
+
+ webhookURL := "https://device-webhook.example.com"
+ err := repo.SetDeviceWebhookConfig("missing-device", &domainChatStorage.DeviceWebhookConfig{
+ WebhookURL: &webhookURL,
+ })
+ if !errors.Is(err, sql.ErrNoRows) {
+ t.Fatalf("expected sql.ErrNoRows for missing device, got %v", err)
+ }
+}
+
func TestSQLiteRepositoryStoresUpdatesRemovesAndHydratesReactions(t *testing.T) {
repo := newTestSQLiteRepository(t)
deviceID := "[email protected]"
diff --git a/src/infrastructure/whatsapp/event_message_handler.go b/src/infrastructure/whatsapp/event_message_handler.go
index 488b936f..b19d0f99 100644
--- a/src/infrastructure/whatsapp/event_message_handler.go
+++ b/src/infrastructure/whatsapp/event_message_handler.go
@@ -163,8 +163,11 @@ func handleWebhookForward(ctx context.Context, evt *events.Message, client *what
}
}
- if !config.ChatwootEnabled &&
- strings.Contains(evt.Info.SourceString(), "broadcast") {
+ // Broadcast/status messages are never forwarded, regardless of Chatwoot:
+ // the Chatwoot pipeline rejects status@broadcast (a relayed status post
+ // would only spawn a noise "Status" contact), and plain webhook consumers
+ // must not receive broadcast noise just because Chatwoot is enabled.
+ if strings.Contains(evt.Info.SourceString(), "broadcast") {
return
}
diff --git a/src/infrastructure/whatsapp/event_message_handler_test.go b/src/infrastructure/whatsapp/event_message_handler_test.go
index 05fc35da..38ccd0ce 100644
--- a/src/infrastructure/whatsapp/event_message_handler_test.go
+++ b/src/infrastructure/whatsapp/event_message_handler_test.go
@@ -74,6 +74,86 @@ func TestHandleMessageReactionStoresReactionAndForwardsWebhook(t *testing.T) {
}
}
+func TestHandleWebhookForwardSkipsBroadcastRegardlessOfChatwoot(t *testing.T) {
+ originalWebhookURLs := config.WhatsappWebhook
+ originalWebhookEvents := config.WhatsappWebhookEvents
+ originalChatwootEnabled := config.ChatwootEnabled
+ originalSubmit := submitWebhookFn
+ originalLog := log
+ defer func() {
+ config.WhatsappWebhook = originalWebhookURLs
+ config.WhatsappWebhookEvents = originalWebhookEvents
+ config.ChatwootEnabled = originalChatwootEnabled
+ submitWebhookFn = originalSubmit
+ log = originalLog
+ }()
+
+ log = waLog.Noop
+ config.WhatsappWebhook = []string{"https://example.test/webhook"}
+ config.WhatsappWebhookEvents = nil
+
+ delivered := make(chan map[string]any, 8)
+ submitWebhookFn = func(_ context.Context, payload map[string]any, _ string, _ *domainChatStorage.DeviceWebhookConfig) error {
+ delivered <- payload
+ return nil
+ }
+
+ // Broadcast/status messages must never reach webhooks, whether Chatwoot is
+ // enabled or not: the Chatwoot pipeline rejects status@broadcast anyway,
+ // and plain webhook consumers must not start receiving broadcast noise
+ // just because Chatwoot is turned on (regression from PR #671).
+ statusChat := types.NewJID("status", types.BroadcastServer)
+ for _, chatwootEnabled := range []bool{false, true} {
+ config.ChatwootEnabled = chatwootEnabled
+ handleWebhookForward(context.Background(), textEventForTest("broadcast-1", statusChat), nil)
+ }
+
+ // Control: a regular DM must still be forwarded, so the guard is proven
+ // to filter broadcasts specifically rather than everything.
+ config.ChatwootEnabled = false
+ dmChat := types.NewJID("628123456789", types.DefaultUserServer)
+ handleWebhookForward(context.Background(), textEventForTest("dm-1", dmChat), nil)
+
+ select {
+ case payload := <-delivered:
+ eventPayload, ok := payload["payload"].(map[string]any)
+ if !ok {
+ t.Fatalf("expected payload map, got %T", payload["payload"])
+ }
+ if got := eventPayload["id"]; got != "dm-1" {
+ t.Fatalf("expected control message dm-1 to be forwarded, got %v", got)
+ }
+ case <-time.After(2 * time.Second):
+ t.Fatal("timed out waiting for control message webhook submission")
+ }
+
+ // Give any (buggy) broadcast forwarding goroutines time to land, then
+ // assert nothing else was delivered.
+ time.Sleep(200 * time.Millisecond)
+ select {
+ case payload := <-delivered:
+ t.Fatalf("broadcast message was forwarded to webhook: %+v", payload)
+ default:
+ }
+}
+
+func textEventForTest(eventID string, chat types.JID) *events.Message {
+ return &events.Message{
+ Info: types.MessageInfo{
+ MessageSource: types.MessageSource{
+ Chat: chat,
+ Sender: types.NewJID("628111111111", types.DefaultUserServer),
+ IsFromMe: false,
+ },
+ ID: eventID,
+ Timestamp: time.Date(2026, time.May, 16, 8, 0, 0, 0, time.UTC),
+ },
+ Message: &waE2E.Message{
+ Conversation: protoString("hello"),
+ },
+ }
+}
+
type messageHandlerRepoSpy struct {
domainChatStorage.IChatStorageRepository
mu sync.Mutex