snapshot of https://github.com/mattermost/mattermost.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
This commit is contained in:
192
server/channels/app/opengraph.go
Normal file
192
server/channels/app/opengraph.go
Normal file
@@ -0,0 +1,192 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"html"
|
||||
"io"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/dyatlov/go-opengraph/opengraph"
|
||||
ogImage "github.com/dyatlov/go-opengraph/opengraph/types/image"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/net/html/charset"
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/app/oembed"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxOpenGraphResponseSize = 1024 * 1024 * 50
|
||||
openGraphMetadataCacheSize = 10000
|
||||
)
|
||||
|
||||
func (a *App) GetOpenGraphMetadata(requestURL string) ([]byte, error) {
|
||||
var ogJSONGeneric []byte
|
||||
err := a.Srv().openGraphDataCache.Get(requestURL, &ogJSONGeneric)
|
||||
if err == nil {
|
||||
return ogJSONGeneric, nil
|
||||
}
|
||||
|
||||
res, err := a.HTTPService().MakeClient(false).Get(requestURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
graph := a.parseOpenGraphMetadata(requestURL, res.Body, res.Header.Get("Content-Type"))
|
||||
|
||||
ogJSON, err := graph.ToJSON()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = a.Srv().openGraphDataCache.SetWithExpiry(requestURL, ogJSON, 1*time.Hour)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ogJSON, nil
|
||||
}
|
||||
|
||||
func (a *App) parseOpenGraphMetadata(requestURL string, body io.Reader, contentType string) *opengraph.OpenGraph {
|
||||
og := opengraph.NewOpenGraph()
|
||||
body = forceHTMLEncodingToUTF8(io.LimitReader(body, MaxOpenGraphResponseSize), contentType)
|
||||
|
||||
if err := og.ProcessHTML(body); err != nil {
|
||||
mlog.Warn("parseOpenGraphMetadata processing failed", mlog.String("request_url", requestURL), mlog.Err(err))
|
||||
}
|
||||
|
||||
makeOpenGraphURLsAbsolute(og, requestURL)
|
||||
|
||||
openGraphDecodeHTMLEntities(og)
|
||||
|
||||
og = filterSVGImagesFromOpenGraph(og)
|
||||
|
||||
// If image proxy enabled modify open graph data to feed though proxy
|
||||
if toProxyURL := a.ImageProxyAdder(); toProxyURL != nil {
|
||||
og = openGraphDataWithProxyAddedToImageURLs(og, toProxyURL)
|
||||
}
|
||||
|
||||
// The URL should be the link the user provided in their message, not a redirected one.
|
||||
if og.URL != "" {
|
||||
og.URL = requestURL
|
||||
}
|
||||
|
||||
return og
|
||||
}
|
||||
|
||||
func forceHTMLEncodingToUTF8(body io.Reader, contentType string) io.Reader {
|
||||
r, err := charset.NewReader(body, contentType)
|
||||
if err != nil {
|
||||
mlog.Warn("forceHTMLEncodingToUTF8 failed to convert", mlog.String("content_type", contentType), mlog.Err(err))
|
||||
return body
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
func makeOpenGraphURLsAbsolute(og *opengraph.OpenGraph, requestURL string) {
|
||||
parsedRequestURL, err := url.Parse(requestURL)
|
||||
if err != nil {
|
||||
mlog.Warn("makeOpenGraphURLsAbsolute failed to parse url", mlog.String("request_url", requestURL), mlog.Err(err))
|
||||
return
|
||||
}
|
||||
|
||||
makeURLAbsolute := func(resultURL string) string {
|
||||
if resultURL == "" {
|
||||
return resultURL
|
||||
}
|
||||
|
||||
parsedResultURL, err := url.Parse(resultURL)
|
||||
if err != nil {
|
||||
mlog.Warn("makeOpenGraphURLsAbsolute failed to parse result", mlog.String("request_url", requestURL), mlog.Err(err))
|
||||
return resultURL
|
||||
}
|
||||
|
||||
if parsedResultURL.IsAbs() {
|
||||
return resultURL
|
||||
}
|
||||
|
||||
return parsedRequestURL.ResolveReference(parsedResultURL).String()
|
||||
}
|
||||
|
||||
og.URL = makeURLAbsolute(og.URL)
|
||||
|
||||
for _, image := range og.Images {
|
||||
image.URL = makeURLAbsolute(image.URL)
|
||||
image.SecureURL = makeURLAbsolute(image.SecureURL)
|
||||
}
|
||||
|
||||
for _, audio := range og.Audios {
|
||||
audio.URL = makeURLAbsolute(audio.URL)
|
||||
audio.SecureURL = makeURLAbsolute(audio.SecureURL)
|
||||
}
|
||||
|
||||
for _, video := range og.Videos {
|
||||
video.URL = makeURLAbsolute(video.URL)
|
||||
video.SecureURL = makeURLAbsolute(video.SecureURL)
|
||||
}
|
||||
}
|
||||
|
||||
func openGraphDataWithProxyAddedToImageURLs(ogdata *opengraph.OpenGraph, toProxyURL func(string) string) *opengraph.OpenGraph {
|
||||
for _, image := range ogdata.Images {
|
||||
var url string
|
||||
if image.SecureURL != "" {
|
||||
url = image.SecureURL
|
||||
} else {
|
||||
url = image.URL
|
||||
}
|
||||
|
||||
image.URL = ""
|
||||
image.SecureURL = toProxyURL(url)
|
||||
}
|
||||
|
||||
return ogdata
|
||||
}
|
||||
|
||||
// filterSVGImagesFromOpenGraph removes SVG images from OpenGraph metadata.
|
||||
func filterSVGImagesFromOpenGraph(og *opengraph.OpenGraph) *opengraph.OpenGraph {
|
||||
if og == nil || len(og.Images) == 0 {
|
||||
return og
|
||||
}
|
||||
|
||||
og.Images = model.FilterSVGImages(og.Images)
|
||||
return og
|
||||
}
|
||||
|
||||
func openGraphDecodeHTMLEntities(og *opengraph.OpenGraph) {
|
||||
og.Title = html.UnescapeString(og.Title)
|
||||
og.Description = html.UnescapeString(og.Description)
|
||||
}
|
||||
|
||||
func (a *App) parseOpenGraphFromOEmbed(requestURL string, body io.Reader) (*opengraph.OpenGraph, error) {
|
||||
oEmbedResponse, err := oembed.ResponseFromJSON(io.LimitReader(body, MaxOpenGraphResponseSize))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "parseOpenGraphFromOEmbed: Unable to parse oEmbed response")
|
||||
}
|
||||
|
||||
og := &opengraph.OpenGraph{
|
||||
Type: "opengraph",
|
||||
Title: oEmbedResponse.Title,
|
||||
URL: requestURL,
|
||||
}
|
||||
|
||||
if oEmbedResponse.ThumbnailURL != "" {
|
||||
og.Images = append(og.Images, &ogImage.Image{
|
||||
Type: "image",
|
||||
URL: oEmbedResponse.ThumbnailURL,
|
||||
Width: uint64(oEmbedResponse.ThumbnailWidth),
|
||||
Height: uint64(oEmbedResponse.ThumbnailHeight),
|
||||
})
|
||||
}
|
||||
|
||||
og = filterSVGImagesFromOpenGraph(og)
|
||||
|
||||
if toProxyURL := a.ImageProxyAdder(); toProxyURL != nil {
|
||||
og = openGraphDataWithProxyAddedToImageURLs(og, toProxyURL)
|
||||
}
|
||||
|
||||
return og, nil
|
||||
}
|
||||
Reference in New Issue
Block a user