Skip to content

feat: add support for X11 forwarding #7205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix piping
  • Loading branch information
kylecarbs committed Apr 21, 2023
commit 9c949f8ea48575d4ea5c1d7b2273e66321f7605f
8 changes: 8 additions & 0 deletions agent/agentssh/x11.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (s *Server) x11Handler(ctx ssh.Context, x11 ssh.X11) bool {

go func() {
defer s.trackListener(listener, false)
handledFirstConnection := false

for {
conn, err := listener.Accept()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x11.SingleConnection is not honored

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Expand All @@ -71,6 +72,13 @@ func (s *Server) x11Handler(ctx ssh.Context, x11 ssh.X11) bool {
s.logger.Warn(ctx, "failed to accept X11 connection", slog.Error(err))
return
}
if x11.SingleConnection && handledFirstConnection {
s.logger.Warn(ctx, "X11 connection rejected because single connection is enabled")
_ = conn.Close()
continue
}
handledFirstConnection = true

unixConn, ok := conn.(*net.UnixConn)
if !ok {
s.logger.Warn(ctx, fmt.Sprintf("failed to cast connection to UnixConn. got: %T", conn))
Expand Down
10 changes: 8 additions & 2 deletions agent/agentssh/x11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ func TestServer_X11(t *testing.T) {
require.NoError(t, err)

x11Chans := c.HandleChannelOpen("x11")
payload := "hello world"
require.Eventually(t, func() bool {
conn, err := net.Dial("unix", filepath.Join(dir, "X0"))
if err == nil {
_, err = conn.Write([]byte(payload))
assert.NoError(t, err)
_ = conn.Close()
}
return err == nil
Expand All @@ -80,9 +83,12 @@ func TestServer_X11(t *testing.T) {
ch, reqs, err := x11.Accept()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if this also checked that reads and writes were piped properly like other listener tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Fixed!

require.NoError(t, err)
go gossh.DiscardRequests(reqs)
err = ch.Close()
got := make([]byte, len(payload))
_, err = ch.Read(got)
require.NoError(t, err)
s.Close()
assert.Equal(t, payload, string(got))
_ = ch.Close()
_ = s.Close()
<-done

// Ensure the Xauthority file was written!
Expand Down