From c3126e627b08d01e8bffd4688c8b0c70b2dab63d Mon Sep 17 00:00:00 2001 From: GIRARD Antoine Date: Wed, 30 May 2018 10:31:01 +0200 Subject: [PATCH] Fix #4081 Check for leading / in base before removing it --- modules/markup/html.go | 4 ++-- modules/markup/html_test.go | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/modules/markup/html.go b/modules/markup/html.go index d4d1bc5c6f9ba..383525477a7b7 100644 --- a/modules/markup/html.go +++ b/modules/markup/html.go @@ -129,8 +129,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/markup/html_test.go b/modules/markup/html_test.go index e475e575e0482..e269041fdd0ef 100644 --- a/modules/markup/html_test.go +++ b/modules/markup/html_test.go @@ -85,6 +85,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...)) }