Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
fc86054f
Unverified
Commit
fc86054f
authored
Jan 04, 2019
by
Kubernetes Prow Robot
Committed by
GitHub
Jan 04, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #72589 from logicalhan/filter
add a content-type filter to apiserver filters to autoset nosniff
parents
48c2a5ce
d0532bdb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
90 additions
and
0 deletions
+90
-0
BUILD
staging/src/k8s.io/apiserver/pkg/server/filters/BUILD
+2
-0
content_type.go
...g/src/k8s.io/apiserver/pkg/server/filters/content_type.go
+28
-0
content_type_test.go
.../k8s.io/apiserver/pkg/server/filters/content_type_test.go
+60
-0
No files found.
staging/src/k8s.io/apiserver/pkg/server/filters/BUILD
View file @
fc86054f
...
@@ -10,6 +10,7 @@ go_test(
...
@@ -10,6 +10,7 @@ go_test(
name = "go_default_test",
name = "go_default_test",
srcs = [
srcs = [
"compression_test.go",
"compression_test.go",
"content_type_test.go",
"cors_test.go",
"cors_test.go",
"maxinflight_test.go",
"maxinflight_test.go",
"timeout_test.go",
"timeout_test.go",
...
@@ -32,6 +33,7 @@ go_library(
...
@@ -32,6 +33,7 @@ go_library(
name = "go_default_library",
name = "go_default_library",
srcs = [
srcs = [
"compression.go",
"compression.go",
"content_type.go",
"cors.go",
"cors.go",
"doc.go",
"doc.go",
"longrunning.go",
"longrunning.go",
...
...
staging/src/k8s.io/apiserver/pkg/server/filters/content_type.go
0 → 100644
View file @
fc86054f
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
filters
import
"net/http"
// WithContentType sets both the Content-Type and the X-Content-Type-Options (nosniff) header
func
WithContentType
(
handler
http
.
Handler
,
contentType
string
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
contentType
)
w
.
Header
()
.
Set
(
"X-Content-Type-Options"
,
"nosniff"
)
handler
.
ServeHTTP
(
w
,
r
)
})
}
staging/src/k8s.io/apiserver/pkg/server/filters/content_type_test.go
0 → 100644
View file @
fc86054f
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
filters
import
(
"net/http"
"net/http/httptest"
"testing"
)
func
noopHandler
()
http
.
HandlerFunc
{
return
http
.
HandlerFunc
(
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
// noop
})
}
func
TestWithContentType
(
t
*
testing
.
T
)
{
mux
:=
http
.
NewServeMux
()
mux
.
Handle
(
"/text"
,
WithContentType
(
noopHandler
(),
"text/plain"
))
mux
.
Handle
(
"/json"
,
WithContentType
(
noopHandler
(),
"application/json"
))
tests
:=
[]
struct
{
description
string
path
string
expectedMimeType
string
}{
{
"/text should return a plain text response"
,
"/text"
,
"text/plain"
},
{
"/json should return a json response"
,
"/json"
,
"application/json"
},
}
for
_
,
test
:=
range
tests
{
path
:=
"http://example.com"
+
test
.
path
t
.
Run
(
path
,
func
(
t
*
testing
.
T
)
{
req
,
err
:=
http
.
NewRequest
(
"GET"
,
path
,
nil
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
w
:=
httptest
.
NewRecorder
()
mux
.
ServeHTTP
(
w
,
req
)
if
nosniffHeader
:=
w
.
Header
()
.
Get
(
"X-Content-Type-Options"
);
nosniffHeader
!=
"nosniff"
{
t
.
Errorf
(
"expected nosniff header to be set, got %v"
,
nosniffHeader
)
}
if
mimeTypeHeader
:=
w
.
Header
()
.
Get
(
"Content-Type"
);
mimeTypeHeader
!=
test
.
expectedMimeType
{
t
.
Errorf
(
"expected %v, got %v"
,
test
.
expectedMimeType
,
mimeTypeHeader
)
}
})
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment