Skip to content

Commit 58ab3a0

Browse files
committed
caddyhttp: Use LimitedReader for HTTPRedirectListener
1 parent a306c5f commit 58ab3a0

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

modules/caddyhttp/httpredirectlistener.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package caddyhttp
1717
import (
1818
"bufio"
1919
"fmt"
20+
"io"
2021
"net"
2122
"net/http"
2223
"sync"
@@ -42,7 +43,11 @@ func init() {
4243
//
4344
// This listener wrapper must be placed BEFORE the "tls" listener
4445
// wrapper, for it to work properly.
45-
type HTTPRedirectListenerWrapper struct{}
46+
type HTTPRedirectListenerWrapper struct {
47+
// MaxHeaderBytes is the maximum size to parse from a client's
48+
// HTTP request headers. Default: 1 MB
49+
MaxHeaderBytes int64 `json:"max_header_bytes,omitempty"`
50+
}
4651

4752
func (HTTPRedirectListenerWrapper) CaddyModule() caddy.ModuleInfo {
4853
return caddy.ModuleInfo{
@@ -56,14 +61,15 @@ func (h *HTTPRedirectListenerWrapper) UnmarshalCaddyfile(d *caddyfile.Dispenser)
5661
}
5762

5863
func (h *HTTPRedirectListenerWrapper) WrapListener(l net.Listener) net.Listener {
59-
return &httpRedirectListener{l}
64+
return &httpRedirectListener{l, h.MaxHeaderBytes}
6065
}
6166

6267
// httpRedirectListener is listener that checks the first few bytes
6368
// of the request when the server is intended to accept HTTPS requests,
6469
// to respond to an HTTP request with a redirect.
6570
type httpRedirectListener struct {
6671
net.Listener
72+
maxHeaderBytes int64
6773
}
6874

6975
// Accept waits for and returns the next connection to the listener,
@@ -74,9 +80,14 @@ func (l *httpRedirectListener) Accept() (net.Conn, error) {
7480
return nil, err
7581
}
7682

83+
maxHeaderBytes := l.maxHeaderBytes
84+
if maxHeaderBytes == 0 {
85+
maxHeaderBytes = 1024 * 1024
86+
}
87+
7788
return &httpRedirectConn{
7889
Conn: c,
79-
r: bufio.NewReader(c),
90+
r: bufio.NewReader(io.LimitReader(c, maxHeaderBytes)),
8091
}, nil
8192
}
8293

0 commit comments

Comments
 (0)