From 584af0486dd8c91707d9f7640bbe85db189c61e2 Mon Sep 17 00:00:00 2001 From: erik Date: Wed, 20 Mar 2024 13:13:23 +0100 Subject: [PATCH] Add Federation specific URL validation --- modules/validation/helpers.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/modules/validation/helpers.go b/modules/validation/helpers.go index 567ad867fe..5edfe8d3c2 100644 --- a/modules/validation/helpers.go +++ b/modules/validation/helpers.go @@ -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]*$`)