Skip to content

Commit 35c3510

Browse files
sapklunny
authored andcommitted
Fix #4081 Check for leading / in base before removing it (#4082)
1 parent 15f6ec9 commit 35c3510

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

modules/util/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ func URLJoin(base string, elems ...string) string {
7272
return ""
7373
}
7474
joinedURL := baseURL.ResolveReference(argURL).String()
75-
if !baseURL.IsAbs() {
76-
return joinedURL[1:] // Removing leading '/'
75+
if !baseURL.IsAbs() && !strings.HasPrefix(base, "/") {
76+
return joinedURL[1:] // Removing leading '/' if needed
7777
}
7878
return joinedURL
7979
}

modules/util/util_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ func TestURLJoin(t *testing.T) {
3232
"a/", "b/c/", "/../d/"),
3333
newTest("https://try.gitea.io/a/b/c#d",
3434
"https://try.gitea.io", "a/b", "c#d"),
35+
newTest("/a/b/d",
36+
"/a/", "b/c/", "/../d/"),
37+
newTest("/a/b/c",
38+
"/a", "b/c/"),
39+
newTest("/a/b/c#hash",
40+
"/a", "b/c#hash"),
3541
} {
3642
assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
3743
}

0 commit comments

Comments
 (0)