From 3fe68e73bf0ba6989b2f4237a63c899870e2eedb Mon Sep 17 00:00:00 2001 From: Adam Warner Date: Wed, 6 Dec 2023 19:12:31 +0000 Subject: [PATCH] [MM-48860] / [GH-21863] Change regex to reduce false positive transforms on incoming webhook text (#21852) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change regex to reduce false positive transforms on incoming webhook text Adds an additional condition to the 2nd capture group to ensure we don't match another `|` char See https://github.com/mattermost/mattermost-server/issues/21800 for details * Add test per suggested comment (switch Pi-hole URL out for Mattermost one 😉) Signed-off-by: Adam Warner --------- Signed-off-by: Adam Warner Co-authored-by: Mattermost Build Co-authored-by: Saturnino Abril --- server/channels/app/webhook.go | 2 +- server/channels/app/webhook_test.go | 31 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/server/channels/app/webhook.go b/server/channels/app/webhook.go index 3f6711149e..b804b6b97e 100644 --- a/server/channels/app/webhook.go +++ b/server/channels/app/webhook.go @@ -264,7 +264,7 @@ func SplitWebhookPost(post *model.Post, maxPostSize int) ([]*model.Post, *model. func (a *App) CreateWebhookPost(c request.CTX, userID string, channel *model.Channel, text, overrideUsername, overrideIconURL, overrideIconEmoji string, props model.StringInterface, postType string, postRootId string) (*model.Post, *model.AppError) { // parse links into Markdown format - linkWithTextRegex := regexp.MustCompile(`<([^\n<\|>]+)\|([^\n>]+)>`) + linkWithTextRegex := regexp.MustCompile(`<([^\n<\|>]+)\|([^\|\n>]+)>`) text = linkWithTextRegex.ReplaceAllString(text, "[${2}](${1})") post := &model.Post{UserId: userID, ChannelId: channel.Id, Message: text, Type: postType, RootId: postRootId} diff --git a/server/channels/app/webhook_test.go b/server/channels/app/webhook_test.go index 2ae65fe51b..0bca4d2042 100644 --- a/server/channels/app/webhook_test.go +++ b/server/channels/app/webhook_test.go @@ -382,6 +382,37 @@ Date: Thu Mar 1 19:46:48 2018 +0300 }) } +func TestCreateWebhookPostLinks(t *testing.T) { + th := Setup(t).InitBasic() + defer th.TearDown() + + th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true }) + + hook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, &model.IncomingWebhook{ChannelId: th.BasicChannel.Id}) + require.Nil(t, err) + defer th.App.DeleteIncomingWebhook(hook.Id) + + for name, tc := range map[string]struct { + input string + expectedOutput string + }{ + "if statement": { + input: "`if(status_int < QUERY_UNKNOWN || status_int >= QUERY_STATUS_MAX)`", + expectedOutput: "`if(status_int < QUERY_UNKNOWN || status_int >= QUERY_STATUS_MAX)`", + }, + "angle bracket link": { + input: "", + expectedOutput: "[Mattermost](https://mattermost.com)", + }, + } { + t.Run(name, func(t *testing.T) { + post, err := th.App.CreateWebhookPost(th.Context, hook.UserId, th.BasicChannel, tc.input, "", "", "", model.StringInterface{}, "", "") + require.Nil(t, err) + require.Equal(t, tc.expectedOutput, post.Message) + }) + } +} + func TestSplitWebhookPost(t *testing.T) { type TestCase struct { Post *model.Post