diff --git a/internal/protocols/httpserv/location_with_trailing_slash.go b/internal/protocols/httpserv/location_with_trailing_slash.go index 6c6db9c4bda..339a4d5caca 100644 --- a/internal/protocols/httpserv/location_with_trailing_slash.go +++ b/internal/protocols/httpserv/location_with_trailing_slash.go @@ -4,9 +4,19 @@ import "net/url" // LocationWithTrailingSlash returns the URL in a relative format, with a trailing slash. func LocationWithTrailingSlash(u *url.URL) string { - l := "./" + u.Path[1:] + "/" + l := "./" + + for i := 1; i < len(u.Path); i++ { + if u.Path[i] == '/' { + l += "../" + } + } + + l += u.Path[1:] + "/" + if u.RawQuery != "" { l += "?" + u.RawQuery } + return l } diff --git a/internal/protocols/httpserv/location_with_trailing_slash_test.go b/internal/protocols/httpserv/location_with_trailing_slash_test.go index f782115a19f..e5d20c3ab33 100644 --- a/internal/protocols/httpserv/location_with_trailing_slash_test.go +++ b/internal/protocols/httpserv/location_with_trailing_slash_test.go @@ -28,6 +28,13 @@ func TestLocationWithTrailingSlash(t *testing.T) { }, "./www.example.com/", }, + { + "slashes in path", + &url.URL{ + Path: "/my/path", + }, + "./../my/path/", + }, } { t.Run(ca.name, func(t *testing.T) { require.Equal(t, ca.loc, LocationWithTrailingSlash(ca.url))