2018-06-12 19:33:53 +00:00
|
|
|
/*
|
|
|
|
Copyright 2017 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 settings
|
|
|
|
|
|
|
|
import (
|
2018-06-13 19:10:33 +00:00
|
|
|
"net/http"
|
2018-06-12 19:33:53 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-07-31 16:16:28 +00:00
|
|
|
"github.com/onsi/ginkgo/v2"
|
2020-02-19 03:08:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-06-12 19:33:53 +00:00
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
const forwardedHeadersHost = "forwarded-headers"
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.DescribeSetting("use-forwarded-headers", func() {
|
2018-06-12 19:33:53 +00:00
|
|
|
f := framework.NewDefaultFramework("forwarded-headers")
|
|
|
|
|
|
|
|
setting := "use-forwarded-headers"
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.BeforeEach(func() {
|
2018-10-29 21:39:04 +00:00
|
|
|
f.NewEchoDeployment()
|
|
|
|
f.UpdateNginxConfigMapData(setting, "false")
|
2018-06-12 19:33:53 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should trust X-Forwarded headers when setting is true", func() {
|
2023-08-31 07:36:48 +00:00
|
|
|
host := forwardedHeadersHost
|
2018-06-12 19:33:53 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.UpdateNginxConfigMapData(setting, "true")
|
2018-06-12 19:33:53 +00:00
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil)
|
2018-10-29 21:39:04 +00:00
|
|
|
f.EnsureIngress(ing)
|
2018-06-12 19:33:53 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-06-12 19:33:53 +00:00
|
|
|
func(server string) bool {
|
2020-06-12 14:50:07 +00:00
|
|
|
return strings.Contains(server, "server_name forwarded-headers") &&
|
2020-09-15 17:22:26 +00:00
|
|
|
strings.Contains(server, "proxy_set_header X-Forwarded-Proto $pass_access_scheme;")
|
2018-06-12 19:33:53 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.By("ensuring single values are parsed correctly")
|
|
|
|
body := f.HTTPTestClient().
|
|
|
|
GET("/").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
WithHeader("X-Forwarded-Port", "1234").
|
|
|
|
WithHeader("X-Forwarded-Proto", "myproto").
|
2021-05-11 17:03:20 +00:00
|
|
|
WithHeader("X-Forwarded-Scheme", "myproto").
|
2020-02-19 03:08:56 +00:00
|
|
|
WithHeader("X-Forwarded-For", "1.2.3.4").
|
|
|
|
WithHeader("X-Forwarded-Host", "myhost").
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK).
|
|
|
|
Body().
|
|
|
|
Raw()
|
|
|
|
|
2023-06-11 18:49:54 +00:00
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "host=myhost")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-host=myhost")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-proto=myproto")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-scheme=myproto")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-port=1234")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-for=1.2.3.4")
|
2020-02-19 03:08:56 +00:00
|
|
|
|
|
|
|
ginkgo.By("ensuring that first entry in X-Forwarded-Host is used as the best host")
|
|
|
|
body = f.HTTPTestClient().
|
|
|
|
GET("/").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
WithHeader("X-Forwarded-Port", "1234").
|
|
|
|
WithHeader("X-Forwarded-Proto", "myproto").
|
|
|
|
WithHeader("X-Forwarded-For", "1.2.3.4").
|
|
|
|
WithHeader("X-Forwarded-Host", "myhost.com, another.host,example.net").
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK).
|
|
|
|
Body().
|
|
|
|
Raw()
|
|
|
|
|
2023-06-11 18:49:54 +00:00
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "host=myhost.com")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-host=myhost.com")
|
2018-06-12 19:33:53 +00:00
|
|
|
})
|
2020-02-19 03:08:56 +00:00
|
|
|
|
|
|
|
ginkgo.It("should not trust X-Forwarded headers when setting is false", func() {
|
2023-08-31 07:36:48 +00:00
|
|
|
host := forwardedHeadersHost
|
2018-06-12 19:33:53 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.UpdateNginxConfigMapData(setting, "false")
|
2018-06-12 19:33:53 +00:00
|
|
|
|
2019-09-01 18:16:52 +00:00
|
|
|
f.EnsureIngress(framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, nil))
|
2018-06-12 19:33:53 +00:00
|
|
|
|
2018-10-29 21:39:04 +00:00
|
|
|
f.WaitForNginxServer(host,
|
2018-06-12 19:33:53 +00:00
|
|
|
func(server string) bool {
|
2020-06-12 14:50:07 +00:00
|
|
|
return strings.Contains(server, "server_name forwarded-headers") &&
|
|
|
|
strings.Contains(server, "proxy_set_header X-Forwarded-Proto $pass_access_scheme;")
|
2018-06-12 19:33:53 +00:00
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
body := f.HTTPTestClient().
|
|
|
|
GET("/").
|
|
|
|
WithHeader("Host", host).
|
|
|
|
WithHeader("X-Forwarded-Port", "1234").
|
|
|
|
WithHeader("X-Forwarded-Proto", "myproto").
|
2021-05-11 17:03:20 +00:00
|
|
|
WithHeader("X-Forwarded-Scheme", "myproto").
|
2020-02-19 03:08:56 +00:00
|
|
|
WithHeader("X-Forwarded-For", "1.2.3.4").
|
|
|
|
WithHeader("X-Forwarded-Host", "myhost").
|
|
|
|
Expect().
|
|
|
|
Status(http.StatusOK).
|
|
|
|
Body().
|
|
|
|
Raw()
|
|
|
|
|
2023-06-11 18:49:54 +00:00
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "host=forwarded-headers")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-port=80")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-proto=http")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-forwarded-scheme=http")
|
|
|
|
assert.Contains(ginkgo.GinkgoT(), body, "x-original-forwarded-for=1.2.3.4")
|
|
|
|
assert.NotContains(ginkgo.GinkgoT(), body, "host=myhost")
|
|
|
|
assert.NotContains(ginkgo.GinkgoT(), body, "x-forwarded-host=myhost")
|
|
|
|
assert.NotContains(ginkgo.GinkgoT(), body, "x-forwarded-proto=myproto")
|
|
|
|
assert.NotContains(ginkgo.GinkgoT(), body, "x-forwarded-scheme=myproto")
|
|
|
|
assert.NotContains(ginkgo.GinkgoT(), body, "x-forwarded-port=1234")
|
|
|
|
assert.NotContains(ginkgo.GinkgoT(), body, "x-forwarded-for=1.2.3.4")
|
2018-06-12 19:33:53 +00:00
|
|
|
})
|
|
|
|
})
|