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): one chokepoint for every E2B create and connect, where egress is pinned (#14624)
Krzysztof Czerwinski81c8b47349b0
diff --git a/autogpt_platform/backend/.env.default b/autogpt_platform/backend/.env.defaultindex 81e5741851..82dcc7065b 100644--- a/autogpt_platform/backend/.env.default+++ b/autogpt_platform/backend/.env.default@@ -283,6 +283,10 @@ ELEVENLABS_API_KEY=# Data & Search ServicesE2B_API_KEY=+# host:port of the credential swap proxy every E2B box egresses through+# (backend.util.e2b_network). Leave empty until the proxy exists: E2B fails+# closed, so a box pointed at nothing has no egress at all.+E2B_EGRESS_PROXY_ADDRESS=EXA_API_KEY=JINA_API_KEY=MEM0_API_KEY=diff --git a/autogpt_platform/backend/backend/blocks/claude_code.py b/autogpt_platform/backend/backend/blocks/claude_code.pyindex fa771c3489..3709a3a6ec 100644--- a/autogpt_platform/backend/backend/blocks/claude_code.py+++ b/autogpt_platform/backend/backend/blocks/claude_code.py@@ -21,6 +21,12 @@ from backend.data.model import (SchemaField,)from backend.integrations.providers import ProviderName+from backend.util.e2b_network import (+ EgressOwner,+ connect_sandbox,+ create_sandbox,+ kill_sandbox,+)from backend.util.sandbox_files import (SandboxFileOutput,extract_and_store_sandbox_files,@@ -321,6 +327,11 @@ class ClaudeCodeBlock(Block):sandbox = Nonesandbox_id = ""+ egress_owner = EgressOwner(+ kind="block",+ id=execution_context.user_id or "anonymous",+ user_id=execution_context.user_id,+ )try:# Either reconnect to existing sandbox or create a new one@@ -338,13 +349,17 @@ class ClaudeCodeBlock(Block):raise PermissionError(f"Sandbox {existing_sandbox_id} does not belong to this user")- sandbox = await BaseAsyncSandbox.connect(- sandbox_id=existing_sandbox_id,+ sandbox = await connect_sandbox(+ BaseAsyncSandbox,+ existing_sandbox_id,+ egress_owner,api_key=e2b_api_key,)else:# Create new sandbox- sandbox = await BaseAsyncSandbox.create(+ sandbox = await create_sandbox(+ BaseAsyncSandbox,+ egress_owner,template=self.DEFAULT_TEMPLATE,api_key=e2b_api_key,timeout=timeout,@@ -499,7 +514,7 @@ class ClaudeCodeBlock(Block):finally:if dispose_sandbox and sandbox:- await sandbox.kill()+ await kill_sandbox(sandbox)def _escape_prompt(self, prompt: str) -> str:"""Escape the prompt for safe shell execution."""diff --git a/autogpt_platform/backend/backend/blocks/code_executor.py b/autogpt_platform/backend/backend/blocks/code_executor.pyindex f384f9f41a..981506903a 100644--- a/autogpt_platform/backend/backend/blocks/code_executor.py+++ b/autogpt_platform/backend/backend/blocks/code_executor.py@@ -23,6 +23,12 @@ from backend.data.model import (SchemaField,)from backend.integrations.providers import ProviderName+from backend.util.e2b_network import (+ EgressOwner,+ connect_sandbox,+ create_sandbox,+ kill_sandbox,+)from backend.util.sandbox_files import (SandboxFileOutput,extract_and_store_sandbox_files,@@ -144,6 +150,10 @@ class BaseE2BExecutorMixin:""" # noqasandbox = Nonefiles: list[SandboxFileOutput] = []+ owner_user_id = execution_context.user_id if execution_context else None+ egress_owner = EgressOwner(+ kind="block", id=owner_user_id or "anonymous", user_id=owner_user_id+ )try:if sandbox_id:# Connect to existing sandbox (ExecuteCodeStepBlock case). The@@ -158,12 +168,14 @@ class BaseE2BExecutorMixin:raise PermissionError(f"Sandbox {sandbox_id} does not belong to this user")- sandbox = await AsyncSandbox.connect(- sandbox_id=sandbox_id, api_key=api_key+ sandbox = await connect_sandbox(+ AsyncSandbox, sandbox_id, egress_owner, api_key=api_key)else:# Create new sandbox (ExecuteCodeBlock/InstantiateCodeSandboxBlock case)- sandbox = await AsyncSandbox.create(+ sandbox = await create_sandbox(+ AsyncSandbox,+ egress_owner,api_key=api_key,template=template_id,timeout=timeout,@@ -184,7 +196,7 @@ class BaseE2BExecutorMixin:code,language=language.value,envs=envs or {},- on_error=lambda e: sandbox.kill(), # Kill the sandbox on error+ on_error=lambda e: kill_sandbox(sandbox), # Kill the sandbox on error)if execution.error:@@ -216,7 +228,7 @@ class BaseE2BExecutorMixin:finally:# Dispose of sandbox if requested to reduce usage costsif dispose_sandbox and sandbox:- await sandbox.kill()+ await kill_sandbox(sandbox)def process_execution_results(self, results: list[E2BExecutionResult]@@ -518,6 +530,7 @@ class InstantiateCodeSandboxBlock(Block, BaseE2BExecutorMixin):api_key=credentials.api_key.get_secret_value(),code=input_data.setup_code,language=input_data.language,+ execution_context=execution_context,template_id=input_data.template_id,setup_commands=input_data.setup_commands,timeout=input_data.timeout,diff --git a/autogpt_platform/backend/backend/blocks/code_executor_test.py b/autogpt_platform/backend/backend/blocks/code_executor_test.pyindex 3d7ddf4413..dfb377bb59 100644--- a/autogpt_platform/backend/backend/blocks/code_executor_test.py+++ b/autogpt_platform/backend/backend/blocks/code_executor_test.py@@ -19,6 +19,7 @@ from backend.blocks.code_executor import (TEST_CREDENTIALS_INPUT,ExecuteCodeBlock,ExecuteCodeStepBlock,+ InstantiateCodeSandboxBlock,ProgrammingLanguage,)from backend.blocks.code_executor_helpers import (@@ -333,6 +334,24 @@ class TestConnectToExistingSandbox:assert execute.await_args.kwargs["execution_context"] is contextassert execute.await_args.kwargs["sandbox_id"] == "sb-mine"+ async def test_instantiate_block_hands_the_caller_to_the_box_it_creates(self):+ """The box is created for this user: without the context its egress+ credential would name nobody, and the proxy could swap nothing in."""+ block = InstantiateCodeSandboxBlock()+ context = ExecutionContext(user_id="user-a", graph_exec_id="gexec-1")+ with patch.object(+ block, "execute_code", AsyncMock(return_value=([], "", "", "", "sb", []))+ ) as execute:+ async for _ in block.run(+ InstantiateCodeSandboxBlock.Input(+ credentials=TEST_CREDENTIALS_INPUT, setup_code="print(1)"+ ),+ credentials=TEST_CREDENTIALS,+ execution_context=context,+ ):+ pass+ assert execute.await_args.kwargs["execution_context"] is context+async def test_without_a_user_no_existing_sandbox_can_be_used(self):block = ExecuteCodeBlock()box = self._sandbox(diff --git a/autogpt_platform/backend/backend/blocks/desktop/_api.py b/autogpt_platform/backend/backend/blocks/desktop/_api.pyindex cf42b631c8..95911309ac 100644--- a/autogpt_platform/backend/backend/blocks/desktop/_api.py+++ b/autogpt_platform/backend/backend/blocks/desktop/_api.py@@ -17,6 +17,13 @@ from typing import Literal, Mapping, Optionalfrom e2b import AsyncSandbox, AsyncVolume, SandboxLifecyclefrom pydantic import BaseModel+from backend.util.e2b_network import (+ EgressOwner,+ connect_sandbox,+ create_sandbox,+ kill_sandbox,+)+DESKTOP_TEMPLATE = "desktop"HOME_PATH = "/home/user"WORKSPACE_PATH = "/home/user/workspace"@@ -100,16 +107,18 @@ class DesktopSession:volume_mounts: Optional[Mapping[str, str]] = None,template: str = DESKTOP_TEMPLATE,metadata: Optional[Mapping[str, str]] = None,+ *,+ owner: EgressOwner,) -> tuple["DesktopSession", PersistenceInfo]:"""Create a desktop sandbox.*volume_mounts* maps mount paths to durable volume names (see``workspace_volume_mounts``); *metadata* is stamped on the sandbox soits owner can find it again through the E2B API if the cached id is- lost.+ lost; *owner* is who the egress proxy sees the box as."""sandbox, persistence = await _create_sandbox_with_volumes(- volume_mounts, api_key, timeout_seconds, template, metadata+ volume_mounts, api_key, timeout_seconds, template, metadata, owner=owner)session = cls(sandbox)try:@@ -127,19 +136,26 @@ class DesktopSession:# rather than leak a sandbox that would bill until timeout and# then sit paused forever.with contextlib.suppress(Exception):- await asyncio.wait_for(sandbox.kill(), timeout=_KILL_TIMEOUT_SECONDS)+ await asyncio.wait_for(+ kill_sandbox(sandbox), timeout=_KILL_TIMEOUT_SECONDS+ )raisereturn session, persistence@classmethodasync def connect(- cls, sandbox_id: str, api_key: str, timeout_seconds: Optional[int] = None+ cls,+ sandbox_id: str,+ api_key: str,+ timeout_seconds: Optional[int] = None,+ *,+ owner: EgressOwner,) -> "DesktopSession":"""Reattach to a desktop; *timeout_seconds* re-arms its running-timelimit, otherwise the SDK's 300 s default would pause a resumed desktopunder the user long before a freshly created one."""- sandbox = await AsyncSandbox.connect(- sandbox_id, api_key=api_key, timeout=timeout_seconds+ sandbox = await connect_sandbox(+ AsyncSandbox, sandbox_id, owner, api_key=api_key, timeout=timeout_seconds)return cls(sandbox)@@ -281,7 +297,7 @@ class DesktopSession:await self.sandbox.pause()async def kill(self) -> None:- await self.sandbox.kill()+ await kill_sandbox(self.sandbox)async def stop_stream(self) -> None:"""Stop serving the screen; the display itself stays up.@@ -378,11 +394,14 @@ async def _create_sandbox_with_volumes(timeout_seconds: int,template: str = DESKTOP_TEMPLATE,metadata: Optional[Mapping[str, str]] = None,+ *,+ owner: EgressOwner,) -> tuple[AsyncSandbox, PersistenceInfo]:kwargs = _sandbox_create_kwargs(api_key, timeout_seconds, template, metadata)if not volume_mounts:sandbox = await asyncio.wait_for(- AsyncSandbox.create(**kwargs), timeout=CREATE_TIMEOUT_SECONDS+ create_sandbox(AsyncSandbox, owner, **kwargs),+ timeout=CREATE_TIMEOUT_SECONDS,)return sandbox, PersistenceInfo()@@ -395,7 +414,7 @@ async def _create_sandbox_with_volumes(for attempt in range(1, MOUNTED_CREATE_ATTEMPTS + 1):try:sandbox = await asyncio.wait_for(- AsyncSandbox.create(**kwargs, volume_mounts=mounts),+ create_sandbox(AsyncSandbox, owner, **kwargs, volume_mounts=mounts),timeout=CREATE_TIMEOUT_SECONDS,)except Exception as exc:@@ -415,7 +434,7 @@ async def _create_sandbox_with_volumes(if kwargs.get("metadata"):kwargs["metadata"] = {**kwargs["metadata"], "autogpt_mounts": "none"}sandbox = await asyncio.wait_for(- AsyncSandbox.create(**kwargs), timeout=CREATE_TIMEOUT_SECONDS+ create_sandbox(AsyncSandbox, owner, **kwargs), timeout=CREATE_TIMEOUT_SECONDS)return sandbox, PersistenceInfo(warning=(diff --git a/autogpt_platform/backend/backend/blocks/desktop/desktop_api_test.py b/autogpt_platform/backend/backend/blocks/desktop/desktop_api_test.pyindex 3d90db6fb0..c3d6386b7f 100644--- a/autogpt_platform/backend/backend/blocks/desktop/desktop_api_test.py+++ b/autogpt_platform/backend/backend/blocks/desktop/desktop_api_test.py@@ -10,9 +10,11 @@ from backend.blocks.desktop._api import (DesktopSession,_create_sandbox_with_volumes,)+from backend.util.e2b_network import EgressOwner_M = "backend.blocks.desktop._api"_META = {"service": "autogpt-platform", "autogpt_mounts": "attached"}+_OWNER = EgressOwner(kind="session", id="sess-1", user_id="user-1")@pytest.fixture(autouse=True)@@ -30,7 +32,7 @@ async def test_mounted_create_is_retried_once_before_falling_back():):cls.create = AsyncMock(side_effect=[RuntimeError("502"), box])sandbox, info = await _create_sandbox_with_volumes(- {WORKSPACE_PATH: "vol-user"}, "k", 900, metadata=_META+ {WORKSPACE_PATH: "vol-user"}, "k", 900, metadata=_META, owner=_OWNER)assert sandbox is box and info.volume_mounted and info.warning is Noneassert cls.create.await_count == 2@@ -48,7 +50,7 @@ async def test_fallback_after_every_mounted_attempt_says_so_in_the_stamp():side_effect=[RuntimeError("no volumes"), RuntimeError("no volumes"), box])sandbox, info = await _create_sandbox_with_volumes(- {WORKSPACE_PATH: "vol-user"}, "k", 900, metadata=_META+ {WORKSPACE_PATH: "vol-user"}, "k", 900, metadata=_META, owner=_OWNER)assert sandbox is box and not info.volume_mounted and info.warningfallback = cls.create.await_args_list[-1].kwargs@@ -70,12 +72,12 @@ async def test_a_create_that_hangs_is_cut_off():):cls.create = AsyncMock(side_effect=never)with pytest.raises(asyncio.TimeoutError):- await _create_sandbox_with_volumes(None, "k", 900)+ await _create_sandbox_with_volumes(None, "k", 900, owner=_OWNER)@pytest.mark.asyncioasync def test_connect_rearms_the_running_time_limit():with patch(f"{_M}.AsyncSandbox") as cls:cls.connect = AsyncMock(return_value=MagicMock())- await DesktopSession.connect("sb-1", "k", timeout_seconds=900)+ await DesktopSession.connect("sb-1", "k", timeout_seconds=900, owner=_OWNER)cls.connect.assert_awaited_once_with("sb-1", api_key="k", timeout=900)diff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.pyindex 8b0a6c4221..2e4ca01511 100644--- a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py@@ -89,8 +89,14 @@ from pydantic import BaseModel, ConfigDictfrom backend.blocks.desktop._api import DesktopSession, resolve_volumefrom backend.data.redis_client import get_redis_async+from backend.util.e2b_network import (+ EgressOwner,+ connect_sandbox,+ create_sandbox,+ forget_sandbox,+)from backend.util.e2b_template import ensure_template, forget_template-from backend.util.sandbox_metadata import MountState, SandboxMetadata+from backend.util.sandbox_metadata import MountState, SandboxMetadata, owned_by_userlogger = logging.getLogger(__name__)@@ -240,6 +246,10 @@ class SandboxOwner(BaseModel):"""return f"{self.key()}:stream"+ def egress_owner(self, user_id: str | None) -> EgressOwner:+ """Who the egress proxy sees this box as (``backend.util.e2b_network``)."""+ return EgressOwner(kind=self.kind, id=self.id, user_id=user_id)+def display_lock_key(self) -> str:"""Redis key held by whoever is turning the screen on right now."""return f"{self.display_key()}:lock"@@ -291,6 +301,8 @@ async def connect_owned(api_key: str,*,timeout: int | None = None,+ user_id: str | None = None,+ pin_egress: bool = True,) -> AsyncSandbox:"""Connect to *sandbox_id* only if E2B says it belongs to *owner*.@@ -303,10 +315,20 @@ async def connect_owned(resumes a paused box and re-arms its running-time limit, so a foreignid must be refused without ever waking someone else's box. *timeout*is that limit for the owner's box (a resumed box would otherwise get the- SDK's default).+ SDK's default). A connect that will run work re-pins the box's egress+ (``backend.util.e2b_network``) for *user_id*; one that only pauses or+ kills passes ``pin_egress=False``."""- await _owned_info(sandbox_id, owner, api_key)- return await AsyncSandbox.connect(sandbox_id, api_key=api_key, timeout=timeout)+ info = await _owned_info(sandbox_id, owner, api_key)+ return await _connect_pinned(+ sandbox_id,+ info,+ owner,+ api_key,+ timeout=timeout,+ user_id=user_id,+ pin_egress=pin_egress,+ )async def _owned_info(@@ -321,6 +343,41 @@ async def _owned_info(return info+async def _connect_pinned(+ sandbox_id: str,+ info: SandboxInfo,+ owner: SandboxOwner,+ api_key: str,+ *,+ timeout: int | None,+ user_id: str | None,+ pin_egress: bool,+) -> AsyncSandbox:+ """Connect to *sandbox_id*, which *info* already showed to be *owner*'s."""+ stamped = info.metadata or {}+ # Whose credentials the proxy may swap in is the box's own record too,+ # not the caller's word: processes of the user it was created for may+ # still be running in it. An expert has one owner today, but nothing at+ # this layer says so, and a box re-pinned for whoever reconnects would+ # let those processes act as them. A mismatch pins the box with no user:+ # it keeps its egress and gets nothing swapped in.+ swap_user_id = user_id if owned_by_user(stamped, user_id) else None+ if pin_egress and user_id and swap_user_id is None:+ logger.warning(+ "[E2B] Sandbox %.12s was not created for the user reconnecting to "+ "it; pinning it without credentials",+ sandbox_id,+ )+ return await connect_sandbox(+ AsyncSandbox,+ sandbox_id,+ owner.egress_owner(swap_user_id),+ apply_network=pin_egress,+ api_key=api_key,+ timeout=timeout,+ )++def _as_owner(owner: "SandboxOwner | str") -> SandboxOwner:"""Accept a bare session id where an owner is expected."""if isinstance(owner, SandboxOwner):@@ -419,6 +476,7 @@ async def _try_reconnect(api_key: str,*,timeout: int | None = None,+ user_id: str | None = None,) -> "AsyncSandbox | None":"""Reconnect to the owner's box, or ``None`` if it is gone.@@ -435,8 +493,14 @@ async def _try_reconnect(# wakes anything. The state read with it says whether this connect is# what resumes the box.info = await _owned_info(sandbox_id, owner, api_key)- sandbox = await AsyncSandbox.connect(- sandbox_id, api_key=api_key, timeout=timeout+ sandbox = await _connect_pinned(+ sandbox_id,+ info,+ owner,+ api_key,+ timeout=timeout,+ user_id=user_id,+ pin_egress=True,)except SandboxNotOwnedError as exc:logger.warning("[E2B] Refusing reconnect: %s", exc)@@ -600,7 +664,9 @@ async def get_or_create_owner_sandbox(if value and value != _CREATING_SENTINEL:# Existing sandbox ID — try to reconnect (auto-resumes if paused).try:- sandbox = await _try_reconnect(value, owner, api_key, timeout=timeout)+ sandbox = await _try_reconnect(+ value, owner, api_key, timeout=timeout, user_id=user_id+ )except Exception as exc:if value in retried_ids:raise@@ -666,7 +732,9 @@ async def get_or_create_owner_sandbox(for attempt in range(1, _SANDBOX_CREATE_MAX_RETRIES + 1):try:sandbox = await asyncio.wait_for(- AsyncSandbox.create(+ create_sandbox(+ AsyncSandbox,+ owner.egress_owner(user_id),template=template,api_key=api_key,timeout=timeout,@@ -728,6 +796,7 @@ async def get_or_create_owner_sandbox(await asyncio.wait_for(sandbox.kill(), timeout=_E2B_API_TIMEOUT_SECONDS)+ await forget_sandbox(sandbox.sandbox_id)raiseexcept asyncio.CancelledError:# Task cancelled during creation — release the slot so followers@@ -744,6 +813,8 @@ async def get_or_create_owner_sandbox(await asyncio.wait_for(sandbox.kill(), timeout=_E2B_API_TIMEOUT_SECONDS)+ with contextlib.suppress(Exception, asyncio.CancelledError):+ await forget_sandbox(sandbox.sandbox_id)raiseexcept Exception:# Release the creation slot so other callers can proceed.@@ -812,10 +883,14 @@ async def _act_on_sandbox(return Falseasync def _run() -> None:- await fn(await connect_owned(sandbox_id, owner, api_key))+ # Nothing egresses before a pause or kill: no re-pin.+ await fn(await connect_owned(sandbox_id, owner, api_key, pin_egress=False))try:await asyncio.wait_for(_run(), timeout=timeout)+ # Paused or killed, the box will not present its proxy credential+ # again: a resume mints a fresh one.+ await forget_sandbox(sandbox_id)if clear_stored_id:await _clear_stored_sandbox_id(owner)logger.info(@@ -899,6 +974,7 @@ async def pause_sandbox_direct(try:await asyncio.wait_for(sandbox.pause(), timeout=_E2B_API_TIMEOUT_SECONDS)logger.info("[E2B] Paused sandbox %.12s for %s", sandbox.sandbox_id, owner)+ await forget_sandbox(sandbox.sandbox_id)if not revoked:await _forget_stream(owner)return Truediff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.pyindex 95e2c907e5..a4c26458ed 100644--- a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py@@ -144,6 +144,14 @@ def _patch_redis(redis: AsyncMock):# ---------------------------------------------------------------------------[email protected](autouse=True)+def _no_proxy_credentials():+ """Pause and kill revoke the box's proxy credential (``e2b_network``),+ which has its own Redis handle; ``TestProxyCredentialIsRevoked`` covers it."""+ with patch("backend.copilot.tools.e2b_sandbox.forget_sandbox", AsyncMock()):+ yield++class TestTryReconnect:def test_reconnect_refuses_a_box_stamped_for_someone_else(self):"""A cached id that resolves to another owner's box is dropped, not used."""@@ -1199,6 +1207,33 @@ class TestConnectOwned:"sb-expert", api_key=_API_KEY, timeout=900)+ @pytest.mark.parametrize(+ "reconnecting, pinned_for",+ [("user-a", "user-a"), ("user-b", None), (None, None)],+ ids=["its own user", "another user", "nobody"],+ )+ def test_a_box_is_pinned_only_for_the_user_it_was_created_for(+ self, reconnecting, pinned_for+ ):+ """Its creator's processes may still be running in it: re-pinned for+ whoever reconnects, they would get to act with that user's accounts.+ A mismatch keeps the box's egress and swaps nothing in."""+ owner = SandboxOwner(kind="expert", id=_EXPERT_ID)+ stamp = owner.creation_metadata(user_id="user-a")+ sb = MagicMock()+ with (+ _patch_sdk() as mock_cls,+ patch(+ "backend.copilot.tools.e2b_sandbox.connect_sandbox",+ AsyncMock(return_value=sb),+ ) as connect,+ ):+ mock_cls.get_info = AsyncMock(return_value=MagicMock(metadata=stamp))+ asyncio.run(connect_owned("sb-1", owner, _API_KEY, user_id=reconnecting))+ egress_owner = connect.await_args.args[2]+ assert egress_owner.user_id == pinned_for+ assert egress_owner.label == f"expert:{_EXPERT_ID}"+@pytest.mark.parametrize("stamp",[@@ -1855,3 +1890,54 @@ class TestExpertBoxRecovery:)assert ok is Falsesb.pause.assert_not_awaited()+++class TestProxyCredentialIsRevoked:+ """A box that is paused or killed will not present its proxy credential+ again; left alone, the record would keep naming its owner to the proxy."""++ _FORGET = "backend.copilot.tools.e2b_sandbox.forget_sandbox"++ def test_on_kill(self):+ sb = _mock_sandbox()+ with (+ _patch_sdk() as mock_cls,+ _patch_redis(_mock_redis(stored_sandbox_id=_SANDBOX_ID)),+ patch(self._FORGET, AsyncMock()) as forget,+ ):+ mock_cls.connect = AsyncMock(return_value=sb)+ assert asyncio.run(kill_sandbox(_SESSION_ID, _API_KEY)) is True+ forget.assert_awaited_once_with(_SANDBOX_ID)++ def test_on_pause(self):+ sb = _mock_sandbox()+ with (+ _patch_sdk() as mock_cls,+ _patch_redis(_mock_redis(stored_sandbox_id=_SANDBOX_ID)),+ patch(self._FORGET, AsyncMock()) as forget,+ ):+ mock_cls.connect = AsyncMock(return_value=sb)+ assert asyncio.run(pause_sandbox(_SESSION_ID, _API_KEY)) is True+ forget.assert_awaited_once_with(_SANDBOX_ID)++ def test_on_the_turn_end_pause(self):+ sb = _mock_sandbox()+ with (+ _patch_redis(_mock_redis()),+ patch(self._FORGET, AsyncMock()) as forget,+ ):+ assert asyncio.run(pause_sandbox_direct(sb, _SESSION_ID)) is True+ forget.assert_awaited_once_with(sb.sandbox_id)++ def test_not_when_the_kill_failed(self):+ """The box is still there and may yet egress: keep its credential."""+ sb = _mock_sandbox()+ sb.kill = AsyncMock(side_effect=RuntimeError("e2b error"))+ with (+ _patch_sdk() as mock_cls,+ _patch_redis(_mock_redis(stored_sandbox_id=_SANDBOX_ID)),+ patch(self._FORGET, AsyncMock()) as forget,+ ):+ mock_cls.connect = AsyncMock(return_value=sb)+ assert asyncio.run(kill_sandbox(_SESSION_ID, _API_KEY)) is False+ forget.assert_not_awaited()diff --git a/autogpt_platform/backend/backend/util/e2b_network.py b/autogpt_platform/backend/backend/util/e2b_network.pynew file mode 100644index 0000000000..ead69fd9c5--- /dev/null+++ b/autogpt_platform/backend/backend/util/e2b_network.py@@ -0,0 +1,336 @@+"""One door for every E2B box: where its egress is pinned.++Every ``AsyncSandbox.create`` and ``.connect`` in the backend goes through+``create_sandbox`` / ``connect_sandbox`` here; ``e2b_network_test`` fails on+any that does not. That is the chokepoint at which a box's network is set:+the credential swap proxy (SECRT-2651) that every box will egress through,+so that the model never holds a real credential and nothing dials out+around the proxy.++Off by default. With ``E2B_EGRESS_PROXY_ADDRESS`` empty, boxes are created+and reconnected exactly as before, egress direct. With an address set,+every create pins the box's outbound TCP to that SOCKS5 proxy and every+reconnect re-applies it: ``update_network`` replaces the whole+configuration, so a box created before the address was set is pinned on+its first reconnect, and one created while it was set stays pinned only+because every reconnect says so again. Do not set the address before the+proxy exists: E2B's proxy option fails closed, so a box pointed at nothing+has no egress at all, pip, npm and gh included.++The per-box proxy credential is how the proxy knows whose connection it is+serving, and that mapping is the tenant boundary on the proxy's side. A+credential is minted for one running stretch of a box: fresh at create,+fresh again at every reconnect, the previous one forgotten. The proxy+looks the username up (``e2b:egress:cred:<username>``) and checks the+secret against the stored digest; the record names the owner and the user+whose credentials may be swapped in. A box cannot claim another owner:+usernames are random, and the secret is handed to E2B's host, which dials+the proxy, never to anything running inside the box.++Known gap, to be settled by the spike on SECRT-2615: E2B tunnels TCP only.+DNS and QUIC leave the box directly. QUIC is turned off in the browsers by+policy in the image; DNS stays an accepted side channel.+"""++import asyncio+import contextlib+import hashlib+import json+import logging+import secrets+from typing import Any, Literal, Optional, TypeVar, cast++from e2b import AsyncSandbox+from e2b.sandbox.sandbox_api import (+ SandboxEgressProxyOpts,+ SandboxNetworkOpts,+ SandboxNetworkUpdate,+)+from pydantic import BaseModel, ConfigDict++from backend.data.redis_client import get_redis_async+from backend.util.settings import Settings++logger = logging.getLogger(__name__)++_CREDENTIAL_KEY_PREFIX = "e2b:egress:cred:"+_BOX_KEY_PREFIX = "e2b:egress:box:"+# A credential lives as long as a paused box can (E2B's paused-sandbox+# lifetime); every reconnect replaces it anyway.+_CREDENTIAL_TTL = 48 * 3600+_KILL_TIMEOUT_SECONDS = 10+_LOCK_KEY_PREFIX = "e2b:egress:lock:"+# A rotation is one E2B call and three Redis writes; the TTL only matters if+# the holder dies mid-way.+_ROTATION_LOCK_TTL = 60+_ROTATION_LOCK_WAIT = 30++S = TypeVar("S", bound=AsyncSandbox)+++class EgressOwner(BaseModel):+ """Whose box a connection comes from: what the proxy audits and swaps for.++ ``session`` and ``expert`` are CoPilot boxes (``SandboxOwner``); ``block``+ is a graph execution's, keyed by the user. *user_id* is whose stored+ credentials the proxy may swap into this box's requests; ``None`` means+ none at all.+ """++ model_config = ConfigDict(frozen=True)++ kind: Literal["session", "expert", "block"]+ id: str+ user_id: Optional[str] = None++ @property+ def label(self) -> str:+ return f"{self.kind}:{self.id}"+++class ProxyCredential(BaseModel):+ """What E2B's host presents to the proxy for one running stretch of a box.++ The secret is 256 random bits, not a password anyone chose: it goes into+ the SOCKS5 ``password`` field, but there is nothing to dictionary-attack,+ so the record keeps a plain SHA-256 digest of it, as for any API token.+ """++ model_config = ConfigDict(frozen=True)++ username: str+ secret: str+++def proxy_address() -> Optional[str]:+ """The swap proxy's ``host:port``, or ``None`` when egress is left direct."""+ return Settings().config.e2b_egress_proxy_address.strip() or None+++def secret_digest(secret: str) -> str:+ """What the record stores instead of the secret; the proxy compares to it."""+ return hashlib.sha256(secret.encode()).hexdigest()+++async def create_sandbox(sandbox_cls: type[S], owner: EgressOwner, **kwargs: Any) -> S:+ """Create a box the way the caller asked, pinned to the proxy if one is set.++ *sandbox_cls* is the SDK class the caller uses (``e2b.AsyncSandbox`` or+ ``e2b_code_interpreter``'s); every other keyword goes to its ``create``.+ The credential is recorded before the box exists so that its very first+ connection already resolves at the proxy.+ """+ if "network" in kwargs:+ raise ValueError("A box's network is decided here, not by the caller")+ address = proxy_address()+ if address is None:+ return await sandbox_cls.create(**kwargs)+ credential = _mint()+ await _remember(credential, owner, sandbox_id=None)+ try:+ sandbox = await sandbox_cls.create(+ network=_network(address, credential), **kwargs+ )+ except BaseException:+ # No box will ever present this credential: do not leave it+ # resolving at the proxy for its TTL.+ with contextlib.suppress(BaseException):+ await _forget(credential.username)+ raise+ try:+ await _remember(credential, owner, sandbox_id=sandbox.sandbox_id)+ await _bind(sandbox.sandbox_id, credential.username)+ except BaseException:+ # The box is on the meter and its handle is about to be lost: kill+ # it rather than leak it, and take its credential with it.+ with contextlib.suppress(BaseException):+ await asyncio.wait_for(sandbox.kill(), timeout=_KILL_TIMEOUT_SECONDS)+ with contextlib.suppress(BaseException):+ await _forget(credential.username)+ raise+ logger.info(+ "[E2B] Created %.12s for %s pinned to the egress proxy",+ sandbox.sandbox_id,+ owner.label,+ )+ return sandbox+++async def connect_sandbox(+ sandbox_cls: type[S],+ sandbox_id: str,+ owner: EgressOwner,+ *,+ apply_network: bool = True,+ **kwargs: Any,+) -> S:+ """Reconnect to a box, re-pinning its egress under a fresh credential.++ *apply_network* is ``False`` for a connect that only pauses or kills the+ box: nothing will egress before it is gone, so there is nothing to pin.+ The new credential is recorded before the update and the old one is+ forgotten after it, so a failed update leaves the box on a credential+ that still resolves.++ The rotation is serialized per box. Several turns share an expert's box+ and may reconnect at once; the order two ``update_network`` calls land at+ E2B is not the order their awaits return, so without the lock the box+ could end up presenting a credential the other reconnect just forgot, and+ E2B's proxy option fails closed.+ """+ # The SDK types ``connect`` as the base class; it returns *sandbox_cls*.+ sandbox = cast(S, await sandbox_cls.connect(sandbox_id, **kwargs))+ address = proxy_address()+ if address is None or not apply_network:+ return sandbox+ async with _rotation_lock(sandbox_id):+ credential = _mint()+ await _remember(credential, owner, sandbox_id=sandbox_id)+ try:+ await sandbox.update_network(_network_update(address, credential))+ except BaseException:+ # The box never got this credential: do not leave it resolving.+ with contextlib.suppress(BaseException):+ await _forget(credential.username)+ # ``connect`` has already resumed the box. One that was pinned+ # before stays pinned, on its previous credential; one that never+ # was (it predates the proxy) is awake with direct egress and its+ # handle is about to be lost, so put it back to sleep.+ with contextlib.suppress(BaseException):+ if not await _bound_username(sandbox_id):+ await asyncio.wait_for(+ sandbox.pause(), timeout=_KILL_TIMEOUT_SECONDS+ )+ raise+ await _bind(sandbox_id, credential.username)+ logger.info(+ "[E2B] Reconnected %.12s for %s, egress re-pinned", sandbox_id, owner.label+ )+ return sandbox+++async def kill_sandbox(sandbox: AsyncSandbox) -> None:+ """Kill a box and revoke its proxy credential: the kill for any caller+ that holds the handle. A kill that raises leaves the credential alone,+ since the box may still be running."""+ await sandbox.kill()+ await forget_sandbox(sandbox.sandbox_id)+++async def forget_sandbox(sandbox_id: str) -> None:+ """Revoke a box's proxy credential: call when the box is paused or killed.++ Nothing will present it again (a resume mints a fresh one), and until its+ TTL it would keep naming this owner to the proxy. Best effort: a failure+ here must not fail the pause or kill it follows.+ """+ # Not skipped while no proxy is configured: a box pinned before the+ # address was removed still has a credential on record.+ try:+ redis = await get_redis_async()+ username = await _bound_username(sandbox_id)+ if username:+ await redis.delete(_CREDENTIAL_KEY_PREFIX + username)+ await redis.delete(_BOX_KEY_PREFIX + sandbox_id)+ except Exception:+ logger.warning(+ "[E2B] Could not revoke the proxy credential of %.12s",+ sandbox_id,+ exc_info=True,+ )+++async def credential_record(username: str) -> Optional[dict[str, Any]]:+ """What the proxy sees for a username: owner, user, box, secret digest."""+ redis = await get_redis_async()+ raw = await redis.get(_CREDENTIAL_KEY_PREFIX + username)+ if not raw:+ return None+ return json.loads(raw.decode() if isinstance(raw, bytes) else raw)+++def _mint() -> ProxyCredential:+ return ProxyCredential(+ username=f"box-{secrets.token_hex(8)}", secret=secrets.token_urlsafe(32)+ )+++def _network(address: str, credential: ProxyCredential) -> SandboxNetworkOpts:+ return {"egress_proxy": _proxy(address, credential)}+++def _network_update(address: str, credential: ProxyCredential) -> SandboxNetworkUpdate:+ # The update replaces the whole configuration: this is the complete set.+ return {"egress_proxy": _proxy(address, credential)}+++def _proxy(address: str, credential: ProxyCredential) -> SandboxEgressProxyOpts:+ return {+ "address": address,+ "username": credential.username,+ "password": credential.secret,+ }+++async def _remember(+ credential: ProxyCredential, owner: EgressOwner, *, sandbox_id: Optional[str]+) -> None:+ redis = await get_redis_async()+ record = {+ "owner": owner.label,+ "user_id": owner.user_id,+ "sandbox_id": sandbox_id,+ "secret_sha256": secret_digest(credential.secret),+ }+ await redis.set(+ _CREDENTIAL_KEY_PREFIX + credential.username,+ json.dumps(record),+ ex=_CREDENTIAL_TTL,+ )+++async def _forget(username: str) -> None:+ redis = await get_redis_async()+ await redis.delete(_CREDENTIAL_KEY_PREFIX + username)+++async def _bound_username(sandbox_id: str) -> Optional[str]:+ redis = await get_redis_async()+ raw = await redis.get(_BOX_KEY_PREFIX + sandbox_id)+ return raw.decode() if isinstance(raw, bytes) else raw+++async def _rotation_lock(sandbox_id: str):+ """One credential rotation per box at a time, across processes."""+ # Imported here: backend.executor's package import reaches back to the+ # blocks, which import this module.+ from backend.executor.cluster_lock import AsyncClusterLock++ lock = AsyncClusterLock(+ await get_redis_async(),+ _LOCK_KEY_PREFIX + sandbox_id,+ owner_id=secrets.token_hex(8),+ timeout=_ROTATION_LOCK_TTL,+ )+ deadline = asyncio.get_running_loop().time() + _ROTATION_LOCK_WAIT+ while await lock.try_acquire() != lock.owner_id:+ if asyncio.get_running_loop().time() >= deadline:+ raise TimeoutError(+ f"Another reconnect is still re-pinning sandbox {sandbox_id[:12]}"+ )+ await asyncio.sleep(0.2)+ try:+ yield+ finally:+ await lock.release()+++async def _bind(sandbox_id: str, username: str) -> None:+ """Make *username* the box's current credential and forget its previous one."""+ redis = await get_redis_async()+ key = _BOX_KEY_PREFIX + sandbox_id+ previous = await _bound_username(sandbox_id)+ await redis.set(key, username, ex=_CREDENTIAL_TTL)+ if previous and previous != username:+ await redis.delete(_CREDENTIAL_KEY_PREFIX + previous)diff --git a/autogpt_platform/backend/backend/util/e2b_network_test.py b/autogpt_platform/backend/backend/util/e2b_network_test.pynew file mode 100644index 0000000000..d617a7a868--- /dev/null+++ b/autogpt_platform/backend/backend/util/e2b_network_test.py@@ -0,0 +1,437 @@+"""The egress chokepoint: every SDK create and connect goes through it, and+with a proxy configured every box is pinned under a credential of its own."""++import asyncio+import json+import re+from pathlib import Path+from unittest.mock import AsyncMock, MagicMock, patch++import pytest++import backend+from backend.util import e2b_network+from backend.util.e2b_network import (+ EgressOwner,+ ProxyCredential,+ connect_sandbox,+ create_sandbox,+ credential_record,+ forget_sandbox,+ kill_sandbox,+ secret_digest,+)++_M = "backend.util.e2b_network"+_OWNER = EgressOwner(kind="expert", id="exp-1", user_id="user-1")+_PROXY = "proxy.agpt.internal:1080"+++def _redis(values: dict[str, str] | None = None) -> MagicMock:+ store: dict[str, str] = dict(values or {})+ r = MagicMock()++ async def _get(key):+ return store.get(key, "").encode() or None++ async def _set(key, value, ex=None, nx=False):+ if nx and key in store:+ return None+ store[key] = value+ return True++ async def _eval(script, numkeys, key, value):+ # The rotation lock's compare-and-delete release.+ if store.get(key) == value:+ del store[key]+ return 1+ return 0++ async def _delete(*keys):+ for key in keys:+ store.pop(key, None)++ r.get = AsyncMock(side_effect=_get)+ r.set = AsyncMock(side_effect=_set)+ r.delete = AsyncMock(side_effect=_delete)+ r.eval = AsyncMock(side_effect=_eval)+ r.store = store+ return r+++def _box(sandbox_id: str = "sb-1") -> MagicMock:+ sb = MagicMock()+ sb.sandbox_id = sandbox_id+ sb.update_network = AsyncMock()+ return sb+++def _sdk(box: MagicMock) -> MagicMock:+ cls = MagicMock()+ cls.create = AsyncMock(return_value=box)+ cls.connect = AsyncMock(return_value=box)+ return cls+++def _configured(address: str | None):+ settings = MagicMock()+ settings.config.e2b_egress_proxy_address = address or ""+ return patch(f"{_M}.Settings", return_value=settings)+++class TestOff:+ @pytest.mark.asyncio+ async def test_a_credential_from_before_the_address_was_removed_is_still_revoked(+ self,+ ):+ redis = _redis(+ {"e2b:egress:box:sb-1": "box-a1", "e2b:egress:cred:box-a1": "{}"}+ )+ with _configured(None), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await forget_sandbox("sb-1")+ assert redis.store == {}++ @pytest.mark.asyncio+ async def test_create_and_connect_are_passed_through_untouched(self):+ box, cls, redis = _box(), None, _redis()+ cls = _sdk(box)+ with _configured(None), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ assert await create_sandbox(cls, _OWNER, template="t", api_key="k") is box+ assert (+ await connect_sandbox(cls, "sb-1", _OWNER, api_key="k", timeout=9)+ is box+ )+ cls.create.assert_awaited_once_with(template="t", api_key="k")+ cls.connect.assert_awaited_once_with("sb-1", api_key="k", timeout=9)+ box.update_network.assert_not_awaited()+ assert redis.store == {}++ @pytest.mark.asyncio+ async def test_a_caller_cannot_bring_its_own_network(self):+ with _configured(None), pytest.raises(ValueError, match="decided here"):+ await create_sandbox(_sdk(_box()), _OWNER, network={"allow_out": []})+++class TestPinned:+ @pytest.mark.asyncio+ async def test_create_pins_the_box_under_a_credential_the_proxy_can_resolve(self):+ box, redis = _box("sb-1"), _redis()+ cls = _sdk(box)+ minted = ProxyCredential(username="box-a1", secret="s3cr3t-256-bits")+ with (+ _configured(_PROXY),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}._mint", return_value=minted),+ ):+ await create_sandbox(cls, _OWNER, template="t", api_key="k")+ proxy = cls.create.await_args.kwargs["network"]["egress_proxy"]+ record = await credential_record("box-a1")++ # What E2B's host presents to the proxy...+ assert proxy == {+ "address": _PROXY,+ "username": "box-a1",+ "password": "s3cr3t-256-bits",+ }+ # ...and what the proxy finds for it: the owner and a digest, never+ # the secret itself.+ assert record == {+ "owner": "expert:exp-1",+ "user_id": "user-1",+ "sandbox_id": "sb-1",+ "secret_sha256": secret_digest("s3cr3t-256-bits"),+ }+ assert "s3cr3t-256-bits" not in json.dumps(redis.store)+ assert redis.store["e2b:egress:box:sb-1"] == "box-a1"++ @pytest.mark.asyncio+ async def test_the_record_exists_before_the_box_does(self):+ """The box's first connection must already resolve at the proxy."""+ redis = _redis()+ cls = _sdk(_box("sb-1"))+ seen: list[int] = []++ async def _create(**kwargs):+ seen.append(+ len([k for k in redis.store if k.startswith("e2b:egress:cred:")])+ )+ return _box("sb-1")++ cls.create = AsyncMock(side_effect=_create)+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(cls, _OWNER, template="t")+ assert seen == [1]++ @pytest.mark.asyncio+ async def test_a_failed_create_takes_its_credential_with_it(self):+ redis = _redis()+ cls = _sdk(_box())+ cls.create = AsyncMock(side_effect=RuntimeError("502"))+ minted = ProxyCredential(username="box-a1", secret="s")+ with (+ _configured(_PROXY),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}._mint", return_value=minted),+ ):+ with pytest.raises(RuntimeError):+ await create_sandbox(cls, _OWNER, template="t")+ assert redis.store == {}++ @pytest.mark.asyncio+ async def test_a_box_whose_bookkeeping_fails_is_killed_not_leaked(self):+ """After create the box is on the meter; if its record cannot be+ written the caller never gets the handle, so nobody else could."""+ box, redis = _box("sb-1"), _redis()+ box.kill = AsyncMock()+ cls = _sdk(box)+ minted = ProxyCredential(username="box-a1", secret="s")+ with (+ _configured(_PROXY),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}._mint", return_value=minted),+ patch(f"{_M}._bind", AsyncMock(side_effect=ConnectionError("redis"))),+ ):+ with pytest.raises(ConnectionError):+ await create_sandbox(cls, _OWNER, template="t")+ box.kill.assert_awaited_once()+ assert "e2b:egress:cred:box-a1" not in redis.store++ @pytest.mark.asyncio+ async def test_reconnect_repins_under_a_fresh_credential_and_forgets_the_old(self):+ box, redis = _box("sb-1"), _redis()+ cls = _sdk(box)+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(cls, _OWNER, template="t")+ first = cls.create.await_args.kwargs["network"]["egress_proxy"]["username"]+ await connect_sandbox(cls, "sb-1", _OWNER, api_key="k", timeout=420)+ second = box.update_network.await_args.args[0]["egress_proxy"]+ assert await credential_record(first) is None+ record = await credential_record(second["username"])++ assert second["username"] != first and second["address"] == _PROXY+ assert record and record["sandbox_id"] == "sb-1"+ cls.connect.assert_awaited_once_with("sb-1", api_key="k", timeout=420)++ @pytest.mark.asyncio+ async def test_a_box_from_before_the_proxy_is_pinned_on_its_first_reconnect(self):+ box, redis = _box("sb-old"), _redis()+ cls = _sdk(box)+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await connect_sandbox(cls, "sb-old", _OWNER)+ update = box.update_network.await_args.args[0]+ assert set(update) == {"egress_proxy"}+ assert update["egress_proxy"]["address"] == _PROXY++ @pytest.mark.asyncio+ async def test_a_failed_update_leaves_the_previous_credential_in_place(self):+ box, redis = _box("sb-1"), _redis()+ cls = _sdk(box)+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(cls, _OWNER, template="t")+ first = cls.create.await_args.kwargs["network"]["egress_proxy"]["username"]+ box.update_network = AsyncMock(side_effect=RuntimeError("502"))+ with pytest.raises(RuntimeError):+ await connect_sandbox(cls, "sb-1", _OWNER)+ assert await credential_record(first) is not None+ assert redis.store["e2b:egress:box:sb-1"] == first++ @pytest.mark.asyncio+ async def test_a_failed_update_forgets_the_credential_the_box_never_got(self):+ box, redis = _box("sb-1"), _redis()+ box.pause = AsyncMock()+ cls = _sdk(box)+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(cls, _OWNER, template="t")+ before = dict(redis.store)+ box.update_network = AsyncMock(side_effect=RuntimeError("502"))+ with pytest.raises(RuntimeError):+ await connect_sandbox(cls, "sb-1", _OWNER)+ # Same records as before the attempt, lock released; and a box that+ # was pinned stays awake on its previous credential.+ assert redis.store == before+ box.pause.assert_not_awaited()++ @pytest.mark.asyncio+ async def test_a_box_that_was_never_pinned_is_put_back_to_sleep(self):+ """``connect`` resumed it with direct egress and the handle is lost."""+ box, redis = _box("sb-old"), _redis()+ box.pause = AsyncMock()+ box.update_network = AsyncMock(side_effect=RuntimeError("502"))+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ with pytest.raises(RuntimeError):+ await connect_sandbox(_sdk(box), "sb-old", _OWNER)+ box.pause.assert_awaited_once()+ assert redis.store == {}++ @pytest.mark.asyncio+ async def test_concurrent_reconnects_leave_the_box_on_a_credential_that_resolves(+ self,+ ):+ """Several turns share an expert's box. Whatever order the two+ updates land in, the credential the box ends up presenting must be the+ one that is still on record."""+ box, redis = _box("sb-1"), _redis()+ cls = _sdk(box)+ applied: list[str] = []+ first_is_in = asyncio.Event()++ async def _update(network):+ # The update lands at E2B at once; the first caller only hears+ # back after the second has come and gone.+ applied.append(network["egress_proxy"]["username"])+ if not first_is_in.is_set():+ first_is_in.set()+ await asyncio.sleep(0.3)++ box.update_network = AsyncMock(side_effect=_update)+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ first = asyncio.create_task(connect_sandbox(cls, "sb-1", _OWNER))+ await first_is_in.wait()+ await connect_sandbox(cls, "sb-1", _OWNER)+ await first+ presented = applied[-1]+ assert await credential_record(presented) is not None+ assert redis.store["e2b:egress:box:sb-1"] == presented+ credentials = [k for k in redis.store if k.startswith("e2b:egress:cred:")]+ assert credentials == ["e2b:egress:cred:" + presented]++ @pytest.mark.asyncio+ async def test_a_rotation_that_never_gets_its_turn_gives_up(self):+ box, redis = _box("sb-1"), _redis({"e2b:egress:lock:sb-1": "someone-else"})+ with (+ _configured(_PROXY),+ patch(f"{_M}.get_redis_async", AsyncMock(return_value=redis)),+ patch(f"{_M}._ROTATION_LOCK_WAIT", 0.3),+ ):+ with pytest.raises(TimeoutError):+ await connect_sandbox(_sdk(box), "sb-1", _OWNER)+ box.update_network.assert_not_awaited()+ assert redis.store == {"e2b:egress:lock:sb-1": "someone-else"}++ @pytest.mark.asyncio+ async def test_a_paused_or_killed_box_has_its_credential_revoked(self):+ box, redis = _box("sb-1"), _redis()+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(_sdk(box), _OWNER, template="t")+ assert redis.store+ await forget_sandbox("sb-1")+ assert redis.store == {}+ await forget_sandbox("sb-never-pinned")++ @pytest.mark.asyncio+ async def test_killing_through_the_handle_revokes_too(self):+ box, redis = _box("sb-1"), _redis()+ box.kill = AsyncMock()+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(_sdk(box), _OWNER, template="t")+ await kill_sandbox(box)+ box.kill.assert_awaited_once()+ assert redis.store == {}++ @pytest.mark.asyncio+ async def test_a_kill_that_fails_keeps_the_credential_of_a_box_still_running(self):+ box, redis = _box("sb-1"), _redis()+ box.kill = AsyncMock(side_effect=RuntimeError("e2b error"))+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await create_sandbox(_sdk(box), _OWNER, template="t")+ with pytest.raises(RuntimeError):+ await kill_sandbox(box)+ assert redis.store["e2b:egress:box:sb-1"]++ @pytest.mark.asyncio+ async def test_a_revocation_that_fails_does_not_fail_the_kill_it_follows(self):+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(side_effect=ConnectionError("redis"))+ ):+ await forget_sandbox("sb-1")++ @pytest.mark.asyncio+ async def test_a_connect_that_only_pauses_or_kills_does_not_repin(self):+ box, redis = _box("sb-1"), _redis()+ cls = _sdk(box)+ with _configured(_PROXY), patch(+ f"{_M}.get_redis_async", AsyncMock(return_value=redis)+ ):+ await connect_sandbox(cls, "sb-1", _OWNER, apply_network=False)+ box.update_network.assert_not_awaited()+ assert redis.store == {}+++# ``.create(`` / ``.connect(`` calls that are not a sandbox's, in files that+# import the SDK. Anything else in such a file has to be listed here to pass.+_NOT_A_SANDBOX = ("AsyncVolume.create(",)+++def test_every_sdk_create_and_connect_goes_through_the_chokepoint():+ """A new direct call would create a box whose egress nobody pinned.++ Any ``.create(`` or ``.connect(`` in a file that imports the SDK counts,+ not only ``AsyncSandbox.create(``: ``connect`` is also an instance method+ that resumes a box, and the class can be imported under another name.+ """+ package = Path(backend.__file__).parent+ roots = [package, package.parent / "scripts"]+ chokepoint = Path(e2b_network.__file__).resolve()+ imports_sdk = re.compile(r"^\s*(from|import)\s+e2b", re.MULTILINE)+ call = re.compile(r"\.(create|connect)\(")+ offenders = []+ for root in roots:+ for path in root.rglob("*.py"):+ if path.name.endswith("_test.py") or path.resolve() == chokepoint:+ continue+ text = path.read_text()+ if not imports_sdk.search(text):+ continue+ for number, line in enumerate(text.splitlines(), 1):+ code = line.split("#", 1)[0]+ # Backticked prose in a docstring is a mention, not a call.+ code = re.sub(r"``[^`]*``|`[^`]*`", "", code)+ if call.search(code) and not any(ok in code for ok in _NOT_A_SANDBOX):+ offenders.append(f"{path.relative_to(package.parent)}:{number}")+ assert offenders == [], (+ "Direct E2B create/connect calls outside backend/util/e2b_network.py: "+ + ", ".join(offenders)+ )+++ "line",+ [+ "sb = await AsyncSandbox.create(template=t)",+ "sb = await Box.connect(sandbox_id)", # imported under another name+ "await sandbox.connect(timeout=60)", # the instance method resumes a box+ "sb = await AsyncSandbox.create(", # the call continues on the next line+ ],+)+def test_the_guard_sees_every_shape_of_a_direct_call(line, tmp_path, monkeypatch):+ package = tmp_path / "backend"+ package.mkdir()+ (package / "sneaky.py").write_text(f"from e2b import AsyncSandbox as Box\n{line}\n")+ (tmp_path / "scripts").mkdir()+ monkeypatch.setattr(backend, "__file__", str(package / "__init__.py"))+ with pytest.raises(AssertionError, match="sneaky.py:2"):+ test_every_sdk_create_and_connect_goes_through_the_chokepoint()diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.pyindex 825068c1dd..c5ee9f49fc 100644--- a/autogpt_platform/backend/backend/util/settings.py+++ b/autogpt_platform/backend/backend/util/settings.py@@ -418,6 +418,14 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):"This is necessary to make sure webhooks find their way.",)+ e2b_egress_proxy_address: str = Field(+ default="",+ description="host:port of the SOCKS5 credential swap proxy every E2B box "+ "egresses through (see backend.util.e2b_network). Empty leaves egress "+ "direct. Do not set it before the proxy exists: E2B fails closed, so a "+ "box pointed at nothing has no egress at all.",+ )+frontend_base_url: str = Field(default="",description="Can be used to explicitly set the base URL for the frontend. "diff --git a/autogpt_platform/backend/scripts/desktop_smoke_test.py b/autogpt_platform/backend/scripts/desktop_smoke_test.pyindex efe26879a7..15c06fd8d4 100644--- a/autogpt_platform/backend/scripts/desktop_smoke_test.py+++ b/autogpt_platform/backend/scripts/desktop_smoke_test.py@@ -18,8 +18,10 @@ import httpxfrom dotenv import load_dotenvfrom backend.blocks.desktop._api import WORKSPACE_PATH, DesktopSession+from backend.util.e2b_network import EgressOwnerVOLUME_NAME = "autogpt-smoke-test"+OWNER = EgressOwner(kind="block", id="smoke-test")TEST_FILE = f"{WORKSPACE_PATH}/persist-test.txt"@@ -41,6 +43,7 @@ async def main() -> None:width=1280,height=720,volume_mounts={WORKSPACE_PATH: VOLUME_NAME},+ owner=OWNER,)timed("cold_start_ready", t, timings)print(f" sandbox_id={session.sandbox_id}")@@ -73,7 +76,7 @@ async def main() -> None:print("6) resume via connect")t = time.monotonic()- resumed = await DesktopSession.connect(session.sandbox_id, api_key)+ resumed = await DesktopSession.connect(session.sandbox_id, api_key, owner=OWNER)timed("resume", t, timings)content = await resumed.sandbox.files.read(TEST_FILE)assert (@@ -90,6 +93,7 @@ async def main() -> None:width=1024,height=768,volume_mounts={WORKSPACE_PATH: VOLUME_NAME},+ owner=OWNER,)try:cross = await other.sandbox.files.read(TEST_FILE)