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 <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2022-04-04 13:27:18 +03:00
коммит произвёл GitHub
родитель 1cc50ff90c
Коммит b1b9774efa
2 изменённых файлов: 30 добавлений и 1 удалений

Просмотреть файл

@@ -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

Просмотреть файл

@@ -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) {