mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Validate External Tracker URL Format (#7089)
* Validate External Tracker URL Format Add some validation checks for external tracker URL format. Fixes #7068 * Don't make {index} a hard requirement * Fix Description * make fmt * move regex to package level * fix copyright date
This commit is contained in:
		@@ -7,6 +7,7 @@ package validation
 | 
			
		||||
import (
 | 
			
		||||
	"net"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
@@ -14,6 +15,8 @@ import (
 | 
			
		||||
 | 
			
		||||
var loopbackIPBlocks []*net.IPNet
 | 
			
		||||
 | 
			
		||||
var externalTrackerRegex = regexp.MustCompile(`({?)(?:user|repo|index)+?(}?)`)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	for _, cidr := range []string{
 | 
			
		||||
		"127.0.0.0/8", // IPv4 loopback
 | 
			
		||||
@@ -75,3 +78,19 @@ func IsValidExternalURL(uri string) bool {
 | 
			
		||||
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// IsValidExternalTrackerURLFormat checks if URL matches required syntax for external trackers
 | 
			
		||||
func IsValidExternalTrackerURLFormat(uri string) bool {
 | 
			
		||||
	if !IsValidExternalURL(uri) {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// check for typoed variables like /{index/ or /[repo}
 | 
			
		||||
	for _, match := range externalTrackerRegex.FindAllStringSubmatch(uri, -1) {
 | 
			
		||||
		if (match[1] == "{" || match[2] == "}") && (match[1] != "{" || match[2] != "}") {
 | 
			
		||||
			return false
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return true
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user