-
Notifications
You must be signed in to change notification settings - Fork 38
/
validate.go
267 lines (221 loc) · 8.26 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package common
import (
"fmt"
"regexp"
"strings"
valid "gopkg.in/go-playground/validator.v9"
)
var (
regexBraTagName = regexp.MustCompile(`^[a-z,A-Z,0-9,\^,\.,\-,\_,\/,\(,\),\:,\&,\ )]+$`)
regexDBName = regexp.MustCompile(`^[a-z,A-Z,0-9,\.,\-,\_,\(,\),\+,\ ]+$`)
regexDiscussTitle = regexp.MustCompile(`^[a-z,A-Z,0-9,\^,\.,\-,\_,\/,\(,\),\',\!,\@,\#,\&,\$,\+,\:,\;,\?,\ )]+$`)
regexDisplayName = regexp.MustCompile(`^[a-z,A-Z,0-9,\.,\-,\,,\',\ ]+$`)
regexFieldName = regexp.MustCompile(`^[a-z,A-Z,0-9,\^,\.,\-,\_,\/,\(,\),\ )]+$`)
regexFolder = regexp.MustCompile(`^[a-z,A-Z,0-9,\.,\-,\_,\/]+$`)
regexLicence = regexp.MustCompile(`^[a-z,A-Z,0-9,\.,\-,\_,\(,\),\ ]+$`)
regexMarkDownSource = regexp.MustCompile(`^[a-z,A-Z,0-9` + ",`," + `‘,’,“,”,\.,\-,\_,\/,\(,\),\[,\],\\,\!,\#,\',\",\@,\$,\*,\%,\^,\&,\+,\=,\:,\;,\<,\>,\,,\?,\~,\|,\ ,\012,\015]+$`)
regexPGTable = regexp.MustCompile(`^[a-z,A-Z,0-9,\.,\-,\_,\(,\),\ ]+$`)
regexUsername = regexp.MustCompile(`^[a-z,A-Z,0-9,\.,\-,\_]+$`)
// For input validation
Validate *valid.Validate
)
func init() {
// Load validation code
Validate = valid.New()
Validate.RegisterValidation("branchortagname", checkBranchOrTagName)
Validate.RegisterValidation("dbname", checkDBName)
Validate.RegisterValidation("discussiontitle", checkDiscussTitle)
Validate.RegisterValidation("displayname", checkDisplayName)
Validate.RegisterValidation("fieldname", checkFieldName)
Validate.RegisterValidation("folder", checkFolder)
Validate.RegisterValidation("licence", checkLicence)
Validate.RegisterValidation("markdownsource", checkMarkDownSource)
Validate.RegisterValidation("pgtable", checkPGTableName)
Validate.RegisterValidation("username", checkUsername)
}
// Custom validation function for branch and tag names.
// At the moment it just allows alphanumeric and "^.-_/():& " chars, though it should probably be extended to cover any
// valid file name
func checkBranchOrTagName(fl valid.FieldLevel) bool {
return regexBraTagName.MatchString(fl.Field().String())
}
// Custom validation function for SQLite database names.
// At the moment it just allows alphanumeric and ".-_()+ " chars, though it should probably be extended to cover any
// valid file name
func checkDBName(fl valid.FieldLevel) bool {
return regexDBName.MatchString(fl.Field().String())
}
// Custom validation function for discussion titles.
// At the moment it just allows alpha and "^.-_/()'!@#&$+:;? " chars
func checkDiscussTitle(fl valid.FieldLevel) bool {
return regexDiscussTitle.MatchString(fl.Field().String())
}
// Custom validation function for display names.
// At the moment it just allows alpha and ".,-' " chars
func checkDisplayName(fl valid.FieldLevel) bool {
return regexDisplayName.MatchString(fl.Field().String())
}
// Custom validation function for SQLite field names
// At the moment it just allows alphanumeric and "^.-_/() " chars, though it should probably be extended to cover all
// valid SQLite field name characters
func checkFieldName(fl valid.FieldLevel) bool {
return regexFieldName.MatchString(fl.Field().String())
}
// Custom validation function for folder names.
// At the moment it allows alphanumeric and ".-_/" chars. Will probably need more characters added.
func checkFolder(fl valid.FieldLevel) bool {
return regexFolder.MatchString(fl.Field().String())
}
// Custom validation function for licence names.
// At the moment it allows alphanumeric and ".-_() " chars. Will probably need more characters added.
func checkLicence(fl valid.FieldLevel) bool {
return regexLicence.MatchString(fl.Field().String())
}
// Custom validation function for Markdown source text.
// At the moment it allows Unicode alphanumeric, "`‘’“”.-_/()[]\#\!'"@$*%^&+=:;<>,?~| ", and "\r\n" chars. Will probably need more characters added.
func checkMarkDownSource(fl valid.FieldLevel) bool {
return regexMarkDownSource.MatchString(fl.Field().String())
}
// Custom validation function for PostgreSQL table names.
// At the moment it just allows alphanumeric and ".-_ " chars (may need to be expanded out at some point).
func checkPGTableName(fl valid.FieldLevel) bool {
return regexPGTable.MatchString(fl.Field().String())
}
// Custom validation function for Usernames.
// At the moment it just allows alphanumeric and ".-_" chars (may need to be expanded out at some point).
func checkUsername(fl valid.FieldLevel) bool {
return regexUsername.MatchString(fl.Field().String())
}
// Checks a username against the list of reserved ones.
func ReservedUsernamesCheck(userName string) error {
reserved := []string{"about", "admin", "administrator", "blog", "dbhub", "compare", "download", "forks", "legal",
"login", "logout", "mail", "news", "pref", "printer", "public", "reference", "register", "root", "star",
"stars", "system", "table", "upload", "uploaddata", "vis", "watchers"}
for _, word := range reserved {
if strings.ToLower(userName) == strings.ToLower(word) {
return fmt.Errorf("That username is not available: %s\n", userName)
}
}
return nil
}
// Validate the provided branch, release, or tag name.
func ValidateBranchName(fieldName string) error {
err := Validate.Var(fieldName, "branchortagname,min=1,max=32") // 32 seems a reasonable first guess
if err != nil {
return err
}
return nil
}
// Validate the SQLite field name.
func ValidateFieldName(fieldName string) error {
err := Validate.Var(fieldName, "required,fieldname,min=1,max=63") // 63 char limit seems reasonable
if err != nil {
return err
}
return nil
}
// Validate the database name.
func ValidateDB(dbName string) error {
err := Validate.Var(dbName, "required,dbname,min=1,max=256") // 256 char limit seems reasonable
if err != nil {
return err
}
return nil
}
// Validate the provided email address.
func ValidateEmail(email string) error {
err := Validate.Var(email, "required,email")
if err != nil {
return err
}
return nil
}
// Validate the provided folder name.
func ValidateFolder(folder string) error {
err := Validate.Var(folder, "folder,max=127")
if err != nil {
return err
}
return nil
}
// Validate the provided licence name.
func ValidateLicence(licence string) error {
err := Validate.Var(licence, "licence,min=3,max=13") // 13 is the length of our longest licence name (thus far)
if err != nil {
return err
}
return nil
}
// Validate the provided markdown .
func ValidateMarkdown(fieldName string) error {
err := Validate.Var(fieldName, "markdownsource,max=1024") // 1024 seems like a reasonable first guess
if err != nil {
return err
}
return nil
}
// Validate the provided PostgreSQL table name.
func ValidatePGTable(table string) error {
// TODO: Improve this to work with all valid SQLite identifiers
// TODO Not seeing a definitive reference page for SQLite yet, so using the PostgreSQL one is
// TODO probably ok as a fallback:
// TODO https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
// TODO: Should we exclude SQLite internal tables too? (eg "sqlite_*" https://sqlite.org/lang_createtable.html)
err := Validate.Var(table, "required,pgtable,max=63")
if err != nil {
return err
}
return nil
}
// Validate the provided discussion or merge request title.
func ValidateDiscussionTitle(fieldName string) error {
err := Validate.Var(fieldName, "discussiontitle,max=120") // 120 seems a reasonable first guess.
if err != nil {
return err
}
return nil
}
// Validate the provided username.
func ValidateUser(user string) error {
err := Validate.Var(user, "required,username,min=2,max=63")
if err != nil {
return err
}
return nil
}
// Validate the provided user and database name.
func ValidateUserDB(user string, db string) error {
err := ValidateUser(user)
if err != nil {
return err
}
err = ValidateDB(db)
if err != nil {
return err
}
return nil
}
// Validate the provided user, database, and table name.
func ValidateUserDBTable(user string, db string, table string) error {
err := ValidateUserDB(user, db)
if err != nil {
return err
}
err = ValidatePGTable(table)
if err != nil {
return err
}
return nil
}
// Validate the provided username and email address.
func ValidateUserEmail(user string, email string) error {
err := ValidateUser(user)
if err != nil {
return err
}
err = ValidateEmail(email)
if err != nil {
return err
}
return nil
}