Skip to content

Commit

Permalink
Fixes withheld TDP messages in proxy (#42578)
Browse files Browse the repository at this point in the history
* Fixes withheld TDP messages in proxy

There exists only a single Handler per proxy, so the h.withheldMessages
slice was shared commonly between desktop sessions. Since it was not
designed with concurrency in mind, it was causing issues.

This commit fixes the issue by removing the h.withheldMessages slice and
replacing it with a per-request slice.

* nil out slice
  • Loading branch information
Isaiah Becker-Mayer authored Jun 10, 2024
1 parent 42a63be commit f194abd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
5 changes: 0 additions & 5 deletions lib/web/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ import (
"github.com/gravitational/teleport/lib/secret"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/session"
"github.com/gravitational/teleport/lib/srv/desktop/tdp"
"github.com/gravitational/teleport/lib/tlsca"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/web/app"
Expand Down Expand Up @@ -188,10 +187,6 @@ type Handler struct {
// an authenticated websocket so unauthenticated sockets dont get left
// open.
wsIODeadline time.Duration

// withheldMessages is a list of any messages that came from the browser which were
// withheld while the user was performing MFA.
withheldMessages []tdp.Message
}

// HandlerOption is a functional argument - an option that can be passed
Expand Down
16 changes: 12 additions & 4 deletions lib/web/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,10 @@ func (h *Handler) createDesktopConnection(
return sendTDPError(err)
}

// Holds any messages withheld while issuing certs.
var withheld []tdp.Message
// Issue certificate for the user/desktop combination and perform MFA ceremony if required.
certs, err := h.issueCerts(ctx, ws, sctx, mfaRequired, certsReq)
certs, err := h.issueCerts(ctx, ws, sctx, mfaRequired, certsReq, &withheld)
if err != nil {
return sendTDPError(err)
}
Expand Down Expand Up @@ -222,11 +224,15 @@ func (h *Handler) createDesktopConnection(
if err != nil {
return sendTDPError(err)
}
for _, msg := range h.withheldMessages {
for _, msg := range withheld {
log.Debugf("Sending withheld message: %v", msg)
if err := tdpConn.WriteMessage(msg); err != nil {
return sendTDPError(err)
}
}
// nil out the slice so we don't hang on to these messages
// for the rest of the connection
withheld = nil

// proxyWebsocketConn hangs here until connection is closed
handleProxyWebsocketConnErr(
Expand Down Expand Up @@ -314,9 +320,10 @@ func (h *Handler) issueCerts(
sctx *SessionContext,
mfaRequired bool,
certsReq *proto.UserCertsRequest,
withheld *[]tdp.Message,
) (certs *proto.Certs, err error) {
if mfaRequired {
certs, err = h.performMFACeremony(ctx, ws, sctx, certsReq)
certs, err = h.performMFACeremony(ctx, ws, sctx, certsReq, withheld)
if err != nil {
return nil, trace.Wrap(err)
}
Expand Down Expand Up @@ -363,6 +370,7 @@ func (h *Handler) performMFACeremony(
ws *websocket.Conn,
sctx *SessionContext,
certsReq *proto.UserCertsRequest,
withheld *[]tdp.Message,
) (_ *proto.Certs, err error) {
ctx, span := h.tracer.Start(ctx, "desktop/performMFACeremony")
defer func() {
Expand Down Expand Up @@ -413,7 +421,7 @@ func (h *Handler) performMFACeremony(
if err != nil {
return nil, trace.Wrap(err)
}
h.withheldMessages = append(h.withheldMessages, msg)
*withheld = append(*withheld, msg)
continue
}

Expand Down

0 comments on commit f194abd

Please sign in to comment.