From 216dbb368e4f65bb933a8470a0ea51725132ce72 Mon Sep 17 00:00:00 2001 From: Taylor McIntyre Date: Tue, 10 Nov 2026 11:17:48 -0500 Subject: [PATCH] ipn/ipnlocal: honor hidden exit node picker policy Clear user-selected exit nodes when ExitNodesPicker is set to hide, unless an exit node is forced by policy. Reject preference edits that select an exit node while the picker is hidden. Updates tailscale/corp#51036 Updates tailscale/corp#51102 Signed-off-by: Taylor McIntyre --- ipn/ipnlocal/local.go | 31 ++++++++- ipn/ipnlocal/local_test.go | 126 +++++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 2 deletions(-) diff --git a/ipn/ipnlocal/local.go b/ipn/ipnlocal/local.go index f97b6d2d5..9e8f8b169 100644 --- a/ipn/ipnlocal/local.go +++ b/ipn/ipnlocal/local.go @@ -2374,7 +2374,25 @@ func (b *LocalBackend) applyExitNodeSysPolicyLocked(prefs *ipn.Prefs) (anyChange } syncs.RequiresMutex(&b.mu) - if exitNodeIDStr, _ := b.polc.GetString(pkey.ExitNodeID, ""); exitNodeIDStr != "" { + pickerVisibility, _ := b.polc.GetString(pkey.ExitNodeMenuVisibility, "") + exitNodeIDStr, _ := b.polc.GetString(pkey.ExitNodeID, "") + exitNodeIPStr, _ := b.polc.GetString(pkey.ExitNodeIP, "") + + // A forced ID or IP takes precedence over the picker visibility policy. + if pickerVisibility == "hide" && exitNodeIDStr == "" && exitNodeIPStr == "" { + if prefs.ExitNodeID != "" || + prefs.ExitNodeIP.IsValid() || + prefs.AutoExitNode != "" || + prefs.InternalExitNodePrior != "" { + prefs.ExitNodeID = "" + prefs.ExitNodeIP = netip.Addr{} + prefs.AutoExitNode = "" + prefs.InternalExitNodePrior = "" + anyChange = true + } + } + + if exitNodeIDStr != "" { exitNodeID := tailcfg.StableNodeID(exitNodeIDStr) // Try to parse the policy setting value as an "auto:"-prefixed [ipn.ExitNodeExpression], @@ -2415,7 +2433,7 @@ func (b *LocalBackend) applyExitNodeSysPolicyLocked(prefs *ipn.Prefs) (anyChange prefs.ExitNodeIP = netip.Addr{} anyChange = true } - } else if exitNodeIPStr, _ := b.polc.GetString(pkey.ExitNodeIP, ""); exitNodeIPStr != "" { + } else if exitNodeIPStr != "" { if prefs.AutoExitNode != "" { prefs.AutoExitNode = "" // mutually exclusive with ExitNodeIP anyChange = true @@ -5507,6 +5525,15 @@ func (b *LocalBackend) EditPrefsAs(mp *ipn.MaskedPrefs, actor ipnauth.Actor) (ip // // b.mu must be held. func (b *LocalBackend) checkEditPrefsAccessLocked(actor ipnauth.Actor, prefs ipn.PrefsView, mp *ipn.MaskedPrefs) error { + pickerVisibility, _ := b.polc.GetString(pkey.ExitNodeMenuVisibility, "") + selectingExitNode := + (mp.ExitNodeIDSet && mp.ExitNodeID != "") || + (mp.ExitNodeIPSet && mp.ExitNodeIP.IsValid()) || + (mp.AutoExitNodeSet && mp.AutoExitNode != "") + if pickerVisibility == "hide" && selectingExitNode { + return fmt.Errorf("exit node selection is disabled by system policy") + } + syncs.RequiresMutex(&b.mu) var errs []error diff --git a/ipn/ipnlocal/local_test.go b/ipn/ipnlocal/local_test.go index bfe4a25a7..8e3ccd8d5 100644 --- a/ipn/ipnlocal/local_test.go +++ b/ipn/ipnlocal/local_test.go @@ -5256,6 +5256,69 @@ func TestApplySysPolicy(t *testing.T) { pkey.CheckUpdates: "never", }, }, + { + name: "hidden-exit-node-picker-clears-user-exit-node-id", + prefs: ipn.Prefs{ + ExitNodeID: "user-node", + }, + wantAnyChange: true, + stringPolicies: map[pkey.Key]string{ + pkey.ExitNodeMenuVisibility: "hide", + }, + }, + { + name: "hidden-exit-node-picker-clears-user-exit-node-ip", + prefs: ipn.Prefs{ + ExitNodeIP: netip.MustParseAddr("100.100.1.1"), + }, + wantAnyChange: true, + stringPolicies: map[pkey.Key]string{ + pkey.ExitNodeMenuVisibility: "hide", + }, + }, + { + name: "hidden-exit-node-picker-clears-auto-exit-node-and-prior", + prefs: ipn.Prefs{ + AutoExitNode: "any", + InternalExitNodePrior: "prior-node", + }, + wantAnyChange: true, + stringPolicies: map[pkey.Key]string{ + pkey.ExitNodeMenuVisibility: "hide", + }, + }, + { + name: "hidden-exit-node-picker-with-no-selection", + stringPolicies: map[pkey.Key]string{ + pkey.ExitNodeMenuVisibility: "hide", + }, + }, + { + name: "hidden-exit-node-picker-preserves-forced-exit-node-id", + prefs: ipn.Prefs{ + ExitNodeID: "forced-node", + }, + wantPrefs: ipn.Prefs{ + ExitNodeID: "forced-node", + }, + stringPolicies: map[pkey.Key]string{ + pkey.ExitNodeMenuVisibility: "hide", + pkey.ExitNodeID: "forced-node", + }, + }, + { + name: "hidden-exit-node-picker-preserves-forced-exit-node-ip", + prefs: ipn.Prefs{ + ExitNodeIP: netip.MustParseAddr("100.100.1.1"), + }, + wantPrefs: ipn.Prefs{ + ExitNodeIP: netip.MustParseAddr("100.100.1.1"), + }, + stringPolicies: map[pkey.Key]string{ + pkey.ExitNodeMenuVisibility: "hide", + pkey.ExitNodeIP: "100.100.1.1", + }, + }, } for _, tt := range tests { @@ -5318,6 +5381,59 @@ func TestApplySysPolicy(t *testing.T) { } } +func TestCheckEditPrefsAccessHiddenExitNodePicker(t *testing.T) { + tests := []struct { + name string + mp *ipn.MaskedPrefs + }{ + { + name: "exit-node-id", + mp: &ipn.MaskedPrefs{ + ExitNodeIDSet: true, + ExitNodeID: "user-node", + }, + }, + { + name: "exit-node-ip", + mp: &ipn.MaskedPrefs{ + ExitNodeIPSet: true, + ExitNodeIP: netip.MustParseAddr("100.100.1.1"), + }, + }, + { + name: "auto-exit-node", + mp: &ipn.MaskedPrefs{ + AutoExitNodeSet: true, + AutoExitNode: "any", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var polc policytest.Config + polc.Set(pkey.ExitNodeMenuVisibility, "hide") + + sys := tsd.NewSystem() + sys.PolicyClient.Set(polc) + lb := newTestLocalBackendWithSys(t, sys) + + lb.mu.Lock() + defer lb.mu.Unlock() + + prefs := ipn.Prefs{} + err := lb.checkEditPrefsAccessLocked( + nil, + prefs.View(), + tt.mp, + ) + if err == nil || err.Error() != "exit node selection is disabled by system policy" { + t.Fatalf("checkEditPrefsAccessLocked error = %v; want exit-node-picker policy error", err) + } + }) + } +} + func TestPreferencePolicyInfo(t *testing.T) { tests := []struct { name string @@ -8563,6 +8679,16 @@ func TestUpdatePrefsOnSysPolicyChange(t *testing.T) { stringSettings: []source.TestSetting[string]{source.TestSettingOf(pkey.ExitNodeID, "foo")}, want: wantPrefsChanges(fieldChange{"ExitNodeID", tailcfg.StableNodeID("foo")}), }, + { + name: "ExitNodesPicker/Hide", + initialPrefs: &ipn.Prefs{ExitNodeID: "foo"}, + stringSettings: []source.TestSetting[string]{ + source.TestSettingOf(pkey.ExitNodeMenuVisibility, "hide"), + }, + want: wantPrefsChanges( + fieldChange{"ExitNodeID", tailcfg.StableNodeID("")}, + ), + }, { name: "EnableRunExitNode", stringSettings: []source.TestSetting[string]{source.TestSettingOf(pkey.EnableRunExitNode, "always")},