-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathxss_helpers_test.go
More file actions
190 lines (179 loc) · 6.59 KB
/
xss_helpers_test.go
File metadata and controls
190 lines (179 loc) · 6.59 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
package libinjection
import (
"strings"
"testing"
)
func TestAsciiEqualFold(t *testing.T) {
tests := []struct {
name string
a, b string
want bool
}{
{name: "equal length, exact match", a: "doctype", b: "doctype", want: true},
{name: "equal length, a uppercase", a: "DOCTYPE", b: "doctype", want: true},
{name: "equal length, b uppercase", a: "doctype", b: "DOCTYPE", want: true},
{name: "equal length, mismatch", a: "data", b: "date", want: false},
{name: "different length", a: "data", b: "dat", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := asciiEqualFold(tt.a, tt.b); got != tt.want {
t.Errorf("asciiEqualFold(%q, %q) = %v, want %v", tt.a, tt.b, got, tt.want)
}
})
}
}
func TestHtmlDecodeByteAt(t *testing.T) {
tests := []struct {
name string
input string
wantVal int
wantConsumed int
}{
{name: "empty string", input: "", wantVal: byteEOF, wantConsumed: 0},
{name: "regular char", input: "a", wantVal: 'a', wantConsumed: 1},
{name: "ampersand only", input: "&", wantVal: '&', wantConsumed: 1},
{name: "ampersand with non-hash", input: "&a", wantVal: '&', wantConsumed: 1},
{name: "&# too short", input: "&#", wantVal: '&', wantConsumed: 1},
{name: "&#non-digit", input: "&#a", wantVal: '&', wantConsumed: 1},
{name: "decimal no terminator", input: "", wantVal: 5, wantConsumed: 3},
{name: "decimal semicolon", input: "", wantVal: 5, wantConsumed: 4},
{name: "decimal non-digit terminator", input: "a", wantVal: 5, wantConsumed: 3},
{name: "decimal overflow", input: "�", wantVal: '&', wantConsumed: 1},
{name: "hex &#x too short", input: "&#x", wantVal: '&', wantConsumed: 1},
{name: "hex uppercase X too short", input: "&#X", wantVal: '&', wantConsumed: 1},
{name: "hex invalid char", input: "&#xG", wantVal: '&', wantConsumed: 1},
{name: "hex no terminator", input: "", wantVal: 5, wantConsumed: 4},
{name: "hex semicolon", input: "", wantVal: 5, wantConsumed: 5},
{name: "hex uppercase X valid", input: "", wantVal: 5, wantConsumed: 4},
{name: "hex non-hex terminator", input: "G", wantVal: 5, wantConsumed: 4},
{name: "hex overflow", input: "�", wantVal: '&', wantConsumed: 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotVal, gotConsumed := htmlDecodeByteAt(tt.input)
if gotVal != tt.wantVal || gotConsumed != tt.wantConsumed {
t.Errorf("htmlDecodeByteAt(%q) = (%d, %d), want (%d, %d)",
tt.input, gotVal, gotConsumed, tt.wantVal, tt.wantConsumed)
}
})
}
}
func TestIsBlackAttr(t *testing.T) {
tests := []struct {
name string
attr string
want int
}{
{
name: "Test with black attribute",
attr: "xmlns",
want: attributeTypeBlack,
},
{
name: "Test with non-black attribute",
attr: "class",
want: attributeTypeNone,
},
{
name: "Test with JavaScript event handler",
attr: "onclick",
want: attributeTypeBlack,
},
{
name: "Test with short attribute",
attr: "a",
want: attributeTypeNone,
},
{
name: "Test with long null attribute that will be stripped",
attr: "a\x00\x00\x00\x00\x00",
want: attributeTypeNone,
},
{
name: "over-length attribute cannot match",
attr: "onclick" + strings.Repeat("x", maxNormalizedTokenLen), // 7+64 = 71 bytes > maxNormalizedTokenLen
want: attributeTypeNone,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isBlackAttr(tt.attr); got != tt.want {
t.Errorf("isBlackAttr() = %v, want %v", got, tt.want)
}
})
}
}
func TestHtmlEncodeStartsWith(t *testing.T) {
tests := []struct {
name string
prefix string
input string
want bool
}{
{name: "exact match", prefix: "DATA", input: "data:", want: true},
{name: "prefix match with trailing content", prefix: "JAVA", input: "javascript:alert(1)", want: true},
{name: "no match", prefix: "DATA", input: "https://example.com", want: false},
{name: "pattern in middle should not match", prefix: "DATA", input: "https://github.com/Simbiat/database", want: false},
{name: "pattern at end should not match", prefix: "DATA", input: "nodata", want: false},
{name: "leading whitespace skipped", prefix: "DATA", input: "\tdata:", want: true},
{name: "embedded null ignored", prefix: "DATA", input: "d\x00ata:", want: true},
{name: "embedded LF ignored", prefix: "DATA", input: "d\nata:", want: true},
{name: "input shorter than prefix", prefix: "DATA", input: "dat", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := htmlEncodeStartsWith(tt.prefix, tt.input); got != tt.want {
t.Errorf("htmlEncodeStartsWith(%q, %q) = %v, want %v", tt.prefix, tt.input, got, tt.want)
}
})
}
}
func TestIsBlackURL(t *testing.T) {
tests := []struct {
name string
url string
want bool
}{
{name: "data URL", url: "data:text/html,<script>alert(1)</script>", want: true},
{name: "javascript URL", url: "javascript:alert(1)", want: true},
{name: "vbscript URL", url: "vbscript:msgbox", want: true},
{name: "https URL", url: "https://example.com", want: false},
{name: "URL containing data in path", url: "https://github.com/Simbiat/database", want: false},
{name: "URL containing java in path", url: "https://example.com/javascript-tutorials", want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isBlackURL(tt.url); got != tt.want {
t.Errorf("isBlackURL(%q) = %v, want %v", tt.url, got, tt.want)
}
})
}
}
func TestIsBlackTag(t *testing.T) {
tests := []struct {
name string
tag string
want bool
}{
{name: "script tag", tag: "SCRIPT", want: true},
{name: "svg exact", tag: "svg", want: true},
{name: "SVG uppercase", tag: "SVG", want: true},
{name: "svg prefixed tag", tag: "svganimate", want: true},
{name: "svg namespaced", tag: "svg:rect", want: true},
{name: "xsl exact", tag: "xsl", want: true},
{name: "xsl prefixed tag", tag: "xsl:template", want: true},
{name: "div tag", tag: "div", want: false},
{name: "span tag", tag: "span", want: false},
{name: "too short", tag: "sv", want: false},
{name: "over-length tag cannot match", tag: "script" + strings.Repeat("x", maxNormalizedTokenLen), want: false}, // 6+64 = 70 bytes > maxNormalizedTokenLen
{name: "over-length SVG prefix cannot match via prefix rule", tag: "svg" + strings.Repeat("x", maxNormalizedTokenLen), want: false}, // 3+64 = 67 bytes > maxNormalizedTokenLen
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isBlackTag(tt.tag); got != tt.want {
t.Errorf("isBlackTag(%q) = %v, want %v", tt.tag, got, tt.want)
}
})
}
}