Skip to content

Commit b7a85e0

Browse files
neildFiloSottile
authored andcommitted
net/http/httputil: close incoming ReverseProxy request body
Reading from an incoming request body after the request handler aborts with a panic can cause a panic, becuse http.Server does not (contrary to its documentation) close the request body in this case. Always close the incoming request body in ReverseProxy.ServeHTTP to ensure that any in-flight outgoing requests using the body do not read from it. Updates #46866 Fixes CVE-2021-36221 Change-Id: I310df269200ad8732c5d9f1a2b00de68725831df Reviewed-on: https://go-review.googlesource.com/c/go/+/333191 Trust: Damien Neil <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]>
1 parent 70fd4e4 commit b7a85e0

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/net/http/httputil/reverseproxy.go

+9
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ func (p *ReverseProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
235235
if req.ContentLength == 0 {
236236
outreq.Body = nil // Issue 16036: nil Body for http.Transport retries
237237
}
238+
if outreq.Body != nil {
239+
// Reading from the request body after returning from a handler is not
240+
// allowed, and the RoundTrip goroutine that reads the Body can outlive
241+
// this handler. This can lead to a crash if the handler panics (see
242+
// Issue 46866). Although calling Close doesn't guarantee there isn't
243+
// any Read in flight after the handle returns, in practice it's safe to
244+
// read after closing it.
245+
defer outreq.Body.Close()
246+
}
238247
if outreq.Header == nil {
239248
outreq.Header = make(http.Header) // Issue 33142: historical behavior was to always allocate
240249
}

src/net/http/httputil/reverseproxy_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,45 @@ func TestReverseProxy_PanicBodyError(t *testing.T) {
11221122
rproxy.ServeHTTP(httptest.NewRecorder(), req)
11231123
}
11241124

1125+
// Issue #46866: panic without closing incoming request body causes a panic
1126+
func TestReverseProxy_PanicClosesIncomingBody(t *testing.T) {
1127+
backend := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1128+
out := "this call was relayed by the reverse proxy"
1129+
// Coerce a wrong content length to induce io.ErrUnexpectedEOF
1130+
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(out)*2))
1131+
fmt.Fprintln(w, out)
1132+
}))
1133+
defer backend.Close()
1134+
backendURL, err := url.Parse(backend.URL)
1135+
if err != nil {
1136+
t.Fatal(err)
1137+
}
1138+
proxyHandler := NewSingleHostReverseProxy(backendURL)
1139+
proxyHandler.ErrorLog = log.New(io.Discard, "", 0) // quiet for tests
1140+
frontend := httptest.NewServer(proxyHandler)
1141+
defer frontend.Close()
1142+
frontendClient := frontend.Client()
1143+
1144+
var wg sync.WaitGroup
1145+
for i := 0; i < 2; i++ {
1146+
wg.Add(1)
1147+
go func() {
1148+
defer wg.Done()
1149+
for j := 0; j < 10; j++ {
1150+
const reqLen = 6 * 1024 * 1024
1151+
req, _ := http.NewRequest("POST", frontend.URL, &io.LimitedReader{R: neverEnding('x'), N: reqLen})
1152+
req.ContentLength = reqLen
1153+
resp, _ := frontendClient.Transport.RoundTrip(req)
1154+
if resp != nil {
1155+
io.Copy(io.Discard, resp.Body)
1156+
resp.Body.Close()
1157+
}
1158+
}
1159+
}()
1160+
}
1161+
wg.Wait()
1162+
}
1163+
11251164
func TestSelectFlushInterval(t *testing.T) {
11261165
tests := []struct {
11271166
name string

0 commit comments

Comments
 (0)