forked from go-sanitize/sanitize
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
192 lines (166 loc) · 4.34 KB
/
Copy pathstring.go
File metadata and controls
192 lines (166 loc) · 4.34 KB
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
package sanitize
import (
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unicode"
)
const (
nullRune = rune('\u0000')
)
// sanitizeStrField sanitizes a string field. Requires the whole
// reflect.Value for the struct because it needs access to both the Value and
// Type of the struct.
func sanitizeStrField(s Sanitizer, structValue reflect.Value, idx int) error {
fieldValue := structValue.Field(idx)
tags := s.fieldTags(structValue.Type().Field(idx).Tag)
if fieldValue.Kind() == reflect.Ptr && !fieldValue.IsNil() {
fieldValue = fieldValue.Elem()
}
isSlice := fieldValue.Kind() == reflect.Slice
var fields []reflect.Value
if !isSlice {
fields = []reflect.Value{fieldValue}
} else {
for i := 0; i < fieldValue.Len(); i++ {
fields = append(fields, fieldValue.Index(i))
}
}
for _, field := range fields {
if !field.CanSet() {
// private/unexported field which cannot be changed, no point trying to sanitize it.
// we will not be able to write it using field.SetString (it will panic while checking whether it is assignable)
continue
}
isPtr := field.Kind() == reflect.Ptr
if isPtr && field.IsNil() {
// Only handle "def" if it is present, then finish san.
if _, ok := tags["def"]; ok {
defStr := tags["def"]
field.Set(reflect.ValueOf(&defStr))
}
return nil
}
if isPtr && !field.IsNil() {
// Dereference then continue as normal.
field = field.Elem()
}
// Always strip out null chars
field.SetString(strings.Map(removeNullChars, field.String()))
// Let's strip out invalid characters before anything else
if _, ok := tags["xss"]; ok {
oldStr := field.String()
field.SetString(xss(oldStr))
}
// Trim must happen before the other tags, no matter what other
// components there are.
if _, ok := tags["trim"]; ok {
// Ignore value of this component, we don't care *how* to trim,
// we just trim.
oldStr := field.String()
field.SetString(strings.Trim(oldStr, " "))
}
// Apply rest of transforms
if _, ok := tags["control"]; ok {
oldStr := field.String()
field.SetString(strings.Map(removeControlRune, oldStr))
}
if _, ok := tags["date"]; ok {
oldStr := field.String()
field.SetString(date(s.dateInput, s.dateKeepFormat, s.dateOutput, oldStr))
}
if _, ok := tags["max"]; ok {
max, err := strconv.ParseInt(tags["max"], 10, 32)
if err != nil {
return err
}
oldStr := field.String()
if max < int64(len(oldStr)) {
field.SetString(oldStr[0:max])
}
}
if _, ok := tags["lower"]; ok {
oldStr := field.String()
field.SetString(strings.ToLower(oldStr))
}
if _, ok := tags["upper"]; ok {
oldStr := field.String()
field.SetString(strings.ToUpper(oldStr))
}
if _, ok := tags["title"]; ok {
oldStr := field.String()
field.SetString(toTitle(oldStr))
}
if _, ok := tags["cap"]; ok {
oldStr := field.String()
field.SetString(toCap(oldStr))
}
}
return nil
}
func toTitle(s string) string {
return strings.Title(strings.ToLower((s)))
}
func toCap(s string) string {
b := make([]byte, len(s))
casediff := byte('a' - 'A')
i := 0
for ; i < len(s); i++ { // Looking for first character
b[i] = s[i]
c := b[i]
if c >= 'A' && c <= 'Z' { // Already capitalized
break
}
if c >= 'a' && c <= 'z' { // Must be capitalized
b[i] -= casediff
break
}
}
i++
for ; i < len(s); i++ { // Lowering all other characters
b[i] = s[i]
c := b[i]
if c >= 'A' && c <= 'Z' {
b[i] += casediff
}
}
return string(b)
}
var replaceWhitespaces = regexp.MustCompile(`\s\s+`)
var blacklistStripping = regexp.MustCompile(`[\p{Me}\p{C}<>=;(){}\[\]?]`)
func xss(s string) string {
s = blacklistStripping.ReplaceAllString(s, " ")
s = replaceWhitespaces.ReplaceAllString(s, " ")
return s
}
func date(in []string, keepFormat bool, out, v string) string {
for _, f := range in {
t, err := time.Parse(f, v)
if err != nil {
continue
}
outf := f
if !keepFormat {
outf = out
}
return t.Format(outf)
}
return ""
}
func removeControlRune(r rune) rune {
if unicode.IsControl(r) {
return -1
}
return r
}
// removeNullChars can be provided to strings.Map to remove null characters from a string
// When it returns a negative value the character is dropped from the string with no replacement.
// see: https://pkg.go.dev/strings#Map
func removeNullChars(r rune) rune {
if r == nullRune {
return -1
}
return r
}