Skip to content

Commit 561f459

Browse files
sapktechknowlogick
authored andcommitted
Fix #4081 Check for leading / in base before removing it (#4083)
1 parent 1177a19 commit 561f459

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

modules/markup/html.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ func URLJoin(base string, elems ...string) string {
129129
return ""
130130
}
131131
joinedURL := baseURL.ResolveReference(argURL).String()
132-
if !baseURL.IsAbs() {
133-
return joinedURL[1:] // Removing leading '/'
132+
if !baseURL.IsAbs() && !strings.HasPrefix(base, "/") {
133+
return joinedURL[1:] // Removing leading '/' if needed
134134
}
135135
return joinedURL
136136
}

modules/markup/html_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ func TestURLJoin(t *testing.T) {
8585
"a/", "b/c/", "/../d/"),
8686
newTest("https://try.gitea.io/a/b/c#d",
8787
"https://try.gitea.io", "a/b", "c#d"),
88+
newTest("/a/b/d",
89+
"/a/", "b/c/", "/../d/"),
90+
newTest("/a/b/c",
91+
"/a", "b/c/"),
92+
newTest("/a/b/c#hash",
93+
"/a", "b/c#hash"),
8894
} {
8995
assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
9096
}

0 commit comments

Comments
 (0)