forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsanitize.go
228 lines (196 loc) · 5.15 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
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
package utils
import (
"strings"
"unicode/utf8"
)
// 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 {
length := len(component)
if length == 0 {
return ""
}
// Escape components that start with . - these are illegal on
// windows and can be used for directory traversal. The . byte
// may appear anywhere else though.
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])
}
// This assumes c is an ascii rune!
func escapeChar(c rune) []rune {
return []rune{'%', rune(hexTable[c>>4]), rune(hexTable[c&15])}
}
// A less rigorous escaper which is suitable for zip paths. We assume
// Zip paths may contain UTF8 unicode but can not contain the
// following characters (https://en.wikipedia.org/wiki/Filename):
// 1. The following will always be escaped: /\*?:|<>%
// 2. Non printables
// 3. A "." or " " at the end or the start of the file.
//
// Characters that do not fit into these rules will be escaped using URL encoding.
func SanitizeStringForZip(component string) string {
result := make([]rune, 0, len(component))
escape := func(char rune) {
result = append(result, escapeChar(char)...)
}
for pos, char := range component {
if pos == 0 && (char == '.' || char == ' ') {
escape(char)
continue
}
// This is just some binary data that is not UTF8 - escape it
// so it can be preserved
if char == utf8.RuneError {
if pos < len(component) {
c := component[pos]
escape(rune(c))
}
continue
}
switch char {
case '?', '*', ':', '|', '<', '>', '%', '/', '\\':
escape(char)
default:
result = append(result, char)
}
// Maximum length on one component.
if pos > 1024 {
break
}
}
length := len(result)
if length == 0 {
return ""
}
// Windows can not have a trailing "." instead swallowing it
// completely.
suffix := result[length-1]
if suffix == '.' || suffix == ' ' {
result = result[:length-1]
escape(suffix)
}
return string(result)
}
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++
}
}
}
func UnsanitizeComponentForZip(component string) string {
result := make([]byte, len(component))
var j, i int
for {
if i >= len(component) {
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++
}
}
}