From abe568b5b3d1ea6a65b0ea00b55b3b9f1c96da20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Tue, 5 Jul 2022 21:13:40 -0700 Subject: [PATCH] Proxify empty URL should not crash --- proxy/image_proxy_test.go | 23 +++++++++++++++++++++++ proxy/proxy.go | 5 ++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/proxy/image_proxy_test.go b/proxy/image_proxy_test.go index c658caef..b3c6920f 100644 --- a/proxy/image_proxy_test.go +++ b/proxy/image_proxy_test.go @@ -220,6 +220,29 @@ func TestProxyFilterWithSrcset(t *testing.T) { } } +func TestProxyFilterWithEmptySrcset(t *testing.T) { + os.Clearenv() + os.Setenv("PROXY_IMAGES", "all") + + var err error + parser := config.NewParser() + config.Opts, err = parser.ParseEnvironmentVariables() + if err != nil { + t.Fatalf(`Parsing failure: %v`, err) + } + + r := mux.NewRouter() + r.HandleFunc("/proxy/{encodedURL}", func(w http.ResponseWriter, r *http.Request) {}).Name("proxy") + + input := `

test

` + expected := `

test

` + output := ImageProxyRewriter(r, input) + + if expected != output { + t.Errorf(`Not expected output: got %s`, output) + } +} + func TestProxyFilterWithPictureSource(t *testing.T) { os.Clearenv() os.Setenv("PROXY_IMAGES", "all") diff --git a/proxy/proxy.go b/proxy/proxy.go index a6705638..ad694233 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -14,5 +14,8 @@ import ( // ProxifyURL generates an URL for a proxified resource. func ProxifyURL(router *mux.Router, link string) string { - return route.Path(router, "proxy", "encodedURL", base64.URLEncoding.EncodeToString([]byte(link))) + if link != "" { + return route.Path(router, "proxy", "encodedURL", base64.URLEncoding.EncodeToString([]byte(link))) + } + return "" }