Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix joined urls #26774

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions modules/setting/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
)

// Scheme describes protocol types
Expand Down Expand Up @@ -160,7 +159,11 @@ func MakeAbsoluteAssetURL(appURL, staticURLPrefix string) string {
}

// StaticURLPrefix is just a path
return util.URLJoin(appURL, strings.TrimSuffix(staticURLPrefix, "/"))
base, err := url.Parse(appURL)
if err != nil {
log.Fatal("Unable to parse url: %v", err)
}
return base.ResolveReference(parsedPrefix).String()
}

return strings.TrimSuffix(staticURLPrefix, "/")
Expand Down
22 changes: 14 additions & 8 deletions modules/util/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package util

import (
"net/url"
"path"
"strings"
)

Expand All @@ -28,14 +27,21 @@ func URLJoin(base string, elems ...string) string {
if err != nil {
return ""
}
joinedPath := path.Join(elems...)
argURL, err := url.Parse(joinedPath)
if err != nil {
return ""

var fragment string
last := len(elems) - 1
if len(elems) > 0 {
if strings.Contains(elems[last], "#") {
elems[last], fragment, _ = strings.Cut(elems[last], "#")
Comment on lines +34 to +35
Copy link
Member

@puni9869 puni9869 Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

banch name can also have # like BranchName#123

"/a/b/c#hash", "/a#123", "b/c#1212"

Copy link
Member Author

@KN4CK3R KN4CK3R Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's correct but does not affect the method because it is called with already escaped parameters.

grafik
The screenshot shows a branch with # with a readme which includes the picture from the repo.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, works for me then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some test cases are failing
image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why this PR is marked as depending on the other PR. If #26745 is merged, the test will work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohk, got it. my bad.

}
elems[last] = strings.TrimSuffix(elems[last], "/") // keep old behaviour
}
joinedURL := baseURL.ResolveReference(argURL).String()

joinedURL := baseURL.JoinPath(elems...)
joinedURL.Fragment = fragment

if !baseURL.IsAbs() && !strings.HasPrefix(base, "/") {
return joinedURL[1:] // Removing leading '/' if needed
return strings.TrimPrefix(joinedURL.String(), "/") // Removing leading '/' if needed
}
return joinedURL
return joinedURL.String()
}
167 changes: 140 additions & 27 deletions modules/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,148 @@ import (
)

func TestURLJoin(t *testing.T) {
type test struct {
Expected string
cases := []struct {
Base string
Elements []string
}
newTest := func(expected, base string, elements ...string) test {
return test{Expected: expected, Base: base, Elements: elements}
Elems []string
Expected string
}{
{
Base: "https://try.gitea.io",
Elems: []string{"a/b", "c"},
Expected: "https://try.gitea.io/a/b/c",
},
{
Base: "https://try.gitea.io/",
Elems: []string{"a/b", "c"},
Expected: "https://try.gitea.io/a/b/c",
},
{
Base: "https://try.gitea.io/",
Elems: []string{"/a/b/", "/c/"},
Expected: "https://try.gitea.io/a/b/c",
},
{
Base: "https://try.gitea.io/",
Elems: []string{"/a/./b/", "../c/"},
Expected: "https://try.gitea.io/a/c",
},
{
Base: "a",
Elems: []string{"b/c/"},
Expected: "a/b/c",
},
{
Base: "a/",
Elems: []string{"b/c/", "/../d/"},
Expected: "a/b/d",
},
{
Base: "https://try.gitea.io",
Elems: []string{"a/b", "c#d"},
Expected: "https://try.gitea.io/a/b/c#d",
},
{
Base: "/a/",
Elems: []string{"b/c/", "/../d/"},
Expected: "/a/b/d",
},
{
Base: "/a",
Elems: []string{"b/c/"},
Expected: "/a/b/c",
},
{
Base: "/a",
Elems: []string{"b/c#hash"},
Expected: "/a/b/c#hash",
},
{
Base: "\x7f", // invalid url
Expected: "",
},
{
Base: "path",
Expected: "path/",
},
{
Base: "/path",
Expected: "/path/",
},
{
Base: "path/",
Expected: "path/",
},
{
Base: "/path/",
Expected: "/path/",
},
{
Base: "path",
Elems: []string{"sub"},
Expected: "path/sub",
},
{
Base: "/path",
Elems: []string{"sub"},
Expected: "/path/sub",
},
{
Base: "https://gitea.com",
Expected: "https://gitea.com/",
},
{
Base: "https://gitea.com/",
Expected: "https://gitea.com/",
},
{
Base: "https://gitea.com",
Elems: []string{"sub/path"},
Expected: "https://gitea.com/sub/path",
},
{
Base: "https://gitea.com/",
Elems: []string{"sub", "path"},
Expected: "https://gitea.com/sub/path",
},
{
Base: "https://gitea.com/",
Elems: []string{"sub", "..", "path"},
Expected: "https://gitea.com/path",
},
{
Base: "https://gitea.com/",
Elems: []string{"sub/..", "path"},
Expected: "https://gitea.com/path",
},
{
Base: "https://gitea.com/",
Elems: []string{"sub", "../path"},
Expected: "https://gitea.com/path",
},
{
Base: "https://gitea.com/",
Elems: []string{"sub/../path"},
Expected: "https://gitea.com/path",
},
{
Base: "https://gitea.com/",
Elems: []string{"sub", ".", "path"},
Expected: "https://gitea.com/sub/path",
},
{
Base: "https://gitea.com/",
Elems: []string{"sub", "/", "path"},
Expected: "https://gitea.com/sub/path",
},
{ // https://github.com/go-gitea/gitea/issues/25632
Base: "/owner/repo/media/branch/main",
Elems: []string{"/../other/image.png"},
Expected: "/owner/repo/media/branch/other/image.png",
},
Comment on lines +149 to +153
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Old code: "/other/image.png"
New code: "/owner/repo/media/branch/other/image.png"

}
for _, test := range []test{
newTest("https://try.gitea.io/a/b/c",
"https://try.gitea.io", "a/b", "c"),
newTest("https://try.gitea.io/a/b/c",
"https://try.gitea.io/", "/a/b/", "/c/"),
newTest("https://try.gitea.io/a/c",
"https://try.gitea.io/", "/a/./b/", "../c/"),
newTest("a/b/c",
"a", "b/c/"),
newTest("a/b/d",
"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"),
Comment on lines -25 to -42
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Converted these test cases into a (for me) more readable format.

} {
assert.Equal(t, test.Expected, URLJoin(test.Base, test.Elements...))

for i, c := range cases {
assert.Equal(t, c.Expected, URLJoin(c.Base, c.Elems...), "Unexpected result in test case %v", i)
}
}

Expand Down
Loading