From b5dec4f4d9104695ef9614b849957abb0ee31c67 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Wed, 22 Jun 2022 09:12:12 +0530 Subject: [PATCH] MM-40450: Follow redirects in image proxy (#20516) For the local proxy, we weren't following redirects. We remove the CheckRedirect function and use the default function, which is good enough for our purposes. https://mattermost.atlassian.net/browse/MM-40450 ```release-note NONE ``` --- services/imageproxy/local.go | 3 --- services/imageproxy/local_test.go | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/services/imageproxy/local.go b/services/imageproxy/local.go index afd4b0dec8..fc98f6eeb8 100644 --- a/services/imageproxy/local.go +++ b/services/imageproxy/local.go @@ -64,9 +64,6 @@ func makeLocalBackend(proxy *ImageProxy) *LocalBackend { } client := proxy.HTTPService.MakeClient(false) - client.CheckRedirect = func(newreq *http.Request, via []*http.Request) error { - return http.ErrUseLastResponse - } return &LocalBackend{ proxy: proxy, diff --git a/services/imageproxy/local_test.go b/services/imageproxy/local_test.go index eca7960c7b..da32132546 100644 --- a/services/imageproxy/local_test.go +++ b/services/imageproxy/local_test.go @@ -193,6 +193,37 @@ func TestLocalBackend_GetImage(t *testing.T) { _, err = ioutil.ReadAll(resp.Body) require.NoError(t, err) }) + + t.Run("Redirect", func(t *testing.T) { + var mock *httptest.Server + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/image.png": + w.Header().Set("Location", mock.URL+"/image2.png") + w.WriteHeader(http.StatusMovedPermanently) + case "/image2.png": + w.Header().Set("Cache-Control", "max-age=2592000, private") + w.Header().Set("Content-Type", "image/png") + w.Header().Set("Content-Length", "10") + + w.WriteHeader(http.StatusOK) + w.Write([]byte("1111111111")) + } + }) + + mock = httptest.NewServer(handler) + defer mock.Close() + + proxy := makeTestLocalProxy() + + recorder := httptest.NewRecorder() + request, _ := http.NewRequest(http.MethodGet, "", nil) + proxy.GetImage(recorder, request, mock.URL+"/image.png") + resp := recorder.Result() + + require.Equal(t, http.StatusOK, resp.StatusCode) + assert.Equal(t, "10", resp.Header.Get("Content-Length")) + }) } func TestLocalBackend_GetImageDirect(t *testing.T) {