Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(backend/copilot): send the browser tools' traffic through an egress proxy (#15859)

Nicholas Tindleec6ab8b03eec
diff --git a/.github/scripts/backend-allowed-skips.json b/.github/scripts/backend-allowed-skips.json
index 9c470c7358..7d1b7a3269 100644
--- a/.github/scripts/backend-allowed-skips.json
+++ b/.github/scripts/backend-allowed-skips.json
@@ -121,6 +121,14 @@
"backend.copilot.stream_drift.drift_causes_test.test_a_silent_turn_keeps_publishing_within_the_stream_ttl",
"backend.copilot.stream_drift.drift_causes_test.test_a_turn_whose_executor_died_is_not_reported_running",
"backend.copilot.test_copilot_e2e.test_session_state_persistence",
+ "backend.copilot.tools.agent_browser_integration_test.test_browser_cannot_open_loopback_directly[127.0.0.1]",
+ "backend.copilot.tools.agent_browser_integration_test.test_browser_cannot_open_loopback_directly[localhost]",
+ "backend.copilot.tools.agent_browser_integration_test.test_browser_does_not_follow_a_link_to_loopback",
+ "backend.copilot.tools.agent_browser_integration_test.test_browser_does_not_follow_a_redirect_to_loopback",
+ "backend.copilot.tools.agent_browser_integration_test.test_browser_ignores_agent_browser_config_files",
+ "backend.copilot.tools.agent_browser_integration_test.test_browser_ignores_agent_browser_environment",
+ "backend.copilot.tools.agent_browser_integration_test.test_browser_reaches_public_pages_but_not_their_internal_requests",
+ "backend.copilot.tools.agent_browser_integration_test.test_browser_webrtc_sends_no_udp_around_the_proxy",
"backend.copilot.tools.agent_browser_integration_test.test_chromium_executable_env_is_set",
"backend.copilot.tools.agent_browser_integration_test.test_close_session",
"backend.copilot.tools.agent_browser_integration_test.test_concurrent_independent_sessions",
diff --git a/autogpt_platform/backend/Dockerfile b/autogpt_platform/backend/Dockerfile
index fa3f0af49a..9e27f4d9b2 100644
--- a/autogpt_platform/backend/Dockerfile
+++ b/autogpt_platform/backend/Dockerfile
@@ -175,6 +175,13 @@ RUN apt-get update \
&& npm install -g [email protected] \
&& rm -rf /tmp/* /root/.npm
+# The Copilot browser reaches the network only through an egress proxy that
+# carries TCP. This policy stops WebRTC from sending UDP around it, in every
+# frame and window; the matching command-line switch is not honoured.
+RUN mkdir -p /etc/chromium/policies/managed \
+ && echo '{"WebRtcIPHandling": "disable_non_proxied_udp"}' \
+ > /etc/chromium/policies/managed/agent-browser-network.json
+
ENV AGENT_BROWSER_EXECUTABLE_PATH=/usr/bin/chromium
WORKDIR /app/autogpt_platform/backend
diff --git a/autogpt_platform/backend/backend/copilot/tools/agent_browser.py b/autogpt_platform/backend/backend/copilot/tools/agent_browser.py
index 5b7445f911..a884221e71 100644
--- a/autogpt_platform/backend/backend/copilot/tools/agent_browser.py
+++ b/autogpt_platform/backend/backend/copilot/tools/agent_browser.py
@@ -12,11 +12,12 @@ which runs a local Chromium instance managed by a persistent daemon.
is one browser action; the LLM chains them naturally
SSRF protection:
- Uses the shared validate_url() from backend.util.request, which is the same
- guard used by HTTP blocks and web_fetch. It resolves ALL DNS answers (not just
- the first), blocks RFC 1918, loopback, link-local, 0.0.0.0/8, multicast,
- and all relevant IPv6 ranges, and applies IDNA encoding to prevent Unicode
- domain attacks.
+ Chromium sends every request through backend.util.egress_proxy, which only
+ connects to public addresses and dials the address it checked. That covers
+ what a URL check cannot see: redirects, link clicks, form posts, scripts,
+ subresources and DNS rebinding. URLs given to browser_navigate are also
+ checked up front with validate_url_host so a refusal comes back as a clear
+ error instead of a failed page load.
Requires:
npm install -g agent-browser
@@ -30,12 +31,14 @@ import base64
import json
import logging
import os
+import re
import shutil
import tempfile
from typing import Any
from backend.copilot.context import get_workspace_manager
from backend.copilot.model import ChatSession
+from backend.util.egress_proxy import egress_proxy_url
from backend.util.request import validate_url_host
from .base import BaseTool
@@ -54,6 +57,61 @@ _CMD_TIMEOUT = 45
# Accessibility tree can be very large; cap it to keep LLM context manageable.
_MAX_SNAPSHOT_CHARS = 20_000
+# Launch options sent with every command. agent-browser relaunches Chromium
+# when they change, so a single command without them would restart the browser
+# with direct network access. "<-loopback>" stops Chromium sending localhost
+# and link-local requests around the proxy. The proxy carries only TCP: QUIC is
+# switched off here, and a Chromium policy in the backend image keeps WebRTC's
+# UDP from going around it.
+_PROXY_BYPASS = "<-loopback>"
+_CHROMIUM_ARGS = "--disable-quic"
+
+# agent-browser also merges in ~/.agent-browser/config.json and a
+# ./agent-browser.json from its working directory. A file planted at either
+# could load an extension that rewrites the browser's proxy. Given --config it
+# reads that file alone, so it is pointed at an empty one.
+_empty_config_path: str | None = None
+
+
+def _empty_config() -> str:
+ """Path to an empty agent-browser config, recreated if it has gone missing
+ so a cleaned-up /tmp cannot break every later browser command."""
+ global _empty_config_path
+ path = _empty_config_path
+ if path is None or not os.path.exists(path):
+ fd, path = tempfile.mkstemp(prefix="agent-browser-", suffix=".json")
+ with os.fdopen(fd, "w") as f:
+ f.write("{}")
+ _empty_config_path = path
+ return path
+
+
+# agent-browser reads its own options (proxy, executable, profile, ...) from
+# anywhere on its command line; they are lower-case words, some taking
+# "=value", plus -V. Apart from the ones this module passes itself, an argument
+# of that shape came from the model or a web page (a cookie name, say) and
+# could relaunch the browser around the proxy.
+_OWN_OPTIONS = frozenset(
+ {"-i", "-c", "--load", "--json", "--domain", "--path", "--annotate"}
+)
+_OPTION_LIKE = re.compile(r"--?[a-z][a-z-]*(=.*)?|-V", re.DOTALL)
+
+# agent-browser also takes options from AGENT_BROWSER_* variables and the usual
+# proxy variables. Some of them (CDP, provider, auto-connect, extensions, init
+# scripts) attach to or start a browser without the launch options above, so
+# none reach it except the path to the Chromium it should launch.
+_KEPT_AGENT_BROWSER_ENV = frozenset({"AGENT_BROWSER_EXECUTABLE_PATH"})
+_PROXY_ENV = frozenset({"HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"})
+
+
+def _agent_browser_env() -> dict[str, str]:
+ return {
+ name: value
+ for name, value in os.environ.items()
+ if (not name.startswith("AGENT_BROWSER_") or name in _KEPT_AGENT_BROWSER_ENV)
+ and name.upper() not in _PROXY_ENV
+ }
+
# ---------------------------------------------------------------------------
# Subprocess helper
@@ -73,13 +131,30 @@ async def _run(
browser state leakage)
--session-name <name> → persist cookies/localStorage across tool calls within
the same session (enables login → navigate flows)
+
+ Chromium's traffic goes through the egress proxy (see the module docstring).
"""
+ if any(_OPTION_LIKE.fullmatch(arg) and arg not in _OWN_OPTIONS for arg in args):
+ return 1, "", "Refused an argument that looks like an agent-browser option."
+ try:
+ proxy = egress_proxy_url()
+ except Exception:
+ logger.exception("[browser] egress proxy failed to start")
+ return 1, "", "The browser's network proxy is unavailable."
cmd = [
"agent-browser",
"--session",
session_name,
"--session-name",
session_name,
+ "--config",
+ _empty_config(),
+ "--proxy",
+ proxy,
+ "--proxy-bypass",
+ _PROXY_BYPASS,
+ "--args",
+ _CHROMIUM_ARGS,
*args,
]
_touched_sessions.add(session_name)
@@ -89,6 +164,7 @@ async def _run(
*cmd,
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
+ env=_agent_browser_env(),
)
stdout, stderr = await asyncio.wait_for(proc.communicate(), timeout=timeout)
return proc.returncode or 0, stdout.decode(), stderr.decode()
diff --git a/autogpt_platform/backend/backend/copilot/tools/agent_browser_integration_test.py b/autogpt_platform/backend/backend/copilot/tools/agent_browser_integration_test.py
index 3876b2f7f4..236812db92 100644
--- a/autogpt_platform/backend/backend/copilot/tools/agent_browser_integration_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/agent_browser_integration_test.py
@@ -21,22 +21,30 @@ Two test tiers:
"""
import concurrent.futures
+import http.server
+import json
import os
import shutil
import subprocess
import tempfile
+import threading
from datetime import datetime, timezone
from urllib.parse import urlparse
import pytest
from backend.copilot.model import ChatSession
-from backend.copilot.tools.agent_browser import BrowserActTool, BrowserNavigateTool
+from backend.copilot.tools.agent_browser import (
+ BrowserActTool,
+ BrowserNavigateTool,
+ _run,
+)
from backend.copilot.tools.models import (
BrowserActResponse,
BrowserNavigateResponse,
ErrorResponse,
)
+from backend.util import egress_proxy
pytestmark = pytest.mark.skipif(
shutil.which("agent-browser") is None,
@@ -349,3 +357,189 @@ async def test_tool_act_missing_target(_close_tool_session):
)
assert isinstance(resp, ErrorResponse)
assert resp.error == "missing_target"
+
+
+# ---------------------------------------------------------------------------
+# Network egress through the real browser
+#
+# One loopback server plays both parts: "entry.test", which the egress proxy
+# is told is public, and an internal service reached as 127.0.0.1/localhost,
+# which the proxy must refuse. Requests are driven through _run, so they skip
+# browser_navigate's up-front URL check and only the proxy stands in the way.
+#
+# These run only where agent-browser is installed, never in CI, so they are
+# excluded from coverage.
+# ---------------------------------------------------------------------------
+
+_EGRESS_SESSION = "integration-egress-session"
+
+
+class _SiteHandler(http.server.BaseHTTPRequestHandler): # pragma: no cover
+ server: "_Site"
+
+ def do_GET(self) -> None:
+ internal = f"http://127.0.0.1:{self.server.server_port}/internal"
+ self.server.hits.append(self.path)
+ if self.path.startswith("/internal"):
+ body = "<title>internal</title>"
+ elif self.path == "/redirect":
+ self.send_response(302)
+ self.send_header("Location", f"{internal}/redirect")
+ self.end_headers()
+ return
+ else:
+ body = (
+ "<title>entry</title>"
+ f'<img src="{internal}/img">'
+ f'<a id="link" href="{internal}/link">internal link</a>'
+ f'<script>fetch("{internal}/fetch")</script>'
+ )
+ payload = body.encode()
+ self.send_response(200)
+ self.send_header("Content-Type", "text/html")
+ self.send_header("Content-Length", str(len(payload)))
+ self.end_headers()
+ self.wfile.write(payload)
+
+ def log_message(self, *args) -> None:
+ pass
+
+
+class _Site(http.server.ThreadingHTTPServer): # pragma: no cover
+ hits: list[str]
+
+ def entry(self, path: str) -> str:
+ return f"http://entry.test:{self.server_port}{path}"
+
+ @property
+ def internal_hits(self) -> list[str]:
+ return [path for path in self.hits if path.startswith("/internal")]
+
+
+async def site(monkeypatch): # pragma: no cover
+ real_policy = egress_proxy.public_addresses
+
+ async def policy(host: str) -> list[str]:
+ if host == "entry.test":
+ return ["127.0.0.1"]
+ return await real_policy(host)
+
+ monkeypatch.setattr(egress_proxy, "public_addresses", policy)
+ server = _Site(("127.0.0.1", 0), _SiteHandler)
+ server.hits = []
+ threading.Thread(target=server.serve_forever, daemon=True).start()
+ yield server
+ await _run(_EGRESS_SESSION, "close", timeout=10)
+ server.shutdown()
+ server.server_close()
+
+
+async def test_browser_reaches_public_pages_but_not_their_internal_requests(
+ site,
+): # pragma: no cover
+ rc, _, stderr = await _run(_EGRESS_SESSION, "open", site.entry("/page"))
+ assert rc == 0, stderr
+ await _run(_EGRESS_SESSION, "wait", "--load", "networkidle")
+ _, title, _ = await _run(_EGRESS_SESSION, "get", "title")
+
+ assert title.strip() == "entry"
+ assert site.internal_hits == []
+
+
+async def test_browser_does_not_follow_a_redirect_to_loopback(site): # pragma: no cover
+ await _run(_EGRESS_SESSION, "open", site.entry("/redirect"))
+
+ assert "/redirect" in site.hits
+ assert site.internal_hits == []
+
+
+async def test_browser_does_not_follow_a_link_to_loopback(site): # pragma: no cover
+ rc, _, stderr = await _run(_EGRESS_SESSION, "open", site.entry("/page"))
+ assert rc == 0, stderr
+ rc, _, stderr = await _run(_EGRESS_SESSION, "click", "#link")
+ assert rc == 0, stderr
+ await _run(_EGRESS_SESSION, "wait", "--load", "load")
+
+ assert site.internal_hits == []
+
+
[email protected]("host", ["127.0.0.1", "localhost"])
+async def test_browser_cannot_open_loopback_directly(site, host): # pragma: no cover
+ await _run(
+ _EGRESS_SESSION, "open", f"http://{host}:{site.server_port}/internal/direct"
+ )
+
+ assert site.internal_hits == []
+
+
+# Gathers ICE candidates from the page and from a popup, which gets a WebRTC
+# constructor of its own. The STUN server is unreachable on purpose: without
+# the image's WebRTC policy Chromium still gathers host candidates, which means
+# it opened UDP sockets the egress proxy cannot see.
+_GATHER_ICE = """(async () => {
+ const gather = (Peer) => new Promise((resolve) => {
+ const found = [];
+ const pc = new Peer({iceServers: [{urls: "stun:127.0.0.1:9"}]});
+ pc.createDataChannel("probe");
+ pc.onicecandidate = (e) => e.candidate ? found.push(e.candidate.candidate)
+ : resolve(found);
+ pc.createOffer().then((offer) => pc.setLocalDescription(offer));
+ setTimeout(() => resolve(found), 5000);
+ });
+ const popup = window.open("about:blank");
+ const found = [
+ ...(await gather(window.RTCPeerConnection)),
+ ...(await gather(popup.RTCPeerConnection)),
+ ];
+ popup.close();
+ return JSON.stringify(found);
+})()"""
+
+
+async def test_browser_ignores_agent_browser_config_files(
+ site, tmp_path, monkeypatch
+): # pragma: no cover
+ # agent-browser would otherwise merge in ./agent-browser.json from its
+ # working directory; such a file could load an extension that rewrites the
+ # proxy. userAgent is a harmless way to see whether it was read.
+ monkeypatch.chdir(tmp_path)
+ (tmp_path / "agent-browser.json").write_text('{"userAgent": "PLANTED"}')
+
+ rc, _, stderr = await _run(_EGRESS_SESSION, "open", site.entry("/page"))
+ assert rc == 0, stderr
+ _, user_agent, _ = await _run(_EGRESS_SESSION, "eval", "navigator.userAgent")
+
+ assert "PLANTED" not in user_agent
+
+
+async def test_browser_ignores_agent_browser_environment(
+ site, monkeypatch
+): # pragma: no cover
+ # With this set, agent-browser would attach to whatever answers there
+ # instead of launching Chromium with the proxy. Nothing listens on port 9,
+ # so the page only opens if the variable never reached agent-browser.
+ monkeypatch.setenv("AGENT_BROWSER_CDP", "http://127.0.0.1:9")
+
+ rc, _, stderr = await _run(_EGRESS_SESSION, "open", site.entry("/page"))
+ assert rc == 0, stderr
+ _, title, _ = await _run(_EGRESS_SESSION, "get", "title")
+
+ assert title.strip() == "entry"
+
+
+async def test_browser_webrtc_sends_no_udp_around_the_proxy(site): # pragma: no cover
+ rc, _, stderr = await _run(_EGRESS_SESSION, "open", site.entry("/page"))
+ assert rc == 0, stderr
+
+ rc, out, stderr = await _run(_EGRESS_SESSION, "eval", _GATHER_ICE)
+ assert rc == 0, stderr
+
+ assert json.loads(json.loads(out)) == []
diff --git a/autogpt_platform/backend/backend/copilot/tools/agent_browser_test.py b/autogpt_platform/backend/backend/copilot/tools/agent_browser_test.py
index 7bbda92a91..12b2eaa97f 100644
--- a/autogpt_platform/backend/backend/copilot/tools/agent_browser_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/agent_browser_test.py
@@ -5,6 +5,7 @@ All subprocess calls are mocked — no agent-browser binary or real browser need
import asyncio
import json
+import os
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -16,10 +17,12 @@ from .agent_browser import (
BrowserActTool,
BrowserNavigateTool,
BrowserScreenshotTool,
+ _empty_config,
_ensure_session,
_fire_and_forget_save,
_has_local_session,
_restore_browser_state,
+ _run,
_save_browser_state,
close_browser_daemon,
close_browser_session,
@@ -32,6 +35,8 @@ from .models import (
ResponseType,
)
+_PROXY_URL = "socks5://127.0.0.1:1080"
+
# ---------------------------------------------------------------------------
# Autouse fixture — mock state persistence helpers for all existing tests.
# Dedicated tests for these helpers call the *original* functions directly
@@ -49,6 +54,9 @@ def _reset_and_mock_state(monkeypatch):
_mod._touched_sessions.clear()
_mod._pending_saves.clear()
_mod._closing_sessions.clear()
+ monkeypatch.setattr(
+ "backend.copilot.tools.agent_browser.egress_proxy_url", lambda: _PROXY_URL
+ )
monkeypatch.setattr(
"backend.copilot.tools.agent_browser._ensure_session", AsyncMock()
)
@@ -877,6 +885,202 @@ class TestRunHelper:
assert "not installed" in stderr
+# ---------------------------------------------------------------------------
+# Network egress: every command keeps Chromium behind the egress proxy
+# ---------------------------------------------------------------------------
+
+
+def _spawn_ok() -> MagicMock:
+ proc = MagicMock(returncode=0)
+ proc.communicate = AsyncMock(return_value=(b"", b""))
+ return proc
+
+
+def _injected(cmds: list[list[str]]) -> bool:
+ """Whether any command carries a second, model- or page-supplied
+ --proxy-bypass after the one _run sets."""
+ return any(cmd.count("--proxy-bypass") > 1 for cmd in cmds)
+
+
+class TestBrowserEgress:
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "args",
+ [
+ ("open", "https://example.com"),
+ ("get", "url"),
+ ("cookies", "set", "sid", "abc", "--domain", "example.com"),
+ ("close",),
+ ],
+ )
+ async def test_every_command_launches_chromium_behind_the_proxy(self, args):
+ # agent-browser relaunches Chromium when launch options change, so a
+ # command without them (even `close`) would drop the proxy.
+ with patch("asyncio.create_subprocess_exec", return_value=_spawn_ok()) as spawn:
+ rc, _, _ = await _run("sess", *args)
+
+ assert rc == 0
+ cmd = list(spawn.call_args.args)
+ assert cmd[cmd.index("--proxy") + 1] == _PROXY_URL
+ assert cmd[cmd.index("--proxy-bypass") + 1] == "<-loopback>"
+ assert "--disable-quic" in cmd[cmd.index("--args") + 1].split(",")
+ with open(cmd[cmd.index("--config") + 1]) as f:
+ assert json.load(f) == {}
+ assert cmd[-len(args) :] == list(args)
+
+ @pytest.mark.asyncio
+ async def test_agent_browser_gets_no_configuration_from_the_environment(
+ self, monkeypatch
+ ):
+ # Each of these would attach to or start a browser without the proxy.
+ for name, value in {
+ "AGENT_BROWSER_CDP": "ws://127.0.0.1:9222",
+ "AGENT_BROWSER_PROVIDER": "browserbase",
+ "AGENT_BROWSER_AUTO_CONNECT": "1",
+ "AGENT_BROWSER_EXTENSIONS": "/tmp/extension",
+ "AGENT_BROWSER_PROXY": "",
+ "HTTP_PROXY": "http://proxy.internal:3128",
+ "no_proxy": "*",
+ }.items():
+ monkeypatch.setenv(name, value)
+ monkeypatch.setenv("AGENT_BROWSER_EXECUTABLE_PATH", "/usr/bin/chromium")
+
+ with patch("asyncio.create_subprocess_exec", return_value=_spawn_ok()) as spawn:
+ await _run("sess", "get", "url")
+
+ env = spawn.call_args.kwargs["env"]
+ assert not [
+ name
+ for name in env
+ if name.startswith("AGENT_BROWSER_")
+ and name != "AGENT_BROWSER_EXECUTABLE_PATH"
+ ]
+ assert "HTTP_PROXY" not in env and "no_proxy" not in env
+ assert env["AGENT_BROWSER_EXECUTABLE_PATH"] == "/usr/bin/chromium"
+ assert env["PATH"] == os.environ["PATH"]
+
+ def test_empty_config_is_recreated_after_tmp_cleanup(self):
+ # A long-lived pod can lose /tmp files; a missing --config file would
+ # make every later browser command fail.
+ first = _empty_config()
+ os.unlink(first)
+
+ second = _empty_config()
+
+ with open(second) as f:
+ assert json.load(f) == {}
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "arg",
+ [
+ "--proxy-bypass",
+ "--proxy",
+ "--args",
+ "--executable-path",
+ "--allow-file-access",
+ "--cdp",
+ "--config",
+ "--restore=other-session",
+ "--restore=a b",
+ "-p",
+ # agent-browser's only upper-case flag: prints its version and
+ # exits 0, so the action would report success without running
+ "-V",
+ ],
+ )
+ async def test_refuses_arguments_shaped_like_agent_browser_options(self, arg):
+ with patch("asyncio.create_subprocess_exec") as spawn:
+ rc, _, stderr = await _run("sess", "fill", arg, "*")
+
+ assert rc == 1
+ assert "option" in stderr
+ spawn.assert_not_called()
+
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "value",
+ [
+ "-5",
+ "-",
+ "--",
+ "- first item",
+ "-- Best regards",
+ "a --proxy b",
+ "--Proxy",
+ # base64url cookie values can start with a dash
+ "-Xy9_zQ-token",
+ ],
+ )
+ async def test_passes_ordinary_text_that_starts_with_a_dash(self, value):
+ with patch("asyncio.create_subprocess_exec", return_value=_spawn_ok()) as spawn:
+ rc, _, _ = await _run("sess", "fill", "@e1", value)
+
+ assert rc == 0
+ assert list(spawn.call_args.args)[-1] == value
+
+ @pytest.mark.asyncio
+ async def test_proxy_start_failure_fails_closed(self, monkeypatch):
+ def broken() -> str:
+ raise RuntimeError("bind failed")
+
+ monkeypatch.setattr(
+ "backend.copilot.tools.agent_browser.egress_proxy_url", broken
+ )
+ with patch("asyncio.create_subprocess_exec") as spawn:
+ rc, _, _ = await _run("sess", "open", "https://example.com")
+
+ assert rc == 1
+ spawn.assert_not_called()
+
+ @pytest.mark.asyncio
+ async def test_browser_act_cannot_turn_the_proxy_off(self):
+ spawned: list[list[str]] = []
+
+ async def spawn(*cmd, **kwargs):
+ spawned.append(list(cmd))
+ return _spawn_ok()
+
+ with patch("asyncio.create_subprocess_exec", side_effect=spawn):
+ result = await BrowserActTool()._execute(
+ user_id=None,
+ session=make_session(),
+ action="fill",
+ target="--proxy-bypass",
+ value="*",
+ )
+
+ assert isinstance(result, ErrorResponse)
+ assert not _injected(spawned)
+
+ @pytest.mark.asyncio
+ async def test_restored_cookie_cannot_turn_the_proxy_off(self):
+ # Cookie names come from the sites the browser visited.
+ state = {
+ "url": "",
+ "cookies": [
+ {"name": "--proxy-bypass", "value": "*", "domain": "example.com"},
+ {"name": "sid", "value": "abc", "domain": "example.com"},
+ ],
+ "local_storage": {"--proxy-bypass": "*"},
+ }
+ mock_mgr = _make_mock_manager()
+ mock_mgr.get_file_info_by_path.return_value = MagicMock(id="file-1")
+ mock_mgr.read_file.return_value = json.dumps(state).encode("utf-8")
+ spawned: list[list[str]] = []
+
+ async def spawn(*cmd, **kwargs):
+ spawned.append(list(cmd))
+ return _spawn_ok()
+
+ with patch("asyncio.create_subprocess_exec", side_effect=spawn):
+ with patch(_GET_MANAGER, new_callable=AsyncMock, return_value=mock_mgr):
+ await _restore_browser_state("sess", "user1", make_session("sess"))
+
+ assert not _injected(spawned)
+ assert any("sid" in cmd for cmd in spawned)
+
+
# ---------------------------------------------------------------------------
# _has_local_session
# ---------------------------------------------------------------------------
diff --git a/autogpt_platform/backend/backend/util/egress_proxy.py b/autogpt_platform/backend/backend/util/egress_proxy.py
new file mode 100644
index 0000000000..80d1f99fb2
--- /dev/null
+++ b/autogpt_platform/backend/backend/util/egress_proxy.py
@@ -0,0 +1,220 @@
+"""A loopback SOCKS5 proxy that only connects to public internet addresses.
+
+`backend.util.request.Requests` checks every hop of every request it makes, but
+programs that do their own networking (the Chromium behind the Copilot browser
+tools) never pass through it: their redirects, page scripts, subresources and
+DNS lookups would reach internal services unchecked. Pointing such a program at
+this proxy puts the same rule on every connection it opens. The proxy resolves
+the target itself, refuses it unless every address is public, and dials the
+exact address it checked, so a DNS answer cannot change between the check and
+the connection.
+"""
+
+import asyncio
+import concurrent.futures
+import ipaddress
+import logging
+import os
+import socket
+import threading
+
+from backend.util.request import resolve_and_check_blocked
+
+logger = logging.getLogger(__name__)
+
+_SETUP_TIMEOUT = 30
+_CONNECT_TIMEOUT = 10
+_STARTUP_TIMEOUT = 10
+_CHUNK_SIZE = 64 * 1024
+
+# SOCKS5 wire values (RFC 1928).
+_SOCKS_VERSION = 5
+_NO_AUTH = 0
+_NO_ACCEPTABLE_METHODS = 0xFF
+_CONNECT = 1
+_ADDR_IPV4 = 1
+_ADDR_DOMAIN = 3
+_ADDR_IPV6 = 4
+_SUCCEEDED = 0
+_NOT_ALLOWED = 2
+_HOST_UNREACHABLE = 4
+_COMMAND_NOT_SUPPORTED = 7
+_ADDRESS_TYPE_NOT_SUPPORTED = 8
+
+_lock = threading.Lock()
+_proxy_url: str | None = None
+_proxy_pid: int | None = None
+
+
+def egress_proxy_url() -> str:
+ """Return this process's proxy as a ``socks5://`` URL, starting it on first
+ use."""
+ global _proxy_url, _proxy_pid
+ with _lock:
+ if _proxy_url is None or _proxy_pid != os.getpid():
+ _proxy_url = _start()
+ _proxy_pid = os.getpid()
+ return _proxy_url
+
+
+def _start() -> str:
+ # Its own thread and loop, so the proxy outlives whichever event loop first
+ # asked for it: the Copilot executor runs each turn on a worker loop.
+ loop = asyncio.new_event_loop()
+ started: concurrent.futures.Future[int] = concurrent.futures.Future()
+
+ def serve() -> None:
+ asyncio.set_event_loop(loop)
+ try:
+ server = loop.run_until_complete(
+ asyncio.start_server(_handle, "127.0.0.1", 0)
+ )
+ except Exception as e:
+ started.set_exception(e)
+ return
+ started.set_result(server.sockets[0].getsockname()[1])
+ loop.run_forever()
+
+ threading.Thread(target=serve, name="egress-proxy", daemon=True).start()
+ return f"socks5://127.0.0.1:{started.result(timeout=_STARTUP_TIMEOUT)}"
+
+
+# IPv6 ranges that carry an IPv4 address a gateway may forward to: NAT64
+# (RFC 6052, RFC 8215), 6to4 (RFC 3056) and IPv4-compatible (RFC 4291).
+# ipaddress.is_global passes some of these depending on the Python version, and
+# on a network that routes them they reach the embedded, possibly private, IPv4.
+_IPV4_EMBEDDING_NETWORKS = (
+ ipaddress.ip_network("64:ff9b::/96"),
+ ipaddress.ip_network("64:ff9b:1::/48"),
+ ipaddress.ip_network("2002::/16"),
+ ipaddress.ip_network("::/96"),
+)
+
+
+def _is_public(address: str) -> bool:
+ ip = ipaddress.ip_address(address)
+ if isinstance(ip, ipaddress.IPv6Address):
+ if ip.ipv4_mapped:
+ ip = ip.ipv4_mapped
+ elif ip.teredo or any(ip in net for net in _IPV4_EMBEDDING_NETWORKS):
+ return False
+ return ip.is_global
+
+
+async def public_addresses(host: str) -> list[str]:
+ """Resolve *host* and return its addresses.
+
+ Raises ``ValueError`` unless every address is public: outside the shared
+ SSRF blocklist, globally routable, and not an IPv6 address that embeds an
+ IPv4 one.
+ """
+ if not host or not host.isascii():
+ raise ValueError(f"Invalid host {host!r}")
+ addresses = await resolve_and_check_blocked(host)
+ for address in addresses:
+ if not _is_public(address):
+ raise ValueError(
+ f"Access to non-public IP address {address} "
+ f"for hostname {host} is not allowed."
+ )
+ return addresses
+
+
+async def _handle(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None:
+ upstream: tuple[asyncio.StreamReader, asyncio.StreamWriter] | None = None
+ try:
+ upstream = await asyncio.wait_for(_open(reader, writer), _SETUP_TIMEOUT)
+ if upstream is not None:
+ upstream_reader, upstream_writer = upstream
+ await asyncio.gather(
+ _pipe(reader, upstream_writer), _pipe(upstream_reader, writer)
+ )
+ except (asyncio.IncompleteReadError, OSError):
+ pass
+ finally:
+ writer.close()
+ if upstream is not None:
+ upstream[1].close()
+
+
+async def _open(
+ reader: asyncio.StreamReader, writer: asyncio.StreamWriter
+) -> tuple[asyncio.StreamReader, asyncio.StreamWriter] | None:
+ """Read the client's SOCKS5 request and connect it if the target is public.
+ Returns None after replying with an error."""
+ version, method_count = await reader.readexactly(2)
+ methods = await reader.readexactly(method_count)
+ if version != _SOCKS_VERSION or _NO_AUTH not in methods:
+ writer.write(bytes([_SOCKS_VERSION, _NO_ACCEPTABLE_METHODS]))
+ return None
+ writer.write(bytes([_SOCKS_VERSION, _NO_AUTH]))
+
+ _, command, _, address_type = await reader.readexactly(4)
+ if address_type == _ADDR_IPV4:
+ host = socket.inet_ntop(socket.AF_INET, await reader.readexactly(4))
+ elif address_type == _ADDR_IPV6:
+ host = socket.inet_ntop(socket.AF_INET6, await reader.readexactly(16))
+ elif address_type == _ADDR_DOMAIN:
+ length = (await reader.readexactly(1))[0]
+ host = (await reader.readexactly(length)).decode("ascii", "replace")
+ else:
+ await _reply(writer, _ADDRESS_TYPE_NOT_SUPPORTED)
+ return None
+ port = int.from_bytes(await reader.readexactly(2), "big")
+ if command != _CONNECT:
+ await _reply(writer, _COMMAND_NOT_SUPPORTED)
+ return None
+
+ try:
+ addresses = await public_addresses(host)
+ except ValueError as e:
+ logger.info(f"Egress proxy refused {host!r} port {port}: {e}")
+ await _reply(writer, _NOT_ALLOWED)
+ return None
+
+ # Dial the checked addresses, never the name: resolving it again could
+ # return a different answer. Only IPv4 is dialed: a network may run NAT64
+ # under any prefix its operator picks, and such an address forwards to an
+ # embedded, possibly private, IPv4 one while looking like a public IPv6
+ # address. No address check can tell the two apart.
+ dialable = [
+ a for a in dict.fromkeys(addresses) if ipaddress.ip_address(a).version == 4
+ ]
+ if not dialable:
+ logger.info(f"Egress proxy refused {host!r} port {port}: no IPv4 address")
+ await _reply(writer, _NOT_ALLOWED)
+ return None
+ for address in dialable:
+ try:
+ upstream = await asyncio.wait_for(
+ asyncio.open_connection(address, port), _CONNECT_TIMEOUT
+ )
+ except OSError:
+ continue
+ try:
+ await _reply(writer, _SUCCEEDED)
+ except BaseException:
+ upstream[1].close()
+ raise
+ return upstream
+
+ await _reply(writer, _HOST_UNREACHABLE)
+ return None
+
+
+async def _reply(writer: asyncio.StreamWriter, code: int) -> None:
+ writer.write(bytes([_SOCKS_VERSION, code, 0, _ADDR_IPV4, 0, 0, 0, 0, 0, 0]))
+ await writer.drain()
+
+
+async def _pipe(source: asyncio.StreamReader, sink: asyncio.StreamWriter) -> None:
+ """Copy *source* into *sink* until either side closes, then close *sink* so
+ the opposite direction ends too."""
+ try:
+ while data := await source.read(_CHUNK_SIZE):
+ sink.write(data)
+ await sink.drain()
+ except OSError:
+ pass
+ finally:
+ sink.close()
diff --git a/autogpt_platform/backend/backend/util/egress_proxy_test.py b/autogpt_platform/backend/backend/util/egress_proxy_test.py
new file mode 100644
index 0000000000..a0d87d5e78
--- /dev/null
+++ b/autogpt_platform/backend/backend/util/egress_proxy_test.py
@@ -0,0 +1,226 @@
+import asyncio
+import socket
+
+import pytest
+
+from backend.util import egress_proxy
+from backend.util.egress_proxy import egress_proxy_url, public_addresses
+
+
+async def _socks_connect(
+ proxy_url: str, host: str, port: int, address_type: int = 3, command: int = 1
+) -> tuple[int, asyncio.StreamReader, asyncio.StreamWriter]:
+ """Open a SOCKS5 CONNECT through the proxy; return the reply code."""
+ proxy_port = int(proxy_url.rsplit(":", 1)[1])
+ reader, writer = await asyncio.open_connection("127.0.0.1", proxy_port)
+ writer.write(b"\x05\x01\x00")
+ assert await reader.readexactly(2) == b"\x05\x00"
+ if address_type == 1:
+ address = socket.inet_pton(socket.AF_INET, host)
+ elif address_type == 4:
+ address = socket.inet_pton(socket.AF_INET6, host)
+ else:
+ address = bytes([len(host)]) + host.encode()
+ writer.write(
+ bytes([5, command, 0, address_type]) + address + port.to_bytes(2, "big")
+ )
+ reply = await reader.readexactly(10)
+ return reply[1], reader, writer
+
+
+class _Server:
+ """A loopback TCP server that echoes and counts its connections."""
+
+ def __init__(self) -> None:
+ self.connections = 0
+ self.server: asyncio.Server | None = None
+
+ async def __aenter__(self) -> "_Server":
+ self.server = await asyncio.start_server(self._echo, "127.0.0.1", 0)
+ return self
+
+ async def __aexit__(self, *exc) -> None:
+ assert self.server is not None
+ self.server.close()
+ await self.server.wait_closed()
+
+ @property
+ def port(self) -> int:
+ assert self.server is not None
+ return self.server.sockets[0].getsockname()[1]
+
+ async def _echo(self, reader, writer) -> None:
+ self.connections += 1
+ writer.write(await reader.read(1024))
+ await writer.drain()
+ writer.close()
+
+
+ "host",
+ [
+ "127.0.0.1",
+ "localhost",
+ "10.0.0.1",
+ "172.16.0.1",
+ "192.168.1.1",
+ "169.254.169.254",
+ "100.64.0.1",
+ "0.0.0.0",
+ "192.0.2.1",
+ "::1",
+ "::",
+ "::ffff:127.0.0.1",
+ "fd00::1",
+ "fe80::1",
+ "fd00:ec2::254",
+ # IPv6 forms that carry an IPv4 address a gateway may forward to
+ "64:ff9b::a00:1",
+ "64:ff9b::7f00:1",
+ "64:ff9b::a9fe:a9fe",
+ "64:ff9b:1::a00:1",
+ "2002:a00:1::1",
+ "2002:7f00:1::1",
+ "2001:0:4136:e378:8000:63bf:f5ff:fffe",
+ "::a00:1",
+ "::7f00:1",
+ # refused even when the embedded IPv4 is public
+ "64:ff9b::808:808",
+ "2002:808:808::1",
+ ],
+)
+async def test_public_addresses_refuses_non_public_hosts(host):
+ with pytest.raises(ValueError):
+ await public_addresses(host)
+
+
+ "host", ["8.8.8.8", "1.1.1.1", "::ffff:8.8.8.8", "2606:4700:4700::1111"]
+)
+async def test_public_addresses_allows_public_hosts(host):
+ assert set(await public_addresses(host)) == {host}
+
+
+ "host, address_type",
+ [("127.0.0.1", 1), ("127.0.0.1", 3), ("localhost", 3), ("::ffff:127.0.0.1", 4)],
+)
+async def test_proxy_refuses_loopback_without_connecting(host, address_type):
+ async with _Server() as server:
+ code, _, writer = await _socks_connect(
+ egress_proxy_url(), host, server.port, address_type
+ )
+ writer.close()
+
+ assert code == egress_proxy._NOT_ALLOWED
+ assert server.connections == 0
+
+
+async def test_proxy_dials_the_checked_address_not_the_name(monkeypatch):
+ # "public.test" does not resolve; the relay only works because the proxy
+ # connects to the address it checked instead of resolving the name again.
+ async def vetted(host: str) -> list[str]:
+ assert host == "public.test"
+ return ["127.0.0.1"]
+
+ monkeypatch.setattr(egress_proxy, "public_addresses", vetted)
+ async with _Server() as server:
+ code, reader, writer = await _socks_connect(
+ egress_proxy_url(), "public.test", server.port
+ )
+ writer.write(b"ping")
+ echoed = await reader.read(1024)
+ writer.close()
+
+ assert code == egress_proxy._SUCCEEDED
+ assert echoed == b"ping"
+ assert server.connections == 1
+
+
+async def test_proxy_never_dials_ipv6():
+ # Passes every address check, as an address under an operator's own NAT64
+ # prefix would, yet forwards to whatever IPv4 address it embeds.
+ code, _, writer = await _socks_connect(
+ egress_proxy_url(), "2600:1f18:64::a00:1", 80, address_type=4
+ )
+ writer.close()
+
+ assert await public_addresses("2600:1f18:64::a00:1")
+ assert code == egress_proxy._NOT_ALLOWED
+
+
+async def test_proxy_dials_the_ipv4_address_of_a_dual_stack_name(monkeypatch):
+ async def vetted(host: str) -> list[str]:
+ return ["2600:1f18:64::a00:1", "127.0.0.1"]
+
+ monkeypatch.setattr(egress_proxy, "public_addresses", vetted)
+ async with _Server() as server:
+ code, reader, writer = await _socks_connect(
+ egress_proxy_url(), "dual.test", server.port
+ )
+ writer.write(b"ping")
+ echoed = await reader.read(1024)
+ writer.close()
+
+ assert code == egress_proxy._SUCCEEDED
+ assert echoed == b"ping"
+
+
+async def test_proxy_refuses_a_client_without_a_supported_auth_method():
+ proxy_port = int(egress_proxy_url().rsplit(":", 1)[1])
+ reader, writer = await asyncio.open_connection("127.0.0.1", proxy_port)
+ writer.write(b"\x05\x01\x02") # offers username/password only
+ reply = await reader.readexactly(2)
+ writer.close()
+
+ assert reply == bytes([5, egress_proxy._NO_ACCEPTABLE_METHODS])
+
+
+async def test_proxy_refuses_an_unknown_address_type():
+ code, _, writer = await _socks_connect(
+ egress_proxy_url(), "example.com", 80, address_type=9
+ )
+ writer.close()
+
+ assert code == egress_proxy._ADDRESS_TYPE_NOT_SUPPORTED
+
+
+async def test_proxy_reports_an_unreachable_target(monkeypatch):
+ async def vetted(host: str) -> list[str]:
+ return ["127.0.0.1"]
+
+ monkeypatch.setattr(egress_proxy, "public_addresses", vetted)
+ with socket.socket() as closed:
+ closed.bind(("127.0.0.1", 0))
+ port = closed.getsockname()[1]
+ code, _, writer = await _socks_connect(egress_proxy_url(), "public.test", port)
+ writer.close()
+
+ assert code == egress_proxy._HOST_UNREACHABLE
+
+
+async def test_proxy_refuses_commands_other_than_connect():
+ async with _Server() as server:
+ code, _, writer = await _socks_connect(
+ egress_proxy_url(), "127.0.0.1", server.port, address_type=1, command=3
+ )
+ writer.close()
+
+ assert code == egress_proxy._COMMAND_NOT_SUPPORTED
+ assert server.connections == 0
+
+
+def test_proxy_url_is_stable_within_a_process():
+ url = egress_proxy_url()
+ assert url.startswith("socks5://127.0.0.1:")
+ assert egress_proxy_url() == url