Skip to content

Commit b98d393

Browse files
4y8zeripath
authored andcommitted
Added support for gopher URLs. (go-gitea#14749)
* Added support for gopher URLs. * Add setting and make this user settable instead Signed-off-by: Andrew Thornton <[email protected]> Co-authored-by: Andrew Thornton <[email protected]>
1 parent a0c65a1 commit b98d393

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed

custom/conf/app.example.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ PATH =
705705
;;
706706
;; Minimum amount of time a user must exist before comments are kept when the user is deleted.
707707
;USER_DELETE_WITH_COMMENTS_MAX_TIME = 0
708+
;; Valid site url schemes for user profiles
709+
;VALID_SITE_URL_SCHEMES=http,https
708710

709711

710712
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

docs/content/doc/advanced/config-cheat-sheet.en-us.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ relation to port exhaustion.
519519
- `NO_REPLY_ADDRESS`: **noreply.DOMAIN** Value for the domain part of the user's email address in the git log if user has set KeepEmailPrivate to true. DOMAIN resolves to the value in server.DOMAIN.
520520
The user's email will be replaced with a concatenation of the user name in lower case, "@" and NO_REPLY_ADDRESS.
521521
- `USER_DELETE_WITH_COMMENTS_MAX_TIME`: **0** Minimum amount of time a user must exist before comments are kept when the user is deleted.
522+
- `VALID_SITE_URL_SCHEMES`: **http, https**: Valid site url schemes for user profiles
522523

523524
### Service - Expore (`service.explore`)
524525

modules/setting/service.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package setting
66

77
import (
88
"regexp"
9+
"strings"
910
"time"
1011

1112
"code.gitea.io/gitea/modules/log"
@@ -55,6 +56,7 @@ var Service struct {
5556
AutoWatchOnChanges bool
5657
DefaultOrgMemberVisible bool
5758
UserDeleteWithCommentsMaxTime time.Duration
59+
ValidSiteURLSchemes []string
5860

5961
// OpenID settings
6062
EnableOpenIDSignIn bool
@@ -120,6 +122,16 @@ func newService() {
120122
Service.DefaultOrgVisibilityMode = structs.VisibilityModes[Service.DefaultOrgVisibility]
121123
Service.DefaultOrgMemberVisible = sec.Key("DEFAULT_ORG_MEMBER_VISIBLE").MustBool()
122124
Service.UserDeleteWithCommentsMaxTime = sec.Key("USER_DELETE_WITH_COMMENTS_MAX_TIME").MustDuration(0)
125+
sec.Key("VALID_SITE_URL_SCHEMES").MustString("http,https")
126+
Service.ValidSiteURLSchemes = sec.Key("VALID_SITE_URL_SCHEMES").Strings(",")
127+
schemes := make([]string, len(Service.ValidSiteURLSchemes))
128+
for _, scheme := range Service.ValidSiteURLSchemes {
129+
scheme = strings.ToLower(strings.TrimSpace(scheme))
130+
if scheme != "" {
131+
schemes = append(schemes, scheme)
132+
}
133+
}
134+
Service.ValidSiteURLSchemes = schemes
123135

124136
if err := Cfg.Section("service.explore").MapTo(&Service.Explore); err != nil {
125137
log.Fatal("Failed to map service.explore settings: %v", err)

modules/validation/binding.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func CheckGitRefAdditionalRulesValid(name string) bool {
5555
func AddBindingRules() {
5656
addGitRefNameBindingRule()
5757
addValidURLBindingRule()
58+
addValidSiteURLBindingRule()
5859
addGlobPatternRule()
5960
addRegexPatternRule()
6061
addGlobOrRegexPatternRule()
@@ -102,6 +103,24 @@ func addValidURLBindingRule() {
102103
})
103104
}
104105

106+
func addValidSiteURLBindingRule() {
107+
// URL validation rule
108+
binding.AddRule(&binding.Rule{
109+
IsMatch: func(rule string) bool {
110+
return strings.HasPrefix(rule, "ValidSiteUrl")
111+
},
112+
IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
113+
str := fmt.Sprintf("%v", val)
114+
if len(str) != 0 && !IsValidSiteURL(str) {
115+
errs.Add([]string{name}, binding.ERR_URL, "Url")
116+
return false, errs
117+
}
118+
119+
return true, errs
120+
},
121+
})
122+
}
123+
105124
func addGlobPatternRule() {
106125
binding.AddRule(&binding.Rule{
107126
IsMatch: func(rule string) bool {

modules/validation/helpers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,25 @@ func IsValidURL(uri string) bool {
5252
return true
5353
}
5454

55+
// IsValidSiteURL checks if URL is valid
56+
func IsValidSiteURL(uri string) bool {
57+
u, err := url.ParseRequestURI(uri)
58+
if err != nil {
59+
return false
60+
}
61+
62+
if !validPort(portOnly(u.Host)) {
63+
return false
64+
}
65+
66+
for _, scheme := range setting.Service.ValidSiteURLSchemes {
67+
if scheme == u.Scheme {
68+
return true
69+
}
70+
}
71+
return false
72+
}
73+
5574
// IsAPIURL checks if URL is current Gitea instance API URL
5675
func IsAPIURL(uri string) bool {
5776
return strings.HasPrefix(strings.ToLower(uri), strings.ToLower(setting.AppURL+"api"))

services/forms/user_form.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ type UpdateProfileForm struct {
226226
Name string `binding:"AlphaDashDot;MaxSize(40)"`
227227
FullName string `binding:"MaxSize(100)"`
228228
KeepEmailPrivate bool
229-
Website string `binding:"ValidUrl;MaxSize(255)"`
229+
Website string `binding:"ValidSiteUrl;MaxSize(255)"`
230230
Location string `binding:"MaxSize(50)"`
231231
Language string
232232
Description string `binding:"MaxSize(255)"`

0 commit comments

Comments
 (0)