From da3c26b9f36601e7d591cf02d7bd5be675c62bee Mon Sep 17 00:00:00 2001 From: Pavel Zavora Date: Mon, 12 Sep 2022 09:43:20 +0200 Subject: [PATCH 1/2] fix(ui): repair BASEPATH handling --- server/url_prefixer.go | 6 ++---- server/url_prefixer_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/server/url_prefixer.go b/server/url_prefixer.go index 8cb8deddfc..78d0dceee7 100644 --- a/server/url_prefixer.go +++ b/server/url_prefixer.go @@ -84,8 +84,8 @@ func (up *URLPrefixer) ServeHTTP(rw http.ResponseWriter, r *http.Request) { return } - isSVG, _ := regexp.Match(".svg$", []byte(r.URL.String())) - if isSVG { + // do not process JS or SVG files, it only harms them + if isIgnored, _ := regexp.Match("\\.(svg|js)$", []byte(r.URL.String())); isIgnored { up.Next.ServeHTTP(rw, r) return } @@ -186,8 +186,6 @@ func NewDefaultURLPrefixer(prefix string, next http.Handler, lg chronograf.Logge []byte(`src="`), []byte(`href="`), []byte(`url(`), - []byte(`new Worker("`), - []byte(`new Worker('`), []byte(`data-basepath="`), // for forwarding basepath to frontend }, } diff --git a/server/url_prefixer_test.go b/server/url_prefixer_test.go index d1cf66678b..054e5a7448 100644 --- a/server/url_prefixer_test.go +++ b/server/url_prefixer_test.go @@ -176,3 +176,37 @@ func Test_Server_Prefixer_NoPrefixingWithoutFlusther(t *testing.T) { t.Error("No Flusher", ":\n Expected Error Message: \"", server.ErrNotFlusher, "\" but saw none. Msgs:", tl.Messages) } } + +func Test_Server_Prefixer_IgnoreJsAndSvg(t *testing.T) { + expected := "Keep it the same please" + backend := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { + fmt.Fprintf(rw, expected) + }) + + tl := &mocks.TestLogger{} + pfx := &server.URLPrefixer{ + Prefix: " error", + Next: backend, + Logger: tl, + Attrs: [][]byte{ + []byte("same"), + }, + } + + ts := httptest.NewServer(pfx) + defer ts.Close() + + for _, fileName := range []string{"/test.js", "/test.svg"} { + res, err := http.Get(ts.URL + fileName) + if err != nil { + t.Fatal("Unexpected error fetching from prefixer: err:", err) + } + actual, err := ioutil.ReadAll(res.Body) + if err != nil { + t.Fatal("Unable to read prefixed body: err:", err) + } + if string(actual) != expected { + t.Error("Prefixing changed content of ", fileName, ":\n\t\tWant:\n", expected, "\n\t\tGot:\n", string(actual)) + } + } +} From 693fcf9edede79a7a6e2ba1c94d29a393aa73cee Mon Sep 17 00:00:00 2001 From: Pavel Zavora Date: Mon, 12 Sep 2022 11:42:15 +0200 Subject: [PATCH 2/2] chore: update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 995d6b6d8c..7465008f34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ ### Bug Fixes +1. [#6001](https://github.com/influxdata/chronograf/pull/6001): Repair UI served under BASEPATH. + ### Other ## v1.10.0 [2022-08-23]