Skip to content

Commit fc86054

Browse files
authored
Merge pull request kubernetes#72589 from logicalhan/filter
add a content-type filter to apiserver filters to autoset nosniff
2 parents 48c2a5c + d0532bd commit fc86054

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

staging/src/k8s.io/apiserver/pkg/server/filters/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ go_test(
1010
name = "go_default_test",
1111
srcs = [
1212
"compression_test.go",
13+
"content_type_test.go",
1314
"cors_test.go",
1415
"maxinflight_test.go",
1516
"timeout_test.go",
@@ -32,6 +33,7 @@ go_library(
3233
name = "go_default_library",
3334
srcs = [
3435
"compression.go",
36+
"content_type.go",
3537
"cors.go",
3638
"doc.go",
3739
"longrunning.go",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package filters
18+
19+
import "net/http"
20+
21+
// WithContentType sets both the Content-Type and the X-Content-Type-Options (nosniff) header
22+
func WithContentType(handler http.Handler, contentType string) http.Handler {
23+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
24+
w.Header().Set("Content-Type", contentType)
25+
w.Header().Set("X-Content-Type-Options", "nosniff")
26+
handler.ServeHTTP(w, r)
27+
})
28+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package filters
18+
19+
import (
20+
"net/http"
21+
"net/http/httptest"
22+
"testing"
23+
)
24+
25+
func noopHandler() http.HandlerFunc {
26+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
27+
// noop
28+
})
29+
}
30+
31+
func TestWithContentType(t *testing.T) {
32+
mux := http.NewServeMux()
33+
mux.Handle("/text", WithContentType(noopHandler(), "text/plain"))
34+
mux.Handle("/json", WithContentType(noopHandler(), "application/json"))
35+
tests := []struct {
36+
description string
37+
path string
38+
expectedMimeType string
39+
}{
40+
{"/text should return a plain text response", "/text", "text/plain"},
41+
{"/json should return a json response", "/json", "application/json"},
42+
}
43+
for _, test := range tests {
44+
path := "http://example.com" + test.path
45+
t.Run(path, func(t *testing.T) {
46+
req, err := http.NewRequest("GET", path, nil)
47+
if err != nil {
48+
t.Fatalf("unexpected error: %v", err)
49+
}
50+
w := httptest.NewRecorder()
51+
mux.ServeHTTP(w, req)
52+
if nosniffHeader := w.Header().Get("X-Content-Type-Options"); nosniffHeader != "nosniff" {
53+
t.Errorf("expected nosniff header to be set, got %v", nosniffHeader)
54+
}
55+
if mimeTypeHeader := w.Header().Get("Content-Type"); mimeTypeHeader != test.expectedMimeType {
56+
t.Errorf("expected %v, got %v", test.expectedMimeType, mimeTypeHeader)
57+
}
58+
})
59+
}
60+
}

0 commit comments

Comments
 (0)