From f8fc5190eeb31b74a5606dd2c381e04aab2c342e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20Vay=C3=A1?= Date: Tue, 29 Jun 2021 19:37:15 +0200 Subject: [PATCH] [MM-36481] Fix emoji regex to support thumbsup (#17846) --- app/slashcommands/command_custom_status.go | 2 +- model/emoji.go | 10 +-- model/emoji_test.go | 3 + model/utils.go | 6 ++ model/utils_test.go | 78 ++++++++++++++++++++++ 5 files changed, 90 insertions(+), 9 deletions(-) diff --git a/app/slashcommands/command_custom_status.go b/app/slashcommands/command_custom_status.go index 56839a030e..b2c1e6097a 100644 --- a/app/slashcommands/command_custom_status.go +++ b/app/slashcommands/command_custom_status.go @@ -81,7 +81,7 @@ func GetCustomStatus(message string) *model.CustomStatus { Text: message, } - firstEmojiLocations := model.ALL_EMOJI_PATTERN.FindIndex([]byte(message)) + firstEmojiLocations := model.EMOJI_PATTERN.FindIndex([]byte(message)) if len(firstEmojiLocations) > 0 && firstEmojiLocations[0] == 0 { // emoji found at starting index customStatus.Emoji = message[firstEmojiLocations[0]+1 : firstEmojiLocations[1]-1] diff --git a/model/emoji.go b/model/emoji.go index bc9b23b891..f990c670d6 100644 --- a/model/emoji.go +++ b/model/emoji.go @@ -16,13 +16,7 @@ const ( EMOJI_SORT_BY_NAME = "name" ) -var EMOJI_PATTERN = regexp.MustCompile(`:[a-zA-Z0-9_-]+:`) - -// ALL_EMOJI_PATTERN is same as the EMOJI_PATTERN except for allowing a '+' character. -// This is to allow the system emoji :+1: to be matched. -// We kept a separate variable to avoid renaming help texts for custom emoji's. -// TODO: Merge ALL_EMOJI_PATTERN with EMOJI_PATTERN after updating custom emoji help texts -var ALL_EMOJI_PATTERN = regexp.MustCompile(`:[a-zA-Z0-9_+-]+:`) +var EMOJI_PATTERN = regexp.MustCompile(`:[a-zA-Z0-9_+-]+:`) var ReverseSystemEmojisMap = makeReverseEmojiMap() @@ -86,7 +80,7 @@ func (emoji *Emoji) IsValid() *AppError { } func IsValidEmojiName(name string) *AppError { - if name == "" || len(name) > EMOJI_NAME_MAX_LENGTH || !IsValidAlphaNumHyphenUnderscore(name, false) || inSystemEmoji(name) { + if name == "" || len(name) > EMOJI_NAME_MAX_LENGTH || !IsValidAlphaNumHyphenUnderscorePlus(name) || inSystemEmoji(name) { return NewAppError("Emoji.IsValid", "model.emoji.name.app_error", nil, "", http.StatusBadRequest) } diff --git a/model/emoji_test.go b/model/emoji_test.go index 1600113258..763b7aa2e7 100644 --- a/model/emoji_test.go +++ b/model/emoji_test.go @@ -50,6 +50,9 @@ func TestEmojiIsValid(t *testing.T) { emoji.Name = "name-" require.Nil(t, emoji.IsValid()) + emoji.Name = "name+" + require.Nil(t, emoji.IsValid()) + emoji.Name = "name_" require.Nil(t, emoji.IsValid()) diff --git a/model/utils.go b/model/utils.go index 903bc0d0d3..0c5a272c26 100644 --- a/model/utils.go +++ b/model/utils.go @@ -438,6 +438,12 @@ func IsValidAlphaNumHyphenUnderscore(s string, withFormat bool) bool { return validSimpleAlphaNumHyphenUnderscore.MatchString(s) } +func IsValidAlphaNumHyphenUnderscorePlus(s string) bool { + + validSimpleAlphaNumHyphenUnderscorePlus := regexp.MustCompile(`^[a-zA-Z0-9+_-]+$`) + return validSimpleAlphaNumHyphenUnderscorePlus.MatchString(s) +} + func Etag(parts ...interface{}) string { etag := CurrentVersion diff --git a/model/utils_test.go b/model/utils_test.go index 059910fd9f..acb466d610 100644 --- a/model/utils_test.go +++ b/model/utils_test.go @@ -476,6 +476,84 @@ func TestIsValidAlphaNumHyphenUnderscore(t *testing.T) { } } +func TestIsValidAlphaNumHyphenUnderscorePlus(t *testing.T) { + cases := []struct { + Input string + Result bool + }{ + { + Input: "test", + Result: true, + }, + { + Input: "test+name", + Result: true, + }, + { + Input: "test+-name", + Result: true, + }, + { + Input: "test_+name", + Result: true, + }, + { + Input: "test++name", + Result: true, + }, + { + Input: "test_-name", + Result: true, + }, + { + Input: "-", + Result: true, + }, + { + Input: "_", + Result: true, + }, + { + Input: "+", + Result: true, + }, + { + Input: "test+", + Result: true, + }, + { + Input: "test++", + Result: true, + }, + { + Input: "test--", + Result: true, + }, + { + Input: "test__", + Result: true, + }, + { + Input: ".", + Result: false, + }, + + { + Input: "test,", + Result: false, + }, + { + Input: "test:name", + Result: false, + }, + } + + for _, tc := range cases { + actual := IsValidAlphaNumHyphenUnderscorePlus(tc.Input) + require.Equalf(t, actual, tc.Result, "case: '%v'\tshould returned: %#v", tc.Input, tc.Result) + } +} + func TestIsValidId(t *testing.T) { cases := []struct { Input string