forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitize.go
134 lines (115 loc) · 3.09 KB
/
sanitize.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
package utils
import (
"strings"
)
// These functions are used to sanitize a path component for storage
// in the data store. The filestore has to handle any type of file
// name, being able to preserve the filename upon storage and
// retrieval. This means it needs to have perfect round trip
// encoding. One possibility is simply to use base64 encoding to
// preserve filename but this will make the filestore hard to use and
// obfuscate the file names. Note that file names do not have to be
// valid utf8 strings! We could for example encode a hash value, a
// value containing path separators or an un-normalized unicode
// string.
var hexTable = []byte("0123456789ABCDEF")
// We are very conservative about our escaping - only maintain ascii
// printables, numerics and some safe symbols. We do not assume our
// underlying filesystem supports unicode! UTF8 encoding will be hex
// encoded for the filesystem.
func shouldEscape(c byte) bool {
if 'A' <= c && c <= 'Z' ||
'a' <= c && c <= 'z' ||
'0' <= c && c <= '9' {
return false
}
switch c {
case '-', '_', '.', '~', ' ', '$':
return false
}
return true
}
func SanitizeString(component string) string {
// Escape components that start with . - these are illegal on
// windows and can be used for directory traversal. The . byte
// may appear anywhere else though.
length := len(component)
if length == 0 {
return ""
}
if component[0] == '.' {
return "%2E" + SanitizeString(component[1:])
}
// Windows can not have a trailing "." instead swallowing it
// completely.
if component[length-1] == '.' {
return component[:length-1] + "%2E"
}
// Prevent components from creating names for files that are
// used internally by the datastore. This will be
// automatically stripped when decoding.
if strings.HasSuffix(component, ".db") ||
strings.HasSuffix(component, "_") {
component += "_"
}
if length > 1024 {
length = 1024
}
result := make([]byte, length*4)
result_idx := 0
for _, c := range []byte(component) {
if !shouldEscape(c) {
result[result_idx] = c
result_idx += 1
} else {
result[result_idx] = '%'
result[result_idx+1] = hexTable[c>>4]
result[result_idx+2] = hexTable[c&15]
result_idx += 3
}
if result_idx > len(result)-1 {
break
}
}
return string(result[:result_idx])
}
func unhex(c byte) byte {
switch {
case '0' <= c && c <= '9':
return c - '0'
case 'a' <= c && c <= 'f':
return c - 'a' + 10
case 'A' <= c && c <= 'F':
return c - 'A' + 10
}
return 0
}
func UnsanitizeComponent(component string) string {
result := make([]byte, len(component))
var j, i int
for {
if i >= len(component) {
// Strip a single trailing _
if j > 0 && result[j-1] == '_' {
j--
}
return string(result[:j])
}
if component[i] == '%' {
// A % escape sequece (eg %0d)
if i+2 < len(component) {
result[j] = unhex(component[i+1])<<4 | unhex(component[i+2])
i += 3
j++
} else {
// Skip trailing % - sometimes this is added by
// windows for files with no extension (foo. -> foo.%)
i++
}
} else {
result[j] = component[i]
i += 1
j++
}
}
}