From 801a7fed19643b7b89929daf53b37e0630e05586 Mon Sep 17 00:00:00 2001 From: Ma Shimiao Date: Thu, 2 Jul 2015 14:57:44 +0800 Subject: [PATCH] api/server: Add missing json check Signed-off-by: Ma Shimiao --- api/server/server.go | 3 +++ integration-cli/docker_api_exec_test.go | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/api/server/server.go b/api/server/server.go index 1659f5834e..1aab4a6ba3 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -1349,6 +1349,9 @@ func (s *Server) postContainerExecCreate(version version.Version, w http.Respons if err := parseForm(r); err != nil { return err } + if err := checkForJson(r); err != nil { + return err + } name := vars["name"] execConfig := &runconfig.ExecConfig{} diff --git a/integration-cli/docker_api_exec_test.go b/integration-cli/docker_api_exec_test.go index b7957480f5..c33a083895 100644 --- a/integration-cli/docker_api_exec_test.go +++ b/integration-cli/docker_api_exec_test.go @@ -4,6 +4,7 @@ package main import ( "bytes" + "encoding/json" "fmt" "net/http" "os/exec" @@ -27,3 +28,24 @@ func (s *DockerSuite) TestExecApiCreateNoCmd(c *check.C) { c.Fatalf("Expected message when creating exec command with no Cmd specified") } } + +func (s *DockerSuite) TestExecApiCreateNoValidContentType(c *check.C) { + name := "exec_test" + dockerCmd(c, "run", "-d", "-t", "--name", name, "busybox", "/bin/sh") + + jsonData := bytes.NewBuffer(nil) + if err := json.NewEncoder(jsonData).Encode(map[string]interface{}{"Cmd": nil}); err != nil { + c.Fatalf("Can not encode data to json %s", err) + } + + res, body, err := sockRequestRaw("POST", fmt.Sprintf("/containers/%s/exec", name), jsonData, "text/plain") + c.Assert(err, check.IsNil) + c.Assert(res.StatusCode, check.Equals, http.StatusInternalServerError) + + b, err := readBody(body) + c.Assert(err, check.IsNil) + + if !bytes.Contains(b, []byte("Content-Type specified")) { + c.Fatalf("Expected message when creating exec command with invalid Content-Type specified") + } +}