Skip to content

Commit 9c949f8

Browse files
committed
Fix piping
1 parent 5ad8633 commit 9c949f8

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

agent/agentssh/x11.go

+8
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func (s *Server) x11Handler(ctx ssh.Context, x11 ssh.X11) bool {
6161

6262
go func() {
6363
defer s.trackListener(listener, false)
64+
handledFirstConnection := false
6465

6566
for {
6667
conn, err := listener.Accept()
@@ -71,6 +72,13 @@ func (s *Server) x11Handler(ctx ssh.Context, x11 ssh.X11) bool {
7172
s.logger.Warn(ctx, "failed to accept X11 connection", slog.Error(err))
7273
return
7374
}
75+
if x11.SingleConnection && handledFirstConnection {
76+
s.logger.Warn(ctx, "X11 connection rejected because single connection is enabled")
77+
_ = conn.Close()
78+
continue
79+
}
80+
handledFirstConnection = true
81+
7482
unixConn, ok := conn.(*net.UnixConn)
7583
if !ok {
7684
s.logger.Warn(ctx, fmt.Sprintf("failed to cast connection to UnixConn. got: %T", conn))

agent/agentssh/x11_test.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ func TestServer_X11(t *testing.T) {
6868
require.NoError(t, err)
6969

7070
x11Chans := c.HandleChannelOpen("x11")
71+
payload := "hello world"
7172
require.Eventually(t, func() bool {
7273
conn, err := net.Dial("unix", filepath.Join(dir, "X0"))
7374
if err == nil {
75+
_, err = conn.Write([]byte(payload))
76+
assert.NoError(t, err)
7477
_ = conn.Close()
7578
}
7679
return err == nil
@@ -80,9 +83,12 @@ func TestServer_X11(t *testing.T) {
8083
ch, reqs, err := x11.Accept()
8184
require.NoError(t, err)
8285
go gossh.DiscardRequests(reqs)
83-
err = ch.Close()
86+
got := make([]byte, len(payload))
87+
_, err = ch.Read(got)
8488
require.NoError(t, err)
85-
s.Close()
89+
assert.Equal(t, payload, string(got))
90+
_ = ch.Close()
91+
_ = s.Close()
8692
<-done
8793

8894
// Ensure the Xauthority file was written!

0 commit comments

Comments
 (0)