summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/post.go16
-rw-r--r--app/post_test.go28
2 files changed, 43 insertions, 1 deletions
diff --git a/app/post.go b/app/post.go
index 41139da63..bc31aee44 100644
--- a/app/post.go
+++ b/app/post.go
@@ -9,6 +9,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
+ "io"
"net/http"
"net/url"
"regexp"
@@ -19,6 +20,7 @@ import (
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
"github.com/mattermost/mattermost-server/utils"
+ "golang.org/x/net/html/charset"
)
var linkWithTextRegex = regexp.MustCompile(`<([^<\|]+)\|([^>]+)>`)
@@ -726,7 +728,10 @@ func (a *App) GetOpenGraphMetadata(requestURL string) *opengraph.OpenGraph {
}
defer consumeAndClose(res)
- if err := og.ProcessHTML(res.Body); err != nil {
+ contentType := res.Header.Get("Content-Type")
+ body := forceHTMLEncodingToUTF8(res.Body, contentType)
+
+ if err := og.ProcessHTML(body); err != nil {
mlog.Error(fmt.Sprintf("GetOpenGraphMetadata processing failed for url=%v with err=%v", requestURL, err.Error()))
}
@@ -735,6 +740,15 @@ func (a *App) GetOpenGraphMetadata(requestURL string) *opengraph.OpenGraph {
return og
}
+func forceHTMLEncodingToUTF8(body io.Reader, contentType string) io.Reader {
+ r, err := charset.NewReader(body, contentType)
+ if err != nil {
+ mlog.Error(fmt.Sprintf("forceHTMLEncodingToUTF8 failed to convert for contentType=%v with err=%v", contentType, err.Error()))
+ return body
+ }
+ return r
+}
+
func makeOpenGraphURLsAbsolute(og *opengraph.OpenGraph, requestURL string) {
parsedRequestURL, err := url.Parse(requestURL)
if err != nil {
diff --git a/app/post_test.go b/app/post_test.go
index 10b957751..aefc0ea35 100644
--- a/app/post_test.go
+++ b/app/post_test.go
@@ -297,6 +297,34 @@ func TestImageProxy(t *testing.T) {
}
}
+func BenchmarkForceHTMLEncodingToUTF8(b *testing.B) {
+ HTML := `
+ <html>
+ <head>
+ <meta property="og:url" content="https://example.com/apps/mattermost">
+ <meta property="og:image" content="https://images.example.com/image.png">
+ </head>
+ </html>
+ `
+ ContentType := "text/html; utf-8"
+
+ b.Run("with converting", func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ r := forceHTMLEncodingToUTF8(strings.NewReader(HTML), ContentType)
+
+ og := opengraph.NewOpenGraph()
+ og.ProcessHTML(r)
+ }
+ })
+
+ b.Run("without converting", func(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ og := opengraph.NewOpenGraph()
+ og.ProcessHTML(strings.NewReader(HTML))
+ }
+ })
+}
+
func TestMakeOpenGraphURLsAbsolute(t *testing.T) {
for name, tc := range map[string]struct {
HTML string