Skip to content

Commit cc10a43

Browse files
committed
net/url: strip relative path components for JoinPath
Signed-off-by: cui fliter <[email protected]>
1 parent de95dca commit cc10a43

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/net/url/url.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,10 @@ func (u *URL) JoinPath(elem ...string) *URL {
11971197
url := *u
11981198
if len(elem) > 0 {
11991199
elem = append([]string{u.EscapedPath()}, elem...)
1200+
// fix issue https://golang.org/issue/54385
1201+
if elem[0] == "" && strings.HasPrefix(elem[1], "../") {
1202+
elem[0] = "/"
1203+
}
12001204
p := path.Join(elem...)
12011205
// path.Join will remove any trailing slashes.
12021206
// Preserve at least one.

src/net/url/url_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,11 @@ func TestJoinPath(t *testing.T) {
20902090
elem: []string{"/go"},
20912091
out: "https://go.googlesource.com/go",
20922092
},
2093+
{
2094+
base: "https://go.googlesource.com",
2095+
elem: []string{"../go"},
2096+
out: "https://go.googlesource.com/go",
2097+
},
20932098
{
20942099
base: "https://go.googlesource.com//",
20952100
elem: []string{"/go", "a", "b", "c"},

0 commit comments

Comments
 (0)