From 7289c0bae3d91b99100ea2b9acc7bc1eacc3bc25 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 4 May 2023 13:50:35 -0500 Subject: [PATCH 1/6] chore: fix proxy 404 page --- enterprise/wsproxy/wsproxy.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/enterprise/wsproxy/wsproxy.go b/enterprise/wsproxy/wsproxy.go index 508167550d51c..3e5b88920e8e1 100644 --- a/enterprise/wsproxy/wsproxy.go +++ b/enterprise/wsproxy/wsproxy.go @@ -235,8 +235,8 @@ func New(ctx context.Context, opts *Options) (*Server, error) { r.Get("/healthz-report", s.healthReport) r.NotFound(func(rw http.ResponseWriter, r *http.Request) { site.RenderStaticErrorPage(rw, r, site.ErrorPageData{ - Status: 404, - Title: "Route Not Found", + Status: http.StatusBadRequest, + Title: "This is a Workspace Proxy", Description: "The route you requested does not exist on this workspace proxy. Maybe you intended to make this request to the primary dashboard? Click below to be redirected to the primary site.", RetryEnabled: false, DashboardURL: opts.DashboardURL.String(), From f5dca20663646be57145555b773334b906574fb7 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 5 May 2023 09:12:40 -0500 Subject: [PATCH 2/6] Update enterprise/wsproxy/wsproxy.go Co-authored-by: Kyle Carberry --- enterprise/wsproxy/wsproxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/enterprise/wsproxy/wsproxy.go b/enterprise/wsproxy/wsproxy.go index 3e5b88920e8e1..b10e7a9e84d23 100644 --- a/enterprise/wsproxy/wsproxy.go +++ b/enterprise/wsproxy/wsproxy.go @@ -236,7 +236,7 @@ func New(ctx context.Context, opts *Options) (*Server, error) { r.NotFound(func(rw http.ResponseWriter, r *http.Request) { site.RenderStaticErrorPage(rw, r, site.ErrorPageData{ Status: http.StatusBadRequest, - Title: "This is a Workspace Proxy", + Title: "Workspace Proxy", Description: "The route you requested does not exist on this workspace proxy. Maybe you intended to make this request to the primary dashboard? Click below to be redirected to the primary site.", RetryEnabled: false, DashboardURL: opts.DashboardURL.String(), From b90d6ccc333c03405fa050e1e29006afaf7b6797 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Fri, 5 May 2023 09:37:35 -0500 Subject: [PATCH 3/6] Fix PR comments --- enterprise/wsproxy/wsproxy.go | 8 +++++--- site/site.go | 4 +++- site/site_test.go | 32 ++++++++++++++++++++++++++++++++ site/static/error.html | 4 ++-- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/enterprise/wsproxy/wsproxy.go b/enterprise/wsproxy/wsproxy.go index b10e7a9e84d23..9fbbfcd6d7806 100644 --- a/enterprise/wsproxy/wsproxy.go +++ b/enterprise/wsproxy/wsproxy.go @@ -235,9 +235,11 @@ func New(ctx context.Context, opts *Options) (*Server, error) { r.Get("/healthz-report", s.healthReport) r.NotFound(func(rw http.ResponseWriter, r *http.Request) { site.RenderStaticErrorPage(rw, r, site.ErrorPageData{ - Status: http.StatusBadRequest, - Title: "Workspace Proxy", - Description: "The route you requested does not exist on this workspace proxy. Maybe you intended to make this request to the primary dashboard? Click below to be redirected to the primary site.", + Title: "Workspace Proxy", + Status: http.StatusBadRequest, + HideStatus: true, + Description: "Workspace Proxies route traffic in terminals and apps directly to your workspace. " + + "This page must be loaded from the dashboard. Click to be redirected!", RetryEnabled: false, DashboardURL: opts.DashboardURL.String(), }) diff --git a/site/site.go b/site/site.go index 28f0880c3c381..e047ab21cb4d4 100644 --- a/site/site.go +++ b/site/site.go @@ -606,7 +606,9 @@ func extractBin(dest string, r io.Reader) (numExtracted int, err error) { // ErrorPageData contains the variables that are found in // site/static/error.html. type ErrorPageData struct { - Status int + Status int + // HideStatus will remove the status code from the page. + HideStatus bool Title string Description string RetryEnabled bool diff --git a/site/site_test.go b/site/site_test.go index c7b301982f71b..55a5da79dcbaa 100644 --- a/site/site_test.go +++ b/site/site_test.go @@ -517,3 +517,35 @@ func TestRenderStaticErrorPage(t *testing.T) { require.Contains(t, bodyStr, "Retry") require.Contains(t, bodyStr, d.DashboardURL) } + +func TestRenderStaticErrorPageNoStatus(t *testing.T) { + t.Parallel() + + d := site.ErrorPageData{ + HideStatus: true, + Status: http.StatusBadGateway, + Title: "Bad Gateway 1234", + Description: "shout out colin", + RetryEnabled: true, + DashboardURL: "https://example.com", + } + + rw := httptest.NewRecorder() + r := httptest.NewRequest("GET", "/", nil) + site.RenderStaticErrorPage(rw, r, d) + + resp := rw.Result() + defer resp.Body.Close() + require.Equal(t, d.Status, resp.StatusCode) + require.Contains(t, resp.Header.Get("Content-Type"), "text/html") + + body, err := io.ReadAll(resp.Body) + require.NoError(t, err) + bodyStr := string(body) + fmt.Println(string(body)) + require.NotContains(t, bodyStr, strconv.Itoa(d.Status)) + require.Contains(t, bodyStr, d.Title) + require.Contains(t, bodyStr, d.Description) + require.Contains(t, bodyStr, "Retry") + require.Contains(t, bodyStr, d.DashboardURL) +} diff --git a/site/static/error.html b/site/static/error.html index f587a63f467ed..82015b2f2624e 100644 --- a/site/static/error.html +++ b/site/static/error.html @@ -7,7 +7,7 @@ - {{ .Error.Status }} - {{ .Error.Title }} + {{- if not .Error.HideStatus }}{{ .Error.Status }} - {{end}}{{ .Error.Title }}