@@ -7,11 +7,12 @@ package repo
77
88import (
99 "errors"
10+ "net/url"
11+ "regexp"
1012 "strings"
1113 "time"
1214
1315 "code.gitea.io/git"
14-
1516 "code.gitea.io/gitea/models"
1617 "code.gitea.io/gitea/modules/auth"
1718 "code.gitea.io/gitea/modules/base"
@@ -21,6 +22,8 @@ import (
2122 "code.gitea.io/gitea/modules/util"
2223 "code.gitea.io/gitea/modules/validation"
2324 "code.gitea.io/gitea/routers/utils"
25+
26+ "github.com/mvdan/xurls"
2427)
2528
2629const (
@@ -33,6 +36,8 @@ const (
3336 tplProtectedBranch base.TplName = "repo/settings/protected_branch"
3437)
3538
39+ var validFormAddress * regexp.Regexp
40+
3641// Settings show a repository's settings page
3742func Settings (ctx * context.Context ) {
3843 ctx .Data ["Title" ] = ctx .Tr ("repo.settings" )
@@ -146,7 +151,38 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
146151 return
147152 }
148153 }
149- if err := ctx .Repo .Mirror .SaveAddress (form .MirrorAddress ); err != nil {
154+
155+ // Validate the form.MirrorAddress
156+ u , err := url .Parse (form .MirrorAddress )
157+ if err != nil {
158+ ctx .Data ["Err_MirrorAddress" ] = true
159+ ctx .RenderWithErr (ctx .Tr ("repo.mirror_address_url_invalid" ), tplSettingsOptions , & form )
160+ return
161+ }
162+
163+ if u .Opaque != "" || ! (u .Scheme == "http" || u .Scheme == "https" || u .Scheme == "git" ) {
164+ ctx .Data ["Err_MirrorAddress" ] = true
165+ ctx .RenderWithErr (ctx .Tr ("repo.mirror_address_protocol_invalid" ), tplSettingsOptions , & form )
166+ return
167+ }
168+
169+ // Now use xurls
170+ address := validFormAddress .FindString (form .MirrorAddress )
171+ if address != form .MirrorAddress && form .MirrorAddress != "" {
172+ ctx .Data ["Err_MirrorAddress" ] = true
173+ ctx .RenderWithErr (ctx .Tr ("repo.mirror_address_url_invalid" ), tplSettingsOptions , & form )
174+ return
175+ }
176+
177+ if u .EscapedPath () == "" || u .Host == "" || ! u .IsAbs () {
178+ ctx .Data ["Err_MirrorAddress" ] = true
179+ ctx .RenderWithErr (ctx .Tr ("repo.mirror_address_url_invalid" ), tplSettingsOptions , & form )
180+ return
181+ }
182+
183+ address = u .String ()
184+
185+ if err := ctx .Repo .Mirror .SaveAddress (address ); err != nil {
150186 ctx .ServerError ("SaveAddress" , err )
151187 return
152188 }
@@ -683,3 +719,11 @@ func DeleteDeployKey(ctx *context.Context) {
683719 "redirect" : ctx .Repo .RepoLink + "/settings/keys" ,
684720 })
685721}
722+
723+ func init () {
724+ var err error
725+ validFormAddress , err = xurls .StrictMatchingScheme (`(https?)|(git)://` )
726+ if err != nil {
727+ panic (err )
728+ }
729+ }
0 commit comments