From 3e12f837325888f1ac5c2de9fbcf593a434a4fb7 Mon Sep 17 00:00:00 2001 From: Alessandro Ros Date: Fri, 1 Dec 2023 21:14:45 +0100 Subject: [PATCH] hls, webrtc: fix appending slash to paths that contain slashes (#2773) --- .../httpserv/location_with_trailing_slash.go | 12 +++++++++++- .../httpserv/location_with_trailing_slash_test.go | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/protocols/httpserv/location_with_trailing_slash.go b/internal/protocols/httpserv/location_with_trailing_slash.go index 6c6db9c4..339a4d5c 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 f782115a..e5d20c3a 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))