Skip to content

Commit

Permalink
when sync'ing users make sure the user's remoteid matches the remote …
Browse files Browse the repository at this point in the history
…issuing the update (mattermost#27203)
  • Loading branch information
wiggin77 authored Jun 4, 2024
1 parent 11c2951 commit 5114c3b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 15 additions & 1 deletion server/platform/services/sharedchannel/sync_recv.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import (
"github.com/mattermost/mattermost/server/v8/platform/services/remotecluster"
)

var (
ErrRemoteIDMismatch = errors.New("remoteID mismatch")
)

func (scs *Service) onReceiveSyncMessage(msg model.RemoteClusterMsg, rc *model.RemoteCluster, response *remotecluster.Response) error {
if msg.Topic != TopicSync {
return fmt.Errorf("wrong topic, expected `%s`, got `%s`", TopicSync, msg.Topic)
Expand Down Expand Up @@ -167,7 +171,7 @@ func (scs *Service) processSyncMessage(c request.CTX, syncMsg *model.SyncMsg, rc

func (scs *Service) upsertSyncUser(c request.CTX, user *model.User, channel *model.Channel, rc *model.RemoteCluster) (*model.User, error) {
var err error
if user.RemoteId == nil || *user.RemoteId == "" {
if SafeString(user.RemoteId) == "" {
user.RemoteId = model.NewString(rc.RemoteId)
}

Expand All @@ -185,6 +189,16 @@ func (scs *Service) upsertSyncUser(c request.CTX, user *model.User, channel *mod
return nil, err
}
} else {
// check if existing user belongs to the remote that issued the update
if SafeString(euser.RemoteId) != SafeString(user.RemoteId) {
scs.server.Log().Log(mlog.LvlSharedChannelServiceError, "RemoteID mismatch sync'ing user",
mlog.String("remote", rc.Name),
mlog.String("user_id", user.Id),
mlog.String("existing_user_remote_id", SafeString(euser.RemoteId)),
mlog.String("update_user_remote_id", SafeString(user.RemoteId)),
)
return nil, fmt.Errorf("error updating user: %w", ErrRemoteIDMismatch)
}
patch := &model.UserPatch{
Username: &user.Username,
Nickname: &user.Nickname,
Expand Down
7 changes: 7 additions & 0 deletions server/platform/services/sharedchannel/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,10 @@ func reducePostsSliceInCache(posts []*model.Post, cache map[string]*model.Post)
}
return reduced
}

func SafeString(p *string) string {
if p == nil {
return ""
}
return *p
}

0 comments on commit 5114c3b

Please sign in to comment.