From daa83d4a78d8e68797b38004e37350465414cabe Mon Sep 17 00:00:00 2001 From: Mylon Suren <23694620+mylonsuren@users.noreply.github.com> Date: Thu, 12 Jan 2023 12:58:45 -0500 Subject: [PATCH] [MM-47896] Channel names can collide with GM names (#21639) * Block channel creation if name matches GM naming pattern * Fix regex and error text * Add test for DM names * Update error string * Trigger build Co-authored-by: Mattermod Co-authored-by: Mattermost Build --- i18n/en.json | 2 +- model/channel.go | 11 ++++++++--- model/channel_test.go | 6 ++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/i18n/en.json b/i18n/en.json index 6bf836abe8..f6f7237066 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -8121,7 +8121,7 @@ }, { "id": "model.channel.is_valid.name.app_error", - "translation": "Invalid channel name. User ids are not permitted in channel name for non-direct message channels." + "translation": "Channel names can't be in a hexadecimal format. Please enter a different channel name." }, { "id": "model.channel.is_valid.purpose.app_error", diff --git a/model/channel.go b/model/channel.go index e900011cc9..29f2b9ab8c 100644 --- a/model/channel.go +++ b/model/channel.go @@ -10,6 +10,7 @@ import ( "errors" "io" "net/http" + "regexp" "sort" "strings" "unicode/utf8" @@ -185,6 +186,8 @@ type ChannelMemberCountByGroup struct { type ChannelOption func(channel *Channel) +var gmNameRegex = regexp.MustCompile("^[a-f0-9]{40}$") + func WithID(ID string) ChannelOption { return func(channel *Channel) { channel.Id = ID @@ -281,9 +284,11 @@ func (o *Channel) IsValid() *AppError { return NewAppError("Channel.IsValid", "model.channel.is_valid.creator_id.app_error", nil, "", http.StatusBadRequest) } - userIds := strings.Split(o.Name, "__") - if o.Type != ChannelTypeDirect && len(userIds) == 2 && IsValidId(userIds[0]) && IsValidId(userIds[1]) { - return NewAppError("Channel.IsValid", "model.channel.is_valid.name.app_error", nil, "", http.StatusBadRequest) + if o.Type != ChannelTypeDirect && o.Type != ChannelTypeGroup { + userIds := strings.Split(o.Name, "__") + if ok := gmNameRegex.MatchString(o.Name); ok || (o.Type != ChannelTypeDirect && len(userIds) == 2 && IsValidId(userIds[0]) && IsValidId(userIds[1])) { + return NewAppError("Channel.IsValid", "model.channel.is_valid.name.app_error", nil, "", http.StatusBadRequest) + } } return nil diff --git a/model/channel_test.go b/model/channel_test.go index 6cee525f37..038720a6d2 100644 --- a/model/channel_test.go +++ b/model/channel_test.go @@ -79,6 +79,12 @@ func TestChannelIsValid(t *testing.T) { o.Purpose = strings.Repeat("0123456789", 25) require.Nil(t, o.IsValid()) + + o.Name = "beu8cc6b3jnxfe9r4na9baooma__36atajbs87dqmpym6o8eiy9saa" + require.NotNil(t, o.IsValid()) + + o.Name = "71b03afcbb2d503d49f87f057549c43db4e19f92" + require.NotNil(t, o.IsValid()) } func TestChannelPreSave(t *testing.T) {