-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
v1_test.go
389 lines (353 loc) · 9.6 KB
/
v1_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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package proxyproto
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"net"
"strconv"
"strings"
"testing"
"time"
)
var (
IPv4AddressesAndPorts = strings.Join([]string{IP4_ADDR, IP4_ADDR, strconv.Itoa(PORT), strconv.Itoa(PORT)}, separator)
IPv4In6AddressesAndPorts = strings.Join([]string{IP4IN6_ADDR, IP4IN6_ADDR, strconv.Itoa(PORT), strconv.Itoa(PORT)}, separator)
IPv4AddressesAndInvalidPorts = strings.Join([]string{IP4_ADDR, IP4_ADDR, strconv.Itoa(INVALID_PORT), strconv.Itoa(INVALID_PORT)}, separator)
IPv6AddressesAndPorts = strings.Join([]string{IP6_ADDR, IP6_ADDR, strconv.Itoa(PORT), strconv.Itoa(PORT)}, separator)
IPv6LongAddressesAndPorts = strings.Join([]string{IP6_LONG_ADDR, IP6_LONG_ADDR, strconv.Itoa(PORT), strconv.Itoa(PORT)}, separator)
fixtureTCP4V1 = "PROXY TCP4 " + IPv4AddressesAndPorts + crlf + "GET /"
fixtureTCP6V1 = "PROXY TCP6 " + IPv6AddressesAndPorts + crlf + "GET /"
fixtureTCP4IN6V1 = "PROXY TCP6 " + IPv4In6AddressesAndPorts + crlf + "GET /"
fixtureTCP6V1Overflow = "PROXY TCP6 " + IPv6LongAddressesAndPorts
fixtureUnknown = "PROXY UNKNOWN" + crlf
fixtureUnknownWithAddresses = "PROXY UNKNOWN " + IPv4AddressesAndInvalidPorts + crlf
)
var invalidParseV1Tests = []struct {
desc string
reader *bufio.Reader
expectedError error
}{
{
desc: "no signature",
reader: newBufioReader([]byte(NO_PROTOCOL)),
expectedError: ErrNoProxyProtocol,
},
{
desc: "prox",
reader: newBufioReader([]byte("PROX")),
expectedError: ErrNoProxyProtocol,
},
{
desc: "proxy lf",
reader: newBufioReader([]byte("PROXY \n")),
expectedError: ErrLineMustEndWithCrlf,
},
{
desc: "proxy crlf",
reader: newBufioReader([]byte("PROXY " + crlf)),
expectedError: ErrCantReadAddressFamilyAndProtocol,
},
{
desc: "proxy no space crlf",
reader: newBufioReader([]byte("PROXY" + crlf)),
expectedError: ErrCantReadAddressFamilyAndProtocol,
},
{
desc: "proxy something crlf",
reader: newBufioReader([]byte("PROXY SOMETHING" + crlf)),
expectedError: ErrCantReadAddressFamilyAndProtocol,
},
{
desc: "incomplete signature TCP4",
reader: newBufioReader([]byte("PROXY TCP4 " + IPv4AddressesAndPorts)),
expectedError: ErrCantReadVersion1Header,
},
{
desc: "invalid IP address",
reader: newBufioReader([]byte("PROXY TCP4 invalid invalid 65533 65533" + crlf)),
expectedError: ErrInvalidAddress,
},
{
desc: "TCP6 with IPv4 addresses",
reader: newBufioReader([]byte("PROXY TCP6 " + IPv4AddressesAndPorts + crlf)),
expectedError: ErrInvalidAddress,
},
{
desc: "TCP4 with IPv6 addresses",
reader: newBufioReader([]byte("PROXY TCP4 " + IPv6AddressesAndPorts + crlf)),
expectedError: ErrInvalidAddress,
},
{
desc: "TCP4 with IPv4 mapped addresses",
reader: newBufioReader([]byte("PROXY TCP4 " + IPv4In6AddressesAndPorts + crlf)),
expectedError: ErrInvalidAddress,
},
{
desc: "TCP4 with invalid port",
reader: newBufioReader([]byte("PROXY TCP4 " + IPv4AddressesAndInvalidPorts + crlf)),
expectedError: ErrInvalidPortNumber,
},
{
desc: "header too long",
reader: newBufioReader([]byte("PROXY UNKNOWN " + IPv6LongAddressesAndPorts + " " + crlf)),
expectedError: ErrVersion1HeaderTooLong,
},
}
func TestReadV1Invalid(t *testing.T) {
for _, tt := range invalidParseV1Tests {
t.Run(tt.desc, func(t *testing.T) {
if _, err := Read(tt.reader); err != tt.expectedError {
t.Fatalf("expected %s, actual %v", tt.expectedError, err)
}
})
}
}
var validParseAndWriteV1Tests = []struct {
desc string
reader *bufio.Reader
expectedHeader *Header
skipWrite bool
}{
{
desc: "TCP4",
reader: bufio.NewReader(strings.NewReader(fixtureTCP4V1)),
expectedHeader: &Header{
Version: 1,
Command: PROXY,
TransportProtocol: TCPv4,
SourceAddr: v4addr,
DestinationAddr: v4addr,
},
},
{
desc: "TCP6",
reader: bufio.NewReader(strings.NewReader(fixtureTCP6V1)),
expectedHeader: &Header{
Version: 1,
Command: PROXY,
TransportProtocol: TCPv6,
SourceAddr: v6addr,
DestinationAddr: v6addr,
},
},
{
desc: "TCP4IN6",
reader: bufio.NewReader(strings.NewReader(fixtureTCP4IN6V1)),
expectedHeader: &Header{
Version: 1,
Command: PROXY,
TransportProtocol: TCPv6,
SourceAddr: v4addr,
DestinationAddr: v4addr,
},
// we skip write test because net.ParseIP converts ::ffff:127.0.0.1 to v4
// instead of preserving the v4 in v6 form, so, after serializing the header,
// we end up with v6 protocol and a v4 IP which is invalid
skipWrite: true,
},
{
desc: "unknown",
reader: bufio.NewReader(strings.NewReader(fixtureUnknown)),
expectedHeader: &Header{
Version: 1,
Command: LOCAL,
TransportProtocol: UNSPEC,
SourceAddr: nil,
DestinationAddr: nil,
},
},
{
desc: "unknown with addresses and ports",
reader: bufio.NewReader(strings.NewReader(fixtureUnknownWithAddresses)),
expectedHeader: &Header{
Version: 1,
Command: LOCAL,
TransportProtocol: UNSPEC,
SourceAddr: nil,
DestinationAddr: nil,
},
},
}
func TestParseV1Valid(t *testing.T) {
for _, tt := range validParseAndWriteV1Tests {
t.Run(tt.desc, func(t *testing.T) {
header, err := Read(tt.reader)
if err != nil {
t.Fatal("unexpected error", err.Error())
}
if !header.EqualsTo(tt.expectedHeader) {
t.Fatalf("expected %#v, actual %#v", tt.expectedHeader, header)
}
})
}
}
func TestWriteV1Valid(t *testing.T) {
for _, tt := range validParseAndWriteV1Tests {
if tt.skipWrite {
continue
}
t.Run(tt.desc, func(t *testing.T) {
var b bytes.Buffer
w := bufio.NewWriter(&b)
if _, err := tt.expectedHeader.WriteTo(w); err != nil {
t.Fatal("unexpected error ", err)
}
w.Flush()
// Read written bytes to validate written header
r := bufio.NewReader(&b)
newHeader, err := Read(r)
if err != nil {
t.Fatal("unexpected error ", err)
}
if !newHeader.EqualsTo(tt.expectedHeader) {
t.Fatalf("expected %#v, actual %#v", tt.expectedHeader, newHeader)
}
})
}
}
// Tests for parseVersion1 overflow - issue #69.
type dataSource struct {
NBytes int
NRead int
}
func (ds *dataSource) Read(b []byte) (int, error) {
if ds.NRead >= ds.NBytes {
return 0, io.EOF
}
avail := ds.NBytes - ds.NRead
if len(b) < avail {
avail = len(b)
}
for i := 0; i < avail; i++ {
b[i] = 0x20
}
ds.NRead += avail
return avail, nil
}
func TestParseVersion1Overflow(t *testing.T) {
ds := &dataSource{}
reader := bufio.NewReader(ds)
bufSize := reader.Size()
ds.NBytes = bufSize * 16
_, _ = parseVersion1(reader)
if ds.NRead > bufSize {
t.Fatalf("read: expected max %d bytes, actual %d\n", bufSize, ds.NRead)
}
}
func listen(t *testing.T) *Listener {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("listen: %v", err)
}
return &Listener{Listener: l}
}
func client(t *testing.T, addr, header string, length int, terminate bool, wait time.Duration, done chan struct{},
result chan error,
) {
c, err := net.Dial("tcp", addr)
if err != nil {
result <- fmt.Errorf("dial: %w", err)
return
}
defer c.Close()
if terminate && length < 2 {
length = 2
}
buf := make([]byte, len(header)+length)
copy(buf, []byte(header))
for i := 0; i < length-2; i++ {
buf[i+len(header)] = 0x20
}
if terminate {
copy(buf[len(header)+length-2:], []byte(crlf))
}
n, err := c.Write(buf)
if err != nil {
result <- fmt.Errorf("write: %w", err)
return
}
if n != len(buf) {
result <- errors.New("write; short write")
return
}
close(result)
time.Sleep(wait)
close(done)
}
func TestVersion1Overflow(t *testing.T) {
done := make(chan struct{})
cliResult := make(chan error)
l := listen(t)
go client(t, l.Addr().String(), fixtureTCP6V1Overflow, 10240, true, 10*time.Second, done, cliResult)
c, err := l.Accept()
if err != nil {
t.Fatalf("accept: %v", err)
}
b := []byte{}
_, err = c.Read(b)
if err == nil {
t.Fatalf("net.Conn: no error reported for oversized header")
}
err = <-cliResult
if err != nil {
t.Fatalf("client error: %v", err)
}
}
func TestVersion1SlowLoris(t *testing.T) {
done := make(chan struct{})
cliResult := make(chan error)
timeout := make(chan error)
l := listen(t)
go client(t, l.Addr().String(), fixtureTCP6V1Overflow, 0, false, 10*time.Second, done, cliResult)
c, err := l.Accept()
if err != nil {
t.Fatalf("accept: %v", err)
}
go func() {
b := []byte{}
_, err = c.Read(b)
timeout <- err
}()
select {
case <-done:
t.Fatalf("net.Conn: reader still blocked after 10 seconds")
case err := <-timeout:
if err == nil {
t.Fatalf("net.Conn: no error reported for incomplete header")
}
}
err = <-cliResult
if err != nil {
t.Fatalf("client error: %v", err)
}
}
func TestVersion1SlowLorisOverflow(t *testing.T) {
done := make(chan struct{})
cliResult := make(chan error)
timeout := make(chan error)
l := listen(t)
go client(t, l.Addr().String(), fixtureTCP6V1Overflow, 10240, false, 10*time.Second, done, cliResult)
c, err := l.Accept()
if err != nil {
t.Fatalf("accept: %v", err)
}
go func() {
b := []byte{}
_, err = c.Read(b)
timeout <- err
}()
select {
case <-done:
t.Fatalf("net.Conn: reader still blocked after 10 seconds")
case err := <-timeout:
if err == nil {
t.Fatalf("net.Conn: no error reported for incomplete and overflowed header")
}
}
err = <-cliResult
if err != nil {
t.Fatalf("client error: %v", err)
}
}