]> Cypherpunks repositories - gostls13.git/commitdiff
net/http/cgi: don't pass nil Body to the child handler
authorMarco <gazerro@open2b.com>
Sun, 24 May 2020 12:58:51 +0000 (14:58 +0200)
committerBryan C. Mills <bcmills@google.com>
Tue, 1 Sep 2020 21:23:24 +0000 (21:23 +0000)
For server requests, the http.Request Body should not be nil.

Fixes #39190

Change-Id: I32de7b6c0f6ca55008fea9fd86089cda0a2dea62
Reviewed-on: https://go-review.googlesource.com/c/go/+/235137
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/net/http/cgi/child.go
src/net/http/cgi/integration_test.go

index 9474175f1791eba414d2443909580c16ed0c7405..d7d813e68a836175c34de41c2102fee87379d405 100644 (file)
@@ -146,6 +146,9 @@ func Serve(handler http.Handler) error {
        if err != nil {
                return err
        }
+       if req.Body == nil {
+               req.Body = http.NoBody
+       }
        if handler == nil {
                handler = http.DefaultServeMux
        }
index 32d59c09a3c52be726386978852960b3677c45b0..eaa090f6fe4f37102b142ff8744c1a4883bfc2d1 100644 (file)
@@ -152,6 +152,23 @@ func TestChildOnlyHeaders(t *testing.T) {
        }
 }
 
+// Test that a child handler does not receive a nil Request Body.
+// golang.org/issue/39190
+func TestNilRequestBody(t *testing.T) {
+       testenv.MustHaveExec(t)
+
+       h := &Handler{
+               Path: os.Args[0],
+               Root: "/test.go",
+               Args: []string{"-test.run=TestBeChildCGIProcess"},
+       }
+       expectedMap := map[string]string{
+               "nil-request-body": "false",
+       }
+       _ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\n\n", expectedMap)
+       _ = runCgiTest(t, h, "POST /test.go?nil-request-body=1 HTTP/1.0\nHost: example.com\nContent-Length: 0\n\n", expectedMap)
+}
+
 // golang.org/issue/7198
 func Test500WithNoHeaders(t *testing.T)     { want500Test(t, "/immediate-disconnect") }
 func Test500WithNoContentType(t *testing.T) { want500Test(t, "/no-content-type") }
@@ -198,6 +215,10 @@ func TestBeChildCGIProcess(t *testing.T) {
                os.Exit(0)
        }
        Serve(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
+               if req.FormValue("nil-request-body") == "1" {
+                       fmt.Fprintf(rw, "nil-request-body=%v\n", req.Body == nil)
+                       return
+               }
                rw.Header().Set("X-Test-Header", "X-Test-Value")
                req.ParseForm()
                if req.FormValue("no-body") == "1" {