diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2024-03-22 04:32:40 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-22 04:32:40 +0800 |
commit | ca4107dc96e5110dec6a8732e7caa3b071222dcf (patch) | |
tree | 059f12fd9adac777da013cb7f4f67525a1209ac9 /modules | |
parent | bfa160fc98a23923b6ce1cd4d99e8970d937d6ec (diff) | |
download | gitea-ca4107dc96e5110dec6a8732e7caa3b071222dcf.tar.gz gitea-ca4107dc96e5110dec6a8732e7caa3b071222dcf.zip |
Refactor external URL detection (#29973)
Follow #29960, `IsExternalURL` is not needed anymore.
Add some tests for `RedirectToCurrentSite`
Diffstat (limited to 'modules')
-rw-r--r-- | modules/httplib/url.go | 8 | ||||
-rw-r--r-- | modules/httplib/url_test.go | 5 |
2 files changed, 10 insertions, 3 deletions
diff --git a/modules/httplib/url.go b/modules/httplib/url.go index b679b44500..903799cb68 100644 --- a/modules/httplib/url.go +++ b/modules/httplib/url.go @@ -32,9 +32,11 @@ func IsCurrentGiteaSiteURL(s string) bool { return false } if u.Path != "" { - u.Path = "/" + util.PathJoinRelX(u.Path) - if !strings.HasSuffix(u.Path, "/") { - u.Path += "/" + cleanedPath := util.PathJoinRelX(u.Path) + if cleanedPath == "" || cleanedPath == "." { + u.Path = "/" + } else { + u.Path += "/" + cleanedPath + "/" } } if urlIsRelative(s, u) { diff --git a/modules/httplib/url_test.go b/modules/httplib/url_test.go index 9b7b242298..9bf09bcf2f 100644 --- a/modules/httplib/url_test.go +++ b/modules/httplib/url_test.go @@ -53,6 +53,8 @@ func TestIsCurrentGiteaSiteURL(t *testing.T) { assert.True(t, IsCurrentGiteaSiteURL(s), "good = %q", s) } bad := []string{ + ".", + "foo", "/", "//", "\\\\", @@ -67,5 +69,8 @@ func TestIsCurrentGiteaSiteURL(t *testing.T) { setting.AppURL = "http://localhost:3000/" setting.AppSubURL = "" + assert.False(t, IsCurrentGiteaSiteURL("//")) + assert.False(t, IsCurrentGiteaSiteURL("\\\\")) + assert.False(t, IsCurrentGiteaSiteURL("http://localhost")) assert.True(t, IsCurrentGiteaSiteURL("http://localhost:3000?key=val")) } |