@ -9,8 +9,6 @@ import (
"errors"
"fmt"
"io/ioutil"
"net/url"
"regexp"
"strings"
"time"
@ -20,6 +18,7 @@ import (
auth "code.gitea.io/gitea/modules/forms"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/migrations"
"code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
@ -30,8 +29,6 @@ import (
"code.gitea.io/gitea/services/mailer"
mirror_service "code.gitea.io/gitea/services/mirror"
repo_service "code.gitea.io/gitea/services/repository"
"mvdan.cc/xurls/v2"
)
const (
@ -44,8 +41,6 @@ const (
tplProtectedBranch base . TplName = "repo/settings/protected_branch"
)
var validFormAddress * regexp . Regexp
// Settings show a repository's settings page
func Settings ( ctx * context . Context ) {
ctx . Data [ "Title" ] = ctx . Tr ( "repo.settings" )
@ -169,40 +164,38 @@ func SettingsPost(ctx *context.Context) {
}
}
// Validate the form.MirrorAddress
u , err := url . Parse ( form . MirrorAddress )
if err != nil {
ctx . Data [ "Err_MirrorAddress" ] = true
ctx . RenderWithErr ( ctx . Tr ( "repo.mirror_address_url_invalid" ) , tplSettingsOptions , & form )
return
address , err := auth . ParseRemoteAddr ( form . MirrorAddress , form . MirrorUsername , form . MirrorPassword )
if err == nil {
err = migrations . IsMigrateURLAllowed ( address , ctx . User )
}
if u . Opaque != "" || ! ( u . Scheme == "http" || u . Scheme == "https" || u . Scheme == "git" ) {
ctx . Data [ "Err_MirrorAddress" ] = true
ctx . RenderWithErr ( ctx . Tr ( "repo.mirror_address_protocol_invalid" ) , tplSettingsOptions , & form )
return
}
if form . MirrorUsername != "" || form . MirrorPassword != "" {
u . User = url . UserPassword ( form . MirrorUsername , form . MirrorPassword )
}
// Now use xurls
address := validFormAddress . FindString ( form . MirrorAddress )
if address != form . MirrorAddress && form . MirrorAddress != "" {
ctx . Data [ "Err_MirrorAddress" ] = true
ctx . RenderWithErr ( ctx . Tr ( "repo.mirror_address_url_invalid" ) , tplSettingsOptions , & form )
return
}
if u . EscapedPath ( ) == "" || u . Host == "" || ! u . IsAbs ( ) {
if err != nil {
if models . IsErrInvalidCloneAddr ( err ) {
ctx . Data [ "Err_MirrorAddress" ] = true
addrErr := err . ( * models . ErrInvalidCloneAddr )
switch {
case addrErr . IsProtocolInvalid :
ctx . RenderWithErr ( ctx . Tr ( "repo.mirror_address_protocol_invalid" ) , tplSettingsOptions , & form )
case addrErr . IsURLError :
ctx . RenderWithErr ( ctx . Tr ( "form.url_error" ) , tplSettingsOptions , & form )
case addrErr . IsPermissionDenied :
if len ( addrErr . PrivateNet ) == 0 {
ctx . RenderWithErr ( ctx . Tr ( "repo.migrate.permission_denied_private_ip" ) , tplSettingsOptions , & form )
} else if ! addrErr . LocalPath {
ctx . RenderWithErr ( ctx . Tr ( "repo.migrate.permission_denied_blocked" ) , tplSettingsOptions , & form )
} else {
ctx . RenderWithErr ( ctx . Tr ( "repo.migrate.permission_denied" ) , tplSettingsOptions , & form )
}
case addrErr . IsInvalidPath :
ctx . RenderWithErr ( ctx . Tr ( "repo.migrate.invalid_local_path" ) , tplSettingsOptions , & form )
default :
ctx . ServerError ( "Unknown error" , err )
}
}
ctx . Data [ "Err_MirrorAddress" ] = true
ctx . RenderWithErr ( ctx . Tr ( "repo.mirror_address_url_invalid" ) , tplSettingsOptions , & form )
return
}
address = u . String ( )
if err := mirror_service . UpdateAddress ( ctx . Repo . Mirror , address ) ; err != nil {
ctx . ServerError ( "UpdateAddress" , err )
return
@ -951,14 +944,6 @@ func DeleteDeployKey(ctx *context.Context) {
} )
}
func init ( ) {
var err error
validFormAddress , err = xurls . StrictMatchingScheme ( ` (https?)|(git):// ` )
if err != nil {
panic ( err )
}
}
// UpdateAvatarSetting update repo's avatar
func UpdateAvatarSetting ( ctx * context . Context , form auth . AvatarForm ) error {
ctxRepo := ctx . Repo . Repository