| 1 | // Package names bevat de padregels voor gebruikers, groepen en repositories. |
| 2 | package names |
| 3 | |
| 4 | import ( |
| 5 | "regexp" |
| 6 | "strings" |
| 7 | |
| 8 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 9 | ) |
| 10 | |
| 11 | // Reserved paths botsen met vaste routes van de webinterface. |
| 12 | var reserved = map[string]bool{ |
| 13 | "admin": true, "api": true, "assets": true, "auth": true, |
| 14 | "explore": true, "groups": true, "healthz": true, "help": true, |
| 15 | "internal": true, "login": true, "logout": true, "new": true, |
| 16 | "settings": true, "static": true, "g": true, |
| 17 | } |
| 18 | |
| 19 | var ( |
| 20 | groupPath = regexp.MustCompile(`^[a-z][a-z0-9_-]{0,62}$`) |
| 21 | repoName = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]{0,62}$`) |
| 22 | userName = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,62}$`) |
| 23 | ) |
| 24 | |
| 25 | // ValidUsername volgt de auth-server: kleine letters. |
| 26 | func ValidUsername(s string) error { |
| 27 | if !userName.MatchString(s) || reserved[s] { |
| 28 | return i18n.E("err.userName", s) |
| 29 | } |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | // ValidGroupPath is het pad van een groep in de URL. |
| 34 | func ValidGroupPath(s string) error { |
| 35 | if !groupPath.MatchString(s) || reserved[s] { |
| 36 | return i18n.E("err.groupPath", s) |
| 37 | } |
| 38 | return nil |
| 39 | } |
| 40 | |
| 41 | // ValidRepoName is de repositorynaam zonder .git. |
| 42 | func ValidRepoName(s string) error { |
| 43 | if !repoName.MatchString(s) || strings.HasSuffix(strings.ToLower(s), ".git") || s == "." || s == ".." { |
| 44 | return i18n.E("err.repoName", s) |
| 45 | } |
| 46 | return nil |
| 47 | } |
| 48 | |
| 49 | // Slug maakt van vrije tekst een pad. "test repo" wordt "test-repo". |
| 50 | // Een repository mag een punt houden. Een groep niet, en begint met een letter. |
| 51 | func Slug(kind, raw string) (string, error) { |
| 52 | s := strings.ToLower(strings.TrimSpace(raw)) |
| 53 | s = foldLatin(s) |
| 54 | var b strings.Builder |
| 55 | gap := false |
| 56 | for _, r := range s { |
| 57 | if slugChar(kind, r) { |
| 58 | if gap && b.Len() > 0 { |
| 59 | b.WriteByte('-') |
| 60 | } |
| 61 | gap = false |
| 62 | b.WriteRune(r) |
| 63 | continue |
| 64 | } |
| 65 | gap = true |
| 66 | } |
| 67 | out := strings.Trim(b.String(), "._") |
| 68 | if len(out) > 63 { |
| 69 | out = strings.Trim(out[:63], "-._") |
| 70 | } |
| 71 | switch kind { |
| 72 | case "repo": |
| 73 | if err := ValidRepoName(out); err != nil { |
| 74 | return "", i18n.E("err.slug") |
| 75 | } |
| 76 | case "group": |
| 77 | if err := ValidGroupPath(out); err != nil { |
| 78 | return "", i18n.E("err.slug") |
| 79 | } |
| 80 | default: |
| 81 | return "", i18n.E("err.slug") |
| 82 | } |
| 83 | return out, nil |
| 84 | } |
| 85 | |
| 86 | func slugChar(kind string, r rune) bool { |
| 87 | if r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || r == '_' { |
| 88 | return true |
| 89 | } |
| 90 | return kind == "repo" && r == '.' |
| 91 | } |
| 92 | |
| 93 | func foldLatin(s string) string { |
| 94 | return strings.NewReplacer( |
| 95 | "à", "a", "á", "a", "â", "a", "ä", "a", "ã", "a", "å", "a", "ā", "a", |
| 96 | "è", "e", "é", "e", "ê", "e", "ë", "e", "ē", "e", |
| 97 | "ì", "i", "í", "i", "î", "i", "ï", "i", "ī", "i", |
| 98 | "ò", "o", "ó", "o", "ô", "o", "ö", "o", "õ", "o", "ø", "o", "ō", "o", |
| 99 | "ù", "u", "ú", "u", "û", "u", "ü", "u", "ū", "u", |
| 100 | "ý", "y", "ÿ", "y", "ñ", "n", "ç", "c", "ß", "ss", |
| 101 | "æ", "ae", "œ", "oe", "ð", "d", "þ", "th", |
| 102 | ).Replace(s) |
| 103 | } |
| 104 | |
| 105 | // ValidBranch laat een branchnaam toe met slashes, zonder ontsnapping. |
| 106 | func ValidBranch(s string) error { |
| 107 | if s == "" || len(s) > 200 || strings.HasPrefix(s, "/") || strings.HasSuffix(s, "/") || strings.Contains(s, "..") || strings.Contains(s, "//") || strings.Contains(s, "@{") || strings.HasSuffix(s, ".lock") { |
| 108 | return i18n.E("err.branchName", s) |
| 109 | } |
| 110 | for _, part := range strings.Split(s, "/") { |
| 111 | if part == "" || part == "." || strings.HasPrefix(part, "-") || strings.HasSuffix(part, ".") { |
| 112 | return i18n.E("err.branchName", s) |
| 113 | } |
| 114 | for _, c := range part { |
| 115 | ok := (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-' |
| 116 | if !ok { |
| 117 | return i18n.E("err.branchName", s) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | // ValidTag beperkt release-tags tot een git-ref die niet ontsnapt. |
| 125 | func ValidTag(s string) error { |
| 126 | if s == "" || len(s) > 80 || strings.HasPrefix(s, "-") || strings.Contains(s, "..") { |
| 127 | return i18n.E("err.tag", s) |
| 128 | } |
| 129 | for _, c := range s { |
| 130 | ok := (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '_' || c == '-' |
| 131 | if !ok { |
| 132 | return i18n.E("err.tagChar", s) |
| 133 | } |
| 134 | } |
| 135 | return nil |
| 136 | } |