mirror of
1
Fork 0

Add Federation specific URL validation

This commit is contained in:
erik 2024-03-20 13:13:23 +01:00
parent 41da150fb3
commit 584af0486d
1 changed files with 27 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"net"
"net/url"
"regexp"
"strconv"
"strings"
"code.gitea.io/gitea/modules/setting"
@ -116,6 +117,32 @@ func IsValidExternalTrackerURLFormat(uri string) bool {
return true
}
func IsValidForgejoActivityPubURL(url string) bool {
if !IsValidURL(url) {
return false
}
if !strings.Contains(url, "api/v1/activitypub") {
return false
}
return true
}
func IsValidFederatedRepoURL(url string) bool {
if !IsValidForgejoActivityPubURL(url) {
return false
}
if !strings.Contains(url, "repository-id") {
return false
}
splitURL := strings.Split(url, "/")
repoIDIndex := len(splitURL) - 1
// Is there a valid integer denoting a repo id?
if _, err := strconv.Atoi(splitURL[repoIDIndex]); err != nil {
return false
}
return true
}
var (
validUsernamePatternWithDots = regexp.MustCompile(`^[\da-zA-Z][-.\w]*$`)
validUsernamePatternWithoutDots = regexp.MustCompile(`^[\da-zA-Z][-\w]*$`)