From b1b9774efad1521342328aac302a6a33006e1ee7 Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Mon, 4 Apr 2022 13:27:18 +0300 Subject: [PATCH] app/post_metadata: add a type cast check for OpenGraph images (#19877) * app/post_metadata: add a type cast check for OpenGraph images * change log level Co-authored-by: Mattermod --- app/post_metadata.go | 8 +++++++- app/post_metadata_test.go | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/post_metadata.go b/app/post_metadata.go index 09c60c3302..0a0b5449e1 100644 --- a/app/post_metadata.go +++ b/app/post_metadata.go @@ -295,7 +295,13 @@ func (a *App) getImagesForPost(post *model.Post, imageURLs []string, isNewPost b imageURLs = append(imageURLs, a.getImagesInMessageAttachments(post)...) case model.PostEmbedOpengraph: - for _, image := range embed.Data.(*opengraph.OpenGraph).Images { + openGraph, ok := embed.Data.(*opengraph.OpenGraph) + if !ok { + mlog.Warn("Could not read the image data: the data could not be casted to OpenGraph", + mlog.String("post_id", post.Id), mlog.String("data type", fmt.Sprintf("%t", embed.Data))) + continue + } + for _, image := range openGraph.Images { var imageURL string if image.SecureURL != "" { imageURL = image.SecureURL diff --git a/app/post_metadata_test.go b/app/post_metadata_test.go index 08500f90bb..25af86deb2 100644 --- a/app/post_metadata_test.go +++ b/app/post_metadata_test.go @@ -1222,6 +1222,29 @@ func TestGetImagesForPost(t *testing.T) { }, }) }) + + t.Run("with an invalid OpenGraph image data", func(t *testing.T) { + th := Setup(t) + defer th.TearDown() + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "127.0.0.1" + }) + + post := &model.Post{ + Metadata: &model.PostMetadata{ + Embeds: []*model.PostEmbed{ + { + Type: model.PostEmbedOpengraph, + Data: map[string]interface{}{}, + }, + }, + }, + } + + images := th.App.getImagesForPost(post, []string{}, false) + assert.Equal(t, images, map[string]*model.PostImage{}) + }) } func TestGetEmojiNamesForString(t *testing.T) {