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.
This commit is contained in:
Harshavardhana 2018-12-22 22:33:04 -08:00 committed by Nitish Tiwari
parent b9b68e9331
commit a536cf5dc0
3 changed files with 5 additions and 5 deletions

View File

@ -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,
}

View File

@ -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

View File

@ -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)