-
Notifications
You must be signed in to change notification settings - Fork 57
/
morty_test.go
227 lines (213 loc) · 4.72 KB
/
morty_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package main
import (
"bytes"
"net/url"
"testing"
)
type AttrTestCase struct {
AttrName []byte
AttrValue []byte
ExpectedOutput []byte
}
type SanitizeURITestCase struct {
Input []byte
ExpectedOutput []byte
ExpectedScheme string
}
type StringTestCase struct {
Input string
ExpectedOutput string
}
var attrTestData []*AttrTestCase = []*AttrTestCase{
&AttrTestCase{
[]byte("href"),
[]byte("./x"),
[]byte(` href="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fx"`),
},
&AttrTestCase{
[]byte("src"),
[]byte("http://x.com/y"),
[]byte(` src="./?mortyurl=http%3A%2F%2Fx.com%2Fy"`),
},
&AttrTestCase{
[]byte("action"),
[]byte("/z"),
[]byte(` action="./?mortyurl=http%3A%2F%2F127.0.0.1%2Fz"`),
},
&AttrTestCase{
[]byte("onclick"),
[]byte("console.log(document.cookies)"),
nil,
},
}
var sanitizeUriTestData []*SanitizeURITestCase = []*SanitizeURITestCase{
&SanitizeURITestCase{
[]byte("http://example.com/"),
[]byte("http://example.com/"),
"http:",
},
&SanitizeURITestCase{
[]byte("HtTPs://example.com/ \t"),
[]byte("https://example.com/"),
"https:",
},
&SanitizeURITestCase{
[]byte(" Ht TPs://example.com/ \t"),
[]byte("https://example.com/"),
"https:",
},
&SanitizeURITestCase{
[]byte("javascript:void(0)"),
[]byte("javascript:void(0)"),
"javascript:",
},
&SanitizeURITestCase{
[]byte(" /path/to/a/file/without/protocol "),
[]byte("/path/to/a/file/without/protocol"),
"",
},
&SanitizeURITestCase{
[]byte(" #fragment "),
[]byte("#fragment"),
"",
},
&SanitizeURITestCase{
[]byte(" qwertyuiop "),
[]byte("qwertyuiop"),
"",
},
&SanitizeURITestCase{
[]byte(""),
[]byte(""),
"",
},
&SanitizeURITestCase{
[]byte(":"),
[]byte(":"),
":",
},
&SanitizeURITestCase{
[]byte(" :"),
[]byte(":"),
":",
},
&SanitizeURITestCase{
[]byte("schéma:"),
[]byte("schéma:"),
"schéma:",
},
}
var urlTestData []*StringTestCase = []*StringTestCase{
&StringTestCase{
"http://x.com/",
"./?mortyurl=http%3A%2F%2Fx.com%2F",
},
&StringTestCase{
"http://[email protected]/",
"./?mortyurl=http%3A%2F%2Fa%40x.com%2F",
},
&StringTestCase{
"#a",
"#a",
},
}
func TestAttrSanitizer(t *testing.T) {
u, _ := url.Parse("http://127.0.0.1/")
rc := &RequestConfig{BaseURL: u}
for _, testCase := range attrTestData {
out := bytes.NewBuffer(nil)
sanitizeAttr(rc, out, testCase.AttrName, testCase.AttrValue, testCase.AttrValue)
res, _ := out.ReadBytes(byte(0))
if !bytes.Equal(res, testCase.ExpectedOutput) {
t.Errorf(
`Attribute parse error. Name: "%s", Value: "%s", Expected: %s, Got: "%s"`,
testCase.AttrName,
testCase.AttrValue,
testCase.ExpectedOutput,
res,
)
}
}
}
func TestSanitizeURI(t *testing.T) {
for _, testCase := range sanitizeUriTestData {
newUrl, scheme := sanitizeURI(testCase.Input)
if !bytes.Equal(newUrl, testCase.ExpectedOutput) {
t.Errorf(
`URL proxifier error. Expected: "%s", Got: "%s"`,
testCase.ExpectedOutput,
newUrl,
)
}
if scheme != testCase.ExpectedScheme {
t.Errorf(
`URL proxifier error. Expected: "%s", Got: "%s"`,
testCase.ExpectedScheme,
scheme,
)
}
}
}
func TestURLProxifier(t *testing.T) {
u, _ := url.Parse("http://127.0.0.1/")
rc := &RequestConfig{BaseURL: u}
for _, testCase := range urlTestData {
newUrl, err := rc.ProxifyURI([]byte(testCase.Input))
if err != nil {
t.Errorf("Failed to parse URL: %s", testCase.Input)
}
if newUrl != testCase.ExpectedOutput {
t.Errorf(
`URL proxifier error. Expected: "%s", Got: "%s"`,
testCase.ExpectedOutput,
newUrl,
)
}
}
}
var BENCH_SIMPLE_HTML []byte = []byte(`<!doctype html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>Test heading</h1>
</body>
</html>`)
func BenchmarkSanitizeSimpleHTML(b *testing.B) {
u, _ := url.Parse("http://127.0.0.1/")
rc := &RequestConfig{BaseURL: u}
b.ResetTimer()
for i := 0; i < b.N; i++ {
out := bytes.NewBuffer(nil)
sanitizeHTML(rc, out, BENCH_SIMPLE_HTML)
}
}
var BENCH_COMPLEX_HTML []byte = []byte(`<!doctype html>
<html>
<head>
<noscript><meta http-equiv="refresh" content="0; URL=./xy"></noscript>
<title>test 2</title>
<script> alert('xy'); </script>
<link rel="stylesheet" href="./core.bundle.css">
<style>
html { background: url(./a.jpg); }
</style
</head>
<body>
<h1>Test heading</h1>
<img src="b.png" alt="imgtitle" />
<form action="/z">
<input type="submit" style="background: url(http://aa.bb/cc)" >
</form>
</body>
</html>`)
func BenchmarkSanitizeComplexHTML(b *testing.B) {
u, _ := url.Parse("http://127.0.0.1/")
rc := &RequestConfig{BaseURL: u}
b.ResetTimer()
for i := 0; i < b.N; i++ {
out := bytes.NewBuffer(nil)
sanitizeHTML(rc, out, BENCH_COMPLEX_HTML)
}
}