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): hand boxes behind the swap proxy placeholders, not tokens (#14868)

Krzysztof Czerwinski51d38c81a11c
diff --git a/autogpt_platform/backend/backend/copilot/integration_creds.py b/autogpt_platform/backend/backend/copilot/integration_creds.py
index 5e3ee0fa75..0e95ff827e 100644
--- a/autogpt_platform/backend/backend/copilot/integration_creds.py
+++ b/autogpt_platform/backend/backend/copilot/integration_creds.py
@@ -26,16 +26,19 @@ import threading
from collections.abc import Mapping
from typing import cast
+import aiohttp
from cachetools import TTLCache
from backend.copilot.providers import SUPPORTED_PROVIDERS
from backend.data.model import APIKeyCredentials, OAuth2Credentials
+from backend.data.redis_client import get_redis_async
from backend.integrations.creds_events import listen_creds_changed
from backend.integrations.creds_manager import (
IntegrationCredentialsManager,
register_creds_changed_hook,
)
from backend.integrations.providers import ProviderName
+from backend.util.request import Requests
from backend.util.retry import continuous_retry
logger = logging.getLogger(__name__)
@@ -45,6 +48,17 @@ PROVIDER_ENV_VARS: dict[str, list[str]] = {
slug: entry["env_vars"] for slug, entry in SUPPORTED_PROVIDERS.items()
}
+_GRANT_KEY_PREFIX = "e2b:egress:grant:"
+# As long as a paused box can keep a placeholder (E2B's paused-sandbox life).
+_GRANT_TTL = 48 * 3600
+# Add a grant and arm its expiry in one script: a member added without its
+# EXPIRE would leave the key with no TTL.
+_GRANT_SCRIPT = (
+ 'redis.call("sadd", KEYS[1], ARGV[1]) '
+ 'redis.call("expire", KEYS[1], ARGV[2]) '
+ "return 1"
+)
+
# 60 s, not the original 300 s: the pub/sub invalidation below is best-effort
# (a Redis blip drops the message), so the TTL is the floor on how long a stale
# token can survive when it fails. Five minutes was long enough for Otto
@@ -109,6 +123,11 @@ _token_cache: _LockedTTLCache = _LockedTTLCache(
_null_cache: _LockedTTLCache = _LockedTTLCache(
maxsize=_CACHE_MAX_SIZE, ttl=_NULL_CACHE_TTL
)
+# Same keys as _token_cache: which stored credential the cached token is. What
+# a box behind the swap proxy is given instead of the token names it.
+_credential_id_cache: _LockedTTLCache = _LockedTTLCache(
+ maxsize=_CACHE_MAX_SIZE, ttl=_TOKEN_CACHE_TTL
+)
# GitHub user identity caches, keyed (user_id, credential_id): a user with two
# GitHub accounts has two identities, and a chat that picked one must not be
@@ -124,6 +143,13 @@ _gh_identity_null_cache: _LockedTTLCache = _LockedTTLCache(
)
+# The identity lookup below is a host-side call with the real token, so it goes
+# through Requests' address checks and redirect rules like every other outbound
+# call. One attempt: a missing identity only costs commit attribution, and the
+# command waits on it.
+_GITHUB_API = Requests(raise_for_status=False, retry_max_attempts=1)
+
+
def _canonical_provider(provider: str) -> str:
"""``"ProviderName.GITHUB"`` -> ``"github"``.
@@ -154,6 +180,7 @@ def invalidate_user_provider_cache(user_id: str, provider: str) -> None:
# Every scope-specific entry for this pair is stale too.
_token_cache.pop_prefix((user_id, provider))
_null_cache.pop_prefix((user_id, provider))
+ _credential_id_cache.pop_prefix((user_id, provider))
if provider == "github":
_gh_identity_cache.pop_prefix((user_id,))
@@ -193,6 +220,7 @@ async def get_provider_token(
credential_id: str | None = None,
*,
strict: bool = False,
+ lock: bool = False,
) -> str | None:
"""Return the user's access token for *provider*, or ``None`` if not connected.
@@ -209,6 +237,9 @@ async def get_provider_token(
token to fall back to raises ``ProviderTokenUnavailable`` instead of
returning ``None``: for a caller to whom "not connected" means something
(the swap proxy scrubs against it), a failure must not look like one.
+ *lock* takes the credentials manager's lock around an OAuth refresh, so two
+ concurrent callers cannot both spend a single-use refresh token; only for
+ a process whose callers share one event loop (see ``refresh_if_needed``).
"""
_ensure_cache_invalidation_listener()
cache_key = _cache_key(user_id, provider, required_scopes, credential_id)
@@ -257,7 +288,7 @@ async def get_provider_token(
for creds in oauth2_creds:
if creds.type == "oauth2":
try:
- fresh = await manager.refresh_if_needed(user_id, creds, lock=False)
+ fresh = await manager.refresh_if_needed(user_id, creds, lock=lock)
token = fresh.access_token.get_secret_value()
except Exception:
logger.warning(
@@ -275,6 +306,7 @@ async def get_provider_token(
# A refresh here publishes, and this process's own listener may evict
# the entry just written; the cost is one extra lookup, not a leak.
_token_cache[cache_key] = token
+ _credential_id_cache[cache_key] = creds.id
return token
# Pass 2: fall back to API key (no expiry, no refresh needed).
@@ -282,6 +314,7 @@ async def get_provider_token(
if creds.type == "api_key":
token = cast(APIKeyCredentials, creds).api_key.get_secret_value()
_token_cache[cache_key] = token
+ _credential_id_cache[cache_key] = creds.id
return token
# Only cache "not connected" when the user truly has no credentials for this
@@ -325,6 +358,150 @@ _listener_start_lock = threading.Lock()
_listener_thread: threading.Thread | None = None
+async def get_provider_credential_id(
+ user_id: str,
+ provider: str,
+ required_scopes: frozenset[str] = frozenset(),
+ credential_id: str | None = None,
+) -> str | None:
+ """The id of the stored credential ``get_provider_token`` picks with the
+ same arguments, or ``None`` if it finds none: the same choice, named
+ instead of handed over."""
+ cache_key = _cache_key(user_id, provider, required_scopes, credential_id)
+ for _ in range(2):
+ if not await get_provider_token(
+ user_id, provider, required_scopes, credential_id
+ ):
+ return None
+ if picked := _credential_id_cache.get(cache_key):
+ return picked
+ # The token was cached without its id (evicted apart from it): look
+ # both up again rather than guess.
+ _token_cache.pop(cache_key, None)
+ return None
+
+
+def swap_placeholder(provider: str, credential_id: str) -> str:
+ """What a box whose egress goes through the credential swap proxy holds
+ instead of *provider*'s token: ``hsurr:<provider>:<credential id>``.
+
+ The proxy puts that credential's value in on the way out, in the
+ ``Authorization`` header of a request to one of the provider's hosts, and
+ only if the credential was granted to that box (``grant_to_box``).
+ Anywhere else it is an inert string: printed, sent elsewhere or copied off
+ the box, it authenticates nothing.
+ """
+ return f"hsurr:{provider}:{credential_id}"
+
+
+# ``git`` does not read GH_TOKEN; ``gh`` does. This helper, set through git's
+# environment-variable config rather than a file in the box, answers git's
+# credential request for github.com with GH_TOKEN, so a push over HTTPS sends
+# it as HTTP Basic, which the proxy swaps. It only ever sees the placeholder.
+_GITHUB_CREDENTIAL_HELPER = (
+ '!f() { test "$1" = get || return 0; '
+ 'echo username=x-access-token; echo "password=$GH_TOKEN"; }; f'
+)
+
+
+def git_credential_helper_env() -> dict[str, str]:
+ """Git config, as environment variables, that answers git's credential
+ request for github.com with ``$GH_TOKEN``."""
+ return {
+ "GIT_CONFIG_COUNT": "1",
+ "GIT_CONFIG_KEY_0": "credential.https://github.com.helper",
+ "GIT_CONFIG_VALUE_0": _GITHUB_CREDENTIAL_HELPER,
+ }
+
+
+async def placeholder_grants(
+ user_id: str,
+ required_scopes: Mapping[str, frozenset[str]] | None = None,
+ selected: Mapping[str, str] | None = None,
+) -> dict[str, str]:
+ """Provider to credential id: for each provider, the credential
+ ``get_integration_env_vars`` would have injected with the same arguments
+ (the chat's pick, else the best match for the requested scopes). A
+ provider with none (not connected, the pick deleted, a failed refresh) is
+ left out."""
+ grants: dict[str, str] = {}
+ for provider in PROVIDER_ENV_VARS:
+ scopes = (required_scopes or {}).get(provider, frozenset())
+ credential_id = await get_provider_credential_id(
+ user_id, provider, scopes, (selected or {}).get(provider)
+ )
+ if credential_id:
+ grants[provider] = credential_id
+ return grants
+
+
+def placeholder_env(grants: Mapping[str, str]) -> dict[str, str]:
+ """The variables for a box behind the swap proxy: each granted provider's
+ placeholder, and every other provider's variables set empty.
+
+ Empty rather than absent: a command's variables are laid over the box's
+ own, so an absent one would fall back to what the box was created with,
+ and a command whose chat has no usable credential would quietly act as
+ another account instead of failing as it does without the proxy. For the
+ same reason git's helper is switched off (``GIT_CONFIG_COUNT=0``) when
+ GitHub has no placeholder.
+ """
+ env: dict[str, str] = {}
+ for provider, var_names in PROVIDER_ENV_VARS.items():
+ credential_id = grants.get(provider)
+ value = swap_placeholder(provider, credential_id) if credential_id else ""
+ for var in var_names:
+ env[var] = value
+ if env.get("GH_TOKEN"):
+ env.update(git_credential_helper_env())
+ else:
+ env["GIT_CONFIG_COUNT"] = "0"
+ return env
+
+
+def _grant_key(sandbox_id: str, provider: str) -> str:
+ return f"{_GRANT_KEY_PREFIX}{sandbox_id}:{provider}"
+
+
+async def grant_to_box(sandbox_id: str, grants: Mapping[str, str]) -> None:
+ """Record that *sandbox_id* was handed these credentials' placeholders.
+
+ The swap service resolves a placeholder only for a credential granted to
+ the box asking (``swap_credentials.resolve_swap_credential``), so a box
+ can use the accounts its chats were given and no other of the user's,
+ whatever id it types. A grant outlives the command that made it (a
+ process it started may still hold the placeholder) and lasts as long as a
+ paused box can; every reconnect that runs work renews it
+ (``renew_box_grants``), so a box paused longer keeps its own.
+ """
+ if not grants:
+ return
+ redis = await get_redis_async()
+ for provider, credential_id in grants.items():
+ await redis.eval(
+ _GRANT_SCRIPT,
+ 1,
+ _grant_key(sandbox_id, provider),
+ credential_id,
+ _GRANT_TTL,
+ )
+
+
+async def renew_box_grants(sandbox_id: str) -> None:
+ """Restart the clock on everything granted to *sandbox_id* (see
+ ``grant_to_box``): for a box reconnected after a long pause."""
+ redis = await get_redis_async()
+ for provider in PROVIDER_ENV_VARS:
+ await redis.expire(_grant_key(sandbox_id, provider), _GRANT_TTL)
+
+
+async def granted_to_box(sandbox_id: str, provider: str) -> set[str]:
+ """The credential ids of *provider* granted to *sandbox_id*."""
+ redis = await get_redis_async()
+ members = await redis.smembers(_grant_key(sandbox_id, provider))
+ return {m.decode() if isinstance(m, bytes) else m for m in members}
+
+
async def get_integration_env_vars(
user_id: str,
required_scopes: Mapping[str, frozenset[str]] | None = None,
@@ -382,26 +559,23 @@ async def get_github_user_git_identity(
_gh_identity_null_cache[key] = True
return None
- import aiohttp
-
try:
- async with aiohttp.ClientSession() as session:
- async with session.get(
- "https://api.github.com/user",
- headers={
- "Authorization": f"token {token}",
- "Accept": "application/vnd.github+json",
- },
- timeout=aiohttp.ClientTimeout(total=5),
- ) as resp:
- if resp.status != 200:
- logger.warning(
- "[git-identity] GitHub /user returned %s for user %s",
- resp.status,
- user_id,
- )
- return None
- data = await resp.json()
+ response = await _GITHUB_API.get(
+ "https://api.github.com/user",
+ headers={
+ "Authorization": f"token {token}",
+ "Accept": "application/vnd.github+json",
+ },
+ timeout=aiohttp.ClientTimeout(total=5),
+ )
+ if response.status != 200:
+ logger.warning(
+ "[git-identity] GitHub /user returned %s for user %s",
+ response.status,
+ user_id,
+ )
+ return None
+ data = response.json()
except Exception as exc:
logger.warning(
"[git-identity] Failed to fetch GitHub profile for user %s: %s",
diff --git a/autogpt_platform/backend/backend/copilot/integration_creds_test.py b/autogpt_platform/backend/backend/copilot/integration_creds_test.py
index a1337b6f92..d7badd95ef 100644
--- a/autogpt_platform/backend/backend/copilot/integration_creds_test.py
+++ b/autogpt_platform/backend/backend/copilot/integration_creds_test.py
@@ -2,6 +2,8 @@
import asyncio
import contextlib
+import shutil
+import subprocess
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -13,14 +15,23 @@ from backend.copilot.integration_creds import (
PROVIDER_ENV_VARS,
ProviderTokenUnavailable,
_consume_creds_changed_events,
+ _credential_id_cache,
_gh_identity_cache,
_gh_identity_null_cache,
_null_cache,
_token_cache,
get_github_user_git_identity,
get_integration_env_vars,
+ get_provider_credential_id,
get_provider_token,
+ git_credential_helper_env,
+ grant_to_box,
+ granted_to_box,
invalidate_user_provider_cache,
+ placeholder_env,
+ placeholder_grants,
+ renew_box_grants,
+ swap_placeholder,
)
from backend.data.model import APIKeyCredentials, OAuth2Credentials
from backend.integrations.credentials_store import IntegrationCredentialsStore
@@ -71,11 +82,13 @@ def clear_caches():
"""Ensure clean caches before and after every test."""
_token_cache.clear()
_null_cache.clear()
+ _credential_id_cache.clear()
_gh_identity_cache.clear()
_gh_identity_null_cache.clear()
yield
_token_cache.clear()
_null_cache.clear()
+ _credential_id_cache.clear()
_gh_identity_cache.clear()
_gh_identity_null_cache.clear()
@@ -643,3 +656,188 @@ async def _write_credential_as_another_process() -> list[CredentialsChangedEvent
register_creds_changed_hook(invalidate_user_provider_cache)
return published
+
+
+class TestPlaceholders:
+ """What a box behind the swap proxy gets instead of a token: a name the
+ proxy resolves, for the same credential the token would have been."""
+
+ older = _github_oauth("older", "tok-older", ["repo", "workflow"])
+ newer = _github_oauth("newer", "tok-newer", ["repo", "read:org"])
+
+ def _manager(self, creds: list) -> MagicMock:
+ manager = MagicMock()
+ manager.store.get_creds_by_provider = AsyncMock(return_value=creds)
+ manager.refresh_if_needed = AsyncMock(side_effect=lambda _u, c, **_k: c)
+ return manager
+
+ def test_the_placeholder_is_the_proxys_format(self):
+ assert swap_placeholder("github", "cred-1") == "hsurr:github:cred-1"
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_it_grants_the_credential_the_token_would_have_been(self):
+ manager = self._manager([self.older, self.newer])
+ with patch("backend.copilot.integration_creds._manager", manager):
+ scoped = await placeholder_grants(
+ _USER, {"github": frozenset({"repo", "read:org"})}
+ )
+ picked = await placeholder_grants(_USER, selected={"github": "older"})
+ default = await placeholder_grants(_USER)
+ assert scoped == {"github": "newer"}
+ assert picked == {"github": "older"}
+ assert default == {"github": "older"}
+ env = placeholder_env(scoped)
+ assert env["GH_TOKEN"] == env["GITHUB_TOKEN"] == "hsurr:github:newer"
+ assert env["GIT_CONFIG_KEY_0"] == "credential.https://github.com.helper"
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_no_stored_value_is_in_it(self):
+ manager = self._manager([self.older, self.newer])
+ with patch("backend.copilot.integration_creds._manager", manager):
+ env = placeholder_env(await placeholder_grants(_USER))
+ for value in env.values():
+ assert "tok-older" not in value and "tok-newer" not in value
+ assert "test-refresh" not in value
+ assert not any("E2B" in name for name in env)
+
+ @pytest.mark.asyncio(loop_scope="session")
+ @pytest.mark.parametrize("pick", ["deleted-id", None])
+ async def test_without_a_usable_credential_the_variables_are_set_empty(self, pick):
+ """Set, not left out: a command's variables are laid over the box's
+ own, which may hold another account's placeholder. git's helper goes
+ with them."""
+ stored = [self.older] if pick else []
+ manager = self._manager(stored)
+ with patch("backend.copilot.integration_creds._manager", manager):
+ grants = await placeholder_grants(
+ "user-without-a-usable-github",
+ selected={"github": pick} if pick else None,
+ )
+ assert grants == {}
+ env = placeholder_env(grants)
+ assert env["GH_TOKEN"] == env["GITHUB_TOKEN"] == ""
+ assert env["GIT_CONFIG_COUNT"] == "0"
+ assert "GIT_CONFIG_KEY_0" not in env
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_grants_are_per_box_and_per_provider(self):
+ store: dict[str, set[str]] = {}
+ ttls: dict[str, int] = {}
+ redis = MagicMock()
+
+ async def sadd(key, member):
+ store.setdefault(key, set()).add(member)
+
+ async def expire(key, seconds):
+ ttls[key] = seconds
+
+ async def smembers(key):
+ return {m.encode() for m in store.get(key, set())}
+
+ async def eval(script, numkeys, key, member, ttl):
+ # The grant script: SADD and EXPIRE together, so no key is left
+ # without a TTL.
+ assert "sadd" in script and "expire" in script and numkeys == 1
+ await sadd(key, member)
+ await expire(key, ttl)
+
+ redis.sadd, redis.expire, redis.smembers = sadd, expire, smembers
+ redis.eval = eval
+ with patch(
+ "backend.copilot.integration_creds.get_redis_async",
+ AsyncMock(return_value=redis),
+ ):
+ await grant_to_box("sb-1", {"github": "older"})
+ await grant_to_box("sb-1", {"github": "newer"})
+ assert await granted_to_box("sb-1", "github") == {"older", "newer"}
+ assert await granted_to_box("sb-2", "github") == set()
+ ttls.clear()
+ await renew_box_grants("sb-1")
+ assert set(ttls.values()) == {48 * 3600}
+ assert "e2b:egress:grant:sb-1:github" in ttls
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_a_token_cached_without_its_id_is_looked_up_again(self):
+ _token_cache[(_USER, _PROVIDER)] = "tok-older"
+ manager = self._manager([self.older])
+ with patch("backend.copilot.integration_creds._manager", manager):
+ assert await get_provider_credential_id(_USER, _PROVIDER) == "older"
+ manager.store.get_creds_by_provider.assert_awaited_once()
+
+ def test_invalidation_drops_the_id_with_the_token(self):
+ _credential_id_cache[(_USER, _PROVIDER)] = "older"
+ invalidate_user_provider_cache(_USER, _PROVIDER)
+ assert (_USER, _PROVIDER) not in _credential_id_cache
+
+ @pytest.mark.skipif(shutil.which("git") is None, reason="needs git")
+ def test_git_answers_its_credential_request_with_the_placeholder(self, tmp_path):
+ env = {
+ "PATH": "/usr/local/bin:/usr/bin:/bin",
+ "HOME": str(tmp_path),
+ "GIT_TERMINAL_PROMPT": "0",
+ "GH_TOKEN": "hsurr:github:cred-1",
+ **git_credential_helper_env(),
+ }
+ result = subprocess.run(
+ ["git", "credential", "fill"],
+ input="protocol=https\nhost=github.com\npath=o/r.git\n\n",
+ capture_output=True,
+ text=True,
+ env=env,
+ check=True,
+ )
+ assert "username=x-access-token\n" in result.stdout
+ assert "password=hsurr:github:cred-1\n" in result.stdout
+
+
+class TestGitIdentityGoesThroughRequests:
+ """A host-side call with the real token: through Requests' address checks
+ and redirect rules, not a bare HTTP client."""
+
+ def _response(self, status: int, body: dict) -> MagicMock:
+ response = MagicMock()
+ response.status = status
+ response.json = MagicMock(return_value=body)
+ return response
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_the_profile_becomes_the_commit_identity(self):
+ get = AsyncMock(
+ return_value=self._response(
+ 200, {"name": "Ada", "email": None, "id": 7, "login": "ada"}
+ )
+ )
+ with (
+ patch(
+ "backend.copilot.integration_creds.get_provider_token",
+ AsyncMock(return_value="tok"),
+ ),
+ patch("backend.copilot.integration_creds._GITHUB_API.get", get),
+ ):
+ identity = await get_github_user_git_identity(_USER)
+ assert identity == {
+ "GIT_AUTHOR_NAME": "Ada",
+ "GIT_AUTHOR_EMAIL": "[email protected]",
+ "GIT_COMMITTER_NAME": "Ada",
+ "GIT_COMMITTER_EMAIL": "[email protected]",
+ }
+ url = get.await_args.args[0]
+ assert url == "https://api.github.com/user"
+ assert get.await_args.kwargs["headers"]["Authorization"] == "token tok"
+
+ @pytest.mark.asyncio(loop_scope="session")
+ @pytest.mark.parametrize("failure", ["status", "raise"])
+ async def test_a_failed_lookup_yields_no_identity(self, failure):
+ get = (
+ AsyncMock(return_value=self._response(401, {}))
+ if failure == "status"
+ else AsyncMock(side_effect=ValueError("blocked address"))
+ )
+ with (
+ patch(
+ "backend.copilot.integration_creds.get_provider_token",
+ AsyncMock(return_value="tok"),
+ ),
+ patch("backend.copilot.integration_creds._GITHUB_API.get", get),
+ ):
+ assert await get_github_user_git_identity(_USER) is None
diff --git a/autogpt_platform/backend/backend/copilot/swap_credentials.py b/autogpt_platform/backend/backend/copilot/swap_credentials.py
index 752c16a23c..cba308217d 100644
--- a/autogpt_platform/backend/backend/copilot/swap_credentials.py
+++ b/autogpt_platform/backend/backend/copilot/swap_credentials.py
@@ -2,10 +2,11 @@
The proxy (``autogpt_platform/swap_proxy``) is a separate service with no
database access and no encryption key. Every box egresses through it; when a
-request carries a placeholder (``hsurr:github``) it asks here for the value,
-naming the user the connection belongs to and the host the request is going
-to. The answer is the credential only if it is bound to that host, so the
-binding is enforced on this side of the wire as well as on the proxy's.
+request carries a placeholder (``hsurr:github:<credential id>``) it asks here
+for the value, naming the user the connection belongs to, the host the request
+is going to and the box asking (its proxy credential). The answer is only
+what that box was granted, and only if the credential is bound to that host,
+so the binding is enforced on this side of the wire as well as on the proxy's.
Lookup, OAuth refresh and cache invalidation are ``get_provider_token``'s, the
same path that fills ``GH_TOKEN`` today: there is no second store.
@@ -19,8 +20,14 @@ from typing import Optional
from pydantic import BaseModel
-from backend.copilot.integration_creds import get_provider_token
+from backend.copilot.integration_creds import (
+ ProviderTokenUnavailable,
+ get_provider_token,
+ granted_to_box,
+)
from backend.copilot.providers import SUPPORTED_PROVIDERS
+from backend.util.e2b_network import credential_record
+from backend.util.exceptions import NotFoundError
class SwapCredential(BaseModel):
@@ -57,26 +64,82 @@ async def get_swap_bindings() -> dict[str, list[str]]:
}
+class NoLiveBox(NotFoundError):
+ """The box the proxy names is not a live box of the user's that gets swaps."""
+
+
+async def _box_record(box: str, user_id: str) -> dict:
+ """The egress record of the box asking, which must be a live box of
+ *user_id*'s that gets swaps. The proxy says which box a request comes
+ from; this is the backend's own record of that box.
+
+ Raises rather than answering ``None``: ``None`` reads as "not connected",
+ and the proxy would pass that box's responses on unscrubbed. An error is
+ what makes it refuse them, which is right for a connection that outlived
+ its box's credential (rotated at every reconnect), and for any mismatch
+ between what the proxy and the backend know.
+ """
+ record = await credential_record(box)
+ if (
+ record is None
+ or record.get("user_id") != user_id
+ or record.get("swaps") is not True
+ ):
+ raise NoLiveBox("no live box of this user's that gets swaps")
+ return record
+
+
async def resolve_swap_credential(
- user_id: str, name: str, host: str
+ user_id: str, name: str, host: str, box: str
) -> Optional[SwapCredential]:
- """*user_id*'s credential called *name*, if it may be sent to *host*.
-
- ``None`` for an unknown name, an unbound host, or a user who has not
- connected the provider: in each case the placeholder goes out as it is.
- A failure to look the token up or refresh it raises
- (``ProviderTokenUnavailable``) rather than answering ``None``: the proxy
- scrubs responses against this answer, so a failure has to reach it as an
- outage (it refuses what it cannot scrub), not as "nothing to scrub".
+ """*user_id*'s credential called *name*, if it may be sent to *host*,
+ for the box whose proxy credential is *box*.
+
+ The values are the credentials of *name* granted to that box
+ (``integration_creds.grant_to_box``), each under its id, which is what
+ ``hsurr:<name>:<credential id>`` names. A chat's box is granted the
+ credential the chat would have been handed as a token (its pick, else the
+ best match for the requested scopes); another of the user's accounts, or
+ an id typed by hand, resolves to nothing.
+
+ ``None`` for an unknown name, an unbound host, or nothing granted that
+ still yields a token (disconnected since): in each case the placeholder
+ goes out as it is. :class:`NoLiveBox` if *box* is not a live box of
+ *user_id*'s that gets swaps (a revoked or rotated credential, a block's
+ box). A failure to read the grants, or to look up or refresh every
+ granted token, raises too (``ProviderTokenUnavailable`` for the latter)
+ rather than answering ``None``: the proxy scrubs responses against this
+ answer, so a failure has to reach it as an outage (it refuses what it
+ cannot scrub), not as "nothing to scrub". One granted credential that
+ fails while another resolves is left out instead: it is never swapped in,
+ so there is no value of it to scrub, and one revoked account must not
+ take down the box's others.
"""
entry = SUPPORTED_PROVIDERS.get(name)
if entry is None or not _host_is_bound(host, entry["swap_hosts"]):
return None
- token = await get_provider_token(user_id, name, strict=True)
- if not token:
+ record = await _box_record(box, user_id)
+ sandbox_id = record.get("sandbox_id")
+ granted = await granted_to_box(sandbox_id, name) if sandbox_id else set()
+ values: dict[str, str] = {}
+ failure: ProviderTokenUnavailable | None = None
+ for credential_id in sorted(granted):
+ # The token cache's path, keyed by the credential: one lookup per
+ # granted credential, not one per stored account. Locked: concurrent resolves for two hosts must not both spend a
+ # single-use refresh token, and this service runs on one event loop.
+ try:
+ token = await get_provider_token(
+ user_id, name, credential_id=credential_id, strict=True, lock=True
+ )
+ except ProviderTokenUnavailable as exc:
+ failure = exc
+ continue
+ if token:
+ values[credential_id] = token
+ if not values:
+ if failure is not None:
+ raise failure
return None
return SwapCredential(
- name=name,
- values={"access_token": token},
- allowed_hosts=list(entry["swap_hosts"]),
+ name=name, values=values, allowed_hosts=list(entry["swap_hosts"])
)
diff --git a/autogpt_platform/backend/backend/copilot/swap_credentials_test.py b/autogpt_platform/backend/backend/copilot/swap_credentials_test.py
index 705af4c528..4220b54c1d 100644
--- a/autogpt_platform/backend/backend/copilot/swap_credentials_test.py
+++ b/autogpt_platform/backend/backend/copilot/swap_credentials_test.py
@@ -1,5 +1,6 @@
"""What the swap proxy is told: a value only for a host it is bound to."""
+import contextlib
import importlib.util
import re
from pathlib import Path
@@ -10,6 +11,7 @@ import pytest
from backend.copilot.integration_creds import ProviderTokenUnavailable
from backend.copilot.providers import SUPPORTED_PROVIDERS
from backend.copilot.swap_credentials import (
+ NoLiveBox,
SwapCredential,
_host_is_bound,
get_swap_bindings,
@@ -21,8 +23,47 @@ _M = "backend.copilot.swap_credentials"
_GITHUB_HOSTS = ["github.com", "api.github.com", "uploads.github.com"]
-def _token(value):
- return patch(f"{_M}.get_provider_token", AsyncMock(return_value=value))
+_BOX = "box-0123456789abcdef"
+_RECORD = {
+ "owner": "session:s-1",
+ "user_id": "user-1",
+ "swaps": True,
+ "sandbox_id": "sb-1",
+}
+
+
[email protected](autouse=True)
+def live_box():
+ """The asking box's egress record: a live CoPilot box of user-1's."""
+ record = dict(_RECORD)
+ with patch(
+ f"{_M}.credential_record", AsyncMock(side_effect=lambda box: record)
+ ) as lookup:
+ yield record
+ for call in lookup.await_args_list:
+ assert call.args == (_BOX,)
+
+
+def _token(tokens, granted=("cred-a",)):
+ """*tokens*: credential id to token (a string is every id's token), and
+ the ids granted to sandbox sb-1."""
+
+ async def token(user_id, name, credential_id=None, strict=False, lock=False):
+ assert lock, "the swap service refreshes under the manager's lock"
+ assert strict, "a failed lookup must reach the proxy as an outage"
+ if isinstance(tokens, dict):
+ return tokens.get(credential_id)
+ return tokens
+
+ async def grants(sandbox_id, name):
+ return set(granted) if sandbox_id == "sb-1" else set()
+
+ with (
+ patch(f"{_M}.get_provider_token", AsyncMock(side_effect=token)) as lookup,
+ patch(f"{_M}.granted_to_box", AsyncMock(side_effect=grants)),
+ ):
+ yield lookup
@pytest.mark.asyncio
@@ -32,15 +73,17 @@ async def test_bindings_carry_hosts_and_no_values():
@pytest.mark.asyncio
@pytest.mark.parametrize("host", ["api.github.com", "GitHub.com", "github.com:443"])
-async def test_a_bound_host_gets_the_users_token(host):
+async def test_a_bound_host_gets_the_credential_granted_to_the_box(host):
with _token("ghp_real") as lookup:
- credential = await resolve_swap_credential("user-1", "github", host)
+ credential = await resolve_swap_credential("user-1", "github", host, _BOX)
assert credential == SwapCredential(
name="github",
- values={"access_token": "ghp_real"},
+ values={"cred-a": "ghp_real"},
allowed_hosts=_GITHUB_HOSTS,
)
- lookup.assert_awaited_once_with("user-1", "github", strict=True)
+ lookup.assert_awaited_once_with(
+ "user-1", "github", credential_id="cred-a", strict=True, lock=True
+ )
@pytest.mark.asyncio
@@ -56,31 +99,137 @@ async def test_a_bound_host_gets_the_users_token(host):
)
async def test_an_unbound_host_gets_nothing_and_the_token_is_never_fetched(host):
with _token("ghp_real") as lookup:
- assert await resolve_swap_credential("user-1", "github", host) is None
+ assert await resolve_swap_credential("user-1", "github", host, _BOX) is None
lookup.assert_not_awaited()
@pytest.mark.asyncio
async def test_an_unknown_name_gets_nothing():
with _token("ghp_real") as lookup:
- assert await resolve_swap_credential("user-1", "gitlab", "gitlab.com") is None
+ assert (
+ await resolve_swap_credential("user-1", "gitlab", "gitlab.com", _BOX)
+ is None
+ )
lookup.assert_not_awaited()
@pytest.mark.asyncio
-async def test_a_user_who_has_not_connected_the_provider_gets_nothing():
+async def test_a_granted_credential_that_yields_no_token_gets_nothing():
with _token(None):
- assert await resolve_swap_credential("user-1", "github", "github.com") is None
+ assert (
+ await resolve_swap_credential("user-1", "github", "github.com", _BOX)
+ is None
+ )
@pytest.mark.asyncio
async def test_a_lookup_that_fails_is_an_error_not_a_user_without_a_token():
"""The proxy scrubs responses against this answer, and refuses what it
cannot scrub only when the backend says it cannot answer."""
- failing = AsyncMock(side_effect=ProviderTokenUnavailable("github"))
- with patch(f"{_M}.get_provider_token", failing):
- with pytest.raises(ProviderTokenUnavailable):
- await resolve_swap_credential("user-1", "github", "github.com")
+ with _token("ghp_real"):
+ with (
+ patch(
+ f"{_M}.get_provider_token",
+ AsyncMock(side_effect=ProviderTokenUnavailable("github")),
+ ),
+ pytest.raises(ProviderTokenUnavailable),
+ ):
+ await resolve_swap_credential("user-1", "github", "github.com", _BOX)
+
+
+async def test_one_failing_account_does_not_take_down_the_boxs_others():
+ """A revoked account left granted to the box is never swapped in, so it
+ has no value to scrub: the box's healthy account still resolves."""
+
+ async def token(user_id, name, credential_id=None, strict=False, lock=False):
+ if credential_id == "cred-a":
+ raise ProviderTokenUnavailable("github")
+ return "ghp_b"
+
+ with (
+ _token("unused", granted=["cred-a", "cred-b"]),
+ patch(f"{_M}.get_provider_token", AsyncMock(side_effect=token)),
+ ):
+ credential = await resolve_swap_credential(
+ "user-1", "github", "github.com", _BOX
+ )
+ assert credential is not None
+ assert credential.values == {"cred-b": "ghp_b"}
+
+
+async def test_every_granted_account_failing_is_still_an_outage():
+ with (
+ _token("unused", granted=["cred-a", "cred-b"]),
+ patch(
+ f"{_M}.get_provider_token",
+ AsyncMock(side_effect=ProviderTokenUnavailable("github")),
+ ),
+ pytest.raises(ProviderTokenUnavailable),
+ ):
+ await resolve_swap_credential("user-1", "github", "github.com", _BOX)
+
+
+async def test_grants_that_cannot_be_read_are_an_error_too():
+ with (
+ patch(f"{_M}.granted_to_box", AsyncMock(side_effect=ConnectionError())),
+ pytest.raises(ConnectionError),
+ ):
+ await resolve_swap_credential("user-1", "github", "github.com", _BOX)
+
+
+async def test_only_what_was_granted_to_the_box_resolves():
+ """The user has accounts A and B; the chat picked B. Typing A's id, or a
+ bare ``hsurr:github``, gets nothing: A was never granted to this box."""
+ with _token({"cred-a": "ghp_a", "cred-b": "ghp_b"}, granted=["cred-b"]) as lookup:
+ credential = await resolve_swap_credential(
+ "user-1", "github", "github.com", _BOX
+ )
+ assert credential is not None
+ assert credential.values == {"cred-b": "ghp_b"}
+ # Only the granted credential is looked up, not every stored one.
+ lookup.assert_awaited_once_with(
+ "user-1", "github", credential_id="cred-b", strict=True, lock=True
+ )
+
+
+async def test_a_box_granted_nothing_gets_nothing(live_box):
+ live_box["sandbox_id"] = "sb-other"
+ with _token("ghp_real") as lookup:
+ assert (
+ await resolve_swap_credential("user-1", "github", "github.com", _BOX)
+ is None
+ )
+ lookup.assert_not_awaited()
+
+
+ "record",
+ [
+ None, # revoked, rotated, or never minted
+ {**_RECORD, "user_id": "user-2"},
+ {**_RECORD, "swaps": False}, # a block's box
+ {**_RECORD, "swaps": "true"},
+ ],
+ ids=["no record", "another user", "does not swap", "not an explicit true"],
+)
+async def test_only_a_live_box_of_the_users_that_swaps_gets_a_value(record):
+ """The proxy names the box; the backend's own record decides. A proxy
+ that asks for a user with no live box of theirs gets nothing, and an
+ error rather than "not connected", so that it refuses that connection's
+ responses instead of passing them on unscrubbed."""
+ with (
+ patch(f"{_M}.credential_record", AsyncMock(return_value=record)),
+ _token("ghp_real") as lookup,
+ pytest.raises(NoLiveBox),
+ ):
+ await resolve_swap_credential("user-1", "github", "github.com", _BOX)
+ lookup.assert_not_awaited()
def test_every_provider_says_where_its_token_may_go():
diff --git a/autogpt_platform/backend/backend/copilot/swap_service.py b/autogpt_platform/backend/backend/copilot/swap_service.py
index 6f537e23a8..f0223a5f14 100644
--- a/autogpt_platform/backend/backend/copilot/swap_service.py
+++ b/autogpt_platform/backend/backend/copilot/swap_service.py
@@ -54,6 +54,6 @@ class SwapCredentialService(AppService):
@expose
async def resolve_swap_credential(
- self, user_id: str, name: str, host: str
+ self, user_id: str, name: str, host: str, box: str
) -> Optional[SwapCredential]:
- return await resolve_swap_credential(user_id, name, host)
+ return await resolve_swap_credential(user_id, name, host, box)
diff --git a/autogpt_platform/backend/backend/copilot/swap_service_test.py b/autogpt_platform/backend/backend/copilot/swap_service_test.py
index 3db9a88373..0b4e74d58f 100644
--- a/autogpt_platform/backend/backend/copilot/swap_service_test.py
+++ b/autogpt_platform/backend/backend/copilot/swap_service_test.py
@@ -6,7 +6,7 @@ import pytest
from fastapi.testclient import TestClient
from starlette.routing import Route
-from backend.copilot.swap_credentials import SwapCredential
+from backend.copilot.swap_credentials import NoLiveBox, SwapCredential
from backend.copilot.swap_service import SwapCredentialService
from backend.data.db_manager import DatabaseManager
from backend.util.service import EXPOSED_FLAG, UnhealthyServiceError
@@ -65,11 +65,16 @@ def test_resolve_swap_credential_answers(client):
) as resolve:
response = client.post(
"/resolve_swap_credential",
- json={"user_id": "user-1", "name": "github", "host": "github.com"},
+ json={
+ "user_id": "user-1",
+ "name": "github",
+ "host": "github.com",
+ "box": "box-1",
+ },
)
assert response.status_code == 200
assert response.json() == credential.model_dump()
- resolve.assert_awaited_once_with("user-1", "github", "github.com")
+ resolve.assert_awaited_once_with("user-1", "github", "github.com", "box-1")
def test_resolve_swap_credential_answers_none(client):
@@ -79,7 +84,12 @@ def test_resolve_swap_credential_answers_none(client):
):
response = client.post(
"/resolve_swap_credential",
- json={"user_id": "user-1", "name": "github", "host": "evil.test"},
+ json={
+ "user_id": "user-1",
+ "name": "github",
+ "host": "evil.test",
+ "box": "box-1",
+ },
)
assert response.status_code == 200
assert response.json() is None
@@ -103,3 +113,22 @@ async def test_health_check_runs_a_query_not_just_the_connected_flag():
):
await SwapCredentialService().health_check()
query.assert_awaited_once()
+
+
+def test_a_box_that_is_not_live_is_an_error_not_an_answer(client):
+ """The proxy reads an error as "cannot say" and refuses that connection's
+ responses; ``None`` would read as "not connected" and pass them on."""
+ with patch(
+ "backend.copilot.swap_service.resolve_swap_credential",
+ AsyncMock(side_effect=NoLiveBox("no live box")),
+ ):
+ response = client.post(
+ "/resolve_swap_credential",
+ json={
+ "user_id": "user-1",
+ "name": "github",
+ "host": "github.com",
+ "box": "box-stale",
+ },
+ )
+ assert response.status_code == 404
diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
index 2ff138931d..1980c55d69 100644
--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec.py
@@ -9,6 +9,13 @@ runs directly on the remote E2B cloud environment. This means:
- **Full internet access**: E2B sandboxes have unrestricted outbound network.
- **Execution isolation**: E2B provides a fresh, containerised Linux environment.
+Connected accounts reach the box as environment variables (``GH_TOKEN`` and
+``GITHUB_TOKEN`` for GitHub). When the box's egress goes through the
+credential swap proxy (``backend.util.e2b_network``), those variables hold a
+placeholder and the proxy puts the real value in on the way out, so no stored
+credential is ever in the box. Without the proxy they hold the real token,
+as they always have.
+
When E2B is *not* configured the tool falls back to **bubblewrap** (bwrap):
OS-level isolation with a whitelist-only filesystem, no network, and resource
limits. Requires bubblewrap to be installed (Linux only).
@@ -38,10 +45,13 @@ from backend.copilot.gate.subject import Subject
from backend.copilot.integration_creds import (
get_github_user_git_identity,
get_integration_env_vars,
+ grant_to_box,
+ placeholder_env,
+ placeholder_grants,
)
from backend.copilot.model import ChatSession
from backend.copilot.sdk.env import config as chat_config
-from backend.util.e2b_network import reattach_command
+from backend.util.e2b_network import proxy_address, reattach_command
from backend.util.sandbox_login import (
changed_login_files,
judged_text,
@@ -137,6 +147,14 @@ def _build_completion_response(
secret_values: list[str],
session_id: str | None,
) -> BashExecResponse:
+ """The command's result, with any injected token replaced by [REDACTED].
+
+ Not a security control: a literal match on one command's output, beaten by
+ base64, ``rev`` or a newline, blind to files read back and to anything sent
+ over the network. It only keeps an accidental ``env`` from printing a
+ token into the chat. Behind the swap proxy there is no token in the box
+ and nothing to redact.
+ """
out = stdout or ""
err = stderr or ""
for secret in secret_values:
@@ -195,9 +213,11 @@ class BashExecTool(BaseTool):
@property
def requires_auth(self) -> bool:
- # True because _execute_on_e2b injects user tokens (GH_TOKEN etc.)
- # when user_id is present. Defense-in-depth: ensures only authenticated
- # users reach the token injection path.
+ # True because _execute_on_e2b hands the box the user's connected
+ # accounts (GH_TOKEN etc.): the tokens themselves without the swap
+ # proxy, placeholders the proxy turns into them with it. Either way
+ # the command acts with the user's accounts, so only an authenticated
+ # user may run one.
return True
async def gate_subject(
@@ -326,9 +346,10 @@ class BashExecTool(BaseTool):
) -> ToolResponseBase:
"""Execute *command* on the E2B sandbox via commands.run().
- Integration tokens (e.g. GH_TOKEN) are injected into the sandbox env
- for any user with connected accounts. E2B has full internet access, so
- CLI tools like ``gh`` work without manual authentication.
+ Connected accounts go into the command's env (e.g. GH_TOKEN), so CLI
+ tools like ``gh`` work without manual authentication: as placeholders
+ when the box egresses through the swap proxy, as the real tokens when
+ it does not.
"""
# The turn stops following a tool after the hung-tool cap and pauses
# the box, so a longer timeout only keeps an unwatched box billing.
@@ -336,15 +357,32 @@ class BashExecTool(BaseTool):
envs: dict[str, str] = {
"PATH": "/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin",
}
- # Collect injected secret values so we can scrub them from output.
+ # Injected token values, redacted from the output (see
+ # _build_completion_response). Placeholders are not secret.
secret_values: list[str] = []
if user_id is not None:
selected = await selected_credentials(session_id)
- integration_env = await get_integration_env_vars(
- user_id, required_scopes, selected
- )
- secret_values = [v for v in integration_env.values() if v]
- envs.update(integration_env)
+ if proxy_address() is not None:
+ grants = await placeholder_grants(user_id, required_scopes, selected)
+ try:
+ # Before the command runs: its placeholders resolve only
+ # for credentials granted to this box.
+ await grant_to_box(sandbox.sandbox_id, grants)
+ except Exception:
+ # The command still runs; its placeholders go out as they
+ # are and fail at the provider, which is the safe side.
+ logger.warning(
+ "[E2B] Could not grant credentials to %.12s",
+ sandbox.sandbox_id,
+ exc_info=True,
+ )
+ envs.update(placeholder_env(grants))
+ else:
+ integration_env = await get_integration_env_vars(
+ user_id, required_scopes, selected
+ )
+ secret_values = [v for v in integration_env.values() if v]
+ envs.update(integration_env)
# Set git author/committer identity from the user's GitHub profile
# so commits made in the sandbox are attributed correctly.
diff --git a/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py b/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py
index 23dc90001a..6df47d95c4 100644
--- a/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/bash_exec_test.py
@@ -19,6 +19,10 @@ from connectrpc.errors import ConnectError
from e2b import CommandExitException, CommandResult
from e2b.envd.rpc import handle_rpc_exception
from e2b.exceptions import NotFoundException, TimeoutException
+from pydantic import SecretStr
+
+from backend.copilot import integration_creds
+from backend.data.model import APIKeyCredentials
from ._test_data import make_session
from .bash_exec import _KILL_TREE_SCRIPT, BashExecTool
@@ -38,6 +42,13 @@ def picked_credentials():
yield
[email protected](autouse=True)
+def proxy_off():
+ """No swap proxy unless a test says so: the behaviour before it existed."""
+ with patch("backend.copilot.tools.bash_exec.proxy_address", return_value=None):
+ yield
+
+
class _FakeRedisLock:
"""redis-py's ``Lock``, held in-process: one holder per name at a time."""
@@ -387,6 +398,179 @@ class TestBashExecSdkToolResultRedirect:
sandbox.commands.run.assert_called_once()
+class TestBashExecBehindTheSwapProxy:
+ """With the box's egress through the swap proxy, the command gets
+ placeholders: the stored value never reaches the box."""
+
+ _REAL = "ghp_the_real_stored_value"
+
+ @pytest.fixture(autouse=True)
+ def proxy_on(self):
+ with patch(
+ "backend.copilot.tools.bash_exec.proxy_address", return_value="proxy:1080"
+ ):
+ yield
+
+ @pytest.fixture(autouse=True)
+ def stored_github_credential(self):
+ manager = MagicMock()
+ manager.store.get_creds_by_provider = AsyncMock(
+ return_value=[
+ APIKeyCredentials(
+ id="cred-picked",
+ provider="github",
+ api_key=SecretStr(self._REAL),
+ title="GitHub",
+ )
+ ]
+ )
+ with (
+ patch.object(integration_creds, "_manager", manager),
+ patch.object(integration_creds, "_ensure_cache_invalidation_listener"),
+ ):
+ integration_creds._token_cache.clear()
+ integration_creds._null_cache.clear()
+ integration_creds._credential_id_cache.clear()
+ yield
+ integration_creds._token_cache.clear()
+ integration_creds._null_cache.clear()
+ integration_creds._credential_id_cache.clear()
+
+ @pytest.fixture(autouse=True)
+ def grants(self):
+ with patch(
+ "backend.copilot.tools.bash_exec.grant_to_box", new=AsyncMock()
+ ) as grant:
+ self.grant = grant
+ yield grant
+
+ async def _run(self, sandbox: MagicMock) -> BashExecResponse:
+ session = make_session(user_id=_USER)
+ sandbox.sandbox_id = "sb-1"
+ with patch(
+ "backend.copilot.tools.bash_exec.get_github_user_git_identity",
+ new=AsyncMock(return_value={"GIT_AUTHOR_NAME": "Ada"}),
+ ) as identity:
+ result = await _make_tool()._execute_on_e2b(
+ sandbox=sandbox,
+ command="gh repo list",
+ timeout=10,
+ session_id=session.session_id,
+ user_id=_USER,
+ )
+ # Commits stay attributed to the picked account.
+ identity.assert_awaited_once_with(_USER, "cred-picked")
+ assert isinstance(result, BashExecResponse)
+ return result
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_the_command_gets_placeholders_and_no_token(self):
+ sandbox = _make_sandbox(stdout="ok")
+ with patch(
+ "backend.copilot.tools.bash_exec.get_integration_env_vars"
+ ) as real_tokens:
+ await self._run(sandbox)
+ real_tokens.assert_not_called()
+ envs = sandbox.commands.run.call_args[1]["envs"]
+ assert envs["GH_TOKEN"] == envs["GITHUB_TOKEN"] == "hsurr:github:cred-picked"
+ assert envs["GIT_CONFIG_KEY_0"] == "credential.https://github.com.helper"
+ assert envs["GIT_AUTHOR_NAME"] == "Ada"
+ assert all(self._REAL not in value for value in envs.values())
+ # Nothing in the box may drive E2B itself (read its rules, reconnect it).
+ assert not any("E2B" in name for name in envs)
+ # Granted to this box before the command ran, or it would not resolve.
+ self.grant.assert_awaited_once_with("sb-1", {"github": "cred-picked"})
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_a_deleted_pick_blanks_the_variables_instead_of_falling_back(
+ self,
+ ):
+ """The box's own environment may hold the default account's
+ placeholder; left unset, the command would quietly act as it."""
+ sandbox = _make_sandbox(stdout="ok")
+ with patch(
+ "backend.copilot.tools.bash_exec.selected_credentials",
+ new=AsyncMock(return_value={"github": "cred-deleted"}),
+ ):
+ session = make_session(user_id=_USER)
+ sandbox.sandbox_id = "sb-1"
+ with patch(
+ "backend.copilot.tools.bash_exec.get_github_user_git_identity",
+ new=AsyncMock(return_value=None),
+ ):
+ await _make_tool()._execute_on_e2b(
+ sandbox=sandbox,
+ command="git push",
+ timeout=10,
+ session_id=session.session_id,
+ user_id=_USER,
+ )
+ envs = sandbox.commands.run.call_args[1]["envs"]
+ assert envs["GH_TOKEN"] == envs["GITHUB_TOKEN"] == ""
+ assert envs["GIT_CONFIG_COUNT"] == "0"
+ self.grant.assert_awaited_once_with("sb-1", {})
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_the_grant_lands_before_the_command_starts(self):
+ """The proxy resolves a placeholder only once it is granted, so the
+ command's first request would be refused if the grant came after."""
+ order: list[str] = []
+ sandbox = _make_sandbox(stdout="ok")
+ run = sandbox.commands.run.side_effect
+
+ async def started(*args, **kwargs):
+ order.append("run")
+ return await run(*args, **kwargs)
+
+ self.grant.side_effect = lambda *_: order.append("grant")
+ sandbox.commands.run = AsyncMock(side_effect=started)
+ await self._run(sandbox)
+ assert order[:2] == ["grant", "run"]
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_a_grant_that_fails_does_not_stop_the_command(self):
+ sandbox = _make_sandbox(stdout="ok")
+ self.grant.side_effect = ConnectionError("redis down")
+ result = await self._run(sandbox)
+ assert result.exit_code == 0
+ sandbox.commands.run.assert_awaited_once()
+
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_a_placeholder_in_the_output_is_not_redacted(self):
+ sandbox = _make_sandbox(stdout="GH_TOKEN=hsurr:github:cred-picked")
+ result = await self._run(sandbox)
+ assert result.stdout == "GH_TOKEN=hsurr:github:cred-picked"
+
+
+class TestBashExecWithoutTheSwapProxy:
+ @pytest.mark.asyncio(loop_scope="session")
+ async def test_the_real_token_is_injected_as_before(self):
+ sandbox = _make_sandbox(stdout="ok")
+ session = make_session(user_id=_USER)
+ with (
+ patch(
+ "backend.copilot.tools.bash_exec.get_integration_env_vars",
+ new=AsyncMock(return_value={"GH_TOKEN": "gh-secret"}),
+ ),
+ patch("backend.copilot.tools.bash_exec.placeholder_grants") as placeholders,
+ patch(
+ "backend.copilot.tools.bash_exec.get_github_user_git_identity",
+ new=AsyncMock(return_value=None),
+ ),
+ ):
+ await _make_tool()._execute_on_e2b(
+ sandbox=sandbox,
+ command="gh repo list",
+ timeout=10,
+ session_id=session.session_id,
+ user_id=_USER,
+ )
+ placeholders.assert_not_called()
+ envs = sandbox.commands.run.call_args[1]["envs"]
+ assert envs["GH_TOKEN"] == "gh-secret"
+ assert "GIT_CONFIG_COUNT" not in envs
+
+
# ---------------------------------------------------------------------------
# Timeouts against a box that behaves like E2B's
# ---------------------------------------------------------------------------
diff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py
index 8b82b10d8f..cc120bec88 100644
--- a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py
+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox.py
@@ -89,12 +89,20 @@ from e2b.exceptions import NotFoundException
from pydantic import BaseModel, ConfigDict
from backend.blocks.desktop._api import DesktopSession, resolve_volume
+from backend.copilot.credential_selection import selected_credentials
+from backend.copilot.integration_creds import (
+ grant_to_box,
+ placeholder_env,
+ placeholder_grants,
+ renew_box_grants,
+)
from backend.data.redis_client import get_redis_async
from backend.util.e2b_network import (
EgressOwner,
connect_sandbox,
create_sandbox,
forget_sandbox,
+ proxy_address,
)
from backend.util.e2b_template import (
SUPERSEDED_TEMPLATES,
@@ -383,7 +391,7 @@ async def _connect_pinned(
"it; pinning it without credentials",
sandbox_id,
)
- return await connect_sandbox(
+ sandbox = await connect_sandbox(
AsyncSandbox,
sandbox_id,
owner.egress_owner(swap_user_id),
@@ -391,6 +399,25 @@ async def _connect_pinned(
api_key=api_key,
timeout=timeout,
)
+ if pin_egress and swap_user_id is not None:
+ await _keep_grants(sandbox_id)
+ return sandbox
+
+
+async def _keep_grants(sandbox_id: str) -> None:
+ """Renew the box's credential grants on a reconnect that will run work.
+
+ A grant lasts as long as a paused box can, but an expert's box can be
+ paused far longer and come back with the placeholders it was created
+ with; each reconnect restarts their clock. Best effort: a failure costs
+ only those placeholders, and commands grant their own again.
+ """
+ if proxy_address() is None:
+ return
+ try:
+ await renew_box_grants(sandbox_id)
+ except Exception as exc:
+ logger.warning("[E2B] Could not renew the grants of %.12s: %s", sandbox_id, exc)
def _as_owner(owner: "SandboxOwner | str") -> SandboxOwner:
@@ -684,6 +711,34 @@ async def _release_turn(owner: SandboxOwner) -> bool:
return False
+async def _placeholder_grants(
+ egress_owner: EgressOwner, session_id: str | None = None
+) -> dict[str, str]:
+ """The credentials a new box starts with in its own environment, when it
+ egresses through the swap proxy, and which are granted to it.
+
+ A chat's own box (*session_id*) starts with the chat's picks, so a chat
+ that picked account B is not also granted the default A. An expert's box
+ is shared by its chats, so no one chat's pick applies: it starts with the
+ user's default for each connected provider.
+
+ Commands get their variables per call as well (``bash_exec``); these are
+ for what does not start through a command, the desktop's browser and
+ terminal among them. Nothing without the proxy: the real token is never
+ put in the box's environment.
+ """
+ if proxy_address() is None or not egress_owner.swaps:
+ return {}
+ assert egress_owner.user_id is not None # ``swaps`` requires one
+ try:
+ selected = await selected_credentials(session_id) if session_id else None
+ return await placeholder_grants(egress_owner.user_id, selected=selected)
+ except Exception as exc:
+ # Commands still get theirs; a box without them is no less safe.
+ logger.warning("[E2B] No placeholder env for %s: %s", egress_owner, exc)
+ return {}
+
+
async def get_or_create_owner_sandbox(
owner: SandboxOwner,
api_key: str,
@@ -814,6 +869,10 @@ async def get_or_create_owner_sandbox(
# At most _SANDBOX_CREATE_MAX_RETRIES − 1 = 2 sandboxes can
# leak per incident.
mounts = await _resolve_volume_mounts(volume_mounts, api_key)
+ box_grants = await _placeholder_grants(
+ owner.egress_owner(user_id), None if owner.is_expert else session_id
+ )
+ box_env = placeholder_env(box_grants) if box_grants else {}
last_exc: Exception | None = None
for attempt in range(1, _SANDBOX_CREATE_MAX_RETRIES + 1):
try:
@@ -832,6 +891,7 @@ async def get_or_create_owner_sandbox(
template=template,
mounts="attached" if mounts else "none",
),
+ **({"envs": box_env} if box_env else {}),
),
timeout=_SANDBOX_CREATE_TIMEOUT_SECONDS,
)
@@ -869,6 +929,17 @@ async def get_or_create_owner_sandbox(
raise last_exc
assert sandbox is not None # guaranteed: last_exc is None iff break was hit
+ if box_grants:
+ try:
+ await grant_to_box(sandbox.sandbox_id, box_grants)
+ except Exception as exc:
+ # Its own placeholders then resolve to nothing; commands
+ # grant theirs again, so the box stays usable.
+ logger.warning(
+ "[E2B] Could not grant credentials to %.12s: %s",
+ sandbox.sandbox_id,
+ exc,
+ )
await take_baseline(sandbox)
if mounts:
with contextlib.suppress(Exception):
diff --git a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py
index a1115f122b..b8af93c486 100644
--- a/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/e2b_sandbox_test.py
@@ -2024,3 +2024,142 @@ class TestProxyCredentialIsRevoked:
mock_cls.connect = AsyncMock(return_value=sb)
assert asyncio.run(kill_sandbox(_SESSION_ID, _API_KEY)) is False
forget.assert_not_awaited()
+
+
+# ---------------------------------------------------------------------------
+# The box's own environment behind the swap proxy
+# ---------------------------------------------------------------------------
+
+
+class TestCreationEnvBehindTheSwapProxy:
+ """A new box behind the proxy starts with placeholders in its own env, for
+ what does not run through a command, and those credentials are granted to
+ it; without the proxy it starts with no integration env at all, as
+ before."""
+
+ def _create(self, *, proxy: str | None, user_id: str | None) -> dict:
+ new_sb = _mock_sandbox("sb-new")
+ redis = _mock_redis(set_nx_result=True, stored_sandbox_id=None)
+ with (
+ _patch_sdk() as mock_cls,
+ _patch_redis(redis),
+ patch(
+ "backend.copilot.tools.e2b_sandbox.proxy_address", return_value=proxy
+ ),
+ patch(
+ "backend.copilot.tools.e2b_sandbox.placeholder_grants",
+ AsyncMock(return_value={"github": "cred-default"}),
+ ) as lookup,
+ patch(
+ "backend.copilot.tools.e2b_sandbox.grant_to_box", AsyncMock()
+ ) as grant,
+ patch(
+ "backend.copilot.tools.e2b_sandbox.selected_credentials",
+ AsyncMock(return_value={"github": "cred-default"}),
+ ),
+ ):
+ mock_cls.create = AsyncMock(return_value=new_sb)
+ asyncio.run(
+ get_or_create_sandbox(
+ _SESSION_ID, _API_KEY, timeout=_TIMEOUT, user_id=user_id
+ )
+ )
+ _, kwargs = mock_cls.create.call_args
+ self.lookup, self.grant = lookup, grant
+ return kwargs
+
+ def test_behind_the_proxy_the_box_starts_with_granted_placeholders(self):
+ kwargs = self._create(proxy="proxy:1080", user_id="user-a")
+ assert kwargs["envs"]["GH_TOKEN"] == "hsurr:github:cred-default"
+ assert kwargs["envs"]["GITHUB_TOKEN"] == "hsurr:github:cred-default"
+ # A chat's own box starts with the chat's pick.
+ self.lookup.assert_awaited_once_with(
+ "user-a", selected={"github": "cred-default"}
+ )
+ self.grant.assert_awaited_once_with("sb-new", {"github": "cred-default"})
+
+ @pytest.mark.parametrize(
+ "session_id, selected",
+ [("sess-1", {"github": "cred-b"}), (None, None)],
+ ids=["a chat's box: its pick", "an expert's box: the default"],
+ )
+ def test_the_box_is_granted_the_chats_pick_not_the_default(
+ self, session_id, selected
+ ):
+ """A chat that picked account B is not also granted the default A."""
+ from backend.util.e2b_network import EgressOwner
+
+ from .e2b_sandbox import _placeholder_grants
+
+ owner = EgressOwner(kind="session", id="sess-1", user_id="user-a")
+ with (
+ patch(
+ "backend.copilot.tools.e2b_sandbox.proxy_address",
+ return_value="proxy:1080",
+ ),
+ patch(
+ "backend.copilot.tools.e2b_sandbox.selected_credentials",
+ AsyncMock(return_value={"github": "cred-b"}),
+ ),
+ patch(
+ "backend.copilot.tools.e2b_sandbox.placeholder_grants",
+ AsyncMock(return_value={}),
+ ) as lookup,
+ ):
+ asyncio.run(_placeholder_grants(owner, session_id))
+ lookup.assert_awaited_once_with("user-a", selected=selected)
+
+ def test_without_the_proxy_the_create_call_is_unchanged(self):
+ kwargs = self._create(proxy=None, user_id="user-a")
+ assert "envs" not in kwargs
+ self.lookup.assert_not_awaited()
+ self.grant.assert_not_awaited()
+
+ def test_a_box_with_no_user_gets_none(self):
+ kwargs = self._create(proxy="proxy:1080", user_id=None)
+ assert "envs" not in kwargs
+ self.lookup.assert_not_awaited()
+
+
+class TestGrantsAreRenewedOnReconnect:
+ """An expert's box can stay paused longer than a grant lasts: a reconnect
+ that will run work renews its grants, behind the proxy only."""
+
+ def _reconnect(self, *, proxy, pin_egress=True, user_id="user-a"):
+ owner = SandboxOwner(kind="expert", id=_EXPERT_ID)
+ stamp = owner.creation_metadata(user_id="user-a")
+ with (
+ _patch_sdk() as mock_cls,
+ patch(
+ "backend.copilot.tools.e2b_sandbox.connect_sandbox",
+ AsyncMock(return_value=MagicMock()),
+ ),
+ patch(
+ "backend.copilot.tools.e2b_sandbox.proxy_address", return_value=proxy
+ ),
+ patch(
+ "backend.copilot.tools.e2b_sandbox.renew_box_grants", AsyncMock()
+ ) as renew,
+ ):
+ mock_cls.get_info = AsyncMock(return_value=MagicMock(metadata=stamp))
+ asyncio.run(
+ connect_owned(
+ "sb-1", owner, _API_KEY, user_id=user_id, pin_egress=pin_egress
+ )
+ )
+ return renew
+
+ def test_behind_the_proxy_a_reconnect_renews_the_grants(self):
+ self._reconnect(proxy="proxy:1080").assert_awaited_once_with("sb-1")
+
+ @pytest.mark.parametrize(
+ "kwargs",
+ [
+ {"proxy": None},
+ {"proxy": "proxy:1080", "pin_egress": False},
+ {"proxy": "proxy:1080", "user_id": "user-b"}, # not the box's user
+ ],
+ ids=["no proxy", "pause or kill", "another user"],
+ )
+ def test_otherwise_nothing_is_renewed(self, kwargs):
+ self._reconnect(**kwargs).assert_not_awaited()
diff --git a/autogpt_platform/backend/backend/util/test_json.py b/autogpt_platform/backend/backend/util/test_json.py
index db059de80f..3a4ab0ad85 100644
--- a/autogpt_platform/backend/backend/util/test_json.py
+++ b/autogpt_platform/backend/backend/util/test_json.py
@@ -1,4 +1,5 @@
import datetime
+import gc
import threading
from collections import OrderedDict
from concurrent.futures import ThreadPoolExecutor
@@ -162,6 +163,10 @@ class TestSafeJson:
data = {}
data["self"] = data # Create circular reference
+ # Anything an earlier test left for the collector goes now: a finalizer
+ # that runs at the recursion limit raises there and fails this test.
+ gc.collect()
+
# This should either work with fallback or raise a reasonable error
try:
result = SafeJson(data)
diff --git a/autogpt_platform/swap_proxy/README.md b/autogpt_platform/swap_proxy/README.md
index c36250c317..f31fe3cb21 100644
--- a/autogpt_platform/swap_proxy/README.md
+++ b/autogpt_platform/swap_proxy/README.md
@@ -36,7 +36,8 @@ users at once.
not stored. The swap everywhere else is kept (`swap_anywhere` on a
credential) for providers that need it, and arrives with the binding
table (SECRT-2616); nothing sets it yet. The backend is asked for values
- per user and per host, and refuses hosts a credential is not bound to.
+ per user, per host and per box, and refuses hosts a credential is not bound
+ to (see "What a box holds").
(`swap_proxy/swap.py`, `swap_proxy/source.py`)
5. **Scrub.** A value echoed back in a text response, or in a websocket message
from the server, is turned back into its placeholder before the box sees it,
@@ -112,6 +113,38 @@ mitmproxy verified for the very host the request names. If the backend cannot
be asked, nothing is swapped: the placeholder goes out literally and the request
fails at the provider, loudly and with nothing leaked.
+## What a box holds
+
+A CoPilot box behind the proxy gets, for each account its user connected, the
+provider's usual variables with a placeholder in them (`GH_TOKEN` and
+`GITHUB_TOKEN` for GitHub; `backend/copilot/integration_creds.py`). A
+placeholder names one stored credential, `hsurr:github:<credential id>`, and
+resolves only for a box that credential was **granted** to:
+
+- Each command (`bash_exec`) is handed the credential the real token used to
+ come from (the chat's pick, else the best match for the scopes asked for),
+ and the backend records the grant for that box before the command runs. A
+ provider with no usable credential gets its variables set empty, not left
+ out, so a command never falls back to another account's placeholder from
+ the box's own environment; git's helper is switched off with it.
+- A new box starts with the user's default credential for each provider in its
+ own environment, granted the same way, for what does not start through a
+ command (the desktop's browser and terminal).
+- A git credential helper, set through git's environment-variable config,
+ answers for `https://github.com` with `x-access-token` and `$GH_TOKEN`: a
+ `git push` over HTTPS sends the placeholder as HTTP Basic, which is swapped
+ here.
+
+The proxy asks for values per user, host and box (the proxy credential the
+connection presented). The backend answers only for a live box of that user's
+that gets swaps, by its own record of the box, and only with the credentials
+granted to it: another of the user's accounts, or an id typed by hand,
+resolves to nothing. A grant lasts as long as a paused box can, so a process a
+command left running keeps working.
+
+Without the proxy (`E2B_EGRESS_PROXY_ADDRESS` empty) commands get the real
+tokens as before, and the box's own environment gets nothing.
+
## Why a separate package
mitmproxy pins its dependencies exactly and needs Python 3.12+, and the E2B SDK
@@ -168,6 +201,11 @@ step**: the proxy reachable at that address, the CA mounted into every replica,
and its certificate in the image the boxes run. Any one of them missing shows up
as every box losing either its egress or its TLS to bound hosts.
+The same address is also what switches boxes from real tokens to placeholders
+(see "What a box holds"): from the moment it is set, a connected account works
+in a box only through the proxy's swap, so the proxy has to be swapping, and
+reaching its backend service, before the address goes out.
+
The same step sets the network policy around the proxy, and that policy is a
security boundary, not housekeeping. The proxy calls one backend service
(`SWAP_PROXY_BACKEND_URL`) and must be able to reach that and nothing else in
@@ -241,6 +279,11 @@ run it by hand: `gh workflow run platform-swap-proxy-ci.yml --ref <branch>`.
## Known limits
+- An expert's box is shared by its chats, so it starts with the user's default
+ account for each provider in its own environment (for the desktop's terminal
+ and browser), and that account is granted to it. A chat on that expert that
+ picked another account can still act as the default from the box. A chat's
+ own box starts with the chat's pick instead.
- Images and binary bodies are neither swapped (above 5 MiB) nor scrubbed (at
any size). What counts as text is the content type the sender declares.
- A value goes into the `Authorization` header and nowhere else. A provider
@@ -284,8 +327,10 @@ run it by hand: `gh workflow run platform-swap-proxy-ci.yml --ref <branch>`.
the case, it does not close it.
- A body in gzip or zstd may hold at most 64 members or frames; more is
treated as undecodable.
-- A connection stays authenticated while it stays open, also after its box's
- credential is rotated.
+- A connection stays open after its box's credential is rotated (every
+ reconnect rotates it), but the backend no longer knows its box: from then on
+ nothing is swapped into it, and its text responses from bound hosts are
+ refused as while the backend is down, until the client opens a new one.
- NAT64 (`64:ff9b::/96`, and its local-use prefix) and 6to4 addresses are judged
by the IPv4 address they carry. Teredo and operator-chosen NAT64 prefixes are
not recognised.
diff --git a/autogpt_platform/swap_proxy/swap_proxy/addon.py b/autogpt_platform/swap_proxy/swap_proxy/addon.py
index 347d9e0934..b2f8ad1f31 100644
--- a/autogpt_platform/swap_proxy/swap_proxy/addon.py
+++ b/autogpt_platform/swap_proxy/swap_proxy/addon.py
@@ -762,7 +762,7 @@ class SwapProxyAddon:
lookup = _Lookup()
for name in sorted(wanted):
try:
- credential = await self._source.resolve(user_id, name, host)
+ credential = await self._source.resolve(user_id, name, host, owner.box)
except SourceUnavailable:
lookup.refused[name] = _UNAVAILABLE
continue
diff --git a/autogpt_platform/swap_proxy/swap_proxy/addon_test.py b/autogpt_platform/swap_proxy/swap_proxy/addon_test.py
index 5036abca55..0541ec8045 100644
--- a/autogpt_platform/swap_proxy/swap_proxy/addon_test.py
+++ b/autogpt_platform/swap_proxy/swap_proxy/addon_test.py
@@ -48,7 +48,7 @@ class Source:
raise SourceUnavailable("bindings")
return {"github"} if host == HOST else set()
- async def resolve(self, user_id, name, host):
+ async def resolve(self, user_id, name, host, box):
if self.down:
raise SourceUnavailable("resolve")
if self.token is None:
@@ -75,7 +75,9 @@ def addon_for(
source = source or Source()
source.anywhere = anywhere
addon = SwapProxyAddon(OwnerDirectory(NoRedis()), source, EgressGuard())
- addon._owners[flow.client_conn] = Owner("session:s-a", "user-a", "sb-1", swaps)
+ addon._owners[flow.client_conn] = Owner(
+ "session:s-a", "user-a", "sb-1", swaps, box="box-0123456789abcdef"
+ )
flow.request.host, flow.request.scheme = HOST, "https"
flow.request.headers["host"] = HOST
flow.server_conn.sni = HOST
diff --git a/autogpt_platform/swap_proxy/swap_proxy/e2e_test.py b/autogpt_platform/swap_proxy/swap_proxy/e2e_test.py
index 682d4f5f5e..3d9b53af9d 100644
--- a/autogpt_platform/swap_proxy/swap_proxy/e2e_test.py
+++ b/autogpt_platform/swap_proxy/swap_proxy/e2e_test.py
@@ -61,14 +61,16 @@ class FakeSource:
self.down = False
self.anywhere = False # the credential's ``swap_anywhere``
self.asked: list[tuple[str, str, str]] = []
+ self.boxes: set[str] = set()
async def bound_names(self, host):
if self.down:
raise SourceUnavailable("down")
return {"github"} if host_in_list(host, self.hosts) else set()
- async def resolve(self, user_id, name, host):
+ async def resolve(self, user_id, name, host, box):
self.asked.append((user_id, name, host))
+ self.boxes.add(box)
if self.down:
raise SourceUnavailable("down")
token = self.tokens.get(user_id)
@@ -303,13 +305,15 @@ async def test_a_placeholder_becomes_the_owners_token_and_the_echo_is_scrubbed(s
async def test_each_box_gets_its_own_users_token(stack):
- proxy, upstream, _ = stack
+ proxy, upstream, source = stack
for box in (BOX_A, BOX_B):
await socks5_request(
proxy.port, *box, UPSTREAM_HOST, upstream.port, http_get(headers=BEARER)
)
sent = [seen["headers"]["authorization"] for seen in upstream.seen]
assert sent == [f"Bearer {TOKEN_A}", f"Bearer {TOKEN_B}"]
+ # The backend is told which box asks: its record of that box decides.
+ assert source.boxes == {BOX_A[0], BOX_B[0]}
@pytest.mark.parametrize(
diff --git a/autogpt_platform/swap_proxy/swap_proxy/owners.py b/autogpt_platform/swap_proxy/swap_proxy/owners.py
index 6aae18a23c..1666fffaa6 100644
--- a/autogpt_platform/swap_proxy/swap_proxy/owners.py
+++ b/autogpt_platform/swap_proxy/swap_proxy/owners.py
@@ -33,12 +33,16 @@ _USERNAME_RE = re.compile(r"box-[0-9a-f]{16}")
class Owner:
"""*user_id* is who the box runs for, and is always audited. Their
credentials are swapped in only if the backend said so (*swaps*): it does
- for CoPilot boxes, not for a block running a graph someone else wrote."""
+ for CoPilot boxes, not for a block running a graph someone else wrote.
+ *box* is the proxy credential the connection presented, which the backend
+ is told with every value it is asked for: its own record of that box is
+ what decides whether it gets one."""
label: str
user_id: Optional[str]
sandbox_id: Optional[str]
swaps: bool = False
+ box: str = ""
@property
def swap_user_id(self) -> Optional[str]:
@@ -81,4 +85,5 @@ class OwnerDirectory:
sandbox_id=record.get("sandbox_id") or None,
# Fail closed: only an explicit true.
swaps=record.get("swaps") is True,
+ box=username,
)
diff --git a/autogpt_platform/swap_proxy/swap_proxy/owners_test.py b/autogpt_platform/swap_proxy/swap_proxy/owners_test.py
index 61774f1a13..9539a67210 100644
--- a/autogpt_platform/swap_proxy/swap_proxy/owners_test.py
+++ b/autogpt_platform/swap_proxy/swap_proxy/owners_test.py
@@ -45,7 +45,7 @@ async def authenticate(raw: Any, username: str = USERNAME, secret: str = SECRET)
async def test_the_right_secret_for_a_minted_username_is_its_owner():
reader = Reader(record())
owner = await OwnerDirectory(reader).authenticate(USERNAME, SECRET)
- assert owner == Owner("session:s-a", "user-a", "sb-1", swaps=True)
+ assert owner == Owner("session:s-a", "user-a", "sb-1", swaps=True, box=USERNAME)
assert owner is not None and owner.swap_user_id == "user-a"
assert reader.asked == [CREDENTIAL_KEY_PREFIX + USERNAME]
diff --git a/autogpt_platform/swap_proxy/swap_proxy/source.py b/autogpt_platform/swap_proxy/swap_proxy/source.py
index a7558ece31..bda90fe3b0 100644
--- a/autogpt_platform/swap_proxy/swap_proxy/source.py
+++ b/autogpt_platform/swap_proxy/swap_proxy/source.py
@@ -43,7 +43,7 @@ class CredentialSource(Protocol):
async def bound_names(self, host: str) -> set[str]: ...
async def resolve(
- self, user_id: str, name: str, host: str
+ self, user_id: str, name: str, host: str, box: str
) -> Optional[Credential]: ...
@@ -53,7 +53,7 @@ class BackendCredentialSource:
self._client = client or httpx.AsyncClient(timeout=_TIMEOUT)
self._bindings: Optional[tuple[float, dict[str, tuple[str, ...]]]] = None
self._credentials: dict[
- tuple[str, str, str], tuple[float, Optional[Credential]]
+ tuple[str, str, str, str], tuple[float, Optional[Credential]]
] = {}
async def bindings(self) -> dict[str, tuple[str, ...]]:
@@ -79,15 +79,20 @@ class BackendCredentialSource:
table = await self.bindings()
return {name for name, hosts in table.items() if host_in_list(host, hosts)}
- async def resolve(self, user_id: str, name: str, host: str) -> Optional[Credential]:
- key = (user_id, name, host.lower())
+ async def resolve(
+ self, user_id: str, name: str, host: str, box: str
+ ) -> Optional[Credential]:
+ """*user_id*'s credential *name* for *host*, asked for the box whose
+ proxy credential is *box*: the backend answers only for a live box of
+ that user's, with what was granted to that box."""
+ key = (user_id, name, host.lower(), box)
now = time.monotonic()
cached = self._credentials.get(key)
if cached and cached[0] > now:
return cached[1]
data = await self._call(
"resolve_swap_credential",
- {"user_id": user_id, "name": name, "host": host},
+ {"user_id": user_id, "name": name, "host": host, "box": box},
)
credential = None
if data is not None:
diff --git a/autogpt_platform/swap_proxy/swap_proxy/source_test.py b/autogpt_platform/swap_proxy/swap_proxy/source_test.py
index daf02f9329..fc9b605708 100644
--- a/autogpt_platform/swap_proxy/swap_proxy/source_test.py
+++ b/autogpt_platform/swap_proxy/swap_proxy/source_test.py
@@ -48,31 +48,47 @@ async def test_resolve_names_the_user_and_the_host_and_is_cached_briefly():
source, calls = _backend(_ok)
credential = None
for _ in range(3):
- credential = await source.resolve("user-a", "github", "api.github.com")
+ credential = await source.resolve("user-a", "github", "api.github.com", "box-1")
assert credential is not None
assert credential.values == {"access_token": TOKEN}
assert credential.allowed_hosts == ("github.com", "api.github.com")
assert calls == [
(
"/resolve_swap_credential",
- {"user_id": "user-a", "name": "github", "host": "api.github.com"},
+ {
+ "user_id": "user-a",
+ "name": "github",
+ "host": "api.github.com",
+ "box": "box-1",
+ },
)
]
async def test_one_users_answer_is_never_anothers():
source, _ = _backend(_ok)
- assert await source.resolve("user-a", "github", "api.github.com") is not None
- assert await source.resolve("user-b", "github", "api.github.com") is None
+ assert (
+ await source.resolve("user-a", "github", "api.github.com", "box-1") is not None
+ )
+ assert await source.resolve("user-b", "github", "api.github.com", "box-1") is None
+
+
+async def test_one_boxs_answer_is_never_anothers():
+ """The backend answers per box (its ceiling, whether it is still live), so
+ a cached answer for one box is not served to another of the same user's."""
+ source, calls = _backend(_ok)
+ await source.resolve("user-a", "github", "api.github.com", "box-1")
+ await source.resolve("user-a", "github", "api.github.com", "box-2")
+ assert [body["box"] for _, body in calls] == ["box-1", "box-2"]
async def test_the_cache_expires(monkeypatch):
source, calls = _backend(_ok)
now = [1000.0]
monkeypatch.setattr(source_module.time, "monotonic", lambda: now[0])
- await source.resolve("user-a", "github", "api.github.com")
+ await source.resolve("user-a", "github", "api.github.com", "box-1")
now[0] += source_module._CREDENTIAL_TTL + 1
- await source.resolve("user-a", "github", "api.github.com")
+ await source.resolve("user-a", "github", "api.github.com", "box-1")
assert len(calls) == 2
@@ -80,6 +96,7 @@ async def test_the_cache_expires(monkeypatch):
"response",
[
httpx.Response(500, json={"type": "RuntimeError"}),
+ httpx.Response(404, json={"type": "NoLiveBox"}), # a stale or foreign box
httpx.Response(200, content=b"not json"),
httpx.Response(200, json={"name": "github"}), # no values
],
@@ -88,7 +105,7 @@ async def test_anything_but_a_clean_answer_is_unavailable_and_not_cached(respons
source, calls = _backend(lambda path, body: response)
for _ in range(2):
with pytest.raises(SourceUnavailable):
- await source.resolve("user-a", "github", "api.github.com")
+ await source.resolve("user-a", "github", "api.github.com", "box-1")
assert len(calls) == 2
diff --git a/autogpt_platform/swap_proxy/swap_proxy/swap_test.py b/autogpt_platform/swap_proxy/swap_proxy/swap_test.py
index 44045a8427..ca438f8953 100644
--- a/autogpt_platform/swap_proxy/swap_proxy/swap_test.py
+++ b/autogpt_platform/swap_proxy/swap_proxy/swap_test.py
@@ -495,6 +495,27 @@ class TestAuthorizationOnly:
s = swap(r, self.PLAIN, host="evil.test")
assert [e.reason for e in s.events] == ["unbound-host"]
+ def test_a_credential_named_by_its_id_is_swapped_as_the_backend_sends_it(
+ self,
+ ):
+ """The backend hands a box ``hsurr:github:<credential id>`` for the
+ account the chat picked, and lists every stored credential under its
+ id; git sends it as Basic through its credential helper."""
+ cid = "0b7c2f5e-6c1d-4a8e-9f3a-2d4e6f8a0b1c"
+ picked = Credential(
+ "github", {"access_token": "ghp_default", cid: TOKEN}, (HOST,)
+ )
+ r = make_request(headers={"Authorization": f"token hsurr:github:{cid}"})
+ swap(r, picked)
+ assert r.headers["Authorization"] == f"token {TOKEN}"
+
+ pair = base64.b64encode(f"x-access-token:hsurr:github:{cid}".encode())
+ r = make_request(headers={"Authorization": f"Basic {pair.decode()}"})
+ swap(r, picked)
+ decoded = base64.b64decode(r.headers["Authorization"].split()[1]).decode()
+ assert decoded == f"x-access-token:{TOKEN}"
+ assert scrub_text(f"t={TOKEN}", [picked]) == f"t=hsurr:github:{cid}"
+
def test_an_echoed_basic_header_goes_back_to_what_the_box_sent(self):
"""A server that echoes the header returns the base64 of the real pair,
which no value matches; the swap remembers exactly that string."""