Browse Source

Fix #4081 Check for leading / in base before removing it (#4082)

for-closed-social
Antoine GIRARD 6 years ago
committed by Lunny Xiao
parent
commit
35c3510335
2 changed files with 8 additions and 2 deletions
  1. +2
    -2
      modules/util/util.go
  2. +6
    -0
      modules/util/util_test.go

+ 2
- 2
modules/util/util.go View File

@ -72,8 +72,8 @@ func URLJoin(base string, elems ...string) string {
return ""
}
joinedURL := baseURL.ResolveReference(argURL).String()
if !baseURL.IsAbs() {
return joinedURL[1:] // Removing leading '/'
if !baseURL.IsAbs() && !strings.HasPrefix(base, "/") {
return joinedURL[1:] // Removing leading '/' if needed
}
return joinedURL
}

+ 6
- 0
modules/util/util_test.go View File

@ -32,6 +32,12 @@ func TestURLJoin(t *testing.T) {
"a/", "b/c/", "/../d/"),
newTest("https://try.gitea.io/a/b/c#d",
"https://try.gitea.io", "a/b", "c#d"),
newTest("/a/b/d",
"/a/", "b/c/", "/../d/"),
newTest("/a/b/c",
"/a", "b/c/"),
newTest("/a/b/c#hash",
"/a", "b/c#hash"),
} {
assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))
}

Loading…
Cancel
Save