-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathuserinput.go
469 lines (407 loc) · 12.5 KB
/
userinput.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package common
import (
"encoding/base64"
"errors"
"fmt"
"html"
"log"
"net/http"
"net/url"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
// CheckUnicode checks if a given string is unicode, and safe for using in SQLite queries (eg no SQLite control characters)
func CheckUnicode(rawInput string, decodeBase64 bool) (str string, err error) {
var decoded []byte
if decodeBase64 {
decoded, err = base64.StdEncoding.DecodeString(rawInput)
if err != nil {
// When base64 decoding fails, automatically try again with base64url formats instead
var err2 error // We use err2, to not overwrite the initial error message
decoded, err2 = base64.URLEncoding.DecodeString(rawInput)
if err2 != nil {
// Try base64URL with no padding character(s) this time
decoded, err2 = base64.RawURLEncoding.DecodeString(rawInput)
if err2 != nil {
// Nope. Seems like a genuine decoding problem
return
}
}
}
} else {
decoded = []byte(rawInput)
}
// Ensure the decoded string is valid UTF-8
if !utf8.Valid(decoded) {
err = fmt.Errorf("SQL string contains invalid characters: '%v'", err)
return
}
// Check for the presence of unicode control characters and similar in the decoded string
invalidChar := false
decodedStr := string(decoded)
for _, j := range decodedStr {
if unicode.IsControl(j) || unicode.Is(unicode.C, j) {
if j != 9 && j != 10 { // 9 == tab, 10 == new line, which are safe to allow. Everything else should (probably) raise an error
invalidChar = true
}
}
}
if invalidChar {
err = fmt.Errorf("SQL string contains invalid characters: '%v'", err)
return
}
// No errors, so return the string
return decodedStr, nil
}
// GetDatabase extracts a database name from GET or POST/PUT data
func GetDatabase(r *http.Request, allowGet bool) (dbName string, err error) {
// Retrieve the variable from the GET or POST/PUT data
var d string
if allowGet {
d = r.FormValue("dbname")
} else {
d = r.PostFormValue("dbname")
}
// Unescape, then validate the database name
dbName, err = url.QueryUnescape(d)
if err != nil {
return "", err
}
err = ValidateDB(dbName)
if err != nil {
return "", errors.New("Invalid database name")
}
return dbName, nil
}
// GetFormBranch return the requested branch name, from get or post data
func GetFormBranch(r *http.Request) (branch string, err error) {
// If no branch was given in the input, returns an empty string
a := r.FormValue("branch")
if a == "" {
return "", nil
}
// Unescape, then validate the branch name
branch, err = url.QueryUnescape(a)
if err != nil {
return "", err
}
err = ValidateBranchName(branch)
if err != nil {
return "", fmt.Errorf("Invalid branch name: '%v'", SanitiseLogString(branch))
}
return branch, nil
}
// GetFormCommit returns the requested database commit, from form data
func GetFormCommit(r *http.Request) (commitID string, err error) {
// If no commit was given in the input, returns an empty string
commitID = r.FormValue("commit")
if commitID == "" {
return "", nil
}
err = ValidateCommitID(commitID)
if err != nil {
return "", fmt.Errorf("Invalid database commit: '%v'", SanitiseLogString(commitID))
}
return commitID, nil
}
// GetFormLicence returns the licence name (if any) present in the form data
func GetFormLicence(r *http.Request) (licenceName string, err error) {
// If no licence name given, return an empty string
l := r.PostFormValue("licence")
if l == "" {
return "", nil
}
// Validate the licence name
err = ValidateLicence(l)
if err != nil {
log.Printf("Validation failed for licence: '%s': %s", SanitiseLogString(l), err)
return "", err
}
licenceName = l
return licenceName, nil
}
// GetFormLive returns the "live" value (if any) present in the form data
func GetFormLive(r *http.Request) (live bool, err error) {
l := r.PostFormValue("live")
if l == "" || strings.ToLower(l) == "false" {
return
}
// Check for true value
live, err = strconv.ParseBool(l)
if err != nil {
err = fmt.Errorf("Error when converting live value '%s' to boolean: %v", html.EscapeString(l), err)
return
}
return
}
// GetFormODC returns the database owner, database name, and commit (if any) present in the form data
func GetFormODC(r *http.Request) (userName string, dbName string, commitID string, err error) {
// Extract the database owner name
userName, err = GetFormOwner(r, false)
if err != nil {
return "", "", "", err
}
// Extract the database name
dbName, err = GetDatabase(r, false)
if err != nil {
return "", "", "", err
}
// Extract the commit string
commitID, err = GetFormCommit(r)
if err != nil {
return "", "", "", err
}
return userName, dbName, commitID, nil
}
// GetFormOwner returns the database owner present in the GET or POST/PUT data
func GetFormOwner(r *http.Request, allowGet bool) (dbOwner string, err error) {
// Retrieve the variable from the GET or POST/PUT data
var o string
if allowGet {
o = r.FormValue("dbowner")
} else {
o = r.PostFormValue("dbowner")
}
// If no database owner given, return
if o == "" {
return "", nil
}
// Unescape, then validate the owner name
dbOwner, err = url.QueryUnescape(o)
if err != nil {
return "", err
}
err = ValidateUser(dbOwner)
if err != nil {
log.Printf("Validation failed for database owner name: %s", err)
return "", err
}
return dbOwner, nil
}
// GetFormSourceURL returns the source URL (if any) present in the form data
func GetFormSourceURL(r *http.Request) (sourceURL string, err error) {
// Validate the source URL
su := r.PostFormValue("sourceurl")
if su != "" {
err = Validate.Var(su, "url,min=5,max=255") // 255 seems like a reasonable first guess
if err != nil {
return sourceURL, errors.New("Validation failed for source URL field")
}
sourceURL = su
}
return sourceURL, err
}
// GetFormTag returns the requested tag name, from get or post data
func GetFormTag(r *http.Request) (tag string, err error) {
// If no tag was given in the input, returns an empty string
a := r.FormValue("tag")
if a == "" {
return "", nil
}
// Unescape, then validate the tag name
c, err := url.QueryUnescape(a)
if err != nil {
return "", err
}
err = ValidateBranchName(c)
if err != nil {
return "", fmt.Errorf("Invalid tag name: '%v'", SanitiseLogString(c))
}
return c, nil
}
// GetFormTable returns the table name present in the GET or POST/PUT data
func GetFormTable(r *http.Request, allowGet bool) (table string, err error) {
// Retrieve the variable from the GET or POST/PUT data
var t string
if allowGet {
t = r.FormValue("table")
} else {
t = r.PostFormValue("table")
}
// If no table name given, return
if t == "" {
return "", nil
}
// Unescape, then validate the table name
table, err = url.QueryUnescape(t)
if err != nil {
return "", err
}
err = ValidatePGTable(table)
if err != nil {
log.Printf("Validation failed for table name: %s", err)
return "", err
}
return table, nil
}
// GetFormUDC returns the username, database, and commit (if any) present in the form data
func GetFormUDC(r *http.Request) (userName string, dbName string, commitID string, err error) {
// Extract the username
userName, err = GetUsername(r, false)
if err != nil {
return "", "", "", err
}
// Extract the database name
dbName, err = GetDatabase(r, false)
if err != nil {
return "", "", "", err
}
// Extract the commit string
commitID, err = GetFormCommit(r)
if err != nil {
return "", "", "", err
}
return userName, dbName, commitID, nil
}
// GetOD returns the requested database owner and database name
func GetOD(ignoreLeading int, r *http.Request) (dbOwner string, dbName string, err error) {
// Split the request URL into path components
pathStrings := strings.Split(r.URL.Path, "/")
// Check that at least an owner/database combination was requested
if len(pathStrings) < (3 + ignoreLeading) {
log.Printf("Something wrong with the requested URL: %v", SanitiseLogString(r.URL.Path))
return "", "", errors.New("Invalid URL")
}
dbOwner = pathStrings[1+ignoreLeading]
dbName = pathStrings[2+ignoreLeading]
// Validate the user supplied owner and database name
err = ValidateUserDB(dbOwner, dbName)
if err != nil {
return "", "", errors.New("Invalid owner or database name")
}
// Everything seems ok
return dbOwner, dbName, nil
}
// GetODC returns the requested database owner, database name, and commit revision
func GetODC(ignoreLeading int, r *http.Request) (dbOwner string, dbName string, commitID string, err error) {
// Grab owner and database name
dbOwner, dbName, err = GetOD(ignoreLeading, r)
if err != nil {
return "", "", "", err
}
// Extract the commit revision
commitID, err = GetFormCommit(r)
if err != nil {
return "", "", "", err
}
// Everything seems ok
return dbOwner, dbName, commitID, nil
}
// GetODT returns the requested database owner, database name, and table name
func GetODT(ignoreLeading int, r *http.Request) (dbOwner string, dbName string, requestedTable string, err error) {
// Grab owner and database name
dbOwner, dbName, err = GetOD(ignoreLeading, r)
if err != nil {
return "", "", "", err
}
// If a specific table was requested, get that info too
requestedTable, err = GetTable(r)
if err != nil {
return "", "", "", err
}
// Everything seems ok
return dbOwner, dbName, requestedTable, nil
}
// GetODTC returns the requested database owner, database name, table name, and commit string
func GetODTC(ignoreLeading int, r *http.Request) (dbOwner string, dbName string, requestedTable string, commitID string, err error) {
// Grab owner and database name
dbOwner, dbName, err = GetOD(ignoreLeading, r)
if err != nil {
return "", "", "", "", err
}
// If a specific table was requested, get that info too
requestedTable, err = GetTable(r)
if err != nil {
return "", "", "", "", err
}
// Extract the commit string
commitID, err = GetFormCommit(r)
if err != nil {
return "", "", "", "", err
}
// Everything seems ok
return dbOwner, dbName, requestedTable, commitID, nil
}
// GetPub returns the requested "public" variable, if present in the form data
// If something goes wrong, it defaults to "false".
func GetPub(r *http.Request) (public bool, err error) {
p := r.PostFormValue("public")
if p == "" {
// No public/private variable found
return false, errors.New("No public/private value present")
}
public, err = strconv.ParseBool(p)
if err != nil {
log.Printf("Error when converting public value to boolean: %v", err)
return false, err
}
return public, nil
}
// GetTable returns the requested table name (if any)
func GetTable(r *http.Request) (requestedTable string, err error) {
requestedTable = r.FormValue("table")
// If a table name was supplied, validate it
// FIXME: We should probably create a validation function for SQLite table names, not use our one for PG
if requestedTable != "" {
err := ValidatePGTable(requestedTable)
if err != nil {
// If the failed table name is "{{ db.Tablename }}", don't bother logging it. It's just a
// search bot picking up the AngularJS string then doing a request with it
if requestedTable != "{{ db.Tablename }}" {
log.Printf("Validation failed for table name: '%s': %s", SanitiseLogString(requestedTable), err)
}
return "", errors.New("Invalid table name")
}
}
// Everything seems ok
return requestedTable, nil
}
// GetUFD returns the username, folder, and database name (if any) present in the form data
func GetUFD(r *http.Request, allowGet bool) (userName string, dbFolder string, dbName string, err error) {
// Extract the username
userName, err = GetUsername(r, allowGet)
if err != nil {
return "", "", "", err
}
// Extract the folder
dbFolder = "/"
// Extract the database name
dbName, err = GetDatabase(r, allowGet)
if err != nil {
return "", "", "", err
}
return userName, dbFolder, dbName, nil
}
// GetUsername returns the username (if any) present in the GET or POST/PUT data
func GetUsername(r *http.Request, allowGet bool) (userName string, err error) {
// Retrieve the variable from the GET or POST/PUT data
var u string
if allowGet {
u = r.FormValue("username")
} else {
u = r.PostFormValue("username")
}
// If no username given, return
if u == "" {
return "", nil
}
// Unescape, then validate the username
userName, err = url.QueryUnescape(u)
if err != nil {
return "", err
}
err = ValidateUser(userName)
if err != nil {
log.Printf("Validation failed for username: %s", err)
return "", err
}
return userName, nil
}
func SanitiseLogString(v string) (result string) {
result = strings.Replace(v, "\n", " ", -1)
result = strings.Replace(result, "\r", "", -1)
result = strings.Replace(result, "'", "\\'", -1)
return result
}