feature/captiveportal: report access to the captive portal in the health warning

The captive portal warning offers the login page's URL, but a GUI can't
tell whether opening it will work, or whether the user has to turn off
their exit node first.

Add the portal-access arg to the warning. It is "available" while the
login page's host is reached outside the tunnel, "active" while the
captive portal is being visited, and unset when no access to it is
provided. The warning is updated as that changes, and its text says
that the login page can be opened without disconnecting Tailscale, or
that websites opened while logging in may be reached outside of it.

Signed-off-by: Nick Khyl <nickk@tailscale.com>
This commit is contained in:
Nick Khyl committed 2026-11-30 06:46:32 +00:00
1 parent 93ad0d862a
commit 1e7874b554
3 files changed
+136 -4

No files matched your search

+23
View File
@@ -173,6 +173,26 @@ func (a *portalAccess) alwaysOnPolicy() (allowed, audit bool) {
return ok, ok
}
// state returns the [argPortalAccess] value for the current state:
// [portalAccessActive] during a visit, [portalAccessAvailable] if an address
// of the login page's host is reached outside the tunnel, or "" otherwise.
func (a *portalAccess) state() string {
a.addrsMu.Lock()
defer a.addrsMu.Unlock()
if !a.active {
return ""
}
if a.visiting {
return portalAccessActive
}
for _, pa := range a.addrs {
if pa.portal {
return portalAccessAvailable
}
}
return ""
}
// setPortal begins providing access to the captive portal with the given
// login page, unless disabled or not supported by this node's configuration,
// or updates the login page if it already does.
@@ -510,6 +530,7 @@ func (a *portalAccess) add(ip netip.Addr, ttl time.Duration, portal bool) {
metricAddrAdded.Add(1)
if portal {
a.e.logf("captive portal access: login page address %v is reached outside the tunnel", ip)
a.e.updateWarning()
}
}
}
@@ -646,6 +667,7 @@ func (a *portalAccess) startVisitLocked(d time.Duration) {
a.useLinkResolversLocked(true)
a.visitTimer = time.AfterFunc(d, a.visitOp.Do)
a.e.updateWarning()
}
// endVisitLocked ends the visit, if any, and forgets the addresses
@@ -673,6 +695,7 @@ func (a *portalAccess) endVisitLocked() {
// Setting it again closes the connections proxied during the visit,
// other than those to the login page's host.
a.egress.SetDirectEgressIPFunc(a.contains, a.noteConn)
a.e.updateWarning()
}
// addrRejection is why an address may not be reached outside the tunnel.
+49
View File
@@ -12,6 +12,7 @@ import (
"time"
"golang.org/x/net/dns/dnsmessage"
"tailscale.com/health"
"tailscale.com/net/dns"
"tailscale.com/net/dns/resolver"
"tailscale.com/net/netmon"
@@ -139,6 +140,45 @@ func (env *testEnv) connectTo(ns *fakeNetstack, addr string) bool {
return closeConn != nil
}
// accessArg returns the warning's [argPortalAccess].
func (env *testEnv) accessArg() string {
return env.ht.CurrentState().Warnings[captivePortalWarnable.Code].Args[argPortalAccess]
}
func TestWarningText(t *testing.T) {
const text = "This network requires you to log in using your web browser."
for access, want := range map[string]string{
"": text,
portalAccessAvailable: text + " You can open the login page without disconnecting Tailscale.",
portalAccessActive: text + " While you log in, websites you open may be reached outside of Tailscale.",
} {
args := health.Args{argPortalURL: "http://portal.example.com/"}
if access != "" {
args[argPortalAccess] = access
}
if got := captivePortalWarnable.Text(args); got != want {
t.Errorf("text with %s=%q is %q; want %q", argPortalAccess, access, got, want)
}
}
}
// TestPortalAccessArgUnset verifies that the warning doesn't claim that
// the login page can be opened when no access to it is provided.
func TestPortalAccessArgUnset(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
env := newTestEnv(t) // no DNS manager or netstack
env.e.maybeDetectOp.Wait()
env.e.reportCaptivePortal(true, "http://198.51.100.1/login")
args := env.ht.CurrentState().Warnings[captivePortalWarnable.Code].Args
if got, ok := args[argPortalAccess]; ok {
t.Errorf("warning's %s = %q; want unset", argPortalAccess, got)
}
if got, want := args[argPortalURL], "http://198.51.100.1/login"; got != want {
t.Errorf("warning's %s = %q; want %q", argPortalURL, got, want)
}
})
}
func TestPortalAccessVisit(t *testing.T) {
synctest.Test(t, func(t *testing.T) {
env, ns := newAccessTestEnv(t, nil)
@@ -149,6 +189,9 @@ func TestPortalAccessVisit(t *testing.T) {
if ns.directEgress == nil || !a.has(portalIP) {
t.Fatal("the login page's address isn't reached outside the tunnel")
}
if got := env.accessArg(); got != portalAccessAvailable {
t.Errorf("warning's %s = %q; want %q", argPortalAccess, got, portalAccessAvailable)
}
// Until the captive portal is visited, only its own host counts.
a.observeDNSResponse(linkAnswer(t, "www.example.org.", 300, addrsOf("www.example.org.", "203.0.113.9")...))
@@ -171,6 +214,9 @@ func TestPortalAccessVisit(t *testing.T) {
if !env.connectTo(ns, portalIP+":8880") || !a.isVisiting() {
t.Fatal("a connection to the captive portal didn't start a visit")
}
if got := env.accessArg(); got != portalAccessActive {
t.Errorf("warning's %s = %q during a visit; want %q", argPortalAccess, got, portalAccessActive)
}
a.observeDNSResponse(linkAnswer(t, "www.example.org.", 300, addrsOf("www.example.org.", "203.0.113.9")...))
if !a.has("203.0.113.9") {
t.Fatal("another name's address not added during a visit")
@@ -196,6 +242,9 @@ func TestPortalAccessVisit(t *testing.T) {
if !a.has(portalIP) {
t.Fatal("the login page's address was forgotten with the visit")
}
if got := env.accessArg(); got != portalAccessAvailable {
t.Errorf("warning's %s = %q after a visit; want %q", argPortalAccess, got, portalAccessAvailable)
}
env.e.reportCaptivePortal(false, "")
if ns.directEgress != nil || a.has(portalIP) {
+64 -4
View File
@@ -49,8 +49,17 @@ var captivePortalWarnable = health.Register(&health.Warnable{
Code: "captive-portal-detected",
Title: "Captive portal detected",
// High severity, because captive portals block all traffic and require user intervention.
Severity: health.SeverityHigh,
Text: health.StaticMessage("This network requires you to log in using your web browser."),
Severity: health.SeverityHigh,
Text: func(args health.Args) string {
const text = "This network requires you to log in using your web browser."
switch args[argPortalAccess] {
case portalAccessAvailable:
return text + " You can open the login page without disconnecting Tailscale."
case portalAccessActive:
return text + " While you log in, websites you open may be reached outside of Tailscale."
}
return text
},
ImpactsConnectivity: true,
PrimaryAction: func(args health.Args) *health.UnhealthyStateAction {
if args[argPortalURL] == "" {
@@ -63,6 +72,21 @@ var captivePortalWarnable = health.Register(&health.Warnable{
// argPortalURL provides captivePortalWarnable with the URL to log in to the captive portal.
const argPortalURL health.Arg = "portal-url"
// argPortalAccess tells captivePortalWarnable's consumers whether the login
// page can be opened as is. It is unset if it can't, such as when an exit
// node is in the way and has to be turned off first. See [portalAccess].
const argPortalAccess health.Arg = "portal-access"
// Values of [argPortalAccess].
const (
// portalAccessAvailable means that the login page's host
// is reached outside the tunnel.
portalAccessAvailable = "available"
// portalAccessActive means that the captive portal is being visited,
// so other hosts that logging in may involve are reached that way too.
portalAccessActive = "active"
)
// Extension is the captive portal detection extension.
// There is one per [ipnext.Host] (and hence per LocalBackend).
type Extension struct {
@@ -100,6 +124,12 @@ type Extension struct {
// access lets this machine reach a detected captive portal.
access portalAccess
// warningMu guards the fields below and serializes
// updates to the [captivePortalWarnable].
warningMu syncs.Mutex
warned bool // whether the warning is set
portalURL string // of the login page, if warned
onDetectionForTest func() // called at the start of each detection
}
@@ -249,7 +279,7 @@ func (e *Extension) scheduleCaptiveDetectionRetry() {
func (e *Extension) reportCaptivePortal(found bool, portalURL string) {
if !found {
if e.captivePortalDetected.Swap(false) {
e.health.SetHealthy(captivePortalWarnable)
e.setWarning(false, "")
e.access.end()
}
return
@@ -258,7 +288,37 @@ func (e *Extension) reportCaptivePortal(found bool, portalURL string) {
metricCaptivePortalDetected.Add(1)
}
e.access.setPortal(e.ctx, portalURL)
e.health.SetUnhealthy(captivePortalWarnable, health.Args{argPortalURL: portalURL})
e.setWarning(true, portalURL)
}
// setWarning sets or clears the [captivePortalWarnable].
func (e *Extension) setWarning(warned bool, portalURL string) {
e.warningMu.Lock()
defer e.warningMu.Unlock()
e.warned, e.portalURL = warned, portalURL
if !warned {
e.health.SetHealthy(captivePortalWarnable)
return
}
e.updateWarningLocked()
}
// updateWarning updates the [captivePortalWarnable], if set,
// with the current state of access to the captive portal.
func (e *Extension) updateWarning() {
e.warningMu.Lock()
defer e.warningMu.Unlock()
if e.warned {
e.updateWarningLocked()
}
}
func (e *Extension) updateWarningLocked() {
args := health.Args{argPortalURL: e.portalURL}
if state := e.access.state(); state != "" {
args[argPortalAccess] = state
}
e.health.SetUnhealthy(captivePortalWarnable, args)
}
// captiveDetectionEnabled reports whether captive portal detection is enabled.