| index 415fa2636..04d85306d 100644 |
| --- a/internal/console/opshots_test.go |
| +++ b/internal/console/opshots_test.go |
| @@ -4,6 +4,7 @@ import ( |
| "bytes" |
| "context" |
| "io" |
| + "net" |
| "net/http" |
| "net/http/httptest" |
| "net/http/httputil" |
| @@ -11,6 +12,7 @@ import ( |
| "os" |
| "os/exec" |
| "path/filepath" |
| + "slices" |
| "strconv" |
| "strings" |
| "testing" |
| @@ -40,6 +42,122 @@ var opShotPages = []struct{ name, path string }{ |
| {"backups", "/fleet/backups"}, |
| } |
| |
| +// opShotsProxy fronts the console for the screenshot browser: every request |
| +// carries the signed-in cookies in place of the browser's own, and asks for |
| +// an uncompressed body so a dark shot can edit the page. |
| +func opShotsProxy(target *url.URL, cookies []*http.Cookie) *httputil.ReverseProxy { |
| + return &httputil.ReverseProxy{Rewrite: func(pr *httputil.ProxyRequest) { |
| + pr.SetURL(target) |
| + pr.Out.Host = pr.In.Host // SetURL would send the target's |
| + // Rewrite starts with the forwarding headers removed: carry the |
| + // client's through, and add its address to X-Forwarded-For. |
| + for _, name := range []string{"Forwarded", "X-Forwarded-Host", "X-Forwarded-Proto"} { |
| + if v, ok := pr.In.Header[name]; ok { |
| + pr.Out.Header[name] = v |
| + } |
| + } |
| + if ip, _, err := net.SplitHostPort(pr.In.RemoteAddr); err == nil { |
| + if prior := pr.In.Header["X-Forwarded-For"]; len(prior) > 0 { |
| + ip = strings.Join(prior, ", ") + ", " + ip |
| + } |
| + pr.Out.Header.Set("X-Forwarded-For", ip) |
| + } |
| + pr.Out.Header.Del("Accept-Encoding") |
| + pr.Out.Header.Del("Cookie") |
| + for _, c := range cookies { |
| + pr.Out.AddCookie(c) |
| + } |
| + }} |
| +} |
| + |
| +// TestOpShotsProxyRequestHeaders pins what the console receives through |
| +// opShotsProxy: the browser's Host, the signed-in cookie alone, and the |
| +// forwarding headers kept with the client's address appended. |
| +func TestOpShotsProxyRequestHeaders(t *testing.T) { |
| + type seen struct { |
| + host, uri string |
| + header http.Header |
| + } |
| + got := make(chan seen, 1) |
| + upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| + got <- seen{r.Host, r.RequestURI, r.Header.Clone()} |
| + })) |
| + defer upstream.Close() |
| + target, err := url.Parse(upstream.URL + "/base") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + front := httptest.NewServer(opShotsProxy(target, []*http.Cookie{{Name: "sid", Value: "signed-in"}})) |
| + defer front.Close() |
| + |
| + for _, tc := range []struct { |
| + name string |
| + in http.Header |
| + want http.Header |
| + }{ |
| + { |
| + name: "first proxy", |
| + in: http.Header{}, |
| + want: http.Header{"X-Forwarded-For": {"127.0.0.1"}}, |
| + }, |
| + { |
| + name: "behind another proxy", |
| + in: http.Header{ |
| + "X-Forwarded-For": {"203.0.113.9", "198.51.100.7"}, |
| + "X-Forwarded-Host": {"shop.example.com"}, |
| + "X-Forwarded-Proto": {"https"}, |
| + "Forwarded": {"for=203.0.113.9"}, |
| + }, |
| + want: http.Header{ |
| + "X-Forwarded-For": {"203.0.113.9, 198.51.100.7, 127.0.0.1"}, |
| + "X-Forwarded-Host": {"shop.example.com"}, |
| + "X-Forwarded-Proto": {"https"}, |
| + "Forwarded": {"for=203.0.113.9"}, |
| + }, |
| + }, |
| + } { |
| + t.Run(tc.name, func(t *testing.T) { |
| + req, err := http.NewRequest(http.MethodGet, front.URL+"/fleet?page=2", nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + req.Host = "console.example.com" |
| + req.Header = tc.in.Clone() |
| + req.Header.Set("User-Agent", "shots") |
| + req.Header.Set("Accept-Encoding", "br") |
| + req.Header.Set("Cookie", "browser=own") |
| + req.Header.Set("Connection", "X-Hop") |
| + req.Header.Set("X-Hop", "dropped") |
| + req.Header.Set("X-Kept", "kept") |
| + resp, err := http.DefaultTransport.RoundTrip(req) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + resp.Body.Close() |
| + r := <-got |
| + if r.host != "console.example.com" || r.uri != "/base/fleet?page=2" { |
| + t.Errorf("host %q uri %q", r.host, r.uri) |
| + } |
| + for name, want := range map[string]string{ |
| + "Cookie": "sid=signed-in", |
| + "Accept-Encoding": "gzip", // the proxy's transport asks for its own |
| + "User-Agent": "shots", |
| + "X-Kept": "kept", |
| + "X-Hop": "", |
| + } { |
| + if v := r.header.Get(name); v != want { |
| + t.Errorf("%s = %q, want %q", name, v, want) |
| + } |
| + } |
| + for _, name := range []string{"X-Forwarded-For", "X-Forwarded-Host", "X-Forwarded-Proto", "Forwarded"} { |
| + if !slices.Equal(r.header[name], tc.want[name]) { |
| + t.Errorf("%s = %q, want %q", name, r.header[name], tc.want[name]) |
| + } |
| + } |
| + }) |
| + } |
| +} |
| + |
| // seedOperatorShotExtras gives the moved pages something to show: an |
| // hour of one box's metrics with a unit, the economics fixture |
| // (seedEconomicsGolden) and a bill on file for this month. |
| @@ -118,16 +236,7 @@ func TestOperatorScreenshots(t *testing.T) { |
| t.Fatal("the signed-in browser holds no cookie") |
| } |
| front := func(dark bool) *httptest.Server { |
| - proxy := httputil.NewSingleHostReverseProxy(target) |
| - director := proxy.Director |
| - proxy.Director = func(r *http.Request) { |
| - director(r) |
| - r.Header.Del("Accept-Encoding") |
| - r.Header.Del("Cookie") |
| - for _, c := range cookies { |
| - r.AddCookie(c) |
| - } |
| - } |
| + proxy := opShotsProxy(target, cookies) |
| if dark { |
| proxy.ModifyResponse = func(resp *http.Response) error { |
| if !strings.HasPrefix(resp.Header.Get("Content-Type"), "text/html") { |