Skip to content

Commit c600cfa

Browse files
authored
Merge pull request moby#27901 from ripcurld00d/load_stdin_valid
Validate docker-load receives a tar file
2 parents a13fc3f + 4426189 commit c600cfa

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

cli/command/image/load.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package image
22

33
import (
4+
"fmt"
45
"io"
56
"os"
67

@@ -49,6 +50,13 @@ func runLoad(dockerCli *command.DockerCli, opts loadOptions) error {
4950
defer file.Close()
5051
input = file
5152
}
53+
54+
// To avoid getting stuck, verify that a tar file is given either in
55+
// the input flag or through stdin and if not display an error message and exit.
56+
if opts.input == "" && dockerCli.In().IsTerminal() {
57+
return fmt.Errorf("requested load from stdin, but stdin is empty")
58+
}
59+
5260
if !dockerCli.Out().IsTerminal() {
5361
opts.quiet = true
5462
}

integration-cli/docker_cli_save_load_unix_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
package main
44

55
import (
6+
"context"
67
"fmt"
78
"io/ioutil"
89
"os"
910
"os/exec"
1011
"strings"
12+
"time"
1113

1214
"github.com/docker/docker/pkg/integration/checker"
1315
"github.com/go-check/check"
@@ -86,3 +88,22 @@ func (s *DockerSuite) TestSaveAndLoadWithProgressBar(c *check.C) {
8688
expected := fmt.Sprintf("The image %s:latest already exists, renaming the old one with ID", name)
8789
c.Assert(out, checker.Contains, expected)
8890
}
91+
92+
// fail because load didn't receive data from stdin
93+
func (s *DockerSuite) TestLoadNoStdinFail(c *check.C) {
94+
pty, tty, err := pty.Open()
95+
c.Assert(err, check.IsNil)
96+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
97+
defer cancel()
98+
cmd := exec.CommandContext(ctx, dockerBinary, "load")
99+
cmd.Stdin = tty
100+
cmd.Stdout = tty
101+
cmd.Stderr = tty
102+
c.Assert(cmd.Run(), check.NotNil) // docker-load should fail
103+
104+
buf := make([]byte, 1024)
105+
106+
n, err := pty.Read(buf)
107+
c.Assert(err, check.IsNil) //could not read tty output
108+
c.Assert(string(buf[:n]), checker.Contains, "requested load from stdin, but stdin is empty")
109+
}

0 commit comments

Comments
 (0)