Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(browser): allow localhost apps by default
trohitgcee1aa335e99
diff --git a/docs-internal/browser.md b/docs-internal/browser.mdindex dafa48c6..a35205bc 100644--- a/docs-internal/browser.md+++ b/docs-internal/browser.md@@ -141,9 +141,12 @@ command has an unknown outcome, safe hand-back retires the managed Chrome; ifthat fails, control remains held rather than resuming the agent prematurely.The policy proxy in `_egress.py` enforces allowed destinations for browser-traffic. `allow_private_network` does not permit OpenCompany's protected ports-or cloud metadata endpoints. This network enforcement is separate from the-control lease and from agent read-only behavior.+traffic. Localhost and loopback addresses (`127.0.0.1`, `::1`) are allowed by+default for local apps. Private LAN addresses still require+`allow_private_network`. Domain allowlists apply to local apps too, and neither+setting permits OpenCompany's protected ports or cloud metadata endpoints.+This network enforcement is separate from the control lease and from agent+read-only behavior.## Normal modediff --git a/server/nodes/browser/_egress.py b/server/nodes/browser/_egress.pyindex 20a2e226..b46e524a 100644--- a/server/nodes/browser/_egress.py+++ b/server/nodes/browser/_egress.py@@ -146,7 +146,7 @@ class EgressProxy:# -- connection handling (proxy thread) ---------------------------------- async def _resolve_allowed(self, host: str, port: int, policy: NetPolicy) -> str:+ async def _resolve_allowed(self, host: str, port: int, policy: NetPolicy) -> list[str]:reason = host_block_reason(host, policy)if reason:raise _Blocked(reason)@@ -164,17 +164,31 @@ class EgressProxy:raise _Blocked(f"{host} could not be resolved") from Nonecandidates = list(dict.fromkeys(ipaddress.ip_address(info[4][0].split("%", 1)[0]) for info in infos))last_reason = "no address"+ allowed = []for address in candidates:reason = address_block_reason(address, port, policy)if reason is None:- return str(address)- last_reason = reason+ allowed.append(str(address))+ else:+ last_reason = reason+ if allowed:+ return allowedraise _Blocked(last_reason)async def _open(self, host: str, port: int, policy: NetPolicy) -> Tuple[asyncio.StreamReader, asyncio.StreamWriter]:- address = await self._resolve_allowed(host, port, policy)+ addresses = await self._resolve_allowed(host, port, policy)try:- return await asyncio.wait_for(asyncio.open_connection(address, port), timeout=_CONNECT_TIMEOUT)+ async with asyncio.timeout(_CONNECT_TIMEOUT):+ last_error = OSError("no reachable address")+ for address in addresses:+ try:+ # localhost may resolve to IPv6 first while the app+ # listens only on IPv4. Try only addresses already+ # checked above; never resolve the hostname again.+ return await asyncio.open_connection(address, port)+ except OSError as exc:+ last_error = exc+ raise last_errorexcept (OSError, asyncio.TimeoutError) as exc:raise ConnectionError(f"could not connect to {host}:{port}: {exc}") from excdiff --git a/server/nodes/browser/_netpolicy.py b/server/nodes/browser/_netpolicy.pyindex 52791e26..f69c5867 100644--- a/server/nodes/browser/_netpolicy.py+++ b/server/nodes/browser/_netpolicy.py@@ -1,7 +1,7 @@"""Where the agent's browser may go. Pure functions, no I/O.A page the agent opens is untrusted, and the agent itself follows untrusted-instructions (prompt injection). From the agent's Chrome, three kinds of+instructions (prompt injection). From the agent's Chrome, these kinds ofaddress must stay out of reach:- **OpenCompany itself**: its ports on this machine (the app, the code@@ -9,8 +9,9 @@ address must stay out of reach:ports. These are refused on every address of this host, always.- **Cloud metadata** (``169.254.169.254`` and friends), which hands out theVM's credentials. Link-local is refused always.-- **Loopback and the private network**, refused unless the operator ticked- "Allow local network" on the Browser node (e.g. to test a local app).+- **The private network**, refused unless the operator ticked "Allow local+ network" on the Browser node. Loopback is allowed by default so the+ browser can test local apps, except on the protected ports above.The egress proxy (``_egress.py``) applies :func:`address_block_reason` to theaddresses it resolved itself, so DNS rebinding cannot swap in a private@@ -107,14 +108,14 @@ def address_block_reason(address: IPAddress, port: int, policy: NetPolicy) -> Opaddress = address.ipv4_mappedif address in _METADATA_ADDRESSES or address.is_link_local:return "cloud metadata and link-local addresses are never reachable"- if address.is_unspecified or address.is_multicast or address.is_reserved:+ if address.is_unspecified or address.is_multicast or (address.is_reserved and not address.is_loopback):return "this address is not reachable from the browser"is_local = address.is_loopback or address in policy.local_addressesif is_local and port in policy.blocked_local_ports:return "OpenCompany's own services are never reachable from the browser"- if is_local:- return None if policy.allow_private_network else "localhost is blocked; allow local network on the Browser node to use it"- if address.is_private or address in _CGNAT:+ if address.is_loopback:+ return None+ if is_local or address.is_private or address in _CGNAT:return None if policy.allow_private_network else "the private network is blocked; allow local network on the Browser node to use it"return None@@ -128,9 +129,6 @@ def host_block_reason(host: str, policy: NetPolicy) -> Optional[str]:return "cloud metadata addresses are never reachable"if not domain_allowed(host, policy.allowed_domains):return f"{host} is not in this Browser node's allowed domains"- if host == "localhost" or host.endswith(".localhost"):- if not policy.allow_private_network:- return "localhost is blocked; allow local network on the Browser node to use it"return Nonediff --git a/server/nodes/browser/browser/__init__.py b/server/nodes/browser/browser/__init__.pyindex 27922d0d..7e7ade44 100644--- a/server/nodes/browser/browser/__init__.py+++ b/server/nodes/browser/browser/__init__.py@@ -191,10 +191,10 @@ class BrowserParams(BrowserToolInput):webmcp_mode: Literal["disabled", "read_only", "all"] = Field(default="read_only", description="Which tools a web page offers (WebMCP) the agent may call.")- allowed_domains: str = Field(default="", description="Comma-separated sites the browser may open. Empty allows every public site.")+ allowed_domains: str = Field(default="", description="Comma-separated sites the browser may open. Empty allows every public site and localhost.")allow_private_network: bool = Field(default=False,- description="Allow localhost and your local network (e.g. to test a local app). OpenCompany's own ports and cloud metadata stay blocked.",+ description="Allow your private local network. Localhost is already allowed. OpenCompany's own ports and cloud metadata stay blocked.",)op_timeout_s: int = Field(default=45, ge=5, le=300, description="Seconds one browser step may take.")request_user_timeout_s: int = Field(default=600, ge=60, le=1800, description="How long to wait for you when the agent asks for help.")diff --git a/server/skills/web_agent/browser-skill/SKILL.md b/server/skills/web_agent/browser-skill/SKILL.mdindex 4b0d2c40..3e93e17f 100644--- a/server/skills/web_agent/browser-skill/SKILL.md+++ b/server/skills/web_agent/browser-skill/SKILL.md@@ -65,7 +65,7 @@ It waits until the owner hands the browser back; the result says `handed_back`,## Limits-- Only public http(s) sites open. localhost, private networks and cloud metadata addresses are blocked unless the operator allowed the local network on this node.+- Public http(s) sites and localhost apps open by default, subject to the node's allowed domains. Private LAN addresses require the operator to allow the local network on this node. OpenCompany's own service ports and cloud metadata addresses are always blocked.- Some sites block automated browsers. If a page keeps refusing you, ask the owner with `request_user`.- Do not paste page content into other sites or messages unless the task asks for it.diff --git a/server/tests/nodes/browser/test_browser_netpolicy.py b/server/tests/nodes/browser/test_browser_netpolicy.pyindex aa30da87..9de827bf 100644--- a/server/tests/nodes/browser/test_browser_netpolicy.py+++ b/server/tests/nodes/browser/test_browser_netpolicy.py@@ -4,6 +4,8 @@ from __future__ import annotationsimport asyncioimport ipaddress+import socket+from unittest.mock import AsyncMockimport pytest@@ -35,12 +37,20 @@ def _policy(**kw):("100.100.100.200", 80, True, True),("fd00:ec2::254", 80, True, True),("fe80::1", 80, True, True),- ("127.0.0.1", 3000, False, True),+ ("127.0.0.1", 3000, False, False),+ ("127.0.0.2", 3000, False, False),+ ("::1", 3000, False, False),+ ("::ffff:127.0.0.1", 3000, False, False),("127.0.0.1", 3000, True, False),+ ("127.0.0.1", APP_PORT, False, True),+ ("::1", APP_PORT, False, True),+ ("::ffff:127.0.0.1", APP_PORT, False, True),("127.0.0.1", APP_PORT, True, True), # OpenCompany itself, always("::1", APP_PORT, True, True),("::ffff:127.0.0.1", APP_PORT, True, True), # IPv4-mapped loopback("10.1.2.3", APP_PORT, True, True), # this host's LAN address, app port+ ("10.1.2.3", 3000, False, True),+ ("10.1.2.3", 3000, True, False),("192.168.1.10", 80, False, True),("192.168.1.10", 80, True, False),("100.64.0.1", 80, False, True),@@ -63,7 +73,13 @@ def test_address_rules(address, port, allow_private, blocked):("javascript:alert(1)", True),("view-source:https://example.com", True),("data:text/html,<script>1</script>", True),- ("http://localhost:3000", True),+ ("http://localhost:3000", False),+ ("http://LOCALHOST.:3000", False),+ ("http://app.localhost:3000", False),+ ("http://127.0.0.1:3000", False),+ ("http://[::1]:3000", False),+ ("http://[::ffff:127.0.0.1]:3000", False),+ ("http://192.168.1.10:3000", True),("http://metadata.google.internal/computeMetadata/v1/", True),("http://169.254.169.254/latest/meta-data/", True),("http://127.0.0.1:5678/ws/internal", True),@@ -84,8 +100,8 @@ def test_allowed_domains_match_on_label_boundaries():assert url_block_reason("https://a.docs.io/", _policy(allowed_domains=allowed)) is None[email protected]("host", ["8.8.8.8", "2001:4860:4860::8888"])-def test_public_literal_ips_do_not_bypass_the_domain_allowlist(host):[email protected]("host", ["8.8.8.8", "2001:4860:4860::8888", "localhost", "app.localhost", "127.0.0.1", "::1"])+def test_navigation_hosts_do_not_bypass_the_domain_allowlist(host):policy = _policy(allowed_domains=("example.com",))url_host = f"[{host}]" if ":" in host else hostassert "allowed domains" in host_block_reason(host, policy)@@ -102,8 +118,10 @@ def test_ip_allowlist_entries_match_only_the_exact_address():def test_an_explicit_ip_allowlist_cannot_override_private_or_metadata_rules():- assert url_block_reason("http://127.0.0.1:3000/", _policy(allowed_domains=("127.0.0.1",))) is not None- assert url_block_reason("http://127.0.0.1:3000/", _policy(allowed_domains=("127.0.0.1",), allow_private_network=True)) is None+ assert url_block_reason("http://127.0.0.1:3000/", _policy(allowed_domains=("127.0.0.1",))) is None+ assert url_block_reason(f"http://127.0.0.1:{APP_PORT}/", _policy(allowed_domains=("127.0.0.1",))) is not None+ assert url_block_reason("http://192.168.1.10:3000/", _policy(allowed_domains=("192.168.1.10",))) is not None+ assert url_block_reason("http://192.168.1.10:3000/", _policy(allowed_domains=("192.168.1.10",), allow_private_network=True)) is Noneassert url_block_reason("http://169.254.169.254/", _policy(allowed_domains=("169.254.169.254",), allow_private_network=True)) is not None@@ -112,6 +130,27 @@ def test_own_ports_come_from_every_port_variable():assert own_ports_from_env(env) == frozenset({5678, 5679, 8233})+async def test_egress_falls_back_only_to_policy_checked_addresses(monkeypatch):+ from nodes.browser._egress import EgressProxy++ resolver = AsyncMock(return_value=[+ (socket.AF_INET6, socket.SOCK_STREAM, 6, "", ("::1", 3000, 0, 0)),+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("192.168.1.10", 3000)),+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("169.254.169.254", 3000)),+ (socket.AF_INET, socket.SOCK_STREAM, 6, "", ("127.0.0.1", 3000)),+ ])+ monkeypatch.setattr(asyncio.get_running_loop(), "getaddrinfo", resolver)+ connection = (object(), object())+ connect = AsyncMock(side_effect=[ConnectionRefusedError("IPv6 is not listening"), connection])+ monkeypatch.setattr(asyncio, "open_connection", connect)++ proxy = EgressProxy(_policy)+ assert await proxy._open("localhost", 3000, _policy()) == connection+ resolver.assert_awaited_once_with("localhost", 3000, type=socket.SOCK_STREAM)+ assert [call.args for call in connect.await_args_list] == [("::1", 3000), ("127.0.0.1", 3000)]++async def _serve_http(body: bytes):async def handle(reader, writer):await reader.readuntil(b"\r\n\r\n")@@ -141,14 +180,24 @@ async def test_egress_proxy_enforces_the_policy_by_resolved_address():proxy = EgressProxy(lambda: state["policy"])port = proxy.start()try:- # Loopback refused without the opt-in, by the resolved address of a name too.+ # Both the hostname and literal loopback work without the LAN opt-in.+ for host in ("localhost", "127.0.0.1"):+ allowed = await _through_proxy(port, f"GET http://{host}:{target_port}/x HTTP/1.1\r\nHost: x\r\n\r\n".encode())+ assert allowed.startswith(b"HTTP/1.1 200") and allowed.endswith(b"hello"), allowed++ tunneled = await _through_proxy(port, (+ f"CONNECT localhost:{target_port} HTTP/1.1\r\nHost: x\r\n\r\n"+ "GET / HTTP/1.1\r\nHost: localhost\r\n\r\n"+ ).encode())+ assert tunneled.startswith(b"HTTP/1.1 200 Connection Established") and tunneled.endswith(b"hello"), tunneled++ private = await _through_proxy(port, b"CONNECT 192.168.1.10:3000 HTTP/1.1\r\nHost: x\r\n\r\n")+ assert private.startswith(b"HTTP/1.1 403")++ # Policy changes still take effect, and localhost cannot reach protected ports.+ state["policy"] = NetPolicy(allow_private_network=True, blocked_local_ports=frozenset({APP_PORT, target_port}))refused = await _through_proxy(port, f"GET http://localhost:{target_port}/ HTTP/1.1\r\nHost: x\r\n\r\n".encode())- assert refused.startswith(b"HTTP/1.1 403")- assert b"Blocked by OpenCompany" in refused-- state["policy"] = NetPolicy(allow_private_network=True, blocked_local_ports=frozenset({APP_PORT}))- allowed = await _through_proxy(port, f"GET http://127.0.0.1:{target_port}/x HTTP/1.1\r\nHost: x\r\n\r\n".encode())- assert allowed.startswith(b"HTTP/1.1 200") and allowed.endswith(b"hello")+ assert refused.startswith(b"HTTP/1.1 403") and b"own services" in refusedtunnel = await _through_proxy(port, f"CONNECT 127.0.0.1:{APP_PORT} HTTP/1.1\r\nHost: x\r\n\r\n".encode())assert tunnel.startswith(b"HTTP/1.1 403")diff --git a/server/tests/nodes/browser/test_browser_node.py b/server/tests/nodes/browser/test_browser_node.pyindex c6a54306..13e1a931 100644--- a/server/tests/nodes/browser/test_browser_node.py+++ b/server/tests/nodes/browser/test_browser_node.py@@ -126,7 +126,16 @@ async def test_navigate_runs_the_script_and_reports_the_page(runtime):assert runtime.controller.state == ControlState.IDLE # the step released control[email protected]("url", ["file:///etc/passwd", "http://localhost:3000", "http://169.254.169.254/", "javascript:alert(1)"])[email protected]("url", ["http://localhost:3000", "http://127.0.0.1:3000", "http://[::1]:3000"])[email protected]("operation", ["navigate", "tabs"])+async def test_local_apps_open_without_enabling_private_network(runtime, url, operation):+ result = await _run({"operation": operation, "url": url, "tab_action": "new"})+ assert result.get("success") is not False, result+ op, args = runtime.cli.calls[-1]+ assert op == operation and args["url"] == url++[email protected]("url", ["file:///etc/passwd", "http://192.168.1.10:3000", "http://127.0.0.1:5678", "http://169.254.169.254/", "javascript:alert(1)"])async def test_refused_urls_never_reach_the_browser(runtime, url):result = await _run({"operation": "navigate", "url": url})assert result.get("success") is False