Buffconn should buffer upto maxHeaderBytes to avoid ErrBufferFull (#7017)

It can happen with erroneous clients which do not send `Host:`
header until 4k worth of header bytes have been read. This can lead
to Peek() method of bufio to fail with ErrBufferFull.

To avoid this we should make sure that Peek buffer is as large as
our maxHeaderBytes count.
master
Harshavardhana 6 years ago committed by Nitish Tiwari
parent b9b68e9331
commit a536cf5dc0
  1. 4
      cmd/http/bufconn.go
  2. 2
      cmd/http/bufconn_test.go
  3. 4
      cmd/http/listener.go

@ -99,10 +99,10 @@ func (c *BufConn) Write(b []byte) (n int, err error) {
}
// newBufConn - creates a new connection object wrapping net.Conn.
func newBufConn(c net.Conn, readTimeout, writeTimeout time.Duration) *BufConn {
func newBufConn(c net.Conn, readTimeout, writeTimeout time.Duration, maxHeaderBytes int) *BufConn {
return &BufConn{
QuirkConn: QuirkConn{Conn: c},
bufReader: bufio.NewReader(c),
bufReader: bufio.NewReaderSize(c, maxHeaderBytes),
readTimeout: readTimeout,
writeTimeout: writeTimeout,
}

@ -49,7 +49,7 @@ func TestBuffConnReadTimeout(t *testing.T) {
t.Errorf("failed to accept new connection. %v", terr)
return
}
bufconn := newBufConn(tcpConn, 1*time.Second, 1*time.Second)
bufconn := newBufConn(tcpConn, 1*time.Second, 1*time.Second, 4096)
defer bufconn.Close()
// Read a line

@ -222,7 +222,7 @@ func (listener *httpListener) start() {
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(listener.tcpKeepAliveTimeout)
bufconn := newBufConn(tcpConn, listener.readTimeout, listener.writeTimeout)
bufconn := newBufConn(tcpConn, listener.readTimeout, listener.writeTimeout, listener.maxHeaderBytes)
if listener.tlsConfig != nil {
ok, err := getPlainText(bufconn)
if err != nil {
@ -261,7 +261,7 @@ func (listener *httpListener) start() {
return
}
bufconn = newBufConn(tlsConn, listener.readTimeout, listener.writeTimeout)
bufconn = newBufConn(tlsConn, listener.readTimeout, listener.writeTimeout, listener.maxHeaderBytes)
}
method, resource, host, err := getMethodResourceHost(bufconn, listener.maxHeaderBytes)

Loading…
Cancel
Save