summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntoine GIRARD <sapk@users.noreply.github.com>2018-05-30 15:23:43 +0200
committerLunny Xiao <xiaolunwen@gmail.com>2018-05-30 21:23:43 +0800
commit35c3510335149c833c7a41a7f3363a031f879584 (patch)
treec5856d76c4026bfe111864061bfe558d47dd28ab
parent15f6ec96327d69d19fcce937811214e113341f58 (diff)
downloadgitea-35c3510335149c833c7a41a7f3363a031f879584.tar.gz
gitea-35c3510335149c833c7a41a7f3363a031f879584.zip
Fix #4081 Check for leading / in base before removing it (#4082)
-rw-r--r--modules/util/util.go4
-rw-r--r--modules/util/util_test.go6
2 files changed, 8 insertions, 2 deletions
diff --git a/modules/util/util.go b/modules/util/util.go
index 4bd2b843f7..b6acb9796e 100644
--- a/modules/util/util.go
+++ b/modules/util/util.go
@@ -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
}
diff --git a/modules/util/util_test.go b/modules/util/util_test.go
index 67d9efe1d2..0d79df6050 100644
--- a/modules/util/util_test.go
+++ b/modules/util/util_test.go
@@ -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...))
}