Skip to content

Commit 4eadd40

Browse files
committed
If autolinking would make posts too long, don't
Substitutions that increase message length have the potential to grow a post past the ability of the database to store it. This manifests to users as their posts inexplicably failing. To avoid this fail state, we check to see if the post-substitution message is too long and, if so, don't apply the changes. Unfortunately, the actual maximum post size is not currently accessible to plugins. In the meantime, we use the smallest value for which the server guarantees support, which is exposed through model.POST_MESSAGE_MAX_RUNES_V1 and is in practice 4000 runes.
1 parent 681b494 commit 4eadd40

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

server/autolinkplugin/plugin.go

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"strings"
77
"sync"
8+
"unicode/utf8"
89

910
"github.com/mattermost/mattermost-server/v5/model"
1011
"github.com/mattermost/mattermost-server/v5/plugin"
@@ -208,6 +209,11 @@ func (p *Plugin) ProcessPost(c *plugin.Context, post *model.Post) (*model.Post,
208209
return nil, ""
209210
}
210211

212+
if runes := utf8.RuneCountInString(postText); runes > model.POST_MESSAGE_MAX_RUNES_V1 {
213+
p.API.LogWarn("Rewritten message would be too long, skipping", "rewrittenLength", runes)
214+
return nil, ""
215+
}
216+
211217
post.Message = postText
212218
}
213219

server/autolinkplugin/plugin_test.go

+10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"net/http"
88
"net/http/httptest"
9+
"strings"
910
"testing"
1011

1112
"github.com/mattermost/mattermost-server/v5/model"
@@ -305,6 +306,9 @@ func TestSpecialCases(t *testing.T) {
305306
Pattern: "(example)",
306307
Template: "test",
307308
Scope: []string{"other-team/town-square"},
309+
}, autolink.Autolink{
310+
Pattern: "(toolong)",
311+
Template: strings.Repeat("1", model.POST_MESSAGE_MAX_RUNES_V1+1),
308312
})
309313
validConfig := Config{
310314
EnableAdminCommand: false,
@@ -331,6 +335,8 @@ func TestSpecialCases(t *testing.T) {
331335
api.On("GetChannel", mock.AnythingOfType("string")).Return(&testChannel, nil)
332336
api.On("GetTeam", mock.AnythingOfType("string")).Return(&testTeam, nil)
333337

338+
api.On("LogWarn", mock.AnythingOfType("string"), mock.AnythingOfType("string"), mock.AnythingOfType("int"))
339+
334340
testUser := model.User{
335341
IsBot: false,
336342
}
@@ -454,6 +460,10 @@ func TestSpecialCases(t *testing.T) {
454460
"check out MM-12345 too",
455461
"check out [MM-12345](https://mattermost.atlassian.net/browse/MM-12345) too",
456462
},
463+
{
464+
"toolong",
465+
"toolong",
466+
},
457467
}
458468

459469
for _, tt := range tests {

0 commit comments

Comments
 (0)