-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfuncs.go
66 lines (64 loc) · 1.46 KB
/
funcs.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
package patrol
import (
"path/filepath"
)
func IsAppServiceID(
key string,
) bool {
if len(key) == 0 ||
len(key) > 63 {
return false
}
// ( 0-9 A-Z a-z - )
// if these accepted characters are to change, we are unwilling to ever support the character '/' and solo '.' or '..' sequences
// the key is intended to only be used in a URI path, there are no plans to use this as a label in a hostname
// if we change this, characters that are not a valid URI path delimiter must be encoded for use in our URL
if key[0] == '-' ||
key[len(key)-1] == '-' {
// we can't accept current directory or parent directory keywords
return false
}
for _, r := range key {
if r != '-' {
if r < '0' ||
r > '9' && r < 'A' ||
r > 'Z' && r < 'a' ||
r > 'z' {
return false
}
}
}
return true
}
func IsPathClean(path string) bool {
if path == "" ||
path == "." {
// no root directories
return false
}
cleaned := filepath.Clean(path)
if cleaned == "." {
// no current root directories
return false
}
if cleaned == path {
// directories match
return true
}
// we don't want to accept // which gets cleaned to / as a clean input
// this would mean we're comparing // as // which we don't want
if cleaned != "/" &&
cleaned+"/" == path {
// paths are allowed to end with a delimiter
return true
}
// does not match
return false
}
func dereference(
data []byte,
) []byte {
safe := make([]byte, len(data))
copy(safe, data)
return safe
}