From 499f1702d13245d78056598ea07b006d80a015dd Mon Sep 17 00:00:00 2001 From: Shivek Khurana Date: Wed, 3 Feb 2021 16:57:04 +0530 Subject: [PATCH] Whitelist media.giphy.com --- protocol/urls/urls.go | 5 +++++ protocol/urls/urls_test.go | 17 ++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/protocol/urls/urls.go b/protocol/urls/urls.go index 1c8aa33f9..979082ad9 100644 --- a/protocol/urls/urls.go +++ b/protocol/urls/urls.go @@ -73,6 +73,11 @@ func LinkPreviewWhitelist() []Site { Address: "giphy.com", ImageSite: true, }, + Site{ + Title: "GIPHY GIFs subdomain", + Address: "media.giphy.com", + ImageSite: true, + }, Site{ Title: "GitHub", Address: "github.com", diff --git a/protocol/urls/urls_test.go b/protocol/urls/urls_test.go index 5efef70dd..d9e93a42a 100644 --- a/protocol/urls/urls_test.go +++ b/protocol/urls/urls_test.go @@ -32,6 +32,11 @@ func TestGetLinkPreviewData(t *testing.T) { } +// split at "." and ignore the first item +func thumbnailUrlWithoutSubdomain(url string) []string { + return strings.Split(url, ".")[1:] +} + func TestGetGiphyPreviewData(t *testing.T) { validGiphyLink := "https://giphy.com/gifs/FullMag-robot-boston-dynamics-dance-lcG3qwtTKSNI2i5vst" previewData, err := GetGiphyPreviewData(validGiphyLink) @@ -46,11 +51,21 @@ func TestGetGiphyPreviewData(t *testing.T) { // Giphy oembed returns links to different servers: https://media1.giphy.com, https://media2.giphy.com and so on // We don't care about the server as long as other parts are equal, so we split at "." and ignore the first item - require.Equal(t, strings.Split(bostonDynamicsEthGifData.ThumbnailURL, ".")[1:], strings.Split(previewData.ThumbnailURL, ".")[1:]) + require.Equal(t, thumbnailUrlWithoutSubdomain(bostonDynamicsEthGifData.ThumbnailURL), thumbnailUrlWithoutSubdomain(previewData.ThumbnailURL)) invalidGiphyLink := "https://giphy.com/gifs/this-gif-does-not-exist-44444" _, err = GetGiphyPreviewData(invalidGiphyLink) require.Error(t, err) + + + // Other link shapes + // shortLink := "https://gph.is/g/aXLyK7P" + mediaLink := "https://media.giphy.com/media/lcG3qwtTKSNI2i5vst/giphy.gif" + + // shortLinkData, _ := GetGiphyPreviewData(shortLink) + mediaLinkData, _ := GetGiphyPreviewData(mediaLink) + + require.Equal(t, thumbnailUrlWithoutSubdomain(mediaLinkData.ThumbnailURL), thumbnailUrlWithoutSubdomain(previewData.ThumbnailURL)) } func TestGetTenorPreviewData(t *testing.T) {