/
names.go
44 lines (36 loc) · 822 Bytes
/
names.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
package zar
import (
"path/filepath"
"strings"
)
func validateName(path string) bool {
// Deny absolute paths
// Deny path traversal
// Allow filenames to contains ".." but not exclusively
// Deny invalid characters
if strings.ContainsAny(path, "<>?:*|") {
return false
}
if len(path) < 1 {
return false
}
// Path must not be absolute
if path[0] == '\\' || path[0] == '/' {
return false
}
path = filepath.Clean(path)
// Disallow traversing up a directory with ".." and prevent relative dir "."
if path[0] == '.' {
if len(path) > 2 {
// check if path is "../"
if path[1] == '.' && (path[2] == '/' || path[2] == '\\') {
return false
}
} else if len(path) > 1 {
// check if path is ".."
return !(path[1] == '.')
}
// file name just starts with a dot
}
return true
}