ssh/tailssh: keep sessions when a prompt-free rule expires

Check mode grants recently approved users a prompt-free Accept rule
with a short RuleExpires. When that rule expired, the connection's
policy recheck matched the base HoldAndDelegate rule instead. The
recheck read that as a policy edit and closed the connection, killing
live sessions when the prompt-free window lapsed.

Remember the rule that matched at authentication. If it has expired and
a hold rule matches instead, keep the connection. Re-authentication is
required only for new connections.

A policy edit that swaps a still-valid accept rule for a hold rule still
closes the connection. Recorder, identity, and accept-env checks still
run on the fallback path.

Updates tailscale/corp#50380

Signed-off-by: James Tucker <[email protected]>
This commit is contained in:
James Tucker committed 2026-10-28 19:06:07 +00:00
1 parent 3a38840ccc
commit 392c91bb86
3 files changed
+71 -9

No files matched your search

+23 -6
View File
@@ -30,12 +30,13 @@ func TestPolicyRecheckRestrictions(t *testing.T) {
src := netip.MustParseAddrPort("1.2.3.4:1234")
dst := netip.MustParseAddrPort("100.100.100.102:22")
tests := []struct {
name string
initial *tailcfg.SSHAction
old *tailcfg.SSHAction
fresh *tailcfg.SSHAction
oldEnv []string
want bool
name string
initial *tailcfg.SSHAction
old *tailcfg.SSHAction
fresh *tailcfg.SSHAction
oldEnv []string
latchExpired bool
want bool
}{
{
name: "duplicate-recorder-replaced",
@@ -137,6 +138,15 @@ func TestPolicyRecheckRestrictions(t *testing.T) {
fresh: hold,
want: true,
},
{
// The latched accept rule expired and the hold rule matched
// instead. Not a revocation: established sessions keep the
// grant and only new connections re-authenticate.
name: "accept-to-hold-on-rule-expiry",
old: &tailcfg.SSHAction{Accept: true},
fresh: hold,
latchExpired: true,
},
{
name: "unchanged",
old: &tailcfg.SSHAction{Accept: true},
@@ -155,6 +165,12 @@ func TestPolicyRecheckRestrictions(t *testing.T) {
localState: &localState{sshEnabled: true},
rule: newPolicyRule(tt.fresh, lu),
}
latched := newPolicyRule(tt.old, lu)
exp := time.Now().Add(time.Hour)
if tt.latchExpired {
exp = time.Now().Add(-time.Hour)
}
latched.RuleExpires = &exp
c := &conn{
srv: &server{
logf: t.Logf,
@@ -164,6 +180,7 @@ func TestPolicyRecheckRestrictions(t *testing.T) {
mappedLocalUser: lu.Username,
action0: initial,
finalAction: tt.old,
latchedRule: latched,
acceptEnv: tt.oldEnv,
info: &sshConnInfo{
sshUser: "alice",
+8 -2
View File
@@ -290,6 +290,7 @@ type conn struct {
action0 *tailcfg.SSHAction // set by clientAuth
finalAction *tailcfg.SSHAction // set by clientAuth
latchedRule *tailcfg.SSHRule // rule matched at auth; rechecks use it to tell rule expiry from a policy edit
info *sshConnInfo // set by setInfo (during clientAuth)
localUser *userMeta // set by clientAuth
@@ -425,7 +426,7 @@ func (c *conn) clientAuth(cm ssh.ConnMetadata) (perms *ssh.Permissions, retErr e
return nil, c.errBanner("failed to get connection info", err)
}
action, localUser, acceptEnv, _, result := c.evaluatePolicy()
action, localUser, acceptEnv, rule, result := c.evaluatePolicy()
switch result {
case accepted:
// do nothing
@@ -444,6 +445,7 @@ func (c *conn) clientAuth(cm ssh.ConnMetadata) (perms *ssh.Permissions, retErr e
}
c.action0 = action
c.latchedRule = rule
if action.Accept || action.HoldAndDelegate != "" {
// Immediately look up user information for purposes of generating
@@ -1163,7 +1165,11 @@ func (c *conn) materialPolicyChangeWithUserLookup(lookup func(string) (*userMeta
freshRecorders, freshFailure := recordingConfig(a, a)
if !a.Accept {
if c.action0.Accept {
return true, "rule changed from Accept to HoldAndDelegate", nil
if c.latchedRule == nil || !c.ruleExpired(c.latchedRule) {
return true, "rule changed from Accept to HoldAndDelegate", nil
}
// Rule expiry fallback, not a policy edit. The grant stands for
// the life of the conn; new connections re-authenticate.
}
// Keep the delegated grant, but compare any recording requirements
// inherited from the hold rule as well as its AcceptEnv above.
+40 -1
View File
@@ -42,6 +42,8 @@ type policyFlipper struct {
mu sync.Mutex
rule *tailcfg.SSHRule
// rules, when non-nil, is served by NetMap in place of rule.
rules []*tailcfg.SSHRule
onNoise func()
}
@@ -55,7 +57,9 @@ func (p *policyFlipper) setRule(r *tailcfg.SSHRule) {
func (p *policyFlipper) NetMap() *netmap.NetworkMap {
p.mu.Lock()
var policy *tailcfg.SSHPolicy
if p.rule != nil {
if p.rules != nil {
policy = &tailcfg.SSHPolicy{Rules: p.rules}
} else if p.rule != nil {
policy = &tailcfg.SSHPolicy{Rules: []*tailcfg.SSHRule{p.rule}}
}
p.mu.Unlock()
@@ -691,6 +695,41 @@ func TestSSHRuleExpiryRearmsOnFreshMatch(t *testing.T) {
})
}
// TestSSHRuleExpiryFallbackKeepsSession: prompt-free check-mode rules
// often have a short RuleExpires, with a base rule that requires a fresh
// check behind them. Expiration must only affect new sessions; an
// existing session keeps running when the accept rule lapses and
// evaluation falls back to the hold rule.
func TestSSHRuleExpiryFallbackKeepsSession(t *testing.T) {
lu := sessionTestUser(t)
exp := time.Now().Add(2 * time.Second)
acceptRule := newPolicyRule(&tailcfg.SSHAction{Accept: true}, lu)
acceptRule.RuleExpires = &exp
holdRule := newPolicyRule(&tailcfg.SSHAction{HoldAndDelegate: "https://unused/hold"}, lu)
p := &policyFlipper{
localState: &localState{sshEnabled: true, varRoot: t.TempDir()},
rules: []*tailcfg.SSHRule{acceptRule, holdRule},
}
_, client, _ := dialPolicyConn(t, p)
sess, err := client.NewSession()
if err != nil {
t.Fatalf("session: %v", err)
}
t.Cleanup(func() { sess.Close() })
if err := sess.Start("sleep 30"); err != nil {
t.Fatalf("session start: %v", err)
}
// The expiry timer fires 2s in. Wait past it: the conn must survive,
// and the hold rule has no expiry, so the recheck must not re-arm the
// timer either.
time.Sleep(6 * time.Second)
if sessionFails(client) {
t.Fatal("conn closed when the latched accept rule expired")
}
}
// TestSSHLoginGraceTimeExpires pins the pre-auth grace timer, in parity
// with sshd's LoginGraceTime. A conn that never completes auth is closed
// at the deadline, shortened for this test. Authed conns are unaffected;