Skip to content

Commit

Permalink
[MM-34926] Short-circuit link processing if the regex is non-terminal (
Browse files Browse the repository at this point in the history
…#166)

* bail link processing if the regex is non-terminal

* check length of `in`
  • Loading branch information
mickmister authored Aug 2, 2021
1 parent 9f8f884 commit 4506199
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions server/autolink/autolink.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,15 @@ func (l Autolink) Replace(message string) string {
in := []byte(message)
out := []byte{}
for {
if len(in) == 0 {
break
}

submatch := l.re.FindSubmatchIndex(in)
if submatch == nil {
break
}

out = append(out, in[:submatch[0]]...)
out = l.re.Expand(out, []byte(l.template), in, submatch)
in = in[submatch[1]:]
Expand Down
46 changes: 46 additions & 0 deletions server/autolink/autolink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package autolink_test
import (
"fmt"
"testing"
"time"

"github.com/mattermost/mattermost-server/v5/model"
"github.com/mattermost/mattermost-server/v5/plugin/plugintest"
Expand Down Expand Up @@ -320,3 +321,48 @@ func TestEquals(t *testing.T) {
})
}
}

func TestWildcard(t *testing.T) {
for _, tc := range []struct {
Name string
Link autolink.Autolink
}{
{
Name: ".*",
Link: autolink.Autolink{
Pattern: ".*",
Template: "My template",
},
},
{
Name: ".*.",
Link: autolink.Autolink{
Pattern: ".*.",
Template: "My template",
},
},
} {
p := setupTestPlugin(t, tc.Link)

message := "Your message"

var post *model.Post
done := make(chan bool)
go func() {
post, _ = p.MessageWillBePosted(nil, &model.Post{
Message: message,
})

done <- true
}()

select {
case <-done:
case <-time.After(1 * time.Millisecond):
panic("wildcard regex timed out")
}

assert.NotNil(t, post, "post is nil")
assert.Equal(t, "My template", post.Message)
}
}

0 comments on commit 4506199

Please sign in to comment.