[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 <mattermod@users.noreply.github.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Mylon Suren
2023-01-12 12:58:45 -05:00
коммит произвёл GitHub
родитель e7deb39046
Коммит daa83d4a78
3 изменённых файлов: 15 добавлений и 4 удалений

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

@@ -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

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

@@ -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) {