-
Notifications
You must be signed in to change notification settings - Fork 7
/
xss_helpers.go
197 lines (175 loc) · 3.79 KB
/
xss_helpers.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
package libinjection
import (
"strings"
)
func isH5White(ch byte) bool {
return ch == '\n' || ch == '\t' || ch == '\v' || ch == '\f' || ch == '\r' || ch == ' '
}
func isBlackTag(s string) bool {
if len(s) < 3 {
return false
}
sUpperWithoutNulls := strings.ToUpper(strings.ReplaceAll(s, "\x00", ""))
for i := 0; i < len(blackTags); i++ {
if sUpperWithoutNulls == blackTags[i] {
return true
}
}
switch sUpperWithoutNulls {
// anything SVG or XSL(t) related
case "SVT", "XSL":
return true
default:
return false
}
}
func isBlackAttr(s string) int {
sUpperWithoutNulls := strings.ToUpper(strings.ReplaceAll(s, "\x00", ""))
length := len(sUpperWithoutNulls)
if length < 2 {
return attributeTypeNone
}
if length >= 5 {
if sUpperWithoutNulls == "XMLNS" || sUpperWithoutNulls == "XLINK" {
// got xmlns or xlink tags
return attributeTypeBlack
}
// JavaScript on.* event handlers
if sUpperWithoutNulls[:2] == "ON" {
eventName := sUpperWithoutNulls[2:]
// got javascript on- attribute name
for _, event := range blackEvents {
if eventName == event.name {
return event.attributeType
}
}
}
}
for _, black := range blacks {
if sUpperWithoutNulls == black.name {
// got banner attribute name
return black.attributeType
}
}
return attributeTypeNone
}
func htmlDecodeByteAt(s string) (int, int) {
length := len(s)
val := 0
if length == 0 {
return byteEOF, 0
}
if s[0] != '&' || length < 2 {
return int(s[0]), 1
}
if s[1] != '#' || len(s) < 3 {
// normally this would be for named entities
// but for this case we don't actually care
return '&', 1
}
if s[2] == 'x' || s[2] == 'X' {
if len(s) < 4 {
return '&', 1
}
ch := int(s[3])
ch = gsHexDecodeMap[ch]
if ch == 256 {
// degenerate case '&#[?]'
return '&', 1
}
val = ch
i := 4
for i < length {
ch = int(s[i])
if ch == ';' {
return val, i + 1
}
ch = gsHexDecodeMap[ch]
if ch == 256 {
return val, i
}
val = val*16 + ch
if val > 0x1000FF {
return '&', 1
}
i++
}
return val, i
}
i := 2
ch := int(s[i])
if ch < '0' || ch > '9' {
return '&', 1
}
val = ch - '0'
i++
for i < length {
ch = int(s[i])
if ch == ';' {
return val, i + 1
}
if ch < '0' || ch > '9' {
return val, i
}
val = val*10 + (ch - '0')
if val > 0x1000FF {
return '&', 1
}
i++
}
return val, i
}
// Does an HTML encoded binary string (const char*, length) start with
// a all uppercase c-string (null terminated), case insensitive!
//
// also ignore any embedded nulls in the HTML string!
func htmlEncodeStartsWith(a, b string) bool {
var (
first = true
bs []byte
pos = 0
length = len(b)
)
for length > 0 {
cb, consumed := htmlDecodeByteAt(b[pos:])
pos += consumed
length -= consumed
if first && cb <= 32 {
// ignore all leading whitespace and control characters
continue
}
first = false
if cb == 0 || cb == 10 {
// always ignore null characters in user input
// always ignore vertical tab characters in user input
continue
}
if cb >= 'a' && cb <= 'z' {
cb -= 0x20
}
bs = append(bs, byte(cb))
}
return strings.Contains(string(bs), a)
}
func isBlackURL(s string) bool {
urls := []string{
"DATA", // data url
"VIEW-SOURCE", // view source url
"VBSCRIPT", // obsolete but interesting signal
"JAVA", // covers JAVA, JAVASCRIPT, + colon
}
// HEY: this is a signed character.
// We are intentionally skipping high-bit characters too
// since they are not ASCII, and Opera sometimes uses UTF-8 whitespace.
//
// Also in EUC-JP some of the high bytes are just ignored.
str := strings.TrimLeftFunc(s, func(r rune) bool {
return r <= 32 || r >= 127
})
for _, url := range urls {
if htmlEncodeStartsWith(url, str) {
return true
}
}
return false
}