|
| 1 | +package ssh |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +// Test_proxyDirectWithIO_serverClosesBeforeStdin reproduces smallstep/cli#1641: |
| 14 | +// when the server closes the connection before the client has closed stdin, the |
| 15 | +// proxycommand must still return promptly. Previously it would block in |
| 16 | +// wg.Wait() forever because the stdin->conn goroutine stayed blocked reading a |
| 17 | +// stdin that never reaches EOF (the ssh client keeps it open until the |
| 18 | +// proxycommand exits). |
| 19 | +func Test_proxyDirectWithIO_serverClosesBeforeStdin(t *testing.T) { |
| 20 | + ln, err := net.Listen("tcp", "127.0.0.1:0") |
| 21 | + require.NoError(t, err) |
| 22 | + defer ln.Close() |
| 23 | + |
| 24 | + // Server sends some data and immediately closes the connection. |
| 25 | + go func() { |
| 26 | + conn, err := ln.Accept() |
| 27 | + if err != nil { |
| 28 | + return |
| 29 | + } |
| 30 | + conn.Write([]byte("hello")) |
| 31 | + conn.Close() |
| 32 | + }() |
| 33 | + |
| 34 | + host, port, err := net.SplitHostPort(ln.Addr().String()) |
| 35 | + require.NoError(t, err) |
| 36 | + |
| 37 | + // stdin that never reaches EOF, simulating the ssh client keeping the |
| 38 | + // proxycommand's stdin open for the lifetime of the session. |
| 39 | + stdinR, stdinW := io.Pipe() |
| 40 | + defer stdinW.Close() // write end intentionally left open during the call |
| 41 | + |
| 42 | + var stdout bytes.Buffer |
| 43 | + done := make(chan error, 1) |
| 44 | + go func() { |
| 45 | + done <- proxyDirectWithIO(host, port, stdinR, &stdout) |
| 46 | + }() |
| 47 | + |
| 48 | + select { |
| 49 | + case err := <-done: |
| 50 | + require.NoError(t, err) |
| 51 | + require.Equal(t, "hello", stdout.String()) |
| 52 | + case <-time.After(5 * time.Second): |
| 53 | + t.Fatal("proxyDirectWithIO did not return after the server closed the connection") |
| 54 | + } |
| 55 | +} |
0 commit comments