summaryrefslogtreecommitdiffstats
path: root/modules/git/ref.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-09-04 11:47:56 +0100
committerGitHub <noreply@github.com>2022-09-04 13:47:56 +0300
commite6b3be460840f1f982d5358198466e7d6f509d21 (patch)
treed3e4cb52c6a7df321e9b4ffdfe6f99f79d392b63 /modules/git/ref.go
parent93a610a819688b54d4565b8cbbae7cc04c552073 (diff)
downloadgitea-e6b3be460840f1f982d5358198466e7d6f509d21.tar.gz
gitea-e6b3be460840f1f982d5358198466e7d6f509d21.zip
Add more checks in migration code (#21011)
When migrating add several more important sanity checks: * SHAs must be SHAs * Refs must be valid Refs * URLs must be reasonable Signed-off-by: Andrew Thornton <art27@cantab.net> Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <matti@mdranta.net>
Diffstat (limited to 'modules/git/ref.go')
-rw-r--r--modules/git/ref.go28
1 files changed, 27 insertions, 1 deletions
diff --git a/modules/git/ref.go b/modules/git/ref.go
index 9fd071ce58..2f459148a2 100644
--- a/modules/git/ref.go
+++ b/modules/git/ref.go
@@ -4,7 +4,10 @@
package git
-import "strings"
+import (
+ "regexp"
+ "strings"
+)
const (
// RemotePrefix is the base directory of the remotes information of git.
@@ -15,6 +18,29 @@ const (
pullLen = len(PullPrefix)
)
+// refNamePatternInvalid is regular expression with unallowed characters in git reference name
+// They cannot have ASCII control characters (i.e. bytes whose values are lower than \040, or \177 DEL), space, tilde ~, caret ^, or colon : anywhere.
+// They cannot have question-mark ?, asterisk *, or open bracket [ anywhere
+var refNamePatternInvalid = regexp.MustCompile(
+ `[\000-\037\177 \\~^:?*[]|` + // No absolutely invalid characters
+ `(?:^[/.])|` + // Not HasPrefix("/") or "."
+ `(?:/\.)|` + // no "/."
+ `(?:\.lock$)|(?:\.lock/)|` + // No ".lock/"" or ".lock" at the end
+ `(?:\.\.)|` + // no ".." anywhere
+ `(?://)|` + // no "//" anywhere
+ `(?:@{)|` + // no "@{"
+ `(?:[/.]$)|` + // no terminal '/' or '.'
+ `(?:^@$)`) // Not "@"
+
+// IsValidRefPattern ensures that the provided string could be a valid reference
+func IsValidRefPattern(name string) bool {
+ return !refNamePatternInvalid.MatchString(name)
+}
+
+func SanitizeRefPattern(name string) string {
+ return refNamePatternInvalid.ReplaceAllString(name, "_")
+}
+
// Reference represents a Git ref.
type Reference struct {
Name string