From 46b05499cca3c32606f2c2337675bbc6cfc59461 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Fri, 1 Feb 2019 17:23:04 -0500 Subject: [PATCH] MM-13932 Fix image metadata for invalid image links (#10217) --- app/post_metadata.go | 2 +- app/post_metadata_test.go | 55 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/app/post_metadata.go b/app/post_metadata.go index b444fd0a05..9bc36ba22f 100644 --- a/app/post_metadata.go +++ b/app/post_metadata.go @@ -192,7 +192,7 @@ func (a *App) getImagesForPost(post *model.Post, imageURLs []string, isNewPost b if _, image, err := a.getLinkMetadata(imageURL, post.CreateAt, isNewPost); err != nil { mlog.Warn("Failed to get dimensions of an image in a post", mlog.String("post_id", post.Id), mlog.String("image_url", imageURL), mlog.Any("err", err)) - } else { + } else if image != nil { images[imageURL] = image } } diff --git a/app/post_metadata_test.go b/app/post_metadata_test.go index 1d40df4d7e..81b347e475 100644 --- a/app/post_metadata_test.go +++ b/app/post_metadata_test.go @@ -496,6 +496,61 @@ func testProxyOpenGraphImage(t *testing.T, th *TestHelper, shouldProxy bool) { } } +func TestGetImagesForPost(t *testing.T) { + t.Run("with an image link", func(t *testing.T) { + th := Setup() + defer th.TearDown() + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "127.0.0.1" + }) + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + file, err := testutils.ReadTestFile("test.png") + require.Nil(t, err) + + w.Header().Set("Content-Type", "image/png") + w.Write(file) + })) + + post := &model.Post{ + Metadata: &model.PostMetadata{}, + } + imageURL := server.URL + "/image.png" + + images := th.App.getImagesForPost(post, []string{imageURL}, false) + + assert.Equal(t, images, map[string]*model.PostImage{ + imageURL: { + Width: 408, + Height: 336, + }, + }) + }) + + t.Run("with an invalid image link", func(t *testing.T) { + th := Setup() + defer th.TearDown() + + th.App.UpdateConfig(func(cfg *model.Config) { + *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "127.0.0.1" + }) + + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + })) + + post := &model.Post{ + Metadata: &model.PostMetadata{}, + } + imageURL := server.URL + "/bad_image.png" + + images := th.App.getImagesForPost(post, []string{imageURL}, false) + + assert.Equal(t, images, map[string]*model.PostImage{}) + }) +} + func TestGetEmojiNamesForString(t *testing.T) { testCases := []struct { Description string