Skip to content

Commit

Permalink
Fix escape-keys by preserving input if invalid
Browse files Browse the repository at this point in the history
Currently, using a custom detach key with an invalid sequence, eats a
part of the sequence, making it weird and difficult to enter some key
sequence.

This fixes by keeping the input read when trying to see if it's the key
sequence or not, and "writing" then is the key sequence is not the right
one, preserving the initial input.

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester committed May 24, 2016
1 parent bf7bae9 commit 0fb6190
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
13 changes: 11 additions & 2 deletions container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ func copyEscapable(dst io.Writer, src io.ReadCloser, keys []byte) (written int64
nr, er := src.Read(buf)
if nr > 0 {
// ---- Docker addition
preservBuf := []byte{}
for i, key := range keys {
preservBuf = append(preservBuf, buf[0:nr]...)
if nr != 1 || buf[0] != key {
break
}
Expand All @@ -496,8 +498,15 @@ func copyEscapable(dst io.Writer, src io.ReadCloser, keys []byte) (written int64
}
nr, er = src.Read(buf)
}
// ---- End of docker
nw, ew := dst.Write(buf[0:nr])
var nw int
var ew error
if len(preservBuf) > 0 {
nw, ew = dst.Write(preservBuf)
nr = len(preservBuf)
} else {
// ---- End of docker
nw, ew = dst.Write(buf[0:nr])
}
if nw > 0 {
written += int64(nw)
}
Expand Down
49 changes: 49 additions & 0 deletions integration-cli/docker_cli_run_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,55 @@ func (s *DockerSuite) TestRunAttachDetachKeysOverrideConfig(c *check.C) {
c.Assert(running, checker.Equals, "true", check.Commentf("expected container to still be running"))
}

func (s *DockerSuite) TestRunAttachInvalidDetachKeySequencePreserved(c *check.C) {
name := "attach-detach"
keyA := []byte{97}
keyB := []byte{98}

dockerCmd(c, "run", "--name", name, "-itd", "busybox", "cat")

cmd := exec.Command(dockerBinary, "attach", "--detach-keys='a,b,c'", name)
stdout, err := cmd.StdoutPipe()
if err != nil {
c.Fatal(err)
}
cpty, tty, err := pty.Open()
if err != nil {
c.Fatal(err)
}
defer cpty.Close()
cmd.Stdin = tty
if err := cmd.Start(); err != nil {
c.Fatal(err)
}
c.Assert(waitRun(name), check.IsNil)

// Invalid escape sequence aba, should print aba in output
if _, err := cpty.Write(keyA); err != nil {
c.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
if _, err := cpty.Write(keyB); err != nil {
c.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
if _, err := cpty.Write(keyA); err != nil {
c.Fatal(err)
}
time.Sleep(100 * time.Millisecond)
if _, err := cpty.Write([]byte("\n")); err != nil {
c.Fatal(err)
}

out, err := bufio.NewReader(stdout).ReadString('\n')
if err != nil {
c.Fatal(err)
}
if strings.TrimSpace(out) != "aba" {
c.Fatalf("expected 'aba', got %q", out)
}
}

// "test" should be printed
func (s *DockerSuite) TestRunWithCPUQuota(c *check.C) {
testRequires(c, cpuCfsQuota)
Expand Down

0 comments on commit 0fb6190

Please sign in to comment.