diff --git a/feature/captiveportal/access.go b/feature/captiveportal/access.go index 8b2e19601..2ded4d803 100644 --- a/feature/captiveportal/access.go +++ b/feature/captiveportal/access.go @@ -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. diff --git a/feature/captiveportal/access_test.go b/feature/captiveportal/access_test.go index 700ff4a5d..d911fc62b 100644 --- a/feature/captiveportal/access_test.go +++ b/feature/captiveportal/access_test.go @@ -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) { diff --git a/feature/captiveportal/captiveportal.go b/feature/captiveportal/captiveportal.go index 686835f4d..ff27e6c29 100644 --- a/feature/captiveportal/captiveportal.go +++ b/feature/captiveportal/captiveportal.go @@ -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.