Merge branch v2.11 into v3.3
This commit is contained in:
commit
f6fb240eb6
44 changed files with 940 additions and 597 deletions
|
@ -616,7 +616,12 @@ func createHTTPServer(ctx context.Context, ln net.Listener, configuration *stati
|
|||
return nil, err
|
||||
}
|
||||
|
||||
handler = denyFragment(handler)
|
||||
if configuration.HTTP.SanitizePath != nil && *configuration.HTTP.SanitizePath {
|
||||
// sanitizePath is used to clean the URL path by removing /../, /./ and duplicate slash sequences,
|
||||
// to make sure the path is interpreted by the backends as it is evaluated inside rule matchers.
|
||||
handler = sanitizePath(handler)
|
||||
}
|
||||
|
||||
if configuration.HTTP.EncodeQuerySemicolons {
|
||||
handler = encodeQuerySemicolons(handler)
|
||||
} else {
|
||||
|
@ -636,6 +641,8 @@ func createHTTPServer(ctx context.Context, ln net.Listener, configuration *stati
|
|||
})
|
||||
}
|
||||
|
||||
handler = denyFragment(handler)
|
||||
|
||||
serverHTTP := &http.Server{
|
||||
Handler: handler,
|
||||
ErrorLog: stdlog.New(logs.NoLevel(log.Logger, zerolog.DebugLevel), "", 0),
|
||||
|
@ -761,3 +768,20 @@ func denyFragment(h http.Handler) http.Handler {
|
|||
h.ServeHTTP(rw, req)
|
||||
})
|
||||
}
|
||||
|
||||
// sanitizePath removes the "..", "." and duplicate slash segments from the URL.
|
||||
// It cleans the request URL Path and RawPath, and updates the request URI.
|
||||
func sanitizePath(h http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||
r2 := new(http.Request)
|
||||
*r2 = *req
|
||||
|
||||
// Cleans the URL raw path and path.
|
||||
r2.URL = r2.URL.JoinPath()
|
||||
|
||||
// Because the reverse proxy director is building query params from requestURI it needs to be updated as well.
|
||||
r2.RequestURI = r2.URL.RequestURI()
|
||||
|
||||
h.ServeHTTP(rw, r2)
|
||||
})
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -382,3 +383,44 @@ func TestKeepAliveH2c(t *testing.T) {
|
|||
// to change.
|
||||
require.Contains(t, err.Error(), "use of closed network connection")
|
||||
}
|
||||
|
||||
func TestSanitizePath(t *testing.T) {
|
||||
tests := []struct {
|
||||
path string
|
||||
expected string
|
||||
}{
|
||||
{path: "/b", expected: "/b"},
|
||||
{path: "/b/", expected: "/b/"},
|
||||
{path: "/../../b/", expected: "/b/"},
|
||||
{path: "/../../b", expected: "/b"},
|
||||
{path: "/a/b/..", expected: "/a"},
|
||||
{path: "/a/b/../", expected: "/a/"},
|
||||
{path: "/a/../../b", expected: "/b"},
|
||||
{path: "/..///b///", expected: "/b/"},
|
||||
{path: "/a/../b", expected: "/b"},
|
||||
{path: "/a/./b", expected: "/a/b"},
|
||||
{path: "/a//b", expected: "/a/b"},
|
||||
{path: "/a/../../b", expected: "/b"},
|
||||
{path: "/a/../c/../b", expected: "/b"},
|
||||
{path: "/a/../../../c/../b", expected: "/b"},
|
||||
{path: "/a/../c/../../b", expected: "/b"},
|
||||
{path: "/a/..//c/.././b", expected: "/b"},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run("Testing case: "+test.path, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var callCount int
|
||||
clean := sanitizePath(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
callCount++
|
||||
assert.Equal(t, test.expected, r.URL.Path)
|
||||
}))
|
||||
|
||||
request := httptest.NewRequest(http.MethodGet, "http://foo"+test.path, http.NoBody)
|
||||
clean.ServeHTTP(httptest.NewRecorder(), request)
|
||||
|
||||
assert.Equal(t, 1, callCount)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -314,7 +314,7 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
|
|||
assert.NotNil(t, handler)
|
||||
|
||||
req := testhelpers.MustNewRequest(http.MethodGet, "http://callme", nil)
|
||||
assert.Equal(t, "", req.Header.Get("User-Agent"))
|
||||
assert.Empty(t, req.Header.Get("User-Agent"))
|
||||
|
||||
if test.userAgent != "" {
|
||||
req.Header.Set("User-Agent", test.userAgent)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue