@marcel91200 / caddy Mirror · read-only / commits / 58ab3a01
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 12, 22:00 UTC

caddyhttp: Use LimitedReader for HTTPRedirectListener

Matthew Holt committed Sep 26, 2023 at 07:32 UTC 58ab3a01a0d4b5f9e8bff56f623ceb906ff603b9
1 file changed +14 -3
modules/caddyhttp/httpredirectlistener.go
+14 -3
@@ -17,6 +17,7 @@ package caddyhttp
17 import (
18 "bufio"
19 "fmt"
20 + "io"
21 "net"
22 "net/http"
23 "sync"
@@ -42,7 +43,11 @@ func init() {
43 //
44 // This listener wrapper must be placed BEFORE the "tls" listener
45 // 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 +}
51
52 func (HTTPRedirectListenerWrapper) CaddyModule() caddy.ModuleInfo {
53 return caddy.ModuleInfo{
@@ -56,7 +61,7 @@ func (h *HTTPRedirectListenerWrapper) UnmarshalCaddyfile(d *caddyfile.Dispenser)
61 }
62
63 func (h *HTTPRedirectListenerWrapper) WrapListener(l net.Listener) net.Listener {
59 - return &httpRedirectListener{l}
64 + return &httpRedirectListener{l, h.MaxHeaderBytes}
65 }
66
67 // httpRedirectListener is listener that checks the first few bytes
@@ -64,6 +69,7 @@ func (h *HTTPRedirectListenerWrapper) WrapListener(l net.Listener) net.Listener
69 // to respond to an HTTP request with a redirect.
70 type httpRedirectListener struct {
71 net.Listener
72 + maxHeaderBytes int64
73 }
74
75 // Accept waits for and returns the next connection to the listener,
@@ -74,9 +80,14 @@ func (l *httpRedirectListener) Accept() (net.Conn, error) {
80 return nil, err
81 }
82
83 + maxHeaderBytes := l.maxHeaderBytes
84 + if maxHeaderBytes == 0 {
85 + maxHeaderBytes = 1024 * 1024
86 + }
87 +
88 return &httpRedirectConn{
89 Conn: c,
79 - r: bufio.NewReader(c),
90 + r: bufio.NewReader(io.LimitReader(c, maxHeaderBytes)),
91 }, nil
92 }
93