MM-10417 Add local image proxy and enable by default (#9967)

* MM-10417 Add local image proxy and enable by default

* Remove unused function

* Add dependencies for willnorris/imageproxy

* Fixed compilation errors

* Lock to the master version of willnorris/imageproxy

* Fix atmos/camo proxy when no SiteURL is specified

* Re-add default values for deprecated settings

* Fix unit tests added by merge

* Pass imageproxy to App struct

* Remove unneeded locking when creating the image proxy

* Remove empty test file
Этот коммит содержится в:
Harrison Healey
2019-01-24 16:11:32 -04:00
коммит произвёл GitHub
родитель e961b4cd0d
Коммит ba5566d1a0
80 изменённых файлов: 19742 добавлений и 177 удалений

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

@@ -12,12 +12,10 @@ func (api *API) InitImage() {
}
func getImage(c *Context, w http.ResponseWriter, r *http.Request) {
// Only redirect to our image proxy if one is enabled. Arbitrary redirects are not allowed for
// security reasons.
if transform := c.App.ImageProxyAdder(); transform != nil {
http.Redirect(w, r, transform(r.URL.Query().Get("url")), http.StatusFound)
if !*c.App.Config().ImageProxySettings.Enable {
http.NotFound(w, r)
return
}
http.NotFound(w, r)
c.App.ImageProxy.GetImage(w, r, r.URL.Query().Get("url"))
}

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

@@ -4,7 +4,9 @@
package api4
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"testing"
@@ -18,45 +20,75 @@ func TestGetImage(t *testing.T) {
th := Setup().InitBasic()
defer th.TearDown()
// Prevent the test client from following a redirect
th.Client.HttpClient.CheckRedirect = func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
}
originURL := "http://foo.bar/baz.gif"
t.Run("proxy disabled", func(t *testing.T) {
imageURL := "http://foo.bar/baz.gif"
r, err := http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+url.QueryEscape(originURL), nil)
require.NoError(t, err)
r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken)
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.ImageProxySettings.Enable = model.NewBool(false)
})
imageProxyType := th.App.Config().ServiceSettings.ImageProxyType
imageProxyOptions := th.App.Config().ServiceSettings.ImageProxyOptions
imageProxyURL := th.App.Config().ServiceSettings.ImageProxyURL
defer func() {
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyType = imageProxyType })
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyOptions = imageProxyOptions })
th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.ImageProxyOptions = imageProxyURL })
}()
r, err := http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+url.QueryEscape(imageURL), nil)
require.NoError(t, err)
r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken)
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.ServiceSettings.ImageProxyType = nil
resp, err := th.Client.HttpClient.Do(r)
require.NoError(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
})
resp, err := th.Client.HttpClient.Do(r)
require.NoError(t, err)
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
t.Run("atmos/camo", func(t *testing.T) {
imageURL := "http://foo.bar/baz.gif"
proxiedURL := "https://proxy.foo.bar/004afe2ef382eb5f30c4490f793f8a8c5b33d8a2/687474703a2f2f666f6f2e6261722f62617a2e676966"
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.ServiceSettings.ImageProxyType = model.NewString("atmos/camo")
cfg.ServiceSettings.ImageProxyOptions = model.NewString("foo")
cfg.ServiceSettings.ImageProxyURL = model.NewString("https://proxy.foo.bar")
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.ImageProxySettings.Enable = model.NewBool(true)
cfg.ImageProxySettings.ImageProxyType = model.NewString("atmos/camo")
cfg.ImageProxySettings.RemoteImageProxyOptions = model.NewString("foo")
cfg.ImageProxySettings.RemoteImageProxyURL = model.NewString("https://proxy.foo.bar")
})
r, err := http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+url.QueryEscape(imageURL), nil)
require.NoError(t, err)
r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken)
resp, err := th.Client.HttpClient.Do(r)
require.NoError(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, proxiedURL, resp.Header.Get("Location"))
})
r, err = http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+originURL, nil)
require.NoError(t, err)
r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken)
t.Run("local", func(t *testing.T) {
th.App.UpdateConfig(func(cfg *model.Config) {
cfg.ImageProxySettings.Enable = model.NewBool(true)
cfg.ImageProxySettings.ImageProxyType = model.NewString("local")
resp, err = th.Client.HttpClient.Do(r)
require.NoError(t, err)
assert.Equal(t, http.StatusFound, resp.StatusCode)
assert.Equal(t, "https://proxy.foo.bar/004afe2ef382eb5f30c4490f793f8a8c5b33d8a2/687474703a2f2f666f6f2e6261722f62617a2e676966", resp.Header.Get("Location"))
// Allow requests to the "remote" image
cfg.ServiceSettings.AllowedUntrustedInternalConnections = model.NewString("127.0.0.1")
})
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Write([]byte("success"))
})
imageServer := httptest.NewServer(handler)
defer imageServer.Close()
r, err := http.NewRequest("GET", th.Client.ApiUrl+"/image?url="+url.QueryEscape(imageServer.URL+"/image.png"), nil)
require.NoError(t, err)
r.Header.Set(model.HEADER_AUTH, th.Client.AuthType+" "+th.Client.AuthToken)
resp, err := th.Client.HttpClient.Do(r)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
respBody, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
assert.Equal(t, "success", string(respBody))
})
}

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

@@ -19,8 +19,10 @@ func (api *API) InitOpenGraph() {
// Dump the image cache if the proxy settings have changed. (need switch URLs to the correct proxy)
api.ConfigService.AddConfigListener(func(before, after *model.Config) {
if (before.ServiceSettings.ImageProxyType != after.ServiceSettings.ImageProxyType) ||
(before.ServiceSettings.ImageProxyURL != after.ServiceSettings.ImageProxyType) {
if (before.ImageProxySettings.Enable != after.ImageProxySettings.Enable) ||
(before.ImageProxySettings.ImageProxyType != after.ImageProxySettings.ImageProxyType) ||
(before.ImageProxySettings.RemoteImageProxyURL != after.ImageProxySettings.RemoteImageProxyURL) ||
(before.ImageProxySettings.RemoteImageProxyOptions != after.ImageProxySettings.RemoteImageProxyOptions) {
openGraphDataCache.Purge()
}
})