From 39146957df189c566103a3df547a17c693c64aac Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 19 Nov 2026 18:31:06 +0000 Subject: [PATCH 1/3] fix(env): send the control token to the REST routes behind the gateway The legacy REST helpers (reset_via_rest, add_via_rest, export_state, reset) take headers and default to the run's grant. Legacy loads, resets, state reads and streamed snapshots send the deployment's token. A grant covers a URL only when it resolves under the grant's address, by whole path segments. A legacy reset refused with 401 or 403 raises instead of falling back to the MCP tool. --- .../src/agentenv_protocol/client.py | 25 ++++- .../tests/test_control_token.py | 18 ++++ src/agent_env/env/envs/_deployment.py | 4 +- src/agent_env/env/envs/mcp_server.py | 6 +- src/agent_env/env/envs/website.py | 5 +- src/agent_env/env/legacy_protocol.py | 53 +++++++---- .../task_step/task_steps/snapshot_env.py | 7 +- .../env/envs/multi_env_local_test.py | 4 +- tst/unit/env/envs/test_env_card_loads.py | 19 ++++ .../envs/test_mcp_server_plugin_provider.py | 8 +- tst/unit/env/envs/test_server_reset.py | 11 +++ tst/unit/env/test_env.py | 4 + tst/unit/env/test_legacy_protocol.py | 94 ++++++++++++++++++- .../task_step/test_snapshot_agent_state.py | 40 +++++++- tst/unit/task_step/test_snapshot_env.py | 63 +++++++++++++ 15 files changed, 321 insertions(+), 40 deletions(-) diff --git a/packages/agentenv-protocol/src/agentenv_protocol/client.py b/packages/agentenv-protocol/src/agentenv_protocol/client.py index 0d76ad4aa..2fa18a8d8 100644 --- a/packages/agentenv-protocol/src/agentenv_protocol/client.py +++ b/packages/agentenv-protocol/src/agentenv_protocol/client.py @@ -77,9 +77,28 @@ def request_headers(url: str, headers: dict[str, str] | None = None) -> dict[str def granted_headers(url: str, grants: dict[str | None, dict[str, str]]) -> dict[str, str]: """What ``grants``, env addresses and the headers that carry their control tokens, give a call to ``url``: those of - the address it is or is under, and none anywhere else.""" - return next((sent for base, sent in grants.items() - if base and (url == base.rstrip("/") or url.startswith(f"{base.rstrip('/')}/"))), {}) + the address it resolves to or under, and none anywhere else.""" + where = _resolved(url) + if where is None: + return {} + for base, sent in grants.items(): + address = _resolved(base) if base else None + if address is not None and where[0] == address[0] and where[1][:len(address[1])] == address[1]: + return sent + return {} + + +def _resolved(url: str) -> tuple[tuple[str, str, int | None], list[str]] | None: + """``url``'s origin and path segments as its server routes them, dot segments resolved; None for a relative url or + one whose path still holds a dot segment once percent-decoded, which a server may resolve out of its address.""" + try: + parsed = httpx.URL(url) + except httpx.InvalidURL: + return None + segments = parsed.path.rstrip("/").split("/") + if not parsed.is_absolute_url or any(segment in (".", "..") for segment in segments): + return None + return (parsed.scheme, parsed.host, parsed.port), segments async def _rpc(base_url: str, method: str, params: dict, timeout: int, verify: bool, diff --git a/packages/agentenv-protocol/tests/test_control_token.py b/packages/agentenv-protocol/tests/test_control_token.py index 88f42ffbd..5474f9fbb 100644 --- a/packages/agentenv-protocol/tests/test_control_token.py +++ b/packages/agentenv-protocol/tests/test_control_token.py @@ -129,6 +129,24 @@ def test_an_agents_token_travels_in_its_own_header_not_authorization(): assert client.control_token_headers(None) == {} and client.control_token_headers("") == {} +@pytest.mark.parametrize("url, granted", [ + ("http://proxy/sandbox/A/svc/mcp-x/agentenv", True), + ("http://PROXY:80/sandbox/A/./svc/x", True), + ("http://proxy/sandbox/A/svc/x/../y", True), + ("http://proxy/sandbox/A/svc/mcp-x/../../../B/x", False), + ("http://proxy/sandbox/A/../B/x", False), + ("http://proxy/sandbox/A/%2e%2e/B/x", False), + ("http://proxy/sandbox/AB/x", False), + ("https://proxy/sandbox/A/x", False), + ("../sandbox/A/x", False), +]) +def test_a_grant_covers_only_urls_that_resolve_under_its_address(url, granted): + """The server resolves dot segments before it routes, so a dotted path that climbs out of the env's address + reaches another env behind the same proxy.""" + assert client.granted_headers(url, {"http://proxy/sandbox/A/": client.control_headers("s3cret")}) == ( + {"Authorization": "Bearer s3cret"} if granted else {}) + + @pytest.mark.asyncio async def test_invoke_extension_and_the_data_plane_send_the_token(monkeypatch): seen = [] diff --git a/src/agent_env/env/envs/_deployment.py b/src/agent_env/env/envs/_deployment.py index 1f5e1ae87..193914bba 100644 --- a/src/agent_env/env/envs/_deployment.py +++ b/src/agent_env/env/envs/_deployment.py @@ -175,8 +175,8 @@ async def reset_server_data(env: MCPServerEnv | WebsiteEnv, *, mcp: bool) -> boo return False else: try: - await legacy_protocol.reset_via_rest(legacy_protocol.environment_base_url(env._gateway_url, name, mcp=mcp), - timeout=60) + legacy_url = legacy_protocol.environment_base_url(env._gateway_url, name, mcp=mcp) + await legacy_protocol.reset_via_rest(legacy_url, timeout=60, headers=data_plane_headers(env, legacy_url)) except httpx.HTTPStatusError as e: if e.response.status_code != 404: raise diff --git a/src/agent_env/env/envs/mcp_server.py b/src/agent_env/env/envs/mcp_server.py index c26a15de0..a42867b16 100644 --- a/src/agent_env/env/envs/mcp_server.py +++ b/src/agent_env/env/envs/mcp_server.py @@ -294,9 +294,9 @@ async def _exec_in_server(self, *command: str) -> None: async def _load_environment_artifact_legacy(self, file_artifact) -> None: container_path = await self._copy_artifact_into_container(file_artifact, file_artifact.filename) - await legacy_protocol.reset( - self._gateway_url, self.environment_name, container_path, max_retries=self._MCP_MAX_RETRIES - ) + base_url = legacy_protocol.environment_base_url(self._gateway_url, self.environment_name, mcp=True) + await legacy_protocol.reset(self._gateway_url, self.environment_name, container_path, + max_retries=self._MCP_MAX_RETRIES, headers=data_plane_headers(self, base_url)) async def validate(self, on_progress: Optional[Callable[[str], None]] = None) -> str: """Run the env validator task and return the task instance ID. diff --git a/src/agent_env/env/envs/website.py b/src/agent_env/env/envs/website.py index 0875528a8..59790d0c2 100644 --- a/src/agent_env/env/envs/website.py +++ b/src/agent_env/env/envs/website.py @@ -372,9 +372,10 @@ async def _load_environment_artifact_legacy(self, file_artifact) -> None: container_path = await self._copy_artifact_into_container(file_artifact, file_artifact.filename) base_url = legacy_protocol.environment_base_url(self._gateway_url, self.environment_name, mcp=False) from agent_env.env.gateway.constants import DATA_PLANE_LOAD_TIMEOUT_S - await legacy_protocol.reset_via_rest(base_url, timeout=60) + headers = data_plane_headers(self, base_url) + await legacy_protocol.reset_via_rest(base_url, timeout=60, headers=headers) try: - await legacy_protocol.add_via_rest(base_url, container_path, timeout=DATA_PLANE_LOAD_TIMEOUT_S) + await legacy_protocol.add_via_rest(base_url, container_path, timeout=DATA_PLANE_LOAD_TIMEOUT_S, headers=headers) except Exception as e: logger.error( "load_environment_artifact: /api/add failed after /api/reset succeeded; " diff --git a/src/agent_env/env/legacy_protocol.py b/src/agent_env/env/legacy_protocol.py index 18d78e234..216397913 100644 --- a/src/agent_env/env/legacy_protocol.py +++ b/src/agent_env/env/legacy_protocol.py @@ -209,21 +209,28 @@ async def reset_via_rest( mock_data_path: Optional[str] = None, timeout: int = 30, verify: bool = True, + *, + headers: Optional[dict[str, str]] = None, ) -> dict: - """POST ``{base_url}/api/reset``. Sends the seed path when given. Raises on non-2xx.""" + """POST ``{base_url}/api/reset`` with ``headers``, else the run's grant (``request_headers``). Sends the seed path + when given. Raises on non-2xx.""" body = {"mock_data_path": mock_data_path} if mock_data_path is not None else None + url = f"{base_url}/api/reset" async with httpx.AsyncClient(verify=verify) as client: - response = await client.post(f"{base_url}/api/reset", json=body, timeout=timeout) + response = await client.post(url, json=body, timeout=timeout, headers=protocol_v1.request_headers(url, headers)) response.raise_for_status() result = response.json() logger.info(f"load_environment_artifact: REST reset succeeded: {result}") return result -async def add_via_rest(base_url: str, file_path: str, timeout: int = 120, verify: bool = True) -> dict: - """POST ``{base_url}/api/add`` with a backend file path. Raises on non-2xx.""" +async def add_via_rest(base_url: str, file_path: str, timeout: int = 120, verify: bool = True, *, + headers: Optional[dict[str, str]] = None) -> dict: + """POST ``{base_url}/api/add`` with a backend file path and ``headers``, else the run's grant. Raises on non-2xx.""" + url = f"{base_url}/api/add" async with httpx.AsyncClient(verify=verify) as client: - response = await client.post(f"{base_url}/api/add", json={"file_path": file_path}, timeout=timeout) + response = await client.post(url, json={"file_path": file_path}, timeout=timeout, + headers=protocol_v1.request_headers(url, headers)) response.raise_for_status() result = response.json() logger.info(f"load_environment_artifact: add response: {result}") @@ -231,36 +238,41 @@ async def add_via_rest(base_url: str, file_path: str, timeout: int = 120, verify async def export_state( - gateway_url: str, environment_name: str, timeout: int = EXPORT_STATE_TIMEOUT_SECONDS, verify: bool = True + gateway_url: str, environment_name: str, timeout: int = EXPORT_STATE_TIMEOUT_SECONDS, verify: bool = True, *, + headers: Optional[dict[str, str]] = None, ) -> dict: - """GET ``/svc/mcp-{name}/export-state`` — the legacy state snapshot for one service.""" - return await _export_state_at(environment_base_url(gateway_url, environment_name, mcp=True), timeout, verify) + """GET ``/svc/mcp-{name}/export-state``, the legacy state snapshot for one service, with ``headers``, else the + run's grant.""" + return await _export_state_at(environment_base_url(gateway_url, environment_name, mcp=True), timeout, verify, headers) -async def _export_state_at(base_url: str, timeout: int, verify: bool) -> dict: +async def _export_state_at(base_url: str, timeout: int, verify: bool, headers: Optional[dict[str, str]]) -> dict: + url = f"{base_url}/export-state" async with httpx.AsyncClient(verify=verify) as client: - response = await client.get(f"{base_url}/export-state", timeout=timeout) + response = await client.get(url, timeout=timeout, headers=protocol_v1.request_headers(url, headers)) response.raise_for_status() return response.json() async def service_state(deployed: Optional[DeployedEnv], gateway_url: Optional[str], environment_name: str) -> dict: - """An MCP service's state as JSON: its v1 ``data/get`` answer when that is data, asked with the deployment's - control token, else ``GET /export-state``. + """An MCP service's state as JSON: its v1 ``data/get`` answer when that is data, else ``GET /export-state``, each + asked with the deployment's control token. A service that exports a file from ``data/get`` (a bundle of its database, to reload from) still serves its state as JSON at ``/export-state``. """ base_url = await v1_base_url(deployed, gateway_url, environment_name, mcp=True) if base_url is None: - return await export_state(gateway_url, environment_name) - response = await protocol_v1.get_data(base_url, - headers=deployed.control_headers_for(base_url) if deployed is not None else None) + legacy_url = environment_base_url(gateway_url, environment_name, mcp=True) + return await export_state(gateway_url, environment_name, + headers=(deployed.control_headers_for(legacy_url) or None) if deployed is not None else None) + headers = (deployed.control_headers_for(base_url) or None) if deployed is not None else None + response = await protocol_v1.get_data(base_url, headers=headers) if not response.parts: return {} if isinstance(response.parts[0], DataPart): return response.parts[0].data - return await _export_state_at(base_url, timeout=EXPORT_STATE_TIMEOUT_SECONDS, verify=True) + return await _export_state_at(base_url, timeout=EXPORT_STATE_TIMEOUT_SECONDS, verify=True, headers=headers) async def offers_tool(mcp_url: str, name: str) -> bool: @@ -322,12 +334,17 @@ async def reset( environment_name: str, mock_data_path: str, max_retries: int = DEFAULT_MCP_MAX_RETRIES, + *, + headers: Optional[dict[str, str]] = None, ) -> Optional[dict]: - """Reset an MCP service to a seed: try REST ``/api/reset``, fall back to the MCP tool.""" + """Reset an MCP service to a seed: try REST ``/api/reset`` with ``headers``, fall back to the MCP tool. A 401 or + 403 raises: it means the token was dropped or wrong, and the tool would reset the service without one.""" base_url = environment_base_url(gateway_url, environment_name, mcp=True) try: - return await reset_via_rest(base_url, mock_data_path) + return await reset_via_rest(base_url, mock_data_path, headers=headers) except Exception as rest_err: + if isinstance(rest_err, httpx.HTTPStatusError) and rest_err.response.status_code in (401, 403): + raise # TODO remove MCP tool fallback once all usages of MCP servers are on REST logger.info(f"load_environment_artifact: REST reset failed ({type(rest_err).__name__}), falling back to MCP tool") await reset_via_mcp_tool(f"{gateway_url}/mcp", environment_name, mock_data_path, max_retries=max_retries) diff --git a/src/agent_env/task_step/task_steps/snapshot_env.py b/src/agent_env/task_step/task_steps/snapshot_env.py index 64284ea91..44ebce964 100644 --- a/src/agent_env/task_step/task_steps/snapshot_env.py +++ b/src/agent_env/task_step/task_steps/snapshot_env.py @@ -536,7 +536,7 @@ async def _export_environment_to_file( if base_url is None and not mcp: return None if base_url is not None: - headers = deployed.control_headers_for(base_url) if deployed is not None else None + headers = (deployed.control_headers_for(base_url) or None) if deployed is not None else None if returns_file_tree(card or {}): upload = await asyncio.to_thread(_snapshot_upload, card or {}, timeout_seconds, getattr(deployed, "sandbox_type", None), FILE_TREE_LIMITS) @@ -582,6 +582,8 @@ async def _export_environment_to_file( raise ValueError( f"FilePart for '{environment_name}' names {uri!r}, which is not on its service's address {base_url}" ) + # The service names this url, so it gets the token only where it resolves under the service. + uri_headers = protocol_v1.granted_headers(uri, {base_url: protocol_v1.request_headers(base_url, headers)}) async with httpx.AsyncClient() as client: async with client.stream( "GET", @@ -589,6 +591,7 @@ async def _export_environment_to_file( timeout=httpx.Timeout( timeout_seconds, connect=_CONNECT_TIMEOUT_SECONDS ), + headers=uri_headers, ) as resp_file: resp_file.raise_for_status() with open(tmp_path, "wb") as f: @@ -601,11 +604,13 @@ async def _export_environment_to_file( return ".json" legacy_url = f"{legacy_protocol.environment_base_url(gateway_url, environment_name, mcp=True)}/export-state" + legacy_headers = (deployed.control_headers_for(legacy_url) or None) if deployed is not None else None async with httpx.AsyncClient() as client: async with client.stream( "GET", legacy_url, timeout=httpx.Timeout(timeout_seconds, connect=_CONNECT_TIMEOUT_SECONDS), + headers=protocol_v1.request_headers(legacy_url, legacy_headers), ) as resp: resp.raise_for_status() seen_first_bytes = False diff --git a/tst/integration/env/envs/multi_env_local_test.py b/tst/integration/env/envs/multi_env_local_test.py index 50c369c60..4b23a0f69 100644 --- a/tst/integration/env/envs/multi_env_local_test.py +++ b/tst/integration/env/envs/multi_env_local_test.py @@ -55,13 +55,13 @@ async def test_a_multi_env_deploys_restores_loads_and_tears_down_on_local_compos base_url = await legacy_protocol.v1_base_url(deployed, deployed.gateway_url, "items") assert (await protocol_v1.get_data(base_url, headers=deployed.control_headers)).model_dump()["parts"][0]["data"] == {"items": ["snap-x", "snap-y"]} slack_state = f"{legacy_protocol.environment_base_url(deployed.gateway_url, 'slack')}/export-state" - assert len(httpx.get(slack_state, timeout=30).json()["channels"]) == 10 + assert len(httpx.get(slack_state, headers=deployed.control_headers, timeout=30).json()["channels"]) == 10 # A multi env without computers resets its servers, each as it resets alone, and fails if one doesn't. await ResetEnvTaskStep(id="reset", version=None, env_id=env.id, fail_task_on_error=True).execute( TaskStepContext(deployed_envs=[deployed])) assert (await protocol_v1.get_data(base_url, headers=deployed.control_headers)).model_dump()["parts"][0]["data"] == {"items": []} - assert httpx.get(slack_state, timeout=30).json()["channels"] == [] + assert httpx.get(slack_state, headers=deployed.control_headers, timeout=30).json()["channels"] == [] await restored.close() assert _stack(work_dir) == "" diff --git a/tst/unit/env/envs/test_env_card_loads.py b/tst/unit/env/envs/test_env_card_loads.py index 37c385d18..533feb432 100644 --- a/tst/unit/env/envs/test_env_card_loads.py +++ b/tst/unit/env/envs/test_env_card_loads.py @@ -95,6 +95,25 @@ async def test_a_child_env_missing_from_the_card_takes_the_legacy_path(sent): assert json.loads(request.content) == {"mock_data_path": "/data/email.json"} +@pytest.mark.asyncio +@pytest.mark.parametrize("token, sent_as", [("s3cret", "Bearer s3cret"), (None, None)], ids=["token", "no-token"]) +async def test_the_legacy_path_sends_its_records_control_token_when_it_has_one(sent, token, sent_as): + # Neither email nor shop serves a card, so the gateway's card, read at its own address, lists neither. + deployed = DeployedGatewayEnv( + env_id="multi", env_version=1, gateway_url=_GW, mcp_url=f"{_GW}/mcp", db_web_url=None, sandbox_id="sb-1", + environment_card_url=f"{_GW}{WELL_KNOWN_PATH}", environment_card=await _card("mcp-slack"), control_token=token) + web = _website() + web._sandbox, web._gateway_url, web._deployed = MagicMock(), _GW, deployed + web._copy_artifact_into_container = AsyncMock(return_value="/tmp/data/shop.json") + web._env_provider = MagicMock(spec=EnvironmentGatewayProvider, install_changelog_triggers=AsyncMock()) + + await _mcp(deployed).load_environment_artifact(_artifact("email")) + await web.load_environment_artifact(_artifact("shop")) + + assert [(r.url.path, r.headers.get("authorization")) for r in sent] == [ + ("/svc/mcp-email/api/reset", sent_as), ("/svc/shop/api/reset", sent_as), ("/svc/shop/api/add", sent_as)] + + @pytest.mark.asyncio async def test_the_legacy_path_refuses_an_environment_of_several_files_before_sending_anything(sent): several = SimpleNamespace(environment_name="email", id="mail-seed", file_artifact_refs={"a.json": None, "b.json": None}) diff --git a/tst/unit/env/envs/test_mcp_server_plugin_provider.py b/tst/unit/env/envs/test_mcp_server_plugin_provider.py index b37528070..3959f27b4 100644 --- a/tst/unit/env/envs/test_mcp_server_plugin_provider.py +++ b/tst/unit/env/envs/test_mcp_server_plugin_provider.py @@ -16,7 +16,7 @@ from agentenv_protocol import DATA_OBJECTS_EXTENSION_URI from agent_env.config import get_config, reset_config -from agent_env.env.env import DeployedEnv, DeployedSandboxEnv +from agent_env.env.env import DeployedEnv, DeployedGatewayEnv, DeployedSandboxEnv from agent_env.env.envs._deployment import LOAD_OPERATIONS from agent_env.env.envs.mcp_server import MCPServerEnv from agent_env.env.envs.multi_env import MultiEnv @@ -228,9 +228,9 @@ async def test_close_closes_the_plugin_that_deployed(): async def test_a_multi_env_child_of_a_plugin_type_is_still_the_gateways(): child = _env() multi = MultiEnv(id="multi", version=1, mcp_server_envs=[child]) - record = SimpleNamespace(env_id="multi", env_version=1, sandbox_id="gw", sandbox_type="modal", sandbox_ids={}, - env_state_instance_ids=[], instance_id="i", mcp_server_name="m", environment_card=None, - environment_card_url=None, gateway_url="https://gw.example") + record = DeployedGatewayEnv(env_id="multi", env_version=1, sandbox_id="gw", sandbox_type="modal", instance_id="i", + mcp_server_name="m", gateway_url="https://gw.example", mcp_url="https://gw.example/mcp", + db_web_url=None) gw = MagicMock(sandbox_id="gw", mode="vm", type="modal") with patch("agent_env.env.env.Env.get", return_value=multi), \ patch("agent_env.providers.build_sandbox_provider", return_value=MagicMock(get_sandbox=AsyncMock(return_value=gw))), \ diff --git a/tst/unit/env/envs/test_server_reset.py b/tst/unit/env/envs/test_server_reset.py index 42adeec6b..6dfa13a96 100644 --- a/tst/unit/env/envs/test_server_reset.py +++ b/tst/unit/env/envs/test_server_reset.py @@ -95,6 +95,17 @@ async def test_an_older_server_is_emptied_through_its_api_reset_and_one_answerin assert [call.args for call in offers.await_args_list] == ([] if reset else [(f"{GATEWAY}/mcp", "email_reset")]) +@pytest.mark.asyncio +@pytest.mark.parametrize("token, sent", [("s3cret", "Bearer s3cret"), (None, None)], ids=["token", "no-token"]) +async def test_an_older_servers_api_reset_sends_its_records_control_token_when_it_has_one(server, token, sent): + env = _server(None) + env._deployed.control_token = token + with patch("agent_env.env.legacy_protocol.v1_base_url", AsyncMock(return_value=None)): + assert await _deployment.reset_server_data(env, mcp=True) is True + assert [(request.url.path, request.headers.get("authorization")) for request in server.requests] == [ + ("/svc/mcp-email/api/reset", sent)] + + @pytest.mark.asyncio async def test_an_older_server_answering_404_resets_through_its_tool_with_no_seed_to_the_data_it_boots_with(server): server.status = 404 diff --git a/tst/unit/env/test_env.py b/tst/unit/env/test_env.py index 03414a75f..8be1611ae 100644 --- a/tst/unit/env/test_env.py +++ b/tst/unit/env/test_env.py @@ -129,6 +129,10 @@ async def test_a_record_with_a_control_token_sends_it_on_each_extension_call_onl assert [deployed.control_headers_for(url) for url in (env, f"{env}/svc/clock/agentenv", f"{env}0/x", "https://override/trajectory")] == [ {"Authorization": "Bearer t0ken"}] * 2 + [{}] * 2 + prefixed = _record(composed, _BASES[2]) + prefixed.control_token = "t0ken" + assert [prefixed.control_headers_for(f"{prefixed.environment_url}{path}") for path in ( + "/svc/clock/../x", "/svc/clock/../../../sb-other/x")] == [{"Authorization": "Bearer t0ken"}, {}] assert DeployedEnv.from_dict(dataclasses.asdict(deployed)).control_token == "t0ken" assert _record(composed).control_headers == {} and _record(composed).control_headers_for(env) == {} diff --git a/tst/unit/env/test_legacy_protocol.py b/tst/unit/env/test_legacy_protocol.py index d426e3259..93ea92264 100644 --- a/tst/unit/env/test_legacy_protocol.py +++ b/tst/unit/env/test_legacy_protocol.py @@ -7,10 +7,11 @@ import httpx import pytest from agentenv_protocol import RPC_PATH, WELL_KNOWN_PATH, DataPart, uploaded_file_part +from agentenv_protocol import client as protocol_v1 from agentenv_protocol.client import GetDataResponse from agent_env.env import legacy_protocol -from agent_env.env.env import DeployedSandboxEnv +from agent_env.env.env import DeployedGatewayEnv, DeployedSandboxEnv @pytest.mark.asyncio @@ -44,7 +45,7 @@ async def test_reset_uses_rest_on_success(monkeypatch): result = await legacy_protocol.reset("http://gw", "email", "/data/seed.json") assert result == {"ok": True} - rest.assert_awaited_once_with("http://gw/svc/mcp-email", "/data/seed.json") + rest.assert_awaited_once_with("http://gw/svc/mcp-email", "/data/seed.json", headers=None) mcp.assert_not_called() @@ -61,6 +62,21 @@ async def test_reset_falls_back_to_mcp_tool(monkeypatch): mcp.assert_awaited_once_with("http://gw/mcp", "email", "/data/seed.json", max_retries=2) +@pytest.mark.asyncio +@pytest.mark.parametrize("status", [401, 403]) +async def test_a_reset_refused_for_its_token_raises_instead_of_falling_back_to_the_mcp_tool(monkeypatch, status): + """A refused token is a dropped or wrong token; the MCP tool would reset the service without one and hide it.""" + seen = _authorizations(monkeypatch, status=status) + mcp = AsyncMock() + monkeypatch.setattr(legacy_protocol, "reset_via_mcp_tool", mcp) + + with pytest.raises(httpx.HTTPStatusError) as refused: + await legacy_protocol.reset("http://gw", "email", "/data/seed.json") + + assert refused.value.response.status_code == status and seen == [("/svc/mcp-email/api/reset", None)] + mcp.assert_not_called() + + def test_service_base_url_mcp_vs_website(): assert legacy_protocol.environment_base_url("http://gw", "email", mcp=True) == "http://gw/svc/mcp-email" assert legacy_protocol.environment_base_url("http://gw", "slack", mcp=False) == "http://gw/svc/slack" @@ -179,6 +195,80 @@ async def test_a_v1_services_state_is_the_data_it_answers_with_else_its_export_s assert ("GET /svc/mcp-slack/export-state" in asked) is reads_export_state +def _authorizations(monkeypatch, rpc_parts: list | None = None, status: int = 200) -> list[tuple[str, str | None]]: + """Each request sent, as its path and its Authorization header; data/get answers with ``rpc_parts``, and every + other route with ``status``.""" + seen, real = [], httpx.AsyncClient + + def handle(request: httpx.Request) -> httpx.Response: + seen.append((request.url.path, request.headers.get("authorization"))) + if request.url.path.endswith("/agentenv"): + return httpx.Response(200, json={"jsonrpc": "2.0", "id": 1, "result": {"parts": rpc_parts or []}}) + return httpx.Response(status, json={}) + + monkeypatch.setattr(httpx, "AsyncClient", lambda *a, **k: real(transport=httpx.MockTransport(handle))) + return seen + + +_LEGACY_CALLS = { + "/svc/mcp-email/api/reset": lambda **kw: legacy_protocol.reset_via_rest("http://gw/svc/mcp-email", "/data/seed.json", **kw), + "/svc/mcp-email/api/add": lambda **kw: legacy_protocol.add_via_rest("http://gw/svc/mcp-email", "/data/seed.json", **kw), + "/svc/mcp-email/export-state": lambda **kw: legacy_protocol.export_state("http://gw", "email", **kw), +} +_GRANT = {"http://gw": protocol_v1.control_headers("granted")} + + +@pytest.mark.asyncio +@pytest.mark.parametrize("path, call", _LEGACY_CALLS.items(), ids=list(_LEGACY_CALLS)) +@pytest.mark.parametrize("grants, headers, sent", [ + ({}, None, None), + (_GRANT, None, "Bearer granted"), + ({"http://other": protocol_v1.control_headers("granted")}, None, None), + (_GRANT, protocol_v1.control_headers("own"), "Bearer own"), + (_GRANT, {}, None), +], ids=["no-grant", "granted", "granted-elsewhere", "own-headers", "own-none"]) +async def test_a_legacy_route_sends_the_headers_it_is_given_else_the_runs_grant(monkeypatch, path, call, grants, headers, sent): + seen = _authorizations(monkeypatch) + with protocol_v1.authorized(grants): + await call(headers=headers) + assert seen == [(path, sent)] + + +def _gateway_record(card: dict, token: str | None) -> DeployedGatewayEnv: + """A record whose card is read at its gateway's own address, as a built-in gateway's is.""" + return DeployedGatewayEnv(env_id="e", env_version=1, gateway_url="http://gw", mcp_url="http://gw/mcp", db_web_url=None, + sandbox_id="sb-1", environment_card_url=f"http://gw{WELL_KNOWN_PATH}", environment_card=card, + control_token=token) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("token, sent", [("t0ken", "Bearer t0ken"), (None, None)], ids=["token", "no-token"]) +async def test_a_services_export_state_is_read_with_its_deployments_control_token(monkeypatch, token, sent): + """slack answers data/get with a bundle, so its state is read from its export-state; email serves no card, so the + gateway's card lists none and its state is read from its export-state behind the gateway.""" + card = {"name": "gw", "children_environments": [{"name": "slack", "url": "/svc/mcp-slack/agentenv"}]} + seen = _authorizations(monkeypatch, [uploaded_file_part("slack.zip", name="slack.zip").model_dump(mode="json")]) + deployed = _gateway_record(card, token) + + await legacy_protocol.service_state(deployed, "http://gw", "slack") + await legacy_protocol.service_state(deployed, "http://gw", "email") + + assert seen == [("/svc/mcp-slack/agentenv", sent), ("/svc/mcp-slack/export-state", sent), + ("/svc/mcp-email/export-state", sent)] + + +@pytest.mark.asyncio +async def test_a_services_export_state_behind_another_gateway_is_read_with_the_runs_grant(monkeypatch): + """The record's token covers its own gateway only; at an override's gateway the run's grant decides.""" + seen = _authorizations(monkeypatch) + deployed = _gateway_record({"name": "gw", "children_environments": []}, "t0ken") + + with protocol_v1.authorized({"http://override": protocol_v1.control_headers("granted")}): + await legacy_protocol.service_state(deployed, "http://override", "email") + + assert seen == [("/svc/mcp-email/export-state", "Bearer granted")] + + def test_definition_servers_are_an_envs_mcp_servers_then_its_websites(): from types import SimpleNamespace diff --git a/tst/unit/task_step/test_snapshot_agent_state.py b/tst/unit/task_step/test_snapshot_agent_state.py index 551f1cd26..71f6384b0 100644 --- a/tst/unit/task_step/test_snapshot_agent_state.py +++ b/tst/unit/task_step/test_snapshot_agent_state.py @@ -107,7 +107,7 @@ async def _supports_v1(_base): monkeypatch.setattr(protocol_v1, "supports_v1", _supports_v1) - async def _export_state(_gw, name): + async def _export_state(_gw, name, headers=None): return {"service": name, "rows": 1} monkeypatch.setattr(legacy_protocol, "export_state", _export_state) @@ -139,7 +139,7 @@ def put(self, *a, **kw): async def _no_v1(_base): return False - async def _export_state(_gw, name): + async def _export_state(_gw, name, headers=None): return {"service": name} monkeypatch.setattr(protocol_v1, "supports_v1", _no_v1) @@ -171,7 +171,7 @@ async def _get_data(base, timeout=30, headers=None): got.append(base) return GetDataResponse(parts=[DataPart(data={"rows": 1})]) - async def _export_state(gw, name): + async def _export_state(gw, name, headers=None): legacy.append((gw, name)) return {"rows": 1} @@ -223,6 +223,40 @@ def handle(request: httpx.Request) -> httpx.Response: assert set(json.loads(ctx.metadata["snapshot_json_url"])) == {"calendar", "contacts"} +@pytest.mark.asyncio +@pytest.mark.parametrize("token, sent", [("t0ken", "Bearer t0ken"), (None, None)], ids=["token", "no-token"]) +async def test_each_services_state_is_read_with_the_records_control_token(monkeypatch, token, sent): + """calendar answers data/get with a bundle, so its state comes from its export-state; contacts serves no card, so + its state comes from its export-state behind the gateway.""" + import httpx + from agentenv_protocol import uploaded_file_part + + from agent_env.env.gateway.constants import WELL_KNOWN_PATH + + store = _Store() + monkeypatch.setattr(artifact_mod, "EnvironmentUniverseArtifact", type("U", (), {"get": staticmethod(lambda _id: _StubUniverse())})) + monkeypatch.setattr(config_mod, "get_config", lambda: type("Cfg", (), {"get_object_store_at": lambda self, object_url: store})()) + seen, real = [], httpx.AsyncClient + bundle = uploaded_file_part("calendar.zip", name="calendar.zip").model_dump(mode="json") + + def handle(request: httpx.Request) -> httpx.Response: + seen.append((request.url.path, request.headers.get("authorization"))) + if request.url.path.endswith("/agentenv"): + return httpx.Response(200, json={"jsonrpc": "2.0", "id": 1, "result": {"parts": [bundle]}}) + return httpx.Response(200, json={"rows": 1}) + + monkeypatch.setattr(httpx, "AsyncClient", lambda *a, **k: real(transport=httpx.MockTransport(handle))) + ctx = _ctx() + record = ctx.deployed_envs[0] + record.environment_card_url, record.environment_card = f"https://gw{WELL_KNOWN_PATH}", await _card("mcp-calendar") + record.control_token = token + + await _step()._capture_universe_state(ctx, _PREFIX) + + assert seen == [("/svc/mcp-calendar/agentenv", sent), ("/svc/mcp-calendar/export-state", sent), + ("/svc/mcp-contacts/export-state", sent)] + + async def _card(*keys: str) -> dict: """The real gateway's composed card over child envs at the given gateway keys (`mcp-{name}` or `{name}`).""" from agent_env.env.gateway.gateway import Gateway diff --git a/tst/unit/task_step/test_snapshot_env.py b/tst/unit/task_step/test_snapshot_env.py index cf75fb7ca..7419c0ddd 100644 --- a/tst/unit/task_step/test_snapshot_env.py +++ b/tst/unit/task_step/test_snapshot_env.py @@ -1043,6 +1043,69 @@ async def test_export_of_a_child_env_missing_from_the_card_streams_legacy_export assert [(r.method, str(r.url)) for r in sent] == [("GET", "https://gw-1/svc/mcp-gdrive/export-state")] +@pytest.mark.asyncio +@pytest.mark.parametrize("token, sent_as", [("t0ken", "Bearer t0ken"), (None, None)], ids=["token", "no-token"]) +async def test_export_streams_export_snapshot_and_export_state_with_the_records_control_token(monkeypatch, tmp_path, token, sent_as): + """gdrive streams its bundle from its export-snapshot; slack serves no card, so its state streams from its export-state.""" + from agent_env.task_step.task_steps import snapshot_env as mod + + record = DeployedGatewayEnv( + env_id="env-1", env_version=3, gateway_url="https://gw-1", mcp_url="https://gw-1/mcp", db_web_url=None, + sandbox_id="sb-1", environment_card_url=f"https://gw-1{WELL_KNOWN_PATH}", + environment_card=await _card({"mcp-gdrive": []}), control_token=token) + sent = _mock_http(monkeypatch, body=b"{}", rpc_result={"parts": [ + {"kind": "file", "file": {"uri": "export-snapshot?stream=1", "name": "gdrive.zip"}}]}) + + for name in ("gdrive", "slack"): + await mod.SnapshotEnvTaskStep._export_environment_to_file("https://gw-1", name, str(tmp_path / name), 30, record) + + assert [(r.method, str(r.url), r.headers.get("authorization")) for r in sent] == [ + ("POST", "https://gw-1/svc/mcp-gdrive/agentenv", sent_as), + ("GET", "https://gw-1/svc/mcp-gdrive/export-snapshot?stream=1", sent_as), + ("GET", "https://gw-1/svc/mcp-slack/export-state", sent_as), + ] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("uri, fetched, sent_as", [ + ("export-snapshot?stream=1", "https://proxy/sb/A/svc/mcp-gdrive/export-snapshot?stream=1", "Bearer t0ken"), + ("../../../B/collect", "https://proxy/sb/B/collect", None), + ("https://proxy/sb/B/collect", "https://proxy/sb/B/collect", None), +], ids=["own-service", "dotted-out", "same-host-elsewhere"]) +async def test_a_streamed_bundle_gets_the_token_only_at_its_own_service(monkeypatch, tmp_path, uri, fetched, sent_as): + """A service names the url its bundle streams from; behind a path-prefixed proxy another env shares the host.""" + from agent_env.task_step.task_steps import snapshot_env as mod + + record = DeployedGatewayEnv( + env_id="env-1", env_version=3, gateway_url="https://proxy/sb/A", mcp_url="https://proxy/sb/A/mcp", db_web_url=None, + sandbox_id="sb-1", environment_card_url=f"https://proxy/sb/A{WELL_KNOWN_PATH}", + environment_card=await _card({"mcp-gdrive": []}), control_token="t0ken") + sent = _mock_http(monkeypatch, rpc_result={"parts": [{"kind": "file", "file": {"uri": uri, "name": "gdrive.zip"}}]}) + + with protocol_v1.authorized({"https://proxy/sb/B": protocol_v1.control_headers("other")}): + await mod.SnapshotEnvTaskStep._export_environment_to_file("https://proxy/sb/A", "gdrive", str(tmp_path / "g"), 30, record) + + assert [(str(r.url), r.headers.get("authorization")) for r in sent[1:]] == [(fetched, sent_as)] + + +@pytest.mark.asyncio +async def test_a_legacy_export_state_behind_another_gateway_is_read_with_the_runs_grant(monkeypatch, tmp_path): + """The record's token covers its own gateway only; at an override's gateway the run's grant decides.""" + from agent_env.task_step.task_steps import snapshot_env as mod + + record = DeployedGatewayEnv( + env_id="env-1", env_version=3, gateway_url="https://gw-1", mcp_url="https://gw-1/mcp", db_web_url=None, + sandbox_id="sb-1", environment_card_url=f"https://gw-1{WELL_KNOWN_PATH}", + environment_card={"name": "gw", "children_environments": []}, control_token="t0ken") + sent = _mock_http(monkeypatch, body=b"{}") + + with protocol_v1.authorized({"https://override": protocol_v1.control_headers("granted")}): + await mod.SnapshotEnvTaskStep._export_environment_to_file("https://override", "slack", str(tmp_path / "s"), 30, record) + + assert [(str(r.url), r.headers.get("authorization")) for r in sent if r.url.path.endswith("/export-state")] == [ + ("https://override/svc/mcp-slack/export-state", "Bearer granted")] + + @pytest.mark.asyncio async def test_snapshot_uses_the_stored_card_only_for_the_deployments_own_gateway(monkeypatch): """An override's gateway has its card read live, and the record's stored card describes its own gateway only.""" From 3a571f84e03f28f96338a8982b4082c4fcc929c8 Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Thu, 19 Nov 2026 18:32:42 +0000 Subject: [PATCH 2/3] feat(gateway): take the control token on the older /svc REST state routes LEGACY_STATE_ROUTES lists the older state routes a backing server serves beside its data plane. On a gateway with a control token, every /svc// route takes the token for each of them, after path normalization. A site's /website route serves none of them. A gateway booted without a token behaves as before. --- src/agent_env/env/gateway/constants.py | 17 +++++- src/agent_env/env/gateway/gateway.py | 31 ++++++---- .../cli/run_bundle_built_envs_test.py | 6 +- tst/unit/env/gateway/control_token_test.py | 60 +++++++++++++++---- 4 files changed, 88 insertions(+), 26 deletions(-) diff --git a/src/agent_env/env/gateway/constants.py b/src/agent_env/env/gateway/constants.py index 35e4d6957..8c6e152c1 100644 --- a/src/agent_env/env/gateway/constants.py +++ b/src/agent_env/env/gateway/constants.py @@ -198,15 +198,26 @@ def sources(names: list[str]) -> str: TOOL_DISABLE_ACTION = "disable" TOOL_ENABLE_ACTION = "enable" # The harness's routes that take the deploy's control token: the ones that change tool access, triggers and -# notifications. The gateway also takes it for the data plane and for an MCP server's own endpoint behind its proxy -# (Gateway._takes_control_token). Every other route stays open for now, the clock's writes included: callers outside -# agent-env set the clock over plain HTTP and don't send the token yet. +# notifications. The gateway also takes it, behind its proxy, for the data plane, for an MCP server's own endpoint and +# for the LEGACY_STATE_ROUTES (Gateway._takes_control_token). Every other route stays open for now, the clock's writes +# included: callers outside agent-env set the clock over plain HTTP and don't send the token yet. CONTROL_ROUTES = frozenset({ f"/tools/{TOOL_DISABLE_ACTION}", f"/tools/{TOOL_ENABLE_ACTION}", "/triggers/register", "/triggers/remove", "/triggers/clear", "/notifications/config", "/notifications/take", "/notifications/clear", CLOCK_FOLLOWERS_PATH, }) +# The older REST routes a backing server serves beside its data plane, which reset, load or export its state. +# Behind the proxy, /svc// takes the control token for each, and anything under it, on a gateway that has one. +# A site's /website route serves none of them, since a site with no frontend of its own serves its backend there and +# an older frontend passes them on. +LEGACY_STATE_ROUTES = ( + "/api/reset", "/api/add", "/api/bridge/apply", + "/export-state", "/export-snapshot", "/snapshot-dirty", + "/reset-memory", "/validate-rows", + "/api/_state", "/api/_reset", "/api/debug/seed", +) +SITE_LEGACY_STATE_ROUTES = frozenset(LEGACY_STATE_ROUTES) class GatewayMode(str, Enum): diff --git a/src/agent_env/env/gateway/gateway.py b/src/agent_env/env/gateway/gateway.py index ae2af8ed6..ddeb14f2a 100644 --- a/src/agent_env/env/gateway/gateway.py +++ b/src/agent_env/env/gateway/gateway.py @@ -74,6 +74,8 @@ WILDCARD, PGWEB_CONSOLE, GatewayMode, + LEGACY_STATE_ROUTES, + SITE_LEGACY_STATE_ROUTES, tool_name_clashes_text, ) from .browser_proxy import PING_INTERVAL_S, BrowserRoutes, TokenRedaction @@ -692,9 +694,9 @@ def _is_pgweb(scope: dict) -> bool: def _takes_control_token(self, scope: dict) -> bool: """Whether a request takes the deploy's control token: a control route, the env's data plane, any path of a SQL - console behind /svc, or one of ``token_paths`` behind /svc, by the path as routed or by the one the backing - server receives (``reaches``), after dot-segment normalisation. A site's /website route never reaches its data - plane.""" + console behind /svc, or one of ``token_paths`` behind /svc (a backing server's data plane, its MCP endpoint and + its LEGACY_STATE_ROUTES), by the path as routed or by the one the backing server receives (``reaches``), after + dot-segment normalisation. A site's /website route never reaches its data plane.""" path = scope["path"] if path in CONTROL_ROUTES or is_child_mcp_path(path, RPC_PATH): return True @@ -970,12 +972,13 @@ async def _proxy_website_request(self, request: Request) -> Response: """Proxy a website request; with a website or state trigger registered, feed it to the engine and hold barriers. A site's data plane and card, and every path its card advertises for agentenv (``site_reserved_paths``), are never reached through its route, so an agent browsing the site can't read its data or what grades it; agentenv - reaches them through /svc//.""" + reaches them through /svc//. On a gateway with a token, neither is SITE_LEGACY_STATE_ROUTES + (``_site_reserved``).""" app = request.path_params["service_name"] card = await self._site_card(app) if card is None: return self._json_response({"error": "website card unavailable"}, 503) - if reserved_site_request(request, card.reserved): + if reserved_site_request(request, self._site_reserved(card)): return self._json_response({"error": "not found"}, 404) response = await self._proxy_rest_request(request, url_map=self.website_urls, site_names=self._names(app, card)) engine = self._trigger_engine @@ -993,14 +996,14 @@ async def _proxy_website_request(self, request: Request) -> Response: async def _proxy_website_socket(self, websocket: WebSocket) -> None: """Relay a WebSocket on a site's route to its server, as its HTTP requests are: its data plane and card, and - every path its card advertises for agentenv (``site_reserved_paths``), are refused, and it goes upstream with - the role, role key and forwarded address an HTTP request would carry.""" + every path its card advertises for agentenv (``site_reserved_paths``), are refused, as ``_site_reserved`` + says, and it goes upstream with the role, role key and forwarded address an HTTP request would carry.""" service_name = websocket.path_params["service_name"] card = await self._site_card(service_name) if card is None: await refuse_websocket(websocket, self._json_response({"error": "website card unavailable"}, 503)) return - if reserved_site_request(websocket, card.reserved): + if reserved_site_request(websocket, self._site_reserved(card)): await refuse_websocket(websocket, self._json_response({"error": "not found"}, 404)) return base_url = self.website_urls.get(service_name) @@ -1011,6 +1014,11 @@ async def _proxy_website_socket(self, websocket: WebSocket) -> None: open_timeout=self.REST_PROXY_CONNECT_TIMEOUT_S, withheld=self._withheld, site_names=self._names(service_name, card)) + def _site_reserved(self, card: SiteCard) -> frozenset[str]: + """The paths a site's route never reaches: those its card advertises for agentenv, and on a gateway with a + token SITE_LEGACY_STATE_ROUTES, which an older site's frontend passes on to its backend.""" + return card.reserved | SITE_LEGACY_STATE_ROUTES if self._control_bearer else card.reserved + async def _fire_website_triggers(self, request: Request, app: str, status: int) -> tuple[list[asyncio.Task], float | None]: """Feed an answered website request to the trigger engine (fail-open).""" @@ -1942,10 +1950,11 @@ def mcp_endpoints(servers: list[InternalMCPServer], website_urls: dict[str, str] def token_paths(servers: list[InternalMCPServer], website_urls: dict[str, str], rest_proxy_urls: dict[str, str]) -> dict[str, tuple[str, ...]]: """The paths under /svc// that take the gateway's control token, by key: every backing server's data plane, - extensions included, and an MCP server's own endpoint (``mcp_endpoints``), which skips the gateway's trajectory and - triggers. A child env registered under its own name, a computer, takes its own token, so none of its paths are here.""" + extensions included, its LEGACY_STATE_ROUTES, and an MCP server's own endpoint (``mcp_endpoints``), which skips + the gateway's trajectory and triggers. A child env registered under its own name, a computer, takes its own token, + so none of its paths are here.""" endpoints = mcp_endpoints(servers, website_urls, rest_proxy_urls) - return {key: (RPC_PATH, *((endpoints[key],) if key in endpoints else ())) + return {key: (RPC_PATH, *LEGACY_STATE_ROUTES, *((endpoints[key],) if key in endpoints else ())) for key, base in rest_proxy_urls.items() if key in website_urls or route_environment_name(key, base, servers, website=False) != key or not any(server.name == key for server in servers)} diff --git a/tst/integration/cli/run_bundle_built_envs_test.py b/tst/integration/cli/run_bundle_built_envs_test.py index a2d862105..9e2e17d52 100644 --- a/tst/integration/cli/run_bundle_built_envs_test.py +++ b/tst/integration/cli/run_bundle_built_envs_test.py @@ -397,9 +397,10 @@ def test_environments_and_a_universe_written_from_a_bundle_load_into_an_env_thro # Checks each items server CONFIG names holds exactly the items loaded into it, read from its export-state route behind -# the multi's gateway. CONFIG goes above it. +# the multi's gateway, with the control token the run's grant gives. CONFIG goes above it. VERIFY_SEEDED = ''' import httpx +from agentenv_protocol.client import request_headers async def verify(mcp_url): @@ -407,7 +408,8 @@ async def verify(mcp_url): results = [] async with httpx.AsyncClient(timeout=30) as client: for server, items in CONFIG["servers"].items(): - response = await client.get(f"{gateway}/svc/mcp-{server}/export-state") + url = f"{gateway}/svc/mcp-{server}/export-state" + response = await client.get(url, headers=request_headers(url)) listed = response.json() if response.status_code == 200 else {} description = f"{server} holds what it was seeded with, {items}: {response.status_code} {listed}" results.append({"id": server, "result": listed.get("items") == items, "description": description}) diff --git a/tst/unit/env/gateway/control_token_test.py b/tst/unit/env/gateway/control_token_test.py index 6a9a22771..b968aaa54 100644 --- a/tst/unit/env/gateway/control_token_test.py +++ b/tst/unit/env/gateway/control_token_test.py @@ -1,5 +1,6 @@ -"""The gateway's control routes, the data plane and an MCP server's own endpoint behind its proxy take the deploy's -control token when it has one; every other route stays open.""" +"""The gateway's control routes, and behind its proxy the data plane, an MCP server's own endpoint and the older REST +routes that reset, load or export state, take the deploy's control token when it has one; every other route stays +open.""" from __future__ import annotations import asyncio @@ -12,7 +13,7 @@ from agent_env.env.gateway import InternalMCPServer from agent_env.env.gateway import rest_proxy as rest_proxy_module -from agent_env.env.gateway.constants import CONTROL_ROUTES, WELL_KNOWN_PATH +from agent_env.env.gateway.constants import CONTROL_ROUTES, LEGACY_STATE_ROUTES, WELL_KNOWN_PATH from agent_env.env.gateway.gateway import Gateway TOKEN = "s3cret" @@ -27,17 +28,37 @@ "/clock/set-time": "PUT", "/clock/advance": "PUT", "/clock/clear": "POST"} -# Behind the proxy: the data plane, its extensions, each service's included, and an MCP server's own endpoint, however -# the path is spelt on its way to the gateway. +LEGACY = {"/api/reset": "POST", "/api/add": "POST", "/api/bridge/apply": "POST", "/export-state": "GET", + "/export-snapshot": "GET", "/snapshot-dirty": "GET", "/reset-memory": "POST", "/validate-rows": "POST", + "/api/_state": "GET", "/api/_reset": "POST", "/api/debug/seed": "POST"} + +# Behind the proxy: the data plane, its extensions, each service's included, an MCP server's own endpoint, and the +# older REST routes on an MCP server's route and a site's, however the path is spelt on its way to the gateway. GUARDED = [("POST", "/agentenv"), ("POST", "/svc/mcp-svc/agentenv"), ("POST", "/svc/mcp-svc/agentenv/ext/grant"), ("POST", "/svc/mcp-svc/x/../agentenv"), ("POST", "/svc/mcp-svc/%61gentenv"), ("POST", "/svc/shop/agentenv"), ("POST", "/svc/mcp-svc/mcp"), ("GET", "/svc/mcp-svc/mcp/"), ("DELETE", "/svc/mcp-svc/./mcp"), ("POST", "/svc/mcp-svc//mcp"), ("POST", "/svc/mcp-svc/public%2Fx/../agentenv"), ("POST", "/svc/mcp-svc/public%2Fx/../mcp"), ("POST", "/svc/mcp-svc/public%2Fx/../agentenv/ext/set_acting_user"), - ("POST", "/svc/mcp-svc/AgentEnv"), ("POST", "/svc/mcp-svc/MCP")] + ("POST", "/svc/mcp-svc/AgentEnv"), ("POST", "/svc/mcp-svc/MCP"), + *((method, f"/svc/{key}{path}") for key in ("mcp-svc", "shop") for path, method in LEGACY.items()), + ("GET", "/svc/mcp-svc/export-snapshot?stream=1"), ("POST", "/svc/shop/api/reset/x"), + ("GET", "/svc/mcp-svc/x/../export-state"), ("GET", "/svc/mcp-svc/%65xport-state"), + ("POST", "/svc/mcp-svc/public%2Fx/../api/reset"), ("POST", "/svc/shop/public%2Fx/../api/add"), + ("POST", "/svc/mcp-svc/API/Reset"), ("GET", "/svc/shop/Export-Snapshot"), ("POST", "/svc/mcp-svc/api//reset"), + ("POST", "/svc/mcp-svc/api/bridge/../bridge/apply"), ("GET", "/svc/shop/api/_state/"), + ("GET", "/svc/shop/API/_State"), ("GET", "/svc/shop/x/../api/_state"), ("GET", "/svc/shop/api/%5Fstate")] PROXIED_OPEN = [("GET", "/svc/mcp-svc/.well-known/agent-env.json"), ("GET", "/svc/mcp-svc/openapi.yaml"), - ("GET", "/svc/mcp-svc/export-state"), ("POST", "/svc/shop/api/orders"), ("POST", "/svc/shop/mcp"), - ("GET", "/website/shop/index.html")] + ("POST", "/svc/shop/api/orders"), ("POST", "/svc/shop/mcp"), ("GET", "/website/shop/index.html"), + ("GET", "/svc/mcp-svc/export-statex"), ("POST", "/svc/mcp-svc/api/resetx"), + ("POST", "/svc/shop/api/address"), ("GET", "/svc/mcp-svc/x/export-state"), + ("POST", "/svc/mcp-svc/api/bridge"), ("GET", "/website/shop/export-statex"), + ("GET", "/svc/shop/api/_statex"), ("GET", "/svc/shop/api/state"), ("GET", "/website/shop/api/state"), + ("GET", "/website/shop/api/_statex"), ("GET", "/svc/shop/api/debug/inspect")] +# A site's route never reaches these on a gateway with a token, however they are spelt, with the token or without. +SITE_REFUSED = [*(f"/website/shop{path}" for path in LEGACY), "/website/shop/api/reset/x", + "/website/shop/export-snapshot?stream=1", "/website/shop/x/../export-state", + "/website/shop/x/../api/add", "/website/shop/public%2Fx/../api/reset", + "/website/shop/x/../api/_state", "/website/shop/api/%5Fstate"] _RealAsyncClient = httpx.AsyncClient @@ -48,6 +69,7 @@ def _client(token: str | None) -> httpx.AsyncClient: def test_the_routes_under_test_are_the_ones_the_gateway_covers(): assert set(CONTROL) == CONTROL_ROUTES + assert set(LEGACY) == set(LEGACY_STATE_ROUTES) @pytest.mark.asyncio @@ -151,7 +173,7 @@ async def _proxied(token: str | None, method: str, path: str, headers: dict[str, "website_urls": {"shop": "http://shop-backend"}, **children}) url = urlsplit(path) scope = {"type": "http", "asgi": {"version": "3.0", "spec_version": "2.3"}, "http_version": "1.1", "method": method, - "scheme": "http", "path": unquote(url.path), "raw_path": url.path.encode(), "query_string": b"", + "scheme": "http", "path": unquote(url.path), "raw_path": url.path.encode(), "query_string": url.query.encode(), "root_path": "", "server": ("gateway", 80), "client": ("agent", 1), "headers": [(b"content-type", b"application/json"), *((name.lower().encode(), value.encode()) for name, value in headers.items())]} @@ -182,7 +204,8 @@ async def test_the_data_plane_and_a_servers_own_endpoint_behind_the_proxy_refuse caplog.set_level(logging.WARNING, logger="agent_env.env.gateway.gateway") assert await _proxied(TOKEN, method, path, headers) == 401 assert backing.paths == [] - assert [r.getMessage() for r in caplog.records] == [f"refused {method} {unquote(path)}: it takes the env's control token"] + assert [r.getMessage() for r in caplog.records] == [ + f"refused {method} {unquote(urlsplit(path).path)}: it takes the env's control token"] @pytest.mark.asyncio @@ -208,6 +231,23 @@ async def test_a_gateway_started_without_a_token_guards_nothing_behind_its_proxy assert await _proxied(None, method, path, {}) != 401 +@pytest.mark.asyncio +@pytest.mark.parametrize("path", SITE_REFUSED) +@pytest.mark.parametrize("headers", [{}, {"Authorization": f"Bearer {TOKEN}"}], ids=["none", "token"]) +async def test_a_sites_route_never_reaches_its_reset_or_add_on_a_gateway_with_a_token(monkeypatch, path, headers): + backing = _Backing(monkeypatch) + assert await _proxied(TOKEN, "POST", path, headers) == 404 + assert [at for at in backing.paths if at != WELL_KNOWN_PATH] == [] + + +@pytest.mark.asyncio +@pytest.mark.parametrize("path", SITE_REFUSED) +async def test_a_sites_route_passes_its_reset_and_add_on_a_gateway_without_a_token(monkeypatch, path): + backing = _Backing(monkeypatch) + assert await _proxied(None, "POST", path, {}) == 200 + assert [at for at in backing.paths if at != WELL_KNOWN_PATH] + + @pytest.mark.asyncio @pytest.mark.parametrize("method, path", [("POST", "/svc/mcp-x/rpc"), ("GET", "/svc/mcp-x/rpc/"), ("POST", "/svc/mcp-x/agentenv")]) async def test_an_mcp_server_whose_endpoint_is_elsewhere_is_guarded_at_its_endpoint(monkeypatch, method, path): From 475e53df0195aeb63326c7473b69b589739a3f8f Mon Sep 17 00:00:00 2001 From: Ubuntu Date: Fri, 20 Nov 2026 23:04:32 +0000 Subject: [PATCH 3/3] fix(gateway): read a site route's path under the site's base path A site at http://host/base receives ../base/x as /base/x, which is /x under its base. The route's path check now reads the path the site receives against the site's own address, decoded, with the base path taken off, so it matches the same paths the site's card names. --- src/agent_env/env/gateway/gateway.py | 33 +++++++++++-------- .../gateway/website_reserved_paths_test.py | 24 ++++++++++++++ 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/agent_env/env/gateway/gateway.py b/src/agent_env/env/gateway/gateway.py index ddeb14f2a..c3045ad1e 100644 --- a/src/agent_env/env/gateway/gateway.py +++ b/src/agent_env/env/gateway/gateway.py @@ -978,7 +978,7 @@ async def _proxy_website_request(self, request: Request) -> Response: card = await self._site_card(app) if card is None: return self._json_response({"error": "website card unavailable"}, 503) - if reserved_site_request(request, self._site_reserved(card)): + if reserved_site_request(request, self._site_reserved(card), self.website_urls.get(app, "http://site")): return self._json_response({"error": "not found"}, 404) response = await self._proxy_rest_request(request, url_map=self.website_urls, site_names=self._names(app, card)) engine = self._trigger_engine @@ -1003,7 +1003,8 @@ async def _proxy_website_socket(self, websocket: WebSocket) -> None: if card is None: await refuse_websocket(websocket, self._json_response({"error": "website card unavailable"}, 503)) return - if reserved_site_request(websocket, self._site_reserved(card)): + if reserved_site_request(websocket, self._site_reserved(card), + self.website_urls.get(service_name, "http://site")): await refuse_websocket(websocket, self._json_response({"error": "not found"}, 404)) return base_url = self.website_urls.get(service_name) @@ -2021,23 +2022,29 @@ def site_reserved_paths(card: dict, base: str = "") -> frozenset[str]: return frozenset(p for p in paths if p != "/") -def site_request_path(request: HTTPConnection) -> str | None: - """The path a site's server receives for a request on its route: the raw path forwarded, its dot segments removed - by httpx and then decoded. It differs from the path as routed here when an encoded slash sits beside a dot segment - (``public%2Fx/../api`` reaches ``/api``). None for a request-target the proxy can't build a URL for: ``forward()`` - refuses it.""" +def site_request_path(request: HTTPConnection, base_url: str = "http://site") -> str | None: + """The path a site's server receives for a request on its route, relative to the path of its address + ``base_url``: the raw path forwarded, decoded and its dot segments removed, with the address's path taken off. It differs from the path as routed here when an encoded slash sits beside a dot segment + (``public%2Fx/../api`` reaches ``/api``), or when dot segments climb out of the address's path and back into it + (``../base/api`` under ``/base`` reaches ``/api``). A path that climbs out and stays out is given whole. None for + a request-target the proxy can't build a URL for: ``forward()`` refuses it.""" try: - return httpx.URL(upstream_url("http://site", request)).path + sent = _normalized(httpx.URL(upstream_url(base_url.rstrip("/"), request)).path) + under = _normalized(httpx.URL(base_url).path).rstrip("/") except (ValueError, httpx.InvalidURL): return None + if under and (sent == under or sent.startswith(under + "/")): + return sent[len(under):] or "/" + return sent -def reserved_site_request(request: HTTPConnection, reserved: frozenset[str] = frozenset()) -> bool: +def reserved_site_request(request: HTTPConnection, reserved: frozenset[str] = frozenset(), + base_url: str = "http://site") -> bool: """Whether a request or WebSocket on a site's route reaches its data plane or env card, or one of the paths - ``reserved`` names (``site_reserved_paths``): by the path as routed here, or by the one its server receives - (``site_request_path``). A request-target the proxy can't build a URL for isn't reserved here: ``forward()`` - refuses it.""" + ``reserved`` names (``site_reserved_paths``): by the path as routed here, or by the one its server at + ``base_url`` receives (``site_request_path``). A request-target the proxy can't build a URL for isn't reserved + here: ``forward()`` refuses it.""" if reserved_site_path("/" + request.path_params.get("path", ""), reserved): return True - sent = site_request_path(request) + sent = site_request_path(request, base_url) return sent is not None and reserved_site_path(sent, reserved) diff --git a/tst/unit/env/gateway/website_reserved_paths_test.py b/tst/unit/env/gateway/website_reserved_paths_test.py index 40c238684..2a7967989 100644 --- a/tst/unit/env/gateway/website_reserved_paths_test.py +++ b/tst/unit/env/gateway/website_reserved_paths_test.py @@ -203,3 +203,27 @@ def site(request): assert (await _http(gw, "OPTIONS", "/website/api/items", headers=preflight))[0] == 200 assert (await _http(gw, "OPTIONS", "/website/api", headers=preflight))[0] == 200 assert ("OPTIONS", "/items") in reached and ("OPTIONS", "/") in reached + + +@pytest.mark.asyncio +async def test_a_site_behind_a_base_path_refuses_its_reserved_paths_when_a_path_climbs_out_of_the_base_and_back(monkeypatch): + reached = [] + card = {"name": "shop", "url": "/agentenv", + "additionalInterfaces": [{"url": "http://shop-website-backend:8000/base/mcp", "transport": "mcp"}]} + + def site(request): + reached.append(request.url.path) + if request.url.path == f"/base{WELL_KNOWN_PATH}": + return httpx.Response(200, json=card) + return httpx.Response(200, content=_streamed()) + + transport = httpx.MockTransport(site) + monkeypatch.setattr(rest_proxy_module.httpx, "AsyncClient", lambda **kw: _RealAsyncClient(transport=transport, **kw)) + gw = _gateway(_Changelog(), website_urls={"shop": "http://shop-website-backend:8000/base"}, + rest_proxy_urls={"shop": "http://shop-website-backend:8000/base"}) + + for path in ("../base/agentenv", "../base/mcp", "../base/.well-known/agent-env.json", "x/../../base/agentenv", + "..%2Fbase/agentenv"): + assert (await _http(gw, "POST", f"/website/shop/{path}", body=b"{}"))[0] == 404, path + assert (await _http(gw, "GET", "/website/shop/../base/orders"))[0] == 200 + assert reached == [f"/base{WELL_KNOWN_PATH}", "/base/orders"]