forked from yahoo/webseclab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_test.go
171 lines (154 loc) · 5.11 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
// 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 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)
}
}
// check that a Transformer with no params does not change the input
func TestNewTransformerNop(t *testing.T) {
t.Parallel()
tr := NewTransformer()
for k, v := range samples {
if v != tr.Transform(v) {
t.Errorf("Error in transform of %s: expecting nop (%s), got the change: %s\n", k, v, tr.Transform(v))
}
}
}
func TestTransformerQuotesOff(t *testing.T) {
t.Parallel()
tname := "double and single quotes 1"
tr := NewTransformer(QuotesOff)
res := tr.Transform(samples[tname])
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, tr, samples[tname])
}
}
func TestTransformerTagsOff(t *testing.T) {
t.Parallel()
tr := NewTransformer(TagsOff)
for _, k := range []string{"tags", "less-than", "greater-than", "script"} {
tname := k
v := samples[k]
res := tr.Transform(v)
if strings.ContainsRune(res, 0x3c) || strings.ContainsRune(res, 0x3e) {
t.Errorf("Error in transform of %s: no wanted replacedment in the result [[%s]] (original: %s)\n", tname, res, v)
}
}
}
func TestTransformerQuotesCook(t *testing.T) {
t.Parallel()
tr := NewTransformer(QuotesCook)
tname := "quotes"
res := tr.Transform(samples[tname])
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()
tr := NewTransformer(TagsCook)
tname := "tags"
res := tr.Transform(samples[tname])
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()
tr := NewTransformer(BackslashEscape)
s := `str with backslash\r\n \" double: \\ xyz`
want := `str with backslash\\r\\n \\" double: \\\\ xyz`
if res := tr.Transform(s); res != want {
t.Errorf("Error in transform: want %s got %s; original: %s\n", want, res, s)
}
}
func TestQuotesBackslashQuoteFullEscape(t *testing.T) {
t.Parallel()
tr := NewTransformer(DoubleQuotesBackslashEscape, BackslashEscape)
s := `str with "quotes" and \ backslash`
want := `str with \\"quotes\\" and \\ backslash`
if res := tr.Transform(s); 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 := StripScript(s); 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 := StripScript(s); res != want {
t.Errorf("Error in transform: want %s got %s; original: %s\n", want, res, s)
}
}
func TestNestedTransformer(t *testing.T) {
t.Parallel()
s := `str<script id="foo">alert('123')<xss></script>`
want := `stralert(123)<xss>`
tr1 := NewTransformer(QuotesOff)
tr2 := NewTransformerRegexp(`</?script[^>]*>`)
rt, ok := tr2.(RegexpTransformer)
if ok != true {
t.Errorf("In TestNestedTransformer, cannot convert Transformer to RegexpTransformer")
return
}
rt.nested = &tr1
if res := rt.Transform(s); res != want {
t.Errorf("Error in the nested transform 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`
tr := NewTransformer(ParensOff)
if res := tr.Transform(s); res != want {
t.Errorf("parens stripping: want %s, got %s (orig: %s)\n", want, res, s)
}
}