Browse code

Validate status= filter to docker ps

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2015/07/01 06:40:27
Showing 3 changed files
... ...
@@ -53,6 +53,9 @@ func (daemon *Daemon) Containers(config *ContainersConfig) ([]*types.Container,
53 53
 
54 54
 	if i, ok := psFilters["status"]; ok {
55 55
 		for _, value := range i {
56
+			if !isValidStateString(value) {
57
+				return nil, errors.New("Unrecognised filter value for status")
58
+			}
56 59
 			if value == "exited" || value == "created" {
57 60
 				all = true
58 61
 			}
... ...
@@ -86,6 +86,18 @@ func (s *State) StateString() string {
86 86
 	return "exited"
87 87
 }
88 88
 
89
+func isValidStateString(s string) bool {
90
+	if s != "paused" &&
91
+		s != "restarting" &&
92
+		s != "running" &&
93
+		s != "dead" &&
94
+		s != "created" &&
95
+		s != "exited" {
96
+		return false
97
+	}
98
+	return true
99
+}
100
+
89 101
 func wait(waitChan <-chan struct{}, timeout time.Duration) error {
90 102
 	if timeout < 0 {
91 103
 		<-waitChan
... ...
@@ -236,6 +236,11 @@ func (s *DockerSuite) TestPsListContainersFilterStatus(c *check.C) {
236 236
 		c.Fatalf("Expected id %s, got %s for running filter, output: %q", secondID[:12], containerOut, out)
237 237
 	}
238 238
 
239
+	out, _, _ = dockerCmdWithTimeout(time.Second*60, "ps", "-a", "-q", "--filter=status=rubbish")
240
+	if !strings.Contains(out, "Unrecognised filter value for status") {
241
+		c.Fatalf("Expected error response due to invalid status filter output: %q", out)
242
+	}
243
+
239 244
 }
240 245
 
241 246
 func (s *DockerSuite) TestPsListContainersFilterID(c *check.C) {