From eedb70355ad59f9d7054dba198a8352554274e0a Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 3 Jan 2019 10:05:58 -0500 Subject: [PATCH] Ensure we set a content-type for healthz Change-Id: I453b1433c69bf26c28da873dbdd1ac25006b8d60 Co-Authored-By: Tim Allclair (St. Clair) --- .../apiserver/pkg/server/healthz/healthz.go | 2 ++ .../apiserver/pkg/server/healthz/healthz_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go index 17d85fbe637..94bec8a3103 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go +++ b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz.go @@ -185,6 +185,8 @@ func handleRootHealthz(checks ...HealthzChecker) http.HandlerFunc { return } + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + w.Header().Set("X-Content-Type-Options", "nosniff") if _, found := r.URL.Query()["verbose"]; !found { fmt.Fprint(w, "ok") return diff --git a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go index 704532237b7..315c5cab6ea 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go +++ b/staging/src/k8s.io/apiserver/pkg/server/healthz/healthz_test.go @@ -40,6 +40,10 @@ func TestInstallHandler(t *testing.T) { if w.Code != http.StatusOK { t.Errorf("expected %v, got %v", http.StatusOK, w.Code) } + c := w.Header().Get("Content-Type") + if c != "text/plain; charset=utf-8" { + t.Errorf("expected %v, got %v", "text/plain", c) + } if w.Body.String() != "ok" { t.Errorf("expected %v, got %v", "ok", w.Body.String()) } @@ -58,6 +62,10 @@ func TestInstallPathHandler(t *testing.T) { if w.Code != http.StatusOK { t.Errorf("expected %v, got %v", http.StatusOK, w.Code) } + c := w.Header().Get("Content-Type") + if c != "text/plain; charset=utf-8" { + t.Errorf("expected %v, got %v", "text/plain", c) + } if w.Body.String() != "ok" { t.Errorf("expected %v, got %v", "ok", w.Body.String()) } @@ -71,6 +79,10 @@ func TestInstallPathHandler(t *testing.T) { if w.Code != http.StatusOK { t.Errorf("expected %v, got %v", http.StatusOK, w.Code) } + c = w.Header().Get("Content-Type") + if c != "text/plain; charset=utf-8" { + t.Errorf("expected %v, got %v", "text/plain", c) + } if w.Body.String() != "ok" { t.Errorf("expected %v, got %v", "ok", w.Body.String()) } @@ -120,6 +132,10 @@ func testMultipleChecks(path string, t *testing.T) { if w.Code != test.expectedStatus { t.Errorf("case[%d] Expected: %v, got: %v", i, test.expectedStatus, w.Code) } + c := w.Header().Get("Content-Type") + if c != "text/plain; charset=utf-8" { + t.Errorf("case[%d] Expected: %v, got: %v", i, "text/plain", c) + } if w.Body.String() != test.expectedResponse { t.Errorf("case[%d] Expected:\n%v\ngot:\n%v\n", i, test.expectedResponse, w.Body.String()) }