[MM-19914] Fix data races in WebSocketEvent (#13039)

* Make WebSocketEvent type immutable

* Update code to use updated immutable WebSocketEvent type

* Export WebSocketEvent fields and mark them as deprecated
Этот коммит содержится в:
Claudio Costa
2019-12-24 09:32:11 +01:00
коммит произвёл GitHub
родитель 9ab7bee0a6
Коммит 80dd2915db
16 изменённых файлов: 216 добавлений и 94 удалений

Просмотреть файл

@@ -185,7 +185,7 @@ func (c *WebConn) writePump() {
"websocket.slow: dropping message",
mlog.String("user_id", c.UserId),
mlog.String("type", msg.EventType()),
mlog.String("channel_id", evt.Broadcast.ChannelId),
mlog.String("channel_id", evt.GetBroadcast().ChannelId),
)
skipSend = true
}
@@ -194,9 +194,7 @@ func (c *WebConn) writePump() {
if !skipSend {
var msgBytes []byte
if evtOk {
cpyEvt := &model.WebSocketEvent{}
*cpyEvt = *evt
cpyEvt.Sequence = c.Sequence
cpyEvt := evt.SetSequence(c.Sequence)
msgBytes = []byte(cpyEvt.ToJson())
c.Sequence++
} else {
@@ -209,7 +207,7 @@ func (c *WebConn) writePump() {
"websocket.full",
mlog.String("user_id", c.UserId),
mlog.String("type", msg.EventType()),
mlog.String("channel_id", evt.Broadcast.ChannelId),
mlog.String("channel_id", evt.GetBroadcast().ChannelId),
mlog.Int("size", len(msg.ToJson())),
)
} else {
@@ -305,11 +303,11 @@ func (webCon *WebConn) shouldSendEventToGuest(msg *model.WebSocketEvent) bool {
var userId string
var canSee bool
switch msg.Event {
switch msg.EventType() {
case model.WEBSOCKET_EVENT_USER_UPDATED:
userId = msg.Data["user"].(*model.User).Id
userId = msg.GetData()["user"].(*model.User).Id
case model.WEBSOCKET_EVENT_NEW_USER:
userId = msg.Data["user_id"].(string)
userId = msg.GetData()["user_id"].(string)
default:
return true
}
@@ -332,7 +330,7 @@ func (webCon *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool {
// If the event contains sanitized data, only send to users that don't have permission to
// see sensitive data. Prevents admin clients from receiving events with bad data
var hasReadPrivateDataPermission *bool
if msg.Broadcast.ContainsSanitizedData {
if msg.GetBroadcast().ContainsSanitizedData {
hasReadPrivateDataPermission = model.NewBool(webCon.App.RolesGrantPermission(webCon.GetSession().GetUserRoles(), model.PERMISSION_MANAGE_SYSTEM.Id))
if *hasReadPrivateDataPermission {
@@ -341,7 +339,7 @@ func (webCon *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool {
}
// If the event contains sensitive data, only send to users with permission to see it
if msg.Broadcast.ContainsSensitiveData {
if msg.GetBroadcast().ContainsSensitiveData {
if hasReadPrivateDataPermission == nil {
hasReadPrivateDataPermission = model.NewBool(webCon.App.RolesGrantPermission(webCon.GetSession().GetUserRoles(), model.PERMISSION_MANAGE_SYSTEM.Id))
}
@@ -352,19 +350,19 @@ func (webCon *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool {
}
// If the event is destined to a specific user
if len(msg.Broadcast.UserId) > 0 {
return webCon.UserId == msg.Broadcast.UserId
if len(msg.GetBroadcast().UserId) > 0 {
return webCon.UserId == msg.GetBroadcast().UserId
}
// if the user is omitted don't send the message
if len(msg.Broadcast.OmitUsers) > 0 {
if _, ok := msg.Broadcast.OmitUsers[webCon.UserId]; ok {
if len(msg.GetBroadcast().OmitUsers) > 0 {
if _, ok := msg.GetBroadcast().OmitUsers[webCon.UserId]; ok {
return false
}
}
// Only report events to users who are in the channel for the event
if len(msg.Broadcast.ChannelId) > 0 {
if len(msg.GetBroadcast().ChannelId) > 0 {
if model.GetMillis()-webCon.LastAllChannelMembersTime > WEBCONN_MEMBER_CACHE_TIME {
webCon.AllChannelMembers = nil
webCon.LastAllChannelMembersTime = 0
@@ -380,15 +378,15 @@ func (webCon *WebConn) ShouldSendEvent(msg *model.WebSocketEvent) bool {
webCon.LastAllChannelMembersTime = model.GetMillis()
}
if _, ok := webCon.AllChannelMembers[msg.Broadcast.ChannelId]; ok {
if _, ok := webCon.AllChannelMembers[msg.GetBroadcast().ChannelId]; ok {
return true
}
return false
}
// Only report events to users who are in the team for the event
if len(msg.Broadcast.TeamId) > 0 {
return webCon.IsMemberOfTeam(msg.Broadcast.TeamId)
if len(msg.GetBroadcast().TeamId) > 0 {
return webCon.IsMemberOfTeam(msg.GetBroadcast().TeamId)
}
if webCon.GetSession().Props[model.SESSION_PROP_IS_GUEST] == "true" {