From 63b2a0e069e4e37b0c1c36a73a3d62b452b2c7a5 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 17 Mar 2023 15:14:10 +0100 Subject: [PATCH] fix: apply API.HTTPHeaders to /webui redirect --- core/corehttp/redirect.go | 15 +++++++++++++-- test/cli/gateway_test.go | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/core/corehttp/redirect.go b/core/corehttp/redirect.go index bcd536d23..4b98963e2 100644 --- a/core/corehttp/redirect.go +++ b/core/corehttp/redirect.go @@ -8,8 +8,14 @@ import ( ) func RedirectOption(path string, redirect string) ServeOption { - handler := &redirectHandler{redirect} return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) { + cfg, err := n.Repo.Config() + if err != nil { + return nil, err + } + + handler := &redirectHandler{redirect, cfg.API.HTTPHeaders} + if len(path) > 0 { mux.Handle("/"+path+"/", handler) } else { @@ -20,9 +26,14 @@ func RedirectOption(path string, redirect string) ServeOption { } type redirectHandler struct { - path string + path string + headers map[string][]string } func (i *redirectHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + for k, v := range i.headers { + w.Header()[k] = v + } + http.Redirect(w, r, i.path, http.StatusFound) } diff --git a/test/cli/gateway_test.go b/test/cli/gateway_test.go index a585925a7..d2a90b04a 100644 --- a/test/cli/gateway_test.go +++ b/test/cli/gateway_test.go @@ -219,6 +219,23 @@ func TestGateway(t *testing.T) { assert.Contains(t, []int{302, 301}, resp.StatusCode) }) + t.Run("GET /webui/ returns user-specified headers", func(t *testing.T) { + t.Parallel() + + header := "Access-Control-Allow-Origin" + values := []string{"http://localhost:3000", "https://webui.ipfs.io"} + + node := harness.NewT(t).NewNode().Init() + node.UpdateConfig(func(cfg *config.Config) { + cfg.API.HTTPHeaders = map[string][]string{header: values} + }) + node.StartDaemon() + + resp := node.APIClient().DisableRedirects().Get("/webui/") + assert.Equal(t, resp.Headers.Values(header), values) + assert.Contains(t, []int{302, 301}, resp.StatusCode) + }) + t.Run("GET /logs returns logs", func(t *testing.T) { t.Parallel() apiClient := node.APIClient()