From aee1600d49d3499ba04e097806ac7355aa8070af Mon Sep 17 00:00:00 2001 From: cyrilzhang-mm <112951043+cyrilzhang-mm@users.noreply.github.com> Date: Thu, 29 Sep 2022 09:23:27 -0400 Subject: [PATCH] [MM-42400] Prevent making groups with reserved names (#21040) --- api4/group_test.go | 16 ++++++++++++++++ i18n/en.json | 4 ++++ model/group.go | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/api4/group_test.go b/api4/group_test.go index 2c0f359caa..b6f155ad1c 100644 --- a/api4/group_test.go +++ b/api4/group_test.go @@ -154,6 +154,16 @@ func TestCreateGroup(t *testing.T) { require.Error(t, err) CheckBadRequestStatus(t, response) + reservedNameGroup := &model.Group{ + DisplayName: "dn_" + model.NewId(), + Name: model.NewString("here"), + Source: model.GroupSourceCustom, + AllowReference: true, + } + _, response, err = th.SystemAdminClient.CreateGroup(reservedNameGroup) + require.Error(t, err) + CheckBadRequestStatus(t, response) + th.SystemAdminClient.Logout() _, response, err = th.SystemAdminClient.CreateGroup(g) require.Error(t, err) @@ -294,6 +304,12 @@ func TestPatchGroup(t *testing.T) { CheckOKStatus(t, response) require.Equal(t, true, patchedG2.AllowReference) + _, response, err = th.SystemAdminClient.PatchGroup(g2.Id, &model.GroupPatch{ + Name: model.NewString("here"), + }) + require.Error(t, err) + CheckBadRequestStatus(t, response) + th.SystemAdminClient.Logout() _, response, err = th.SystemAdminClient.PatchGroup(group.Id, gp) require.Error(t, err) diff --git a/i18n/en.json b/i18n/en.json index 0b4e92e034..c2b52f87a3 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -8643,6 +8643,10 @@ "id": "model.group.name.invalid_length.app_error", "translation": "Name must be 1 to 64 lowercase alphanumeric characters." }, + { + "id": "model.group.name.reserved_name.app_error", + "translation": "group name already exists as a reserved name" + }, { "id": "model.group.remote_id.app_error", "translation": "invalid remote id property for group." diff --git a/model/group.go b/model/group.go index 352ad2cf9f..f6ce813c38 100644 --- a/model/group.go +++ b/model/group.go @@ -218,6 +218,10 @@ func (group *Group) IsValidName() *AppError { return NewAppError("Group.IsValidName", "model.group.name.invalid_length.app_error", map[string]any{"GroupNameMaxLength": GroupNameMaxLength}, "", http.StatusBadRequest) } + if *group.Name == UserNotifyAll || *group.Name == ChannelMentionsNotifyProp || *group.Name == UserNotifyHere { + return NewAppError("IsValidName", "model.group.name.reserved_name.app_error", nil, "", http.StatusBadRequest) + } + if !validGroupnameChars.MatchString(*group.Name) { return NewAppError("Group.IsValidName", "model.group.name.invalid_chars.app_error", nil, "", http.StatusBadRequest) }