Skip to content

Commit

Permalink
MM-49048: do not omit needed wsbroadcast vars (#22156) (#22178)
Browse files Browse the repository at this point in the history
* MM-49048: do not omit needed wsbroadcast vars

It seems that we are omitting ContainsSanitizedData, and
ContainsSensitiveData variables in WebsocketBroadcast.
This created a bug in cluster mode, in which we were sending duplicate
events to users whereas normally they would have access to only one of those.

Co-authored-by: Mattermost Build <build@mattermost.com>
(cherry picked from commit 70800b2)

Co-authored-by: Kyriakos Z <3829551+koox00@users.noreply.github.com>
  • Loading branch information
mattermost-build and koox00 committed Feb 1, 2023
1 parent 41e8157 commit 69befe3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
29 changes: 29 additions & 0 deletions app/platform/web_hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package platform

import (
"encoding/json"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -551,3 +552,31 @@ func BenchmarkGetHubForUserId(b *testing.B) {
hubSink = th.Service.GetHubForUserId(th.BasicUser.Id)
}
}

func TestClusterBroadcast(t *testing.T) {
testCluster := &testlib.FakeClusterInterface{}

th := SetupWithCluster(t, testCluster)
defer th.TearDown()

ev := model.NewWebSocketEvent("test_event", "", "", "", nil, "")
broadcast := &model.WebsocketBroadcast{
ContainsSanitizedData: true,
ContainsSensitiveData: true,
}
ev = ev.SetBroadcast(broadcast)
th.Service.Publish(ev)

messages := testCluster.GetMessages()

var clusterEvent struct {
Event string `json:"event"`
Data map[string]any `json:"data"`
Broadcast *model.WebsocketBroadcast `json:"broadcast"`
Sequence int64 `json:"seq"`
}

err := json.Unmarshal(messages[0].Data, &clusterEvent)
require.NoError(t, err)
require.Equal(t, clusterEvent.Broadcast, broadcast)
}
16 changes: 8 additions & 8 deletions model/websocket_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ type WebSocketMessage interface {
}

type WebsocketBroadcast struct {
OmitUsers map[string]bool `json:"omit_users"` // broadcast is omitted for users listed here
UserId string `json:"user_id"` // broadcast only occurs for this user
ChannelId string `json:"channel_id"` // broadcast only occurs for users in this channel
TeamId string `json:"team_id"` // broadcast only occurs for users in this team
ConnectionId string `json:"connection_id"` // broadcast only occurs for this connection
OmitConnectionId string `json:"omit_connection_id"` // broadcast is omitted for this connection
ContainsSanitizedData bool `json:"-"`
ContainsSensitiveData bool `json:"-"`
OmitUsers map[string]bool `json:"omit_users"` // broadcast is omitted for users listed here
UserId string `json:"user_id"` // broadcast only occurs for this user
ChannelId string `json:"channel_id"` // broadcast only occurs for users in this channel
TeamId string `json:"team_id"` // broadcast only occurs for users in this team
ConnectionId string `json:"connection_id"` // broadcast only occurs for this connection
OmitConnectionId string `json:"omit_connection_id"` // broadcast is omitted for this connection
ContainsSanitizedData bool `json:"contains_sanitized_data,omitempty"` // broadcast only occurs for non-sysadmins
ContainsSensitiveData bool `json:"contains_sensitive_data,omitempty"` // broadcast only occurs for sysadmins
// ReliableClusterSend indicates whether or not the message should
// be sent through the cluster using the reliable, TCP backed channel.
ReliableClusterSend bool `json:"-"`
Expand Down

0 comments on commit 69befe3

Please sign in to comment.