-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_test.go
More file actions
256 lines (210 loc) · 7.4 KB
/
Copy pathfuzz_test.go
File metadata and controls
256 lines (210 loc) · 7.4 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package fasthttp
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/binary"
"fmt"
"net/textproto"
"net/url"
"reflect"
"strings"
"testing"
)
func FuzzCookieParse(f *testing.F) {
f.Add([]byte(`xxx=yyy`))
f.Add([]byte(`xxx=yyy; expires=Tue, 10 Nov 2009 23:00:00 GMT; domain=foobar.com; path=/a/b`))
f.Add([]byte(" \n\t\""))
f.Fuzz(func(t *testing.T, cookie []byte) {
var c Cookie
_ = c.ParseBytes(cookie)
w := bytes.Buffer{}
if _, err := c.WriteTo(&w); err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
func FuzzVisitHeaderParams(f *testing.F) {
f.Add([]byte(`application/json; v=1; foo=bar; q=0.938; param=param; param="big fox"; q=0.43`))
f.Add([]byte(`*/*`))
f.Add([]byte(`\\`))
f.Add([]byte(`text/plain; foo="\\\"\'\\''\'"`))
f.Fuzz(func(t *testing.T, header []byte) {
VisitHeaderParams(header, func(key, value []byte) bool {
if len(key) == 0 {
t.Errorf("Unexpected length zero parameter, failed input was: %s", header)
}
return true
})
})
}
func FuzzResponseReadLimitBody(f *testing.F) {
f.Add([]byte("HTTP/1.1 200 OK\r\nContent-Type: aa\r\nContent-Length: 10\r\n\r\n9876543210"), 1024)
f.Add([]byte(" 0\nTrAnsfer-EnCoding:0\n\n0\r\n1:0\n 00\n 000\n\n"), 24922)
f.Add([]byte(" 0\n0:\n 0\n :\n"), 1048532)
// Case found by OSS-Fuzz.
b, err := base64.StdEncoding.DecodeString("oeYAdyAyClRyYW5zZmVyLUVuY29kaW5nOmlka7AKCjANCiA6MAogOgogOgogPgAAAAAAAAAgICAhICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgCiA6CiA6CiAgOgogOgogYDogCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIAogOgogOgogIDoKIDoKIGA6IAoKIDoKBSAgOgogOgogOgogOgogIDoKIDoKIGA6IAAgIAA6CiA6CiA6CjoKIDoKIDoWCiAyIOgKIDogugogOjAKIDoKIDoKBSAgOgogOgogOgogOgogIDoKIDoKIGA6IAAgIAAAAAAAAABaYQ==")
if err != nil {
panic(err)
}
f.Add(b[:len(b)-2], int(binary.LittleEndian.Uint16(b[len(b)-2:])))
f.Fuzz(func(t *testing.T, body []byte, maxBodySize int) {
if len(body) > 1024*1024 || maxBodySize > 1024*1024 {
return
}
// Only test with a max for the body, otherwise a very large Content-Length will just OOM.
if maxBodySize <= 0 {
return
}
res := AcquireResponse()
defer ReleaseResponse(res)
_ = res.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), maxBodySize)
})
}
func FuzzRequestReadLimitBody(f *testing.F) {
f.Add([]byte("POST /a HTTP/1.1\r\nHost: a.com\r\nTransfer-Encoding: chunked\r\nContent-Type: aa\r\n\r\n6\r\nfoobar\r\n3\r\nbaz\r\n0\r\nfoobar\r\n\r\n"), 1024)
f.Fuzz(func(t *testing.T, body []byte, maxBodySize int) {
if len(body) > 1024*1024 || maxBodySize > 1024*1024 {
return
}
// Only test with a max for the body, otherwise a very large Content-Length will just OOM.
if maxBodySize <= 0 {
return
}
req := AcquireRequest()
defer ReleaseRequest(req)
_ = req.ReadLimitBody(bufio.NewReader(bytes.NewReader(body)), maxBodySize)
})
}
func FuzzURIUpdateBytes(f *testing.F) {
f.Add([]byte(`http://foobar.com/aaa/bb?cc`))
f.Add([]byte(`//foobar.com/aaa/bb?cc`))
f.Add([]byte(`/aaa/bb?cc`))
f.Add([]byte(`xx?yy=abc`))
f.Fuzz(func(t *testing.T, uri []byte) {
var u URI
u.UpdateBytes(uri)
w := bytes.Buffer{}
if _, err := u.WriteTo(&w); err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
func FuzzURIParse(f *testing.F) {
f.Add(`http://foobar.com/aaa/bb?cc#dd`)
f.Add(`http://google.com?github.com`)
f.Add(`http://google.com#@github.com`)
// Hosts hiding an uppercase letter and a non-UTF-8 byte behind
// percent-encoding, see the host comparison below.
f.Add(`http://[%255%2c%2c%2c%2c%4c%2c2c%2c%2c]`)
f.Add(`http://aa0aaa%80000000000`)
f.Fuzz(func(t *testing.T, uri string) {
// Limit the size of the URI to avoid OOMs or timeouts.
// When using Server or Client the maximum URI is dicated by the maximum header size,
// which defaults to defaultReadBufferSize (4096 bytes).
if len(uri) > defaultReadBufferSize {
return
}
var u URI
uri = strings.ToLower(uri)
if !strings.HasPrefix(uri, "http://") && !strings.HasPrefix(uri, "https://") {
return
}
if u.Parse(nil, []byte(uri)) != nil {
return
}
nu, err := url.Parse(uri)
if err != nil {
return
}
// fasthttp lowercases the host after unescaping it, net/url keeps
// the case of percent-decoded bytes, and the ToLower above cannot
// reach them ("%4c" decodes to 'L'). Lower the expectation the same
// way fasthttp does; strings.ToLower would mangle non-UTF-8 bytes
// like a decoded "%80".
expectedHost := []byte(nu.Host)
lowercaseBytes(expectedHost)
if !bytes.Equal(u.Host(), expectedHost) {
t.Fatalf("%q: unexpected host: %q. Expecting %q", uri, u.Host(), expectedHost)
}
if string(u.QueryString()) != nu.RawQuery {
t.Fatalf("%q: unexpected query string: %q. Expecting %q", uri, u.QueryString(), nu.RawQuery)
}
})
}
func FuzzHeaderScanner(f *testing.F) {
f.Add([]byte("Host: example.com\r\nUser-Agent: Go-http-client/1.1\r\nAccept-Encoding: gzip, deflate\r\n\r\n"))
f.Add([]byte("Content-Type: application/x-www-form-urlencoded\r\nContent-Length: 27\r\n\r\nname=John+Doe&age=30"))
f.Add([]byte(" 3 :\r\n\r\n"))
f.Fuzz(func(t *testing.T, data []byte) {
if !bytes.Contains(data, []byte("\r\n\r\n")) {
return
}
if len(data) > 1024*1024 {
return
}
t.Logf("%q", data)
tmp, herr := textproto.NewReader(bufio.NewReader(bytes.NewReader(data))).ReadMIMEHeader()
h := map[string][]string(tmp)
var s headerScanner
s.b = data
f := make(map[string][]string)
for s.next() {
// ReadMIMEHeader normalizes header keys, headerScanner doesn't by default.
normalizeHeaderKey(s.key, false)
// textproto.ReadMIMEHeader will validate the header value, since we compare
// errors we should do this as well.
for _, c := range s.value {
if !validHeaderValueByte(c) {
s.err = fmt.Errorf("malformed MIME header: invalid byte %q in value %q for key %q", c, s.value, s.key)
}
}
if s.err != nil {
break
}
key := string(s.key)
value := string(s.value)
if _, ok := f[key]; !ok {
f[key] = []string{}
}
f[key] = append(f[key], value)
}
if s.err != nil && herr == nil {
t.Errorf("unexpected error from headerScanner: %v: %v", s.err, h)
} else if s.err == nil && herr != nil {
t.Errorf("unexpected error from textproto.NewReader: %v: %v", herr, f)
}
if !reflect.DeepEqual(h, f) {
t.Errorf("headers mismatch:\ntextproto: %v\nfasthttp: %v", h, f)
}
})
}
func FuzzRequestReadLimitBodyAllocations(f *testing.F) {
f.Add([]byte("POST /a HTTP/1.1\r\nHost: a.com\r\nTransfer-Encoding: chunked\r\nContent-Type: aa\r\n\r\n6\r\nfoobar\r\n3\r\nbaz\r\n0\r\nfoobar\r\n\r\n"), 1024)
f.Add([]byte("POST /a HTTP/1.1\r\nHost: a.com\r\nWithTabs: \t v1 \t\r\nWithTabs-Start: \t \t v1 \r\nWithTabs-End: v1 \t \t\t\t\r\nWithTabs-Multi-Line: \t v1 \t;\r\n \t v2 \t;\r\n\t v3\r\n\r\n"), 1024)
f.Fuzz(func(t *testing.T, body []byte, maxBodySize int) {
if len(body) > 1024*1024 || maxBodySize > 1024*1024 {
return
}
// Only test with a max for the body, otherwise a very large Content-Length will just OOM.
if maxBodySize <= 0 {
return
}
t.Logf("%d %q", maxBodySize, body)
req := Request{}
a := bytes.NewReader(body)
b := bufio.NewReader(a)
if err := req.ReadLimitBody(b, maxBodySize); err != nil {
return
}
n := testing.AllocsPerRun(200, func() {
req.Reset()
a.Reset(body)
b.Reset(a)
_ = req.ReadLimitBody(b, maxBodySize)
})
if n != 0 {
t.Fatalf("expected 0 allocations, got %f", n)
}
})
}