validate.gno
4.61 Kb · 183 lines
1package agentmarketplace
2
3import (
4 "sort"
5 "strconv"
6 "strings"
7)
8
9// Input limits.
10const (
11 minNameLen = 3
12 maxNameLen = 48
13 minTitleLen = 3
14 maxTitleLen = 80
15 minDescLen = 3
16 maxDescLen = 280
17 maxNoteLen = 80
18 minTagLen = 2
19 maxTagLen = 24
20 maxTags = 8
21 maxTagsInputLen = 200
22 maxRateUgnot = 1000000000000 // 1,000,000 GNOT
23 minBudgetUgnot = 1
24 maxBudgetUgnot = 1000000000000
25 ugnotPerGnot = 1000000
26 minScore = 1
27 maxScore = 5
28 maxCommentLen = 120
29 commitHashLen = 64
30 minRevealSecs = 60
31 maxRevealSecs = 30 * 24 * 60 * 60
32)
33
34// isSafeChar is the character whitelist for every free-text field. It keeps
35// user-supplied strings from injecting markdown, HTML, gnoweb tags or
36// table pipes into Render output, and keeps them single-line.
37func isSafeChar(c rune) bool {
38 switch {
39 case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':
40 return true
41 }
42 switch c {
43 case ' ', '-', '.', ',', '(', ')', '\'', '/', '+', ':', '@', '#', '%', '&', '=':
44 return true
45 }
46 return false
47}
48
49// checkText validates a free-text field and panics with a readable message.
50func checkText(field, s string, minLen, maxLen int) {
51 if len(s) < minLen || len(s) > maxLen {
52 panic(field + " must be " + strconv.Itoa(minLen) + "-" + strconv.Itoa(maxLen) + " characters")
53 }
54 if s[0] == ' ' || s[len(s)-1] == ' ' {
55 panic(field + " must not start or end with a space")
56 }
57 for _, c := range s {
58 if !isSafeChar(c) {
59 panic(field + " contains an unsupported character: only letters, digits, spaces and - . , ( ) ' / + : @ # % & = are allowed")
60 }
61 }
62}
63
64func checkRateKind(kind string) {
65 switch kind {
66 case RateHourly, RateFlat:
67 return
68 }
69 panic("rate kind must be one of: hourly, flat")
70}
71
72func checkRate(rate int64) {
73 if rate < 1 || rate > maxRateUgnot {
74 panic("rate must be between 1 and " + strconv.FormatInt(maxRateUgnot, 10) + " ugnot")
75 }
76}
77
78func checkBudget(budget int64) {
79 if budget < minBudgetUgnot || budget > maxBudgetUgnot {
80 panic("budget must be between " + strconv.FormatInt(minBudgetUgnot, 10) + " and " + strconv.FormatInt(maxBudgetUgnot, 10) + " ugnot")
81 }
82}
83
84// checkSpecialty validates a single required specialty tag.
85func checkSpecialty(tag string) string {
86 normalized := strings.ToLower(strings.TrimSpace(tag))
87 checkTag("specialty", normalized)
88 return normalized
89}
90
91func checkTag(field, tag string) {
92 if len(tag) < minTagLen || len(tag) > maxTagLen {
93 panic(field + " must be " + strconv.Itoa(minTagLen) + "-" + strconv.Itoa(maxTagLen) + " characters")
94 }
95 if strings.Contains(tag, ",") {
96 panic(field + " must be a single tag, not a comma-separated list")
97 }
98 if tag[0] == ' ' || tag[len(tag)-1] == ' ' {
99 panic(field + " must not start or end with a space")
100 }
101 for _, c := range tag {
102 if !isSafeChar(c) {
103 panic(field + " contains an unsupported character: only letters, digits, spaces and - . , ( ) ' / + : @ # % & = are allowed")
104 }
105 }
106}
107
108// parseTags canonicalizes a comma-separated specialty list: lowercased,
109// trimmed, unique, sorted, joined with ", ".
110func parseTags(raw string) string {
111 checkText("tags", raw, minTagLen, maxTagsInputLen)
112 parts := strings.Split(raw, ",")
113 var tags []string
114 for _, p := range parts {
115 tag := strings.ToLower(strings.TrimSpace(p))
116 if tag == "" {
117 continue
118 }
119 checkTag("tag", tag)
120 if containsTag(tags, tag) {
121 continue
122 }
123 tags = append(tags, tag)
124 if len(tags) > maxTags {
125 panic("an agent can list at most " + strconv.Itoa(maxTags) + " specialty tags")
126 }
127 }
128 if len(tags) == 0 {
129 panic("tags must list at least one specialty")
130 }
131 sort.Strings(tags)
132 return strings.Join(tags, ", ")
133}
134
135func containsTag(tags []string, tag string) bool {
136 for _, t := range tags {
137 if t == tag {
138 return true
139 }
140 }
141 return false
142}
143
144func agentHasSpecialty(agentTags, specialty string) bool {
145 for _, t := range strings.Split(agentTags, ", ") {
146 if t == specialty {
147 return true
148 }
149 }
150 return false
151}
152
153func checkScore(score int) {
154 if score < minScore || score > maxScore {
155 panic("score must be an integer from 1 to 5")
156 }
157}
158
159func checkComment(comment string) {
160 if comment == "" {
161 return
162 }
163 checkText("comment", comment, 1, maxCommentLen)
164}
165
166func checkCommitHash(h string) string {
167 h = strings.ToLower(h)
168 if len(h) != commitHashLen {
169 panic("commit hash must be a 64-character hex SHA-256 digest")
170 }
171 for _, c := range h {
172 if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
173 panic("commit hash must be a 64-character hex SHA-256 digest")
174 }
175 }
176 return h
177}
178
179func checkRevealDeadline(secs int64) {
180 if secs < minRevealSecs || secs > maxRevealSecs {
181 panic("reveal deadline must be between 60 seconds and 30 days")
182 }
183}