[MM-48860] / [GH-21863] Change regex to reduce false positive transforms on incoming webhook text (#21852)

* 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 <me@adamwarner.co.uk>

---------

Signed-off-by: Adam Warner <me@adamwarner.co.uk>
Co-authored-by: Mattermost Build <build@mattermost.com>
Co-authored-by: Saturnino Abril <saturnino.abril@gmail.com>
Этот коммит содержится в:
Adam Warner
2023-12-06 19:12:31 +00:00
коммит произвёл GitHub
родитель a24ef3f07d
Коммит 3fe68e73bf
2 изменённых файлов: 32 добавлений и 1 удалений

Просмотреть файл

@@ -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}

Просмотреть файл

@@ -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: "<https://mattermost.com|Mattermost>",
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