Skip to content

Commit

Permalink
Prevent web ui SSH connections from leaking (#41501)
Browse files Browse the repository at this point in the history
The session closing in TerminalHandler.Close did not always close
established ssh sessions if the client closed the web socket
at any point between the new browser tab being opened and the session
being established. In these cases the SSH connection was leaked for
the duration of the Proxy process. To ensure sessions are always
terminated a check to see if the client closed the web socket was
added prior to writing the session metadata and after the shell
is established for the ssh session.
  • Loading branch information
rosstimothy committed May 14, 2024
1 parent aa3ec40 commit 8678485
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 2 deletions.
106 changes: 106 additions & 0 deletions lib/web/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9833,3 +9833,109 @@ func Test_consumeTokenForAPICall(t *testing.T) {
})
}
}

func TestWebSocketClosedBeforeSSHSessionCreated(t *testing.T) {
t.Parallel()
s := newWebSuiteWithConfig(t, webSuiteConfig{disableDiskBasedRecording: true})

ctx, cancel := context.WithCancel(s.ctx)
t.Cleanup(cancel)

pack := s.authPack(t, "foo")

req := TerminalRequest{
Server: s.node.ID(),
Login: pack.login,
Term: session.TerminalParams{
W: 100,
H: 100,
},
}

data, err := json.Marshal(req)
require.NoError(t, err)

u := url.URL{
Host: s.webServer.Listener.Addr().String(),
Scheme: client.WSS,
Path: "/v1/webapi/sites/-current-/connect/ws",
}

q := u.Query()
q.Set("params", string(data))
u.RawQuery = q.Encode()

header := http.Header{}
header.Add("Origin", "http://localhost")
for _, cookie := range pack.cookies {
header.Add("Cookie", cookie.String())
}

dialer := websocket.Dialer{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}

ws, resp, err := dialer.Dial(u.String(), header)
if err != nil {
var sb strings.Builder
sb.WriteString("websocket dial")
if resp != nil {
fmt.Fprintf(&sb, "; status code %v;", resp.StatusCode)
fmt.Fprintf(&sb, "headers: %v; body: ", resp.Header)
io.Copy(&sb, resp.Body)
}
require.NoError(t, err, sb.String())
}
require.NoError(t, resp.Body.Close())

require.NoError(t, makeAuthReqOverWS(ws, pack.session.Token))

wsClosedChan := make(chan struct{})

// Create a stream that closes the web socket when the server writes the session metadata
// to the client. At this point, the SSH connection to the target should be in flight but
// not yet established.
stream := NewTerminalStream(ctx, TerminalStreamConfig{
WS: ws,
Logger: utils.NewLogger(),
Handlers: map[string]WSHandlerFunc{
defaults.WebsocketSessionMetadata: func(ctx context.Context, envelope Envelope) {
if envelope.Type != defaults.WebsocketSessionMetadata {
return
}

var sessResp siteSessionGenerateResponse
if err := json.Unmarshal([]byte(envelope.Payload), &sessResp); err != nil {
return
}

assert.NoError(t, ws.WriteControl(websocket.CloseMessage, nil, time.Now().Add(time.Second)))
close(wsClosedChan)
},
},
})
t.Cleanup(func() { require.NoError(t, stream.Close()) })

// Set a read deadline to unblock ReadAll below in the event of a bug
// preventing the ws from closing above.
require.NoError(t, stream.SetReadDeadline(time.Now().Add(30*time.Second)))

// Wait for the web socket to be closed above.
select {
case <-wsClosedChan:
case <-time.After(10 * time.Second):
t.Fatal("timed out waiting for session metadata")
}

// Validate that the SSH connection is terminated in response to the WS closing.
require.Eventually(t, func() bool {
return s.node.ActiveConnections() == 0
}, 10*time.Second, 100*time.Millisecond)

// Validate that reading nothing was permitted.
out, err := io.ReadAll(stream)
require.NoError(t, err)
require.Empty(t, out)
}
24 changes: 22 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 Expand Up @@ -1095,6 +1111,10 @@ func (t *WSStream) writeError(msg string) {
}
}

func (t *WSStream) SetReadDeadline(deadline time.Time) error {
return t.ws.SetReadDeadline(deadline)
}

func (t *WSStream) processMessages(ctx context.Context) {
defer func() {
t.close()
Expand Down

0 comments on commit 8678485

Please sign in to comment.