Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-25 11:16:11 +01:00

Add Federation specific URL validation

This commit is contained in:
erik 2024-03-20 13:13:23 +01:00
parent 41da150fb3
commit 584af0486d

View file

@ -7,6 +7,7 @@ import (
"net" "net"
"net/url" "net/url"
"regexp" "regexp"
"strconv"
"strings" "strings"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
@ -116,6 +117,32 @@ func IsValidExternalTrackerURLFormat(uri string) bool {
return true 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 ( var (
validUsernamePatternWithDots = regexp.MustCompile(`^[\da-zA-Z][-.\w]*$`) validUsernamePatternWithDots = regexp.MustCompile(`^[\da-zA-Z][-.\w]*$`)
validUsernamePatternWithoutDots = regexp.MustCompile(`^[\da-zA-Z][-\w]*$`) validUsernamePatternWithoutDots = regexp.MustCompile(`^[\da-zA-Z][-\w]*$`)