[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>
Этот коммит содержится в:
@@ -8121,7 +8121,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"id": "model.channel.is_valid.name.app_error",
|
"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",
|
"id": "model.channel.is_valid.purpose.app_error",
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -185,6 +186,8 @@ type ChannelMemberCountByGroup struct {
|
|||||||
|
|
||||||
type ChannelOption func(channel *Channel)
|
type ChannelOption func(channel *Channel)
|
||||||
|
|
||||||
|
var gmNameRegex = regexp.MustCompile("^[a-f0-9]{40}$")
|
||||||
|
|
||||||
func WithID(ID string) ChannelOption {
|
func WithID(ID string) ChannelOption {
|
||||||
return func(channel *Channel) {
|
return func(channel *Channel) {
|
||||||
channel.Id = ID
|
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)
|
return NewAppError("Channel.IsValid", "model.channel.is_valid.creator_id.app_error", nil, "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
userIds := strings.Split(o.Name, "__")
|
if o.Type != ChannelTypeDirect && o.Type != ChannelTypeGroup {
|
||||||
if o.Type != ChannelTypeDirect && len(userIds) == 2 && IsValidId(userIds[0]) && IsValidId(userIds[1]) {
|
userIds := strings.Split(o.Name, "__")
|
||||||
return NewAppError("Channel.IsValid", "model.channel.is_valid.name.app_error", nil, "", http.StatusBadRequest)
|
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
|
return nil
|
||||||
|
|||||||
@@ -79,6 +79,12 @@ func TestChannelIsValid(t *testing.T) {
|
|||||||
|
|
||||||
o.Purpose = strings.Repeat("0123456789", 25)
|
o.Purpose = strings.Repeat("0123456789", 25)
|
||||||
require.Nil(t, o.IsValid())
|
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) {
|
func TestChannelPreSave(t *testing.T) {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user