aboutsummaryrefslogtreecommitdiffstats
path: root/routers/utils
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2024-03-22 04:32:40 +0800
committerGitHub <noreply@github.com>2024-03-22 04:32:40 +0800
commitca4107dc96e5110dec6a8732e7caa3b071222dcf (patch)
tree059f12fd9adac777da013cb7f4f67525a1209ac9 /routers/utils
parentbfa160fc98a23923b6ce1cd4d99e8970d937d6ec (diff)
downloadgitea-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 'routers/utils')
-rw-r--r--routers/utils/utils.go16
-rw-r--r--routers/utils/utils_test.go39
2 files changed, 0 insertions, 55 deletions
diff --git a/routers/utils/utils.go b/routers/utils/utils.go
index 1f4d11fd3c..3035073d5c 100644
--- a/routers/utils/utils.go
+++ b/routers/utils/utils.go
@@ -5,26 +5,10 @@ package utils
import (
"html"
- "net/url"
"strings"
-
- "code.gitea.io/gitea/modules/setting"
)
// SanitizeFlashErrorString will sanitize a flash error string
func SanitizeFlashErrorString(x string) string {
return strings.ReplaceAll(html.EscapeString(x), "\n", "<br>")
}
-
-// IsExternalURL checks if rawURL points to an external URL like http://example.com
-func IsExternalURL(rawURL string) bool {
- parsed, err := url.Parse(rawURL)
- if err != nil {
- return true
- }
- appURL, _ := url.Parse(setting.AppURL)
- if len(parsed.Host) != 0 && strings.Replace(parsed.Host, "www.", "", 1) != strings.Replace(appURL.Host, "www.", "", 1) {
- return true
- }
- return false
-}
diff --git a/routers/utils/utils_test.go b/routers/utils/utils_test.go
index 440aad87c6..6e7f3c33cd 100644
--- a/routers/utils/utils_test.go
+++ b/routers/utils/utils_test.go
@@ -5,47 +5,8 @@ package utils
import (
"testing"
-
- "code.gitea.io/gitea/modules/setting"
-
- "github.com/stretchr/testify/assert"
)
-func TestIsExternalURL(t *testing.T) {
- setting.AppURL = "https://try.gitea.io/"
- type test struct {
- Expected bool
- RawURL string
- }
- newTest := func(expected bool, rawURL string) test {
- return test{Expected: expected, RawURL: rawURL}
- }
- for _, test := range []test{
- newTest(false,
- "https://try.gitea.io"),
- newTest(true,
- "https://example.com/"),
- newTest(true,
- "//example.com"),
- newTest(true,
- "http://example.com"),
- newTest(false,
- "a/"),
- newTest(false,
- "https://try.gitea.io/test?param=false"),
- newTest(false,
- "test?param=false"),
- newTest(false,
- "//try.gitea.io/test?param=false"),
- newTest(false,
- "/hey/hey/hey#3244"),
- newTest(true,
- "://missing protocol scheme"),
- } {
- assert.Equal(t, test.Expected, IsExternalURL(test.RawURL))
- }
-}
-
func TestSanitizeFlashErrorString(t *testing.T) {
tests := []struct {
name string