2019-12-25 01:50:25 +00:00
|
|
|
/*
|
|
|
|
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 security
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
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"
|
2019-12-25 01:50:25 +00:00
|
|
|
|
|
|
|
"k8s.io/ingress-nginx/test/e2e/framework"
|
|
|
|
)
|
|
|
|
|
2020-02-16 18:27:58 +00:00
|
|
|
var _ = framework.IngressNginxDescribe("[Security] request smuggling", func() {
|
2019-12-25 01:50:25 +00:00
|
|
|
f := framework.NewDefaultFramework("request-smuggling")
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.BeforeEach(func() {
|
2019-12-25 01:50:25 +00:00
|
|
|
f.NewEchoDeployment()
|
|
|
|
})
|
|
|
|
|
2020-02-19 03:08:56 +00:00
|
|
|
ginkgo.It("should not return body content from error_page", func() {
|
2019-12-25 01:50:25 +00:00
|
|
|
host := "foo.bar.com"
|
|
|
|
|
|
|
|
snippet := `
|
|
|
|
server {
|
|
|
|
listen 80;
|
|
|
|
server_name notlocalhost;
|
|
|
|
location /_hidden/index.html {
|
|
|
|
return 200 'This should be hidden!';
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
f.UpdateNginxConfigMapData("http-snippet", snippet)
|
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
// TODO: currently using a self hosted HTTPBun instance results in a 499, we
|
|
|
|
// should move away from using httpbun.com once we have the httpbun
|
|
|
|
// deployment as part of the framework
|
2019-12-25 01:50:25 +00:00
|
|
|
ing := framework.NewSingleIngress(host, "/", host, f.Namespace, framework.EchoService, 80, map[string]string{
|
2023-05-10 14:43:02 +00:00
|
|
|
"nginx.ingress.kubernetes.io/auth-signin": "https://httpbun.com/bearer/d4bcba7a-0def-4a31-91a7-47e420adf44b",
|
|
|
|
"nginx.ingress.kubernetes.io/auth-url": "https://httpbun.com/basic-auth/user/passwd",
|
2019-12-25 01:50:25 +00:00
|
|
|
})
|
|
|
|
f.EnsureIngress(ing)
|
|
|
|
|
|
|
|
f.WaitForNginxServer(host,
|
|
|
|
func(server string) bool {
|
|
|
|
return strings.Contains(server, fmt.Sprintf("server_name %v", host))
|
|
|
|
})
|
|
|
|
|
|
|
|
out, err := smugglingRequest(host, f.GetNginxIP(), 80)
|
2020-02-19 03:08:56 +00:00
|
|
|
assert.Nil(ginkgo.GinkgoT(), err, "obtaining response of request smuggling check")
|
|
|
|
assert.NotContains(ginkgo.GinkgoT(), out, "This should be hidden!")
|
2019-12-25 01:50:25 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
func smugglingRequest(host, addr string, port int) (string, error) {
|
2020-09-03 02:01:13 +00:00
|
|
|
hostPort := net.JoinHostPort(addr, fmt.Sprintf("%v", port))
|
|
|
|
conn, err := net.Dial("tcp", hostPort)
|
2019-12-25 01:50:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
2023-07-03 12:50:52 +00:00
|
|
|
if err := conn.SetDeadline(time.Now().Add(time.Second * 10)); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-12-25 01:50:25 +00:00
|
|
|
|
|
|
|
_, err = fmt.Fprintf(conn, "GET /echo HTTP/1.1\r\nHost: %v\r\nContent-Length: 56\r\n\r\nGET /_hidden/index.html HTTP/1.1\r\nHost: notlocalhost\r\n\r\n", host)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// wait for /_hidden/index.html response
|
2020-06-27 15:57:00 +00:00
|
|
|
framework.Sleep()
|
2019-12-25 01:50:25 +00:00
|
|
|
|
2023-08-31 07:36:48 +00:00
|
|
|
buf := make([]byte, 1024)
|
2019-12-25 01:50:25 +00:00
|
|
|
r := bufio.NewReader(conn)
|
|
|
|
_, err = r.Read(buf)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(buf), nil
|
|
|
|
}
|