From f5fdb18b2e1b008f7da1e8838175228efdda07c4 Mon Sep 17 00:00:00 2001 From: James Tucker Date: Mon, 26 Oct 2026 20:47:17 +0000 Subject: [PATCH] ssh/tailssh: give delegated action fetches one timeout owner Let fetchSSHAction own the per-fetch deadline and pass the connection cancellation context directly from clientAuth. Remove the duplicate timeout and its deferred cancellation inside the action loop. Derive the longer transport watchdog from the fetch timeout, keeping both durations unchanged. Use virtual time to test the fetch deadline across retries, earlier parent deadlines, and parent cancellation. Verify that a successful fetch releases its request context. Updates tailscale/corp#50380 Change-Id: I185958ca19566f04cbd68f33e4b1dad3d18aebb7 Signed-off-by: James Tucker --- ssh/tailssh/delegate_test.go | 97 ++++++++++++++++++++++++++++++++++++ ssh/tailssh/tailssh.go | 30 +++++------ 2 files changed, 112 insertions(+), 15 deletions(-) create mode 100644 ssh/tailssh/delegate_test.go diff --git a/ssh/tailssh/delegate_test.go b/ssh/tailssh/delegate_test.go new file mode 100644 index 000000000..c8c4a33c9 --- /dev/null +++ b/ssh/tailssh/delegate_test.go @@ -0,0 +1,97 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +//go:build (linux && !android) || (darwin && !ios) || freebsd || openbsd || plan9 + +package tailssh + +import ( + "context" + "errors" + "io" + "net/http" + "strings" + "testing" + "testing/synctest" + "time" +) + +type delegateBackend struct { + ipnLocalBackend + request func(*http.Request) (*http.Response, error) +} + +func (b *delegateBackend) DoNoiseRequest(req *http.Request) (*http.Response, error) { + return b.request(req) +} + +func TestFetchSSHActionTimeout(t *testing.T) { + for _, mode := range []string{"timeout", "retry-timeout", "parent-deadline", "parent-cancel"} { + t.Run(mode, func(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + start := time.Now() + wantDeadline := start.Add(delegateFetchTimeout) + wantElapsed := delegateFetchTimeout + wantErr := context.DeadlineExceeded + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + switch mode { + case "parent-deadline": + var stop context.CancelFunc + ctx, stop = context.WithTimeout(ctx, time.Minute) + defer stop() + wantElapsed = time.Minute + wantDeadline = start.Add(wantElapsed) + case "parent-cancel": + time.AfterFunc(time.Minute, cancel) + wantElapsed = time.Minute + wantErr = context.Canceled + } + calls := 0 + b := &delegateBackend{request: func(req *http.Request) (*http.Response, error) { + calls++ + if got, ok := req.Context().Deadline(); !ok || !got.Equal(wantDeadline) { + t.Errorf("request deadline = %v, %v; want %v", got, ok, wantDeadline) + } + if mode == "retry-timeout" { + return nil, errors.New("temporarily unavailable") + } + <-req.Context().Done() + return nil, req.Context().Err() + }} + c := &conn{srv: &server{lb: b, logf: t.Logf}} + if _, err := c.fetchSSHAction(ctx, "https://unused/action"); !errors.Is(err, wantErr) { + t.Errorf("fetchSSHAction error = %v, want %v", err, wantErr) + } + if got := time.Since(start); got != wantElapsed { + t.Errorf("fetch duration = %v, want %v", got, wantElapsed) + } + if calls == 0 || mode == "retry-timeout" && calls < 2 { + t.Errorf("request calls = %d", calls) + } + }) + }) + } +} + +func TestFetchSSHActionCancelsRequestOnSuccess(t *testing.T) { + var requestContext context.Context + b := &delegateBackend{request: func(req *http.Request) (*http.Response, error) { + requestContext = req.Context() + return &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(`{"Accept":true}`)), + }, nil + }} + c := &conn{srv: &server{lb: b, logf: t.Logf}} + action, err := c.fetchSSHAction(context.Background(), "https://unused/action") + if err != nil { + t.Fatal(err) + } + if !action.Accept { + t.Fatal("delegated action did not accept") + } + if err := requestContext.Err(); err != context.Canceled { + t.Errorf("request context error = %v, want context.Canceled", err) + } +} diff --git a/ssh/tailssh/tailssh.go b/ssh/tailssh/tailssh.go index 8a4f48185..7ae12a5f0 100644 --- a/ssh/tailssh/tailssh.go +++ b/ssh/tailssh/tailssh.go @@ -490,19 +490,16 @@ func (c *conn) clientAuth(cm ssh.ConnMetadata) (perms *ssh.Permissions, retErr e url := action.HoldAndDelegate - // The delegate wait is bounded by fetchSSHAction's own 30-minute - // context. Extend the login grace to cover it plus buffer, so - // the grace timer never kills a live approval flow. A wedged - // fetch still gets torn down at the raw socket. + // Allow the delegate fetch to reach its own timeout before + // login grace closes the transport. Keep the transport deadline + // in case the fetch does not return on context cancellation. c.extendLoginGrace(holdDelegateGrace) // Connection teardown also cancels the delegate fetch. - parent := c.preAuthCtx - if parent == nil { - parent = context.Background() + ctx := c.preAuthCtx + if ctx == nil { + ctx = context.Background() } - ctx, cancel := context.WithTimeout(parent, 30*time.Minute) - defer cancel() metricHolds.Add(1) url = c.expandDelegateURLLocked(url) @@ -921,11 +918,14 @@ func (c *conn) newSSHSession(s gliderssh.Session) *sshSession { } } -// holdDelegateGrace is how long the login-grace timer is extended for a -// conn waiting on a HoldAndDelegate approval. It is fetchSSHAction's own -// 30-minute cap plus buffer, so the grace timer never kills a live -// approval flow. A wedged fetch is still torn down at the socket. -const holdDelegateGrace = 35 * time.Minute +const ( + // delegateFetchTimeout bounds one delegated action fetch, including retries. + delegateFetchTimeout = 30 * time.Minute + + // holdDelegateGrace is the login-grace period while fetching a delegated + // action. It allows the fetch to time out before closing the transport. + holdDelegateGrace = delegateFetchTimeout + 5*time.Minute +) // armLoginGrace arms the pre-auth login-grace timer; on expiry the // connection is closed at the raw socket. The timer is stopped at auth @@ -1276,7 +1276,7 @@ func acceptEnvNarrowed(old, fresh []string) bool { } func (c *conn) fetchSSHAction(ctx context.Context, url string) (*tailcfg.SSHAction, error) { - ctx, cancel := context.WithTimeout(ctx, 30*time.Minute) + ctx, cancel := context.WithTimeout(ctx, delegateFetchTimeout) defer cancel() bo := backoff.NewBackoff("fetch-ssh-action", c.logf, 10*time.Second) for {