mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
lib/netutil: limit the time needed for reading proxy protocol headers
This should prevent from misconfigured proxies and from possible Slowloris-type DoS attacks (see https://en.wikipedia.org/wiki/Slowloris_(computer_security) ) Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3335
This commit is contained in:
parent
cb374677a9
commit
28d92a2f31
1 changed files with 13 additions and 0 deletions
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
||||
)
|
||||
|
@ -15,6 +16,12 @@ type proxyProtocolConn struct {
|
|||
}
|
||||
|
||||
func newProxyProtocolConn(c net.Conn) (net.Conn, error) {
|
||||
// Limit the time needed for reading the proxy protocol header.
|
||||
d := time.Now().Add(5 * time.Second)
|
||||
if err := c.SetReadDeadline(d); err != nil {
|
||||
return nil, fmt.Errorf("cannot set deadline for reading proxy protocol header: %s", err)
|
||||
}
|
||||
|
||||
remoteAddr, err := readProxyProto(c)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("proxy protocol error: %w", err)
|
||||
|
@ -22,6 +29,12 @@ func newProxyProtocolConn(c net.Conn) (net.Conn, error) {
|
|||
if remoteAddr == nil {
|
||||
remoteAddr = c.RemoteAddr()
|
||||
}
|
||||
|
||||
// Reset the read deadline.
|
||||
if err := c.SetReadDeadline(time.Time{}); err != nil {
|
||||
return nil, fmt.Errorf("cannot reset deadline after reading proxy protocol header: %s", err)
|
||||
}
|
||||
|
||||
return &proxyProtocolConn{
|
||||
Conn: c,
|
||||
remoteAddr: remoteAddr,
|
||||
|
|
Loading…
Reference in a new issue