Files
pipelines/scripts/web_access.py
T
alan ad6cdab418 Listen on loopback and tailnet by default; make the plain-HTTP LAN opt-in
The web proxy binds the wildcard and drops connections that are not
loopback or Tailscale at both ends. A LAN listener is opt-in (web.lan) and
refuses sign-in and signed-in pages over plain HTTP unless
web.lan_plain_login is set. Existing installs keep their bind address with
a warning. Without the LAN the mDNS advert carries only the tailnet and
public routes (lan=0).
2026-11-10 01:02:02 +00:00

114 lines
4.9 KiB
Python

"""Where the authenticated web proxy listens, as the native installer decides it.
`web.host` in the installation manifest is either "tailnet" (the default for
new installs: the proxy admits only loopback and this machine's Tailscale
addresses, see web/src/listenPolicy.js) or a literal IPv4 bind address (the
behaviour before; existing installs keep it, with a warning when it reaches
beyond loopback). A plain-HTTP LAN listener is opt-in with `web.lan`, and
signing in over it additionally needs `web.lan_plain_login`.
"""
import ipaddress
import platform
import shutil
import subprocess
from pathlib import Path
from urllib.parse import urlsplit
TAILNET = "tailnet"
FIREWALL = Path("/usr/libexec/ApplicationFirewall/socketfilterfw")
def resolve(host, lan, lan_plain_login, supplied):
"""The web settings for this install and the warnings to print.
host/lan/lan_plain_login are the parsed flags after the saved settings were
applied (None when neither gave a value); supplied is the set of flag names
on the command line."""
if lan and "--web-lan" in supplied and "--web-host" not in supplied:
host = TAILNET # opting into the LAN listener selects the new model
host = host or TAILNET
if host != TAILNET:
try:
ipaddress.IPv4Address(host)
except ValueError:
raise ValueError("Invalid --web-host; use 'tailnet' (loopback + Tailscale) "
"or an IPv4 address such as 127.0.0.1") from None
if lan and "--web-lan" in supplied:
raise ValueError("--web-lan applies to --web-host tailnet; a literal --web-host binds that address only")
lan = bool(lan) and host == TAILNET
plain = bool(lan_plain_login) and lan
warnings = []
if host != TAILNET and not ipaddress.IPv4Address(host).is_loopback:
warnings.append(
f"WARNING: the web proxy listens on {host} over plain HTTP, so on an untrusted network "
"(cafe or hotel Wi-Fi) the web password and the 30-day session cookie cross it unencrypted. "
"Recommended: ./install --web-host tailnet (loopback and Tailscale only; add --web-lan "
"for a trusted LAN).")
if plain:
warnings.append(
"WARNING: web.lan_plain_login is on: the web password and session cookie are accepted over "
"plain HTTP on the LAN. Use it only on a network you trust; prefer the tailnet HTTPS address.")
return {"host": host, "lan": lan, "lan_plain_login": plain}, warnings
def lan_sign_in(web):
"""Whether a browser on the LAN can sign in over plain HTTP."""
if web.get("host") == TAILNET:
return web.get("lan") is True and web.get("lan_plain_login") is True
return not ipaddress.IPv4Address(web["host"]).is_loopback
def tailnet_ipv4(runner=subprocess.run):
"""This machine's Tailscale IPv4 address, or None."""
tailscale = shutil.which("tailscale")
if not tailscale:
return None
try:
result = runner([tailscale, "ip", "-4"], capture_output=True, text=True, timeout=10)
except (OSError, subprocess.SubprocessError):
return None
if result.returncode:
return None
for line in (result.stdout or "").split():
try:
if ipaddress.IPv4Address(line) in ipaddress.IPv4Network("100.64.0.0/10"):
return line
except ValueError:
continue
return None
def default_url_host(web, lan_ip, tailnet_ip):
"""The host of the default web URL when PIPELINES_WEB_PUBLIC_URL is unset."""
if web["host"] == TAILNET:
return lan_ip if lan_sign_in(web) else (tailnet_ip or "127.0.0.1")
return lan_ip if web["host"] == "0.0.0.0" else web["host"]
def public_url_warning(url, web):
"""A recorded plain-HTTP LAN web URL that the proxy now refuses."""
parsed = urlsplit(url or "")
try:
address = ipaddress.ip_address(parsed.hostname or "")
except ValueError:
return None
if (parsed.scheme == "http" and not address.is_loopback
and address not in ipaddress.ip_network("100.64.0.0/10") and not lan_sign_in(web)):
return (f"WARNING: the recorded web URL {url} is a plain-HTTP LAN address that no longer accepts "
"sign-in. Set PIPELINES_WEB_PUBLIC_URL to the tailnet HTTPS address and rerun ./install.")
return None
def firewall_note(runner=subprocess.run):
"""A read-only report of the macOS application firewall (never changed)."""
if platform.system() != "Darwin" or not FIREWALL.exists():
return None
try:
result = runner([str(FIREWALL), "--getglobalstate"], capture_output=True, text=True, timeout=10)
except (OSError, subprocess.SubprocessError):
return None
if "disabled" in (result.stdout or "").lower():
return ("Note: the macOS firewall is off. The installer does not change it; consider turning it on "
"(System Settings > Network > Firewall) so other local services are not exposed either.")
return None