Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ func (p *Listener) Addr() net.Addr {
func NewConn(conn net.Conn, opts ...func(*Conn)) *Conn {
// For v1 the header length is at most 108 bytes.
// For v2 the header length is at most 52 bytes plus the length of the TLVs.
// We use 256 bytes to be safe.
const bufSize = 256
// PP2_SUBTYPE_SSL_CLIENT_CERT might be a few kilobytes. We use 4096 bytes
// to be safe.
const bufSize = 4096
br := bufio.NewReaderSize(conn, bufSize)

pConn := &Conn{
Expand Down
20 changes: 16 additions & 4 deletions v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var (
invalidRune = byte('\x99')

// Lengths to use in tests.
lengthPadded = uint16(84)
lengthPadded = uint16(84)
lengthTLVsTooLarge = uint16(10 * 1024)

lengthEmptyBytes = func() []byte {
a := make([]byte, 2)
Expand All @@ -27,6 +28,11 @@ var (
binary.BigEndian.PutUint16(a, lengthPadded)
return a
}()
lengthTLVsTooLargeBytes = func() []byte {
a := make([]byte, 2)
binary.BigEndian.PutUint16(a, lengthTLVsTooLarge)
return a
}()

// If life gives you lemons, make mojitos.
portBytes = func() []byte {
Expand Down Expand Up @@ -60,9 +66,10 @@ var (
_, _ = iorand.Read(tlv)
return tlv
}()
fixtureIPv4V2TLV = fixtureWithTLV(lengthV4Bytes, fixtureIPv4Address, fixtureTLV)
fixtureIPv6V2TLV = fixtureWithTLV(lengthV6Bytes, fixtureIPv6Address, fixtureTLV)
fixtureUnspecTLV = fixtureWithTLV(lengthUnspecBytes, []byte{}, fixtureTLV)
fixtureIPv4V2TLV = fixtureWithTLV(lengthV4Bytes, fixtureIPv4Address, fixtureTLV)
fixtureIPv6V2TLV = fixtureWithTLV(lengthV6Bytes, fixtureIPv6Address, fixtureTLV)
fixtureUnspecTLV = fixtureWithTLV(lengthUnspecBytes, []byte{}, fixtureTLV)
fixtureTLVsTooLarge = append(append(lengthTLVsTooLargeBytes, fixtureIPv4Address...), make([]byte, lengthTLVsTooLarge-lengthV4)...)

// Arbitrary bytes following proxy bytes.
arbitraryTailBytes = []byte{'\x99', '\x97', '\x98'}
Expand Down Expand Up @@ -153,6 +160,11 @@ var invalidParseV2Tests = []struct {
reader: newBufioReader(append(append(SIGV2, byte(LOCAL), byte(UNSPEC)), fixtureUnspecTLV[:2]...)),
expectedError: ErrInvalidLength,
},
{
desc: "TCPv4 with length too large",
reader: newBufioReader(append(append(SIGV2, byte(PROXY), byte(TCPv4)), fixtureTLVsTooLarge...)),
expectedError: ErrInvalidLength,
},
}

func TestParseV2Invalid(t *testing.T) {
Expand Down
Loading