-
Notifications
You must be signed in to change notification settings - Fork 66
/
transform_test.go
207 lines (186 loc) · 6.77 KB
/
transform_test.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
// Copyright 2015, Yahoo Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package webseclab
import (
"strings"
"testing"
)
var samples = map[string]string{"plain string": `plain`,
"quotes": `single quote: ', double quote: "abc", together: '"'`,
"string with spaces": `plain with spaces`,
"double quotes": `double-quotes: ""`,
"single quotes": `single quote: '`,
"tags": `some text <xss> more text`,
"less-than": `matrix A < B`,
"greater-than": `matrix A >= B`,
"script": `<script>alert(xss)</script>`,
"parens": `onerror=alert(12345)`,
}
func TestNoOp(t *testing.T) {
for label, s := range samples {
if res := Transform(s, NoOp); res != s {
t.Errorf("Want %s (identity), got %s in %s\n", s, res, label)
}
}
}
func TestUnescapeUnicode(t *testing.T) {
t.Parallel()
s := `\u0022\u003e\u003cscript\u003ealert(123)\u3c\u2fscript\u3e`
want := `"><script>alert(123)</script>`
esc := UnescapeUnicode(s)
if esc != want {
t.Errorf("Want %s, got: %s\n", want, esc)
}
}
func TestPercentToSlash(t *testing.T) {
t.Parallel()
src := `%5Cx5c%5Cx5c%5Cx27%5Cx5c%5Cx5c%5Cx22`
esc := "/x5c/x5c/x27/x5c/x5c/x22"
res := percentToSlash(src)
if res != esc {
t.Errorf("Want %s, got %s\n", esc, res)
}
}
func TestUnescapeToHex(t *testing.T) {
t.Parallel()
src := "http://example.com\x5c\x5c\x27\x5c\x5c\x22"
esc := "http://example.com/x5c/x5c/x27/x5c/x5c/x22"
res := unescapeToHex(src)
if res != esc {
t.Errorf("Want %s, got %s\n", esc, res)
}
}
func TestTransformerQuotesOff(t *testing.T) {
t.Parallel()
tname := "double and single quotes 1"
res := Transform(samples[tname], QuotesOff)
if strings.ContainsRune(res, 0x22) || strings.ContainsRune(res, 0x27) {
t.Errorf("Error in transform of %s: quotes are not replaced in the result [[%s]]: original %s\n", tname, res, samples[tname])
}
}
func TestRemoveTags(t *testing.T) {
t.Parallel()
s := `hi<xss> nasty tags<script src="foo"> abound<b>around</b>!`
w := `hi nasty tags abound around !`
if res := Transform(s, TagsOff); res != w {
t.Errorf("tags off except textarea close:\nwant %s\ngot %s\n(orig: %s)\n", w, res, s)
}
}
func TestTransformerTagsOff(t *testing.T) {
t.Parallel()
for _, k := range []string{"tags", "less-than", "greater-than", "script"} {
tname := k
v := samples[k]
res := Transform(v, TagCharsOff)
if strings.ContainsRune(res, 0x3c) || strings.ContainsRune(res, 0x3e) {
t.Errorf("Error in transform of %s: no wanted replacement in the result [[%s]] (original: %s)\n", tname, res, v)
}
}
}
func TestTransformerQuotesCook(t *testing.T) {
t.Parallel()
tname := "quotes"
res := Transform(samples[tname], QuotesCook)
if res != `single quote: ', double quote: "abc", together: '"'` {
t.Errorf("Error in transform of %s: quotes are not properly cooked [[%s]]: original %s\n", tname, res, samples[tname])
}
}
func TestTransformerTagsCook(t *testing.T) {
t.Parallel()
tname := "tags"
res := Transform(samples[tname], TagsCook)
if res != `some text <xss> more text` {
t.Errorf("Error in transform of %s: tags are not properly cooked [[%s]]: original %s\n", tname, res, samples[tname])
}
}
func TestBackslashEscape(t *testing.T) {
t.Parallel()
s := `str with backslash\r\n \" double: \\ xyz`
want := `str with backslash\\r\\n \\" double: \\\\ xyz`
if res := Transform(s, BackslashEscape); res != want {
t.Errorf("Error in transform: want %s got %s; original: %s\n", want, res, s)
}
}
func TestQuotesBackslashQuoteFullEscape(t *testing.T) {
t.Parallel()
s := `str with "quotes" and \ backslash`
want := `str with \\"quotes\\" and \\ backslash`
if res := Transform(s, DoubleQuotesBackslashEscape, BackslashEscape); res != want {
t.Errorf("Error in transform: want %s got %s; original: %s\n", want, res, s)
}
}
func TestBackslashDoublequotesEscape(t *testing.T) {
t.Parallel()
s := `str with "quotes" and \ backslash and both \"`
want := `str with \"quotes\" and \\ backslash and both \\\"`
if res := Transform(s, BackslashEscapeDoubleQuotesAndBackslash); res != want {
t.Errorf("Error in transform: want %s got %s; original: %s\n", want, res, s)
}
}
func TestScriptOff(t *testing.T) {
t.Parallel()
s := `str<script>alert(123)</script>`
want := `stralert(123)`
if res := Transform(s, ScriptOff); res != want {
t.Errorf("Error in transform: want %s got %s; original: %s\n", want, res, s)
}
}
func TestScriptOffWithAttr(t *testing.T) {
t.Parallel()
s := `str<script id="foo">alert(123)</script>`
want := `stralert(123)`
if res := Transform(s, ScriptOff); res != want {
t.Errorf("Error in TestScriptOffWithAttr: want %s got %s; original: %s\n", want, res, s)
}
}
func TestTextareaClose(t *testing.T) {
t.Parallel()
inp := [...]string{`str<textarea>foo</textarea>`, `str<textarea>foo</TEXTAREA>`, `str<textarea>foo</TeXtArEa>`}
want := `str<textarea>foo`
for _, s := range inp {
if res := Transform(s, TextareaCloseOff); res != want {
t.Errorf("Error in StripTextareaClose: want %s got %s; original: %s\n", want, res, s)
}
}
}
func TestQuotesOffScriptOff(t *testing.T) {
t.Parallel()
s := `str<script id="foo">alert('123')<xss></script>`
want := `stralert(123)<xss>`
if res := Transform(s, QuotesOff, ScriptOff); res != want {
t.Errorf("Error in the TestQuotesOffScriptOff test: want %s got %s; original: %s\n", want, res, s)
}
}
func TestParensOff(t *testing.T) {
t.Parallel()
s := `onerror=alert(12345)`
want := `onerror=alert12345`
if res := Transform(s, ParensOff); res != want {
t.Errorf("parens stripping: want %s, got %s (orig: %s)\n", want, res, s)
}
}
func TestRemoveTagsUntilTextareaClose(t *testing.T) {
t.Parallel()
s := `name="in" rows="5" cols="60">"><script>alert("xss");</script>xsrcin</textarea>foo<xss>bar`
w := `name="in" rows="5" cols="60">">alert("xss");xsrcin</textarea>foo<xss>bar`
if res := RemoveTagsUntilTextareaClose(s); res != w {
t.Errorf("tags off except textarea close:\nwant %s\ngot %s\n(orig: %s)\n", w, res, s)
}
}
func TestTagsOffExceptTextareaClose(t *testing.T) {
t.Parallel()
s := `name="in" rows="5" cols="60">"><script>alert("xss");</script>xsrcin</textarea>foo<xss>bar`
w := `name="in" rows="5" cols="60">"> alert("xss"); xsrcin</textarea>foo bar`
if res := Transform(s, TagsOffExceptTextareaClose); res != w {
t.Errorf("tags off except textarea close:\nwant %s\ngot %s\n(orig: %s)\n", w, res, s)
}
}
func TestTextareaSafe(t *testing.T) {
t.Parallel()
s := `name="in" rows="5" cols="60">"><script>alert("xss");</script>xsrcin</textarea>foo<xss>danger`
w := `name="in" rows="5" cols="60">"><script>alert("xss");</script>xsrcin</textarea>foo danger`
if res := Transform(s, TextareaSafe); res != w {
t.Errorf("textarea safe filter:\nwant %s\ngot %s\n(orig: %s)\n", w, res, s)
}
}