Skip to content
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

[v14] Prevent web ui SSH connections from leaking #41519

Merged
merged 1 commit into from
May 14, 2024
Merged
Changes from all commits
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
20 changes: 18 additions & 2 deletions lib/web/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,14 @@ func (t *TerminalHandler) makeClient(ctx context.Context, stream *TerminalStream
// to allow future window changes.
tc.OnShellCreated = func(s *tracessh.Session, c *tracessh.Client, _ io.ReadWriteCloser) (bool, error) {
t.stream.sessionCreated(s)

// The web session was closed by the client while the ssh connection was being established.
// Attempt to close the SSH session instead of proceeding with the window change request.
if t.closedByClient.Load() {
t.log.Debug("websocket was closed by client, terminating established ssh connection to host")
return false, trace.Wrap(s.Close())
}

if err := s.WindowChange(ctx, t.term.H, t.term.W); err != nil {
t.log.Error(err)
}
Expand Down Expand Up @@ -808,9 +816,17 @@ func (t *TerminalHandler) streamTerminal(ctx context.Context, tc *client.Telepor
t.stream.writeError(err.Error())
return
}

defer nc.Close()

// If the session was terminated by client while the connection to the host
// was being established, then return early before creating the shell. Any terminations
// by the client from here on out should either get caught in the OnShellCreated callback
// set on the [tc] or in [TerminalHandler.Close].
if t.closedByClient.Load() {
t.log.Debug("websocket was closed by client, aborting establishing ssh connection to host")
return
}

var beforeStart func(io.Writer)
if t.participantMode == types.SessionModeratorMode {
beforeStart = func(out io.Writer) {
Expand Down Expand Up @@ -850,7 +866,7 @@ func (t *TerminalHandler) streamTerminal(ctx context.Context, tc *client.Telepor
}

if err := t.stream.Close(); err != nil {
t.log.WithError(err).Error("Unable to send close event to web client.")
t.log.WithError(err).Error("Unable to close client web socket.")
return
}

Expand Down
Loading