Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(ui_server): verify wildcard tailscale peers

cyh38ac4bff0b54
diff --git a/tests/test_ui_remote_access_auth.py b/tests/test_ui_remote_access_auth.py
index dac4b55ed..333c44395 100644
--- a/tests/test_ui_remote_access_auth.py
+++ b/tests/test_ui_remote_access_auth.py
@@ -37,6 +37,30 @@ def _mock_no_interfaces(monkeypatch) -> None:
monkeypatch.setattr("vibe.ui_server.psutil.net_if_addrs", lambda: {})
+def _mock_tailscale_whois(
+ monkeypatch,
+ peer: str,
+ *,
+ addresses: list[str] | None = None,
+ allowed_ips: list[str] | None = None,
+) -> None:
+ peer_address = ipaddress.ip_address(peer)
+ prefix = peer_address.max_prefixlen
+ monkeypatch.setattr(ui_server, "_TAILSCALE_PEER_CACHE", {})
+ monkeypatch.setattr(
+ ui_server,
+ "_tailscale_whois",
+ lambda address: {
+ "Machine": {
+ "Addresses": addresses or [str(peer_address)],
+ "AllowedIPs": allowed_ips or [f"{peer_address}/{prefix}"],
+ }
+ }
+ if address == peer_address
+ else None,
+ )
+
+
def _save_config(tmp_path) -> V2Config:
config = V2Config(
mode="self_host",
@@ -1285,10 +1309,32 @@ def test_setup_host_wildcard_with_reverse_proxy_header_is_not_local(monkeypatch,
assert response.get_json()["error"] == "remote_access_host_mismatch"
+def test_setup_host_wildcard_with_reverse_proxy_header_skips_interface_probe(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ _save_config_with_setup_host(tmp_path, "0.0.0.0")
+ monkeypatch.setattr(
+ ui_server,
+ "_local_interface_network",
+ lambda *args, **kwargs: (_ for _ in ()).throw(AssertionError("interface probe should be skipped")),
+ )
+
+ response = app.test_client().get(
+ "/dashboard",
+ base_url="http://100.97.103.112:5123",
+ environ_base={"REMOTE_ADDR": "100.97.103.5"},
+ headers={"X-Forwarded-For": "203.0.113.10"},
+ follow_redirects=False,
+ )
+
+ assert response.status_code == 503
+ assert response.get_json()["error"] == "remote_access_host_mismatch"
+
+
def test_setup_host_wildcard_allows_actual_tailscale_interface_host(monkeypatch, tmp_path):
monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
_save_config_with_setup_host(tmp_path, "0.0.0.0")
_mock_interface(monkeypatch, "100.97.103.112", 32, name="tailscale0")
+ _mock_tailscale_whois(monkeypatch, "100.97.103.5")
response = app.test_client().get(
"/health",
@@ -1299,6 +1345,41 @@ def test_setup_host_wildcard_allows_actual_tailscale_interface_host(monkeypatch,
assert response.status_code == 200
+def test_setup_host_wildcard_rejects_tailscale_peer_without_whois(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ _save_config_with_setup_host(tmp_path, "0.0.0.0")
+ _mock_interface(monkeypatch, "100.97.103.112", 32, name="tailscale0")
+ monkeypatch.setattr(ui_server, "_TAILSCALE_PEER_CACHE", {})
+ monkeypatch.setattr(ui_server, "_tailscale_whois", lambda address: None)
+
+ response = app.test_client().get(
+ "/dashboard",
+ base_url="http://100.97.103.112:5123",
+ environ_base={"REMOTE_ADDR": "100.97.103.5"},
+ follow_redirects=False,
+ )
+
+ assert response.status_code == 503
+ assert response.get_json()["error"] == "remote_access_host_mismatch"
+
+
+def test_setup_host_wildcard_rejects_tailscale_subnet_router_peer(monkeypatch, tmp_path):
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ _save_config_with_setup_host(tmp_path, "0.0.0.0")
+ _mock_interface(monkeypatch, "100.97.103.112", 32, name="tailscale0")
+ _mock_tailscale_whois(monkeypatch, "100.97.103.5", allowed_ips=["100.97.103.5/32", "192.168.50.0/24"])
+
+ response = app.test_client().get(
+ "/dashboard",
+ base_url="http://100.97.103.112:5123",
+ environ_base={"REMOTE_ADDR": "100.97.103.5"},
+ follow_redirects=False,
+ )
+
+ assert response.status_code == 503
+ assert response.get_json()["error"] == "remote_access_host_mismatch"
+
+
def test_setup_host_wildcard_does_not_trust_unconfigured_tailscale_host(monkeypatch, tmp_path):
monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
_save_config_with_setup_host(tmp_path, "0.0.0.0")
@@ -1333,6 +1414,7 @@ def test_setup_host_ipv6_wildcard_allows_tailscale_ula_interface_host(monkeypatc
monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
_save_config_with_setup_host(tmp_path, "::")
_mock_interface(monkeypatch, "fd7a:115c:a1e0::5", 128, name="tailscale0")
+ _mock_tailscale_whois(monkeypatch, "fd7a:115c:a1e0::20")
response = app.test_client().get(
"/health",
@@ -1382,6 +1464,7 @@ def test_setup_host_wildcard_trusts_utun_when_tailscale_reports_local_ip(monkeyp
address = ipaddress.ip_address("100.97.103.112")
_mock_interface(monkeypatch, str(address), 32, name="utun4")
monkeypatch.setattr(ui_server, "_tailscale_local_addresses", lambda: frozenset({address}))
+ _mock_tailscale_whois(monkeypatch, "100.97.103.5")
response = app.test_client().get(
"/health",
@@ -1415,6 +1498,7 @@ def test_setup_host_ipv6_wildcard_trusts_utun_when_tailscale_reports_local_ip(mo
address = ipaddress.ip_address("fd7a:115c:a1e0::5")
_mock_interface(monkeypatch, str(address), 128, name="utun4")
monkeypatch.setattr(ui_server, "_tailscale_local_addresses", lambda: frozenset({address}))
+ _mock_tailscale_whois(monkeypatch, "fd7a:115c:a1e0::20")
response = app.test_client().get(
"/health",
diff --git a/vibe/ui_server.py b/vibe/ui_server.py
index cd9768104..a6d90e0c6 100644
--- a/vibe/ui_server.py
+++ b/vibe/ui_server.py
@@ -299,6 +299,8 @@ _WILDCARD_TRUST_OVERLAY_INTERFACE_PREFIXES = (
_TAILSCALE_UTUN_INTERFACE_PREFIXES = ("utun",)
_TAILSCALE_IP_CACHE_TTL_SECONDS = 30.0
_TAILSCALE_IP_CACHE: tuple[float, frozenset[ipaddress._BaseAddress]] | None = None
+_TAILSCALE_PEER_CACHE_TTL_SECONDS = 30.0
+_TAILSCALE_PEER_CACHE: dict[ipaddress._BaseAddress, tuple[float, bool]] = {}
def _is_private_address(address: ipaddress._BaseAddress) -> bool:
@@ -308,19 +310,20 @@ def _is_private_address(address: ipaddress._BaseAddress) -> bool:
def _is_private_peer() -> bool:
+ address = _request_peer_address()
+ return address is not None and _is_private_address(address)
+
+
+def _request_peer_address() -> ipaddress._BaseAddress | None:
remote_addr = (request.remote_addr or "").strip()
if not remote_addr or remote_addr == "localhost":
- return False
+ return None
try:
address = ipaddress.ip_address(remote_addr)
except ValueError:
- return False
- if _is_private_address(address):
- return True
+ return None
mapped = getattr(address, "ipv4_mapped", None)
- if mapped is not None:
- return _is_private_address(mapped)
- return False
+ return mapped or address
def _local_interface_network(
@@ -562,6 +565,82 @@ def _tailscale_local_addresses() -> frozenset[ipaddress._BaseAddress]:
return cached
+def _tailscale_whois(peer_address: ipaddress._BaseAddress) -> dict[str, Any] | None:
+ env = {**os.environ, "TAILSCALE_BE_CLI": "1"}
+ for candidate in _tailscale_cli_candidates():
+ try:
+ result = subprocess.run(
+ [candidate, "whois", "--json", str(peer_address)],
+ capture_output=True,
+ text=True,
+ timeout=1.5,
+ check=False,
+ env=env,
+ )
+ except Exception:
+ continue
+ if result.returncode != 0:
+ continue
+ try:
+ payload = json.loads(result.stdout)
+ except Exception:
+ continue
+ return payload if isinstance(payload, dict) else None
+ return None
+
+
+def _json_list(payload: dict[str, Any], *keys: str) -> list[Any]:
+ for key in keys:
+ value = payload.get(key)
+ if isinstance(value, list):
+ return value
+ return []
+
+
+def _is_tailscale_host_route(network: ipaddress._BaseNetwork) -> bool:
+ if network.prefixlen != network.max_prefixlen:
+ return False
+ return _is_tailscale_overlay_address(network.network_address)
+
+
+def _is_trusted_tailscale_peer(peer_address: ipaddress._BaseAddress) -> bool:
+ global _TAILSCALE_PEER_CACHE
+
+ if not _is_tailscale_overlay_address(peer_address):
+ return False
+
+ now = time.monotonic()
+ cached = _TAILSCALE_PEER_CACHE.get(peer_address)
+ if cached is not None:
+ cached_at, trusted = cached
+ if now - cached_at < _TAILSCALE_PEER_CACHE_TTL_SECONDS:
+ return trusted
+
+ payload = _tailscale_whois(peer_address)
+ trusted = False
+ if payload is not None:
+ machine = payload.get("Machine") or payload.get("machine") or {}
+ if isinstance(machine, dict):
+ addresses = set()
+ for raw_address in _json_list(machine, "Addresses", "addresses"):
+ try:
+ addresses.add(ipaddress.ip_address(str(raw_address)))
+ except ValueError:
+ continue
+ allowed_networks = []
+ for raw_network in _json_list(machine, "AllowedIPs", "allowedIPs", "allowedIps"):
+ try:
+ allowed_networks.append(ipaddress.ip_network(str(raw_network), strict=False))
+ except ValueError:
+ continue
+ trusted = bool(addresses and peer_address in addresses and allowed_networks)
+ if trusted:
+ trusted = all(_is_tailscale_host_route(network) for network in allowed_networks)
+
+ _TAILSCALE_PEER_CACHE[peer_address] = (now, trusted)
+ return trusted
+
+
def _allows_wildcard_setup_host_trust(interface_name: str, address: ipaddress._BaseAddress) -> bool:
normalized_name = interface_name.lower()
if _is_tailscale_overlay_address(address):
@@ -587,6 +666,8 @@ def _is_wildcard_setup_host_request(config: V2Config | None) -> bool:
setup_host = _normalized_host(getattr(config.ui, "setup_host", ""))
if not _is_wildcard_setup_host(setup_host):
return False
+ if _has_forwarded_metadata():
+ return False
try:
host_address = ipaddress.ip_address(_normalized_host(request.host))
@@ -598,10 +679,11 @@ def _is_wildcard_setup_host_request(config: V2Config | None) -> bool:
return False
if _local_interface_network(host_address, interface_filter=_allows_wildcard_setup_host_trust) is None:
return False
- if _has_forwarded_metadata():
- return False
if not _is_private_peer():
return False
+ if _is_tailscale_overlay_address(host_address):
+ peer_address = _request_peer_address()
+ return peer_address is not None and _is_trusted_tailscale_peer(peer_address)
return _peer_shares_setup_host_network(host_address)