[MM-23727] Make channel validation consistent on the server (#14230)

* MM-23727 Ensure user ids not allowed in channel name:

MM-23727 Move channel name validation to model level

* MM-23727 Update wording
This commit is contained in:
Farhan Munshi
2020-04-26 12:38:33 -04:00
committed by GitHub
parent 036f9384b4
commit 7bc630a600
4 changed files with 17 additions and 20 deletions

View File

@@ -144,10 +144,6 @@ func (a *App) CreateChannelWithUser(channel *model.Channel, userId string) (*mod
return nil, model.NewAppError("CreateChannelWithUser", "api.channel.create_channel.direct_channel.app_error", nil, "", http.StatusBadRequest)
}
if strings.Index(channel.Name, "__") > 0 {
return nil, model.NewAppError("CreateChannelWithUser", "api.channel.create_channel.invalid_character.app_error", nil, "", http.StatusBadRequest)
}
if len(channel.TeamId) == 0 {
return nil, model.NewAppError("CreateChannelWithUser", "app.channel.create_channel.no_team_id.app_error", nil, "", http.StatusBadRequest)
}
@@ -464,14 +460,6 @@ func (a *App) GetGroupChannel(userIds []string) (*model.Channel, *model.AppError
// UpdateChannel updates a given channel by its Id. It also publishes the CHANNEL_UPDATED event.
func (a *App) UpdateChannel(channel *model.Channel) (*model.Channel, *model.AppError) {
userIds := strings.Split(channel.Name, "__")
if channel.Type != model.CHANNEL_DIRECT &&
len(userIds) == 2 &&
model.IsValidId(userIds[0]) &&
model.IsValidId(userIds[1]) {
return nil, model.NewAppError("UpdateChannel", "api.channel.update_channel.invalid_character.app_error", nil, "", http.StatusBadRequest)
}
_, err := a.Srv().Store.Channel().Update(channel)
if err != nil {
return nil, err

View File

@@ -744,6 +744,14 @@ func TestRenameChannel(t *testing.T) {
"",
"",
},
{
"Success on rename open channel with consecutive underscores in name",
th.createChannel(th.BasicTeam, model.CHANNEL_OPEN),
false,
"foo__bar",
"foo__bar",
"New Display Name",
},
{
"Fail on rename direct message channel",
th.CreateDmChannel(th.BasicUser2),

View File

@@ -259,10 +259,6 @@
"id": "api.channel.create_channel.direct_channel.app_error",
"translation": "Must use createDirectChannel API service for direct message channel creation."
},
{
"id": "api.channel.create_channel.invalid_character.app_error",
"translation": "Invalid character '__' in channel name for non-direct channel."
},
{
"id": "api.channel.create_channel.max_channel_limit.app_error",
"translation": "Unable to create more than {{.MaxChannelsPerTeam}} channels for current team."
@@ -427,10 +423,6 @@
"id": "api.channel.update_channel.deleted.app_error",
"translation": "The channel has been archived or deleted."
},
{
"id": "api.channel.update_channel.invalid_character.app_error",
"translation": "Invalid channel name. User ids are not permitted in channel name for non-direct message channels."
},
{
"id": "api.channel.update_channel.tried.app_error",
"translation": "Tried to perform an invalid update of the default channel {{.Channel}}."
@@ -4510,6 +4502,10 @@
"id": "model.channel.is_valid.id.app_error",
"translation": "Invalid Id."
},
{
"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."
},
{
"id": "model.channel.is_valid.purpose.app_error",
"translation": "Invalid purpose."

View File

@@ -234,6 +234,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 != CHANNEL_DIRECT && 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
}