Read all PROXYv2 header bytes (#12861)

In PROXYv2 protocol there can be more bytes after IP/ports data that can be ignored but need to be consumed. This change handles that.
This commit is contained in:
Przemko Robakowski
2022-05-25 01:34:35 +00:00
committed by GitHub
parent d873ea4fa6
commit 8b601f9cde
+10
View File
@@ -28,6 +28,7 @@ import (
"net"
"strconv"
"strings"
"unsafe"
"github.com/gravitational/trace"
)
@@ -213,9 +214,11 @@ func ReadProxyLineV2(reader *bufio.Reader) (*ProxyLine, error) {
if cmd != ProxyCommand {
return nil, trace.BadParameter("unsupported command %d", cmd)
}
var size uint16
switch header.Protocol {
case ProtocolTCP4:
var addr proxyV2Address4
size = uint16(unsafe.Sizeof(addr))
if err := binary.Read(reader, binary.BigEndian, &addr); err != nil {
return nil, trace.Wrap(err)
}
@@ -224,6 +227,7 @@ func ReadProxyLineV2(reader *bufio.Reader) (*ProxyLine, error) {
ret.Destination = net.TCPAddr{IP: addr.Destination[:], Port: int(addr.DestinationPort)}
case ProtocolTCP6:
var addr proxyV2Address6
size = uint16(unsafe.Sizeof(addr))
if err := binary.Read(reader, binary.BigEndian, &addr); err != nil {
return nil, trace.Wrap(err)
}
@@ -234,5 +238,11 @@ func ReadProxyLineV2(reader *bufio.Reader) (*ProxyLine, error) {
return nil, trace.BadParameter("unsupported protocol %x", header.Protocol)
}
if header.Length > size {
if _, err := io.CopyN(io.Discard, reader, int64(header.Length-size)); err != nil {
return nil, trace.Wrap(err)
}
}
return &ret, nil
}