@@ -7,11 +7,12 @@ package repo
7
7
8
8
import (
9
9
"errors"
10
+ "net/url"
11
+ "regexp"
10
12
"strings"
11
13
"time"
12
14
13
15
"code.gitea.io/git"
14
-
15
16
"code.gitea.io/gitea/models"
16
17
"code.gitea.io/gitea/modules/auth"
17
18
"code.gitea.io/gitea/modules/base"
@@ -21,6 +22,8 @@ import (
21
22
"code.gitea.io/gitea/modules/util"
22
23
"code.gitea.io/gitea/modules/validation"
23
24
"code.gitea.io/gitea/routers/utils"
25
+
26
+ "github.com/mvdan/xurls"
24
27
)
25
28
26
29
const (
@@ -33,6 +36,8 @@ const (
33
36
tplProtectedBranch base.TplName = "repo/settings/protected_branch"
34
37
)
35
38
39
+ var validFormAddress * regexp.Regexp
40
+
36
41
// Settings show a repository's settings page
37
42
func Settings (ctx * context.Context ) {
38
43
ctx .Data ["Title" ] = ctx .Tr ("repo.settings" )
@@ -146,7 +151,38 @@ func SettingsPost(ctx *context.Context, form auth.RepoSettingForm) {
146
151
return
147
152
}
148
153
}
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 {
150
186
ctx .ServerError ("SaveAddress" , err )
151
187
return
152
188
}
@@ -683,3 +719,11 @@ func DeleteDeployKey(ctx *context.Context) {
683
719
"redirect" : ctx .Repo .RepoLink + "/settings/keys" ,
684
720
})
685
721
}
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