From 03d576b6de44babefe60507d8aa232d4e02a60dc Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 18:41:16 -0800 Subject: [PATCH 1/6] feat(computer): a browser alone reaches the open web but never an internal address (FD-4969, M8.21) A browser running alone goes out only through a proxy, and neither of its proxies checked where it connected: the headless base's stand-in on 3128 forwarded every host, and the shared site proxy's forward port took a live site's hosts to whatever their names resolved to. - Both now resolve each name themselves and connect only to an address they checked (`allowed`): one that isn't globally routable, or is multicast, is refused at once with a 403, an IPv4-mapped, 6to4 or NAT64 address judged as the IPv4 address it carries. So loopback, private, link-local (the metadata service), shared and reserved addresses and their IPv6 equivalents are refused whether a page or a CDP agent names them, by address or by a name. - HTTPS is still tunnelled with CONNECT, never decrypted; plain HTTP is forwarded. - Each connection is a JSON line in the proxy's log when it ends (host, port, address, bytes each way, start, duration), and each refusal one with its reason. - The stand-in waits 30 s to connect and keeps an idle connection 300 s (it dropped one at 60 s). - The headless base's `own_loopback` setting, false by default, lets the browser reach the computer's own loopback, as the browser computer's tests, which serve their pages inside the computer, now set. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- src/agent_env/computers/README.md | 8 +- .../ubuntu-24.04-headless/artifact.toml | 4 + .../ubuntu-24.04-headless/site-proxy.py | 150 ++++++++++++--- .../env/envs/computer/utils/site_proxy.py | 77 +++++++- .../computer/browser_computer_test.py | 99 ++++++++-- .../computer/browser_gateway_test.py | 10 +- tst/unit/env/envs/test_computer_open_web.py | 171 ++++++++++++++++++ tst/unit/env/envs/test_computer_site_proxy.py | 68 ++++++- 9 files changed, 524 insertions(+), 65 deletions(-) create mode 100644 tst/unit/env/envs/test_computer_open_web.py diff --git a/AGENTS.md b/AGENTS.md index 62d6457c7..0c8822f16 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -121,7 +121,7 @@ and deserialized through a registry. | Package | Contents | |---|---| -| `env/` | `Env` base class: a subclass declares `type`, implements `from_dict` and `async deploy(...) -> DeployedEnv`, and may override `reset`, `close` and `load_file_artifact_universe`; teardown closes each env a run deployed before terminating its sandboxes by id. Built-ins in `env/envs/`: `MCPServerEnv` (`mcp_server`), `MultiEnv` (`multi`, MCP servers plus websites), `ServiceDBEnv` (`service_db`, PostgreSQL), `WebsiteEnv` (`website`, one site: a server image, `backend_image` in its env.toml, serving the pages and API, ready when its card's `website/v1` `ready` answers a `WebsiteReadiness` that says so, as `agentenv_protocol.website` (`website_readiness`, `env_readiness`) defines a website env's health for the deploy and `DeployedEnv.readiness()` alike, or, for a card without `ready`, when its `health` path answers 200, which is deprecated; the gateway container's `env/gateway/constants.py` mirrors the URI and op, a test keeping them equal; a separate frontend image is deprecated, warned of at put and optional in a bundle, and a stored `backend_docker_image_artifact` reads as the server image; a live one, `live = true`, is a real site whose server serves none of its pages, its `live`, `domains` and `allow_hosts` read at put from its server's `@website(...)` (`utils/card_naming.py` `WebsiteSource`) unless given, its `settings`' secrets only as references the gateway resolves as it writes the site's container (`WEBSITE_SETTINGS_ENV`); the gateway gives it no site route, a computer's site table leaves it out, a computer whose network is an allowlist is let reach its names (`MultiEnv.live_hosts`), a browser alone's forward port takes them straight to the real site (`site_proxy.py` `direct`), and a run that deploys one is marked live, `reproducible: false`), `GatewayEnv` (`gateway_server`), `ComputerDriverEnv` (`computer_driver`, `env/envs/computer/driver_env.py`: a computer driver's files and what its class declares for each platform it supports, read at put in a process of its own, `agentenv_protocol.computer._declare`, where the driver's own dependencies needn't be installed, with the `port` it serves on and its install by OS, `[install.]`, or for one distribution `[install."/"]`, which replaces its OS type's there, as its `known_failures` do (`env/envs/computer/utils/install.py`): files, apt `packages` with extra `sources`, a `python` requirements file for a venv, `downloads` by URL and sha256, `run` scripts, the services the base's init runs, and `ports`; it deploys only as a computer env's driver), `ComputerEnv` (`computer`, in `env/envs/computer/`: a base computer and a driver, matched at put by gate 1 before anything is stored, then composed by `composer.py` into an image for each form and arch on a builder of the put's sandbox provider, by default the env's (`utils/builder.py`; `--sandbox` on `env build`, `bundle register` and `run`): this machine's Docker for `local`, else a VM of the provider, which fetches the put's stored files by signed URL, takes the rest inline and pushes to the image store itself, so it builds only with stores a remote VM reaches; each arch goes to the first link that runs it natively, else one that emulates it (a VM registers QEMU; `--no-emulate` leaves it `skipped` instead, for a later put), and its pair records the provider that built it (`builder`); a run or register that reuses a computer env composes what it has left (`pending`), a run only the forms its sandbox's deploys boot (`booted_forms`: on each link, the first form it boots natively, by the deploy's own rule, `boot.first_native`; a compose of a disk adds its container), and a deploy composes first what it boots that no compose started, and a container it runs natively that was built but not probed, as one whose probe a setting that didn't resolve stopped, booting it only once ready (`ComputerEnv.compose_unstarted`); tagged `agentenv-computers:` in the image store by a key of the image's Dockerfile, build context and lock so any env with the same inputs reuses it; every layer's packages, requirements and downloads are resolved at put in a container of the base for each arch (`utils/packages.py`, `utils/resolve_in_base.py`), stored in the object store by sha256 under `agentenv-packages/`, and listed in a lock stored by a digest of its inputs, which any env with the same inputs reuses, and builds install only the lock's files, every install step `RUN --network=none`; each pair's status goes into its metadata, `composed`, the arches built are written as its computer, a `computer` artifact over one index, and `agent-env env build` resumes what a put left; gate 3 (`utils/probing.py`) then boots each native build on the put's sandbox provider and probes it at `full`, making it `ready` or `failed`, and a build no provider runs natively stays `unverified`. Its deploy, through the `computer` env provider (`providers/env_providers/env_computer_provider.py`), boots its first `ready` build on a sandbox provider that runs its arch natively (`native_platforms()`), with no gateway in front and the driver's card as the env card, its `/dev/shm` the most its apps' Linux installs ask for (`shm_mb`, in MiB, which its composed computer and the deploy record keep; `--shm-size` on a container, `AGENTENV_SHM_MB` read by a Linux disk's init, passed only to a provider whose `SHARED_MEMORY` says it gives that much, else the sandbox's default, which the deploy logs), then checks its `display`; `deploy_env` `env_params` take a `form`, `computer_settings` (deploy-phase values of the base's settings; a secret one, here or on the env, only as a bare `secret:`/`env:` reference) and `driver_settings` (the driver's `ComputerSettings`, such as its default coordinate space); a `network` that allows nothing runs it on local Docker's internal network behind a relay (`providers/sandbox_providers/local_docker/relay/`); `reset` and `close` have the driver end its recordings first (`computer-recording/v1` `end_all`, each uploading what's left and its manifest under the grant it started with), and `reset` then boots a fresh container in its place (`reset_container`), keeping the computer's address but not an agent's MCP session to the old driver; its `load_file_artifact_universe` writes files under the session user's home through the driver's `data/add`, `Downloads` by default, each file's bytes through a read grant when the store's grants reach it and its network allows everything, else inline (`env/envs/computer/utils/inside.py`)), `ComputerAppEnv` (`computer_app`, `env/envs/computer/app_env.py`: an app on a computer, a `ComputerAppEnvironment` class its env.toml's `module` names, read at put by the same `_declare`, which declares each OS with its own decorator (`@linux(...)`, or `@install(os_type, spec)` for any OS type; `@linux(distribution=...)` or `@install("/", spec)` for one distribution, which replaces its OS type's there) holding its install, its launch and, optionally, a skill and settings and build settings that add to the class's there, and install steps and ready checks for every OS or those their `os=` names; everything that reads its settings, launch or steps asks for the computer's OS type; deployed alone it is a one-app computer env, a `ComputerEnv` of the computer and driver its env.toml names, the composer installing it under `/opt/agentenv/apps//` with its services as supervisord programs beside its own server (`agentenv_protocol.computer.serve_app`, in a venv with the protocol's `app` extra, on `.agentenv/server.sock` in its folder), which adds its launcher, launches a `boot` app once per boot and says through `computer-app/v1` whether it's ready, as the driver's `computer-ready` asks, its card named after it, and `env_params` `app_settings` handing it values of its `Settings` in `AGENTENV_APP_SETTINGS`, a secret one only as a bare reference, over its env.toml's `[settings]` and a computer env's `settings` beside it in `apps`, each by top-level field (`params.app_settings`; checked at put, a required one left to the deploy; the deploy record keeps the merged values as `app_settings`, secrets withheld); its env.toml's `build_settings`, values of its `BuildSettings` checked at put, are written to `.agentenv/build_settings.json` in its folder, so they're part of the image's key, its install steps read them, and a deploy can't change them). An env type whose services take environment data defines `load_environment_artifact` and `load_environment_universe_artifact`; `load_artifact` refuses an environment artifact aimed at a type that doesn't, at save and before it loads. An env built on the protocol SDK can serve agents a skill (`@agent_skill`, `skill/v1`), which `add_skills` `env_ids` installs under its environment name (behind a gateway, each server's under that server's name); a skill with files beside its SKILL.md goes as a bundle stored once per content under `agentenv-env-skills/` in the object store. `env/gateway/gateway.py` is the env-side gateway server, container-only code. Its site route, `/website//`, never forwards a site's data plane or card (`/agentenv/…`, `/.well-known/agent-env.json`), which agentenv reaches through `/svc//`; a site with no frontend image is served by its server image there. Its CDP route, `/browser//cdp//` (`env/gateway/browser_proxy.py`), reaches the website browser behind the `/svc` child ``, found from that child's card (it, or an app among its children, offering `browser/v1` `cdp` with `/cdp/` in its `computer-app/v1` `agent_routes`): it relays the browser's discovery and WebSocket both ways, each leg pinged every 20 s, and every CDP address the browser hands out, in `json/version`, `json/list` and its `cdp` answer through `/svc//`, comes back at the gateway, everything before `/cdp//` becoming the address the caller reached the gateway at and `/browser/`; each connection is a `browser_connection` trajectory event, without its token. While its clock is armed it serves agents `get_time` (`env/gateway/get_time.py`), which answers in UTC and, given an IANA `time_zone`, also in that zone; executors' working `hours` (`env/gateway/triggers.py`) take a fixed `utc_offset` or an IANA `time_zone`. | +| `env/` | `Env` base class: a subclass declares `type`, implements `from_dict` and `async deploy(...) -> DeployedEnv`, and may override `reset`, `close` and `load_file_artifact_universe`; teardown closes each env a run deployed before terminating its sandboxes by id. Built-ins in `env/envs/`: `MCPServerEnv` (`mcp_server`), `MultiEnv` (`multi`, MCP servers plus websites), `ServiceDBEnv` (`service_db`, PostgreSQL), `WebsiteEnv` (`website`, one site: a server image, `backend_image` in its env.toml, serving the pages and API, ready when its card's `website/v1` `ready` answers a `WebsiteReadiness` that says so, as `agentenv_protocol.website` (`website_readiness`, `env_readiness`) defines a website env's health for the deploy and `DeployedEnv.readiness()` alike, or, for a card without `ready`, when its `health` path answers 200, which is deprecated; the gateway container's `env/gateway/constants.py` mirrors the URI and op, a test keeping them equal; a separate frontend image is deprecated, warned of at put and optional in a bundle, and a stored `backend_docker_image_artifact` reads as the server image; a live one, `live = true`, is a real site whose server serves none of its pages, its `live`, `domains` and `allow_hosts` read at put from its server's `@website(...)` (`utils/card_naming.py` `WebsiteSource`) unless given, its `settings`' secrets only as references the gateway resolves as it writes the site's container (`WEBSITE_SETTINGS_ENV`); the gateway gives it no site route, a computer's site table leaves it out, a computer whose network is an allowlist is let reach its names (`MultiEnv.live_hosts`), a browser alone's forward port takes them straight to the real site (`site_proxy.py` `direct`), as the headless base's stand-in proxy takes any host, each only to an address it resolved and checked itself (`allowed`: never loopback, private, link-local or the metadata service), and a run that deploys one is marked live, `reproducible: false`), `GatewayEnv` (`gateway_server`), `ComputerDriverEnv` (`computer_driver`, `env/envs/computer/driver_env.py`: a computer driver's files and what its class declares for each platform it supports, read at put in a process of its own, `agentenv_protocol.computer._declare`, where the driver's own dependencies needn't be installed, with the `port` it serves on and its install by OS, `[install.]`, or for one distribution `[install."/"]`, which replaces its OS type's there, as its `known_failures` do (`env/envs/computer/utils/install.py`): files, apt `packages` with extra `sources`, a `python` requirements file for a venv, `downloads` by URL and sha256, `run` scripts, the services the base's init runs, and `ports`; it deploys only as a computer env's driver), `ComputerEnv` (`computer`, in `env/envs/computer/`: a base computer and a driver, matched at put by gate 1 before anything is stored, then composed by `composer.py` into an image for each form and arch on a builder of the put's sandbox provider, by default the env's (`utils/builder.py`; `--sandbox` on `env build`, `bundle register` and `run`): this machine's Docker for `local`, else a VM of the provider, which fetches the put's stored files by signed URL, takes the rest inline and pushes to the image store itself, so it builds only with stores a remote VM reaches; each arch goes to the first link that runs it natively, else one that emulates it (a VM registers QEMU; `--no-emulate` leaves it `skipped` instead, for a later put), and its pair records the provider that built it (`builder`); a run or register that reuses a computer env composes what it has left (`pending`), a run only the forms its sandbox's deploys boot (`booted_forms`: on each link, the first form it boots natively, by the deploy's own rule, `boot.first_native`; a compose of a disk adds its container), and a deploy composes first what it boots that no compose started, and a container it runs natively that was built but not probed, as one whose probe a setting that didn't resolve stopped, booting it only once ready (`ComputerEnv.compose_unstarted`); tagged `agentenv-computers:` in the image store by a key of the image's Dockerfile, build context and lock so any env with the same inputs reuses it; every layer's packages, requirements and downloads are resolved at put in a container of the base for each arch (`utils/packages.py`, `utils/resolve_in_base.py`), stored in the object store by sha256 under `agentenv-packages/`, and listed in a lock stored by a digest of its inputs, which any env with the same inputs reuses, and builds install only the lock's files, every install step `RUN --network=none`; each pair's status goes into its metadata, `composed`, the arches built are written as its computer, a `computer` artifact over one index, and `agent-env env build` resumes what a put left; gate 3 (`utils/probing.py`) then boots each native build on the put's sandbox provider and probes it at `full`, making it `ready` or `failed`, and a build no provider runs natively stays `unverified`. Its deploy, through the `computer` env provider (`providers/env_providers/env_computer_provider.py`), boots its first `ready` build on a sandbox provider that runs its arch natively (`native_platforms()`), with no gateway in front and the driver's card as the env card, its `/dev/shm` the most its apps' Linux installs ask for (`shm_mb`, in MiB, which its composed computer and the deploy record keep; `--shm-size` on a container, `AGENTENV_SHM_MB` read by a Linux disk's init, passed only to a provider whose `SHARED_MEMORY` says it gives that much, else the sandbox's default, which the deploy logs), then checks its `display`; `deploy_env` `env_params` take a `form`, `computer_settings` (deploy-phase values of the base's settings; a secret one, here or on the env, only as a bare `secret:`/`env:` reference) and `driver_settings` (the driver's `ComputerSettings`, such as its default coordinate space); a `network` that allows nothing runs it on local Docker's internal network behind a relay (`providers/sandbox_providers/local_docker/relay/`); `reset` and `close` have the driver end its recordings first (`computer-recording/v1` `end_all`, each uploading what's left and its manifest under the grant it started with), and `reset` then boots a fresh container in its place (`reset_container`), keeping the computer's address but not an agent's MCP session to the old driver; its `load_file_artifact_universe` writes files under the session user's home through the driver's `data/add`, `Downloads` by default, each file's bytes through a read grant when the store's grants reach it and its network allows everything, else inline (`env/envs/computer/utils/inside.py`)), `ComputerAppEnv` (`computer_app`, `env/envs/computer/app_env.py`: an app on a computer, a `ComputerAppEnvironment` class its env.toml's `module` names, read at put by the same `_declare`, which declares each OS with its own decorator (`@linux(...)`, or `@install(os_type, spec)` for any OS type; `@linux(distribution=...)` or `@install("/", spec)` for one distribution, which replaces its OS type's there) holding its install, its launch and, optionally, a skill and settings and build settings that add to the class's there, and install steps and ready checks for every OS or those their `os=` names; everything that reads its settings, launch or steps asks for the computer's OS type; deployed alone it is a one-app computer env, a `ComputerEnv` of the computer and driver its env.toml names, the composer installing it under `/opt/agentenv/apps//` with its services as supervisord programs beside its own server (`agentenv_protocol.computer.serve_app`, in a venv with the protocol's `app` extra, on `.agentenv/server.sock` in its folder), which adds its launcher, launches a `boot` app once per boot and says through `computer-app/v1` whether it's ready, as the driver's `computer-ready` asks, its card named after it, and `env_params` `app_settings` handing it values of its `Settings` in `AGENTENV_APP_SETTINGS`, a secret one only as a bare reference, over its env.toml's `[settings]` and a computer env's `settings` beside it in `apps`, each by top-level field (`params.app_settings`; checked at put, a required one left to the deploy; the deploy record keeps the merged values as `app_settings`, secrets withheld); its env.toml's `build_settings`, values of its `BuildSettings` checked at put, are written to `.agentenv/build_settings.json` in its folder, so they're part of the image's key, its install steps read them, and a deploy can't change them). An env type whose services take environment data defines `load_environment_artifact` and `load_environment_universe_artifact`; `load_artifact` refuses an environment artifact aimed at a type that doesn't, at save and before it loads. An env built on the protocol SDK can serve agents a skill (`@agent_skill`, `skill/v1`), which `add_skills` `env_ids` installs under its environment name (behind a gateway, each server's under that server's name); a skill with files beside its SKILL.md goes as a bundle stored once per content under `agentenv-env-skills/` in the object store. `env/gateway/gateway.py` is the env-side gateway server, container-only code. Its site route, `/website//`, never forwards a site's data plane or card (`/agentenv/…`, `/.well-known/agent-env.json`), which agentenv reaches through `/svc//`; a site with no frontend image is served by its server image there. Its CDP route, `/browser//cdp//` (`env/gateway/browser_proxy.py`), reaches the website browser behind the `/svc` child ``, found from that child's card (it, or an app among its children, offering `browser/v1` `cdp` with `/cdp/` in its `computer-app/v1` `agent_routes`): it relays the browser's discovery and WebSocket both ways, each leg pinged every 20 s, and every CDP address the browser hands out, in `json/version`, `json/list` and its `cdp` answer through `/svc//`, comes back at the gateway, everything before `/cdp//` becoming the address the caller reached the gateway at and `/browser/`; each connection is a `browser_connection` trajectory event, without its token. While its clock is armed it serves agents `get_time` (`env/gateway/get_time.py`), which answers in UTC and, given an IANA `time_zone`, also in that zone; executors' working `hours` (`env/gateway/triggers.py`) take a fixed `utc_offset` or an IANA `time_zone`. | | `artifact/` | Immutable versioned artifacts; every `put()` creates a new version. The document goes to the document store, binary payloads to the object store. Every artifact but an environment universe, whose `metadata` key holds its legacy metadata file ids, carries a `metadata` dict outside its versioned fields, changed by compare-and-swap (`merge_metadata`). Built-ins: `CliArtifact` (`cli`), `ComputerArtifact` (`computer`, a base computer: its architectures, OS, session user, settings and container form, whose image `put` reads from its registry into `metadata.validated_images`), `FileArtifact` (`file`), `FileArtifactUniverse` (`file_artifact_universe`), `DockerImageArtifact` (`docker_image`), `EnvironmentArtifact` (`environment`: its environment's files by path, stored as the one wrapped `file_artifact_ref` every environment artifact had when it has one file at its filename, else as `file_artifact_refs`; `get_file_artifacts()` reads either, and a load hands each file to the env's `data/add` with its path, which a pre-v1 load, taking one file, refuses), `EnvironmentUniverseArtifact` (`environment_universe`), `SkillArtifact` (`skill`); `[artifacts] type_aliases` maps another stored spelling of a type onto one of them. A `DockerImageArtifact` holds a tar.gz of its image, or, written by `put_ref` (`--image-ref` on `env mcp-server put` and `a2a-agent put`), only a registry reference with its tag pinned to a digest, which sandboxes pull, or a published image's tag with its `image_digest` and its build context, which sandboxes pull by the digest and build from the context when they can't (the infra images, and a bundle agent built from a shipped agent's sources, `Plan.published`), or, written by `put_context`, only its build context, which a VM sandbox builds before running it (`VmSandbox.load_docker_images`) and Modal builds before creating its container (`ModalSandboxProvider.prepare_image`, outside a fallback chain's deadline); anything else that runs images by name (a container on the local provider, a lone server on any provider but Modal) refuses such an image. A build context (`utils/docker/context.py`) is what `docker build` would send, `.dockerignore` applied, written so equal files give equal bytes and an equal `source_digest`. | | `task/`, `task_step/` | A `Task` holds its `TaskStep`s inline; `Task.run()` executes them as a DAG. `depends_on` (None means all prior steps) gates a step, independent steps run concurrently, `fail_task_on_error` makes a failure fatal or tolerated, `retry_config` rolls a failed span back through the step journal and re-dispatches it. Built-in steps live in `task_step/task_steps/` (`deploy_env`, `deploy_agent`, `prompt_agent`, the verifiers under `verifiers/`, among them `start_computer_recording` and `end_computer_recording`, and more). On an env whose card serves computer-exec/v1 (a computer), `run_code` with `env_id` and `run_container_unit_tests_verifier` with `env_id` run inside it as its session user through the driver: files staged with `data/add` and read with `data/get`, commands run with `run`; `env_outcome_verifier` hands a `verify` that takes an `env_url` keyword the env's address. A computer's framework-only routes (everything but MCP and the card) take the per-deploy control token its provider boots it with (`DeployedEnv.control_token`, `AGENTENV_CONTROL_TOKEN`); framework calls send `deployed.control_headers`, `Task.run` runs every step under `protocol_v1.authorized` with each deployed env's (a `verify` included), and a `run_code` script inside the computer gets `input["env"]["headers"]`. `teardown_run` and the `teardown_sandboxes` step share one core (`task/teardown.py` `tear_down`): the recordings the run left open end with fresh grants, each env still up closes (`Env.close`; a computer env ends any recording still open), what stayed open is registered from its namespace, then every sandbox is terminated by id. `task/episode/` runs a task with some of its agents played from outside, as a training loop does (`EpisodeSpec`). | | `store/` | Four store ABCs with local and cloud implementations: `DocumentStore` (SQLite, MongoDB, DynamoDB, Firestore with MongoDB compatibility), `ObjectStore` (filesystem, S3, Cloud Storage), `ImageStore` (local OCI registry, ECR), `SecretStore` (env vars or file, AWS Secrets Manager, Google Cloud Secret Manager). `VersionedEntityStore` implements the shared versioned get/put logic, `QueryBuilder` is the immutable chained query API, `store/base.py` holds the error types. `store/routing.py` is the local namespaces, `@local` and `@hf` (`is_local_id`), each with a documents file of its own: while the CLI or a `build_app` server runs, a local id and what a local task's run records go to local stores beside the configured ones, and that run's writes to the configured stores are refused. A new backend must pass the conformance kits in `tst/store/`. | diff --git a/src/agent_env/computers/README.md b/src/agent_env/computers/README.md index c5e8ca415..68a39ba53 100644 --- a/src/agent_env/computers/README.md +++ b/src/agent_env/computers/README.md @@ -14,8 +14,12 @@ building it. Name it from another bundle as `ubuntu-24.04`, the alias `aliases.t (`display = "none"`), for one website browser, which runs headless there, and the `page` driver, whose screen is the browser's page: a browser computer, which agentenv's `chromium` deploys alone as. supervisord runs every program as the session user, among them a stand-in site proxy on `127.0.0.1:3128`, the browser's only route out when the computer -routes no sites, which forwards every request to wherever it names, refusing nothing: the sandbox's network policy is -all that limits it. A computer that routes sites names the shared site proxy's forward port, `127.0.0.1:3129`, to the +routes no sites. It forwards a request or a `CONNECT` tunnel to any host, never decrypting a tunnel, but only to an +address it resolved and checked itself: one that isn't globally routable (loopback, private, link-local and the +metadata service, their IPv6 equivalents) is refused at once with a 403, whether a page or an agent driving the browser +names it, and each connection and refusal is a JSON line in `/var/log/agentenv/site-proxy.log`. The sandbox's network +policy still applies beneath it. Its `own_loopback` setting (default `false`) lets the browser reach the computer's own +loopback, as a test that serves its pages inside the computer needs. A computer that routes sites names the shared site proxy's forward port, `127.0.0.1:3129`, to the browser instead: it serves the run's sites, refuses every other host and runs out of the agent's reach. It has a container form only. Name it from another bundle as `ubuntu-24.04-headless`, or by its id, `@local/agentenv-framework/computers/ubuntu-24.04-headless`. diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/artifact.toml b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/artifact.toml index dfcd4d670..7896fe4c8 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/artifact.toml +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/artifact.toml @@ -12,3 +12,7 @@ input_channel = "env" [config.timezone] default = "UTC" phase = "deploy" + +[config.own_loopback] +default = false +phase = "deploy" diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py index 5eacb8004..37db13ebd 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py @@ -1,14 +1,59 @@ -"""A stand-in for the shared site proxy in this base: it forwards every request the browser sends it, absolute-form -HTTP and CONNECT tunnels alike, to wherever it names, refusing nothing, on 127.0.0.1:3128.""" +"""A stand-in for the shared site proxy in this base, on 127.0.0.1:3128: it forwards every request the browser sends +it, absolute-form HTTP and CONNECT tunnels alike, to the host it names, never decrypting a tunnel. It resolves each +name itself and connects only to an address it checked: one that isn't globally routable (loopback, private, +link-local, the metadata service, their IPv6 equivalents) is refused at once, so neither a page nor an agent driving +the browser reaches the computer's own network. The computer's own loopback is reached only when its deploy sets the +base's `own_loopback` setting, as a test serving its pages inside the computer does. Each connection is logged as a +JSON line when it ends, and each refusal with its reason.""" import http.server +import ipaddress +import json +import os import select import socket import socketserver +import sys +import time import urllib.parse PORT = 3128 -IDLE_S = 60 +CONNECT_TIMEOUT_S = 30 +IDLE_S = 300 HOP = {"connection", "keep-alive", "proxy-connection", "proxy-authorization"} +NAT64 = ipaddress.ip_network("64:ff9b::/96") +#: Whether the deploy lets the browser reach this computer's own loopback (the base's `own_loopback` setting). +OWN_LOOPBACK = os.environ.get("AGENTENV_SETTING_OWN_LOOPBACK", "").lower() in ("true", "1") + + +def allowed(address: str) -> bool: + """Whether the proxy may connect to ``address``: a globally routable one, not multicast, an IPv6 address that + carries an IPv4 one (mapped, 6to4 or NAT64) judged as that IPv4 address.""" + ip = ipaddress.ip_address(address.split("%", 1)[0]) + if isinstance(ip, ipaddress.IPv6Address): + if ip.ipv4_mapped is not None: + ip = ip.ipv4_mapped + elif ip.sixtofour is not None: + ip = ip.sixtofour + elif ip in NAT64: + ip = ipaddress.IPv4Address(int(ip) & 0xFFFFFFFF) + return ip.is_global and not ip.is_multicast + + +def _loopback(address: str) -> bool: + ip = ipaddress.ip_address(address.split("%", 1)[0]) + return ip.is_loopback or isinstance(ip, ipaddress.IPv6Address) and bool(ip.ipv4_mapped and ip.ipv4_mapped.is_loopback) + + +def resolve(host: str, port: int) -> tuple[list[str], list[str]]: + """The addresses ``host`` resolves to that the proxy may connect to, in the resolver's order, and those it may + not; raises OSError when it doesn't resolve.""" + found = list(dict.fromkeys(info[4][0] for info in socket.getaddrinfo(host, port, type=socket.SOCK_STREAM))) + usable = [address for address in found if allowed(address) or OWN_LOOPBACK and _loopback(address)] + return usable, [address for address in found if address not in usable] + + +def log(**fields) -> None: + print(json.dumps(fields), flush=True) class Proxy(http.server.BaseHTTPRequestHandler): @@ -17,52 +62,110 @@ class Proxy(http.server.BaseHTTPRequestHandler): def do_CONNECT(self): host, _, port = self.path.rpartition(":") host = host.removeprefix("[").removesuffix("]") - try: - upstream = socket.create_connection((host, int(port)), timeout=10) - except (OSError, ValueError): - self.send_error(502) + if not port.isdigit(): + self.send_error(400) + return + if (upstream := self._open(host, int(port), "CONNECT")) is None: return self.send_response(200, "Connection established") self.end_headers() self.close_connection = True - _pipe(self.connection, upstream) + self._relay(upstream, host, int(port), b"") def forward(self): url = urllib.parse.urlsplit(self.path) - try: - upstream = socket.create_connection((url.hostname, url.port or 80), timeout=10) - except (OSError, TypeError): - self.send_error(502) + if not url.hostname: + self.send_error(400) + return + port = url.port or 80 + if (upstream := self._open(url.hostname, port, self.command)) is None: return target = (url.path or "/") + (f"?{url.query}" if url.query else "") headers = "".join(f"{k}: {v}\r\n" for k, v in self.headers.items() if k.lower() not in HOP) - upstream.sendall(f"{self.command} {target} HTTP/1.1\r\n{headers}Connection: close\r\n\r\n".encode("latin-1")) - if length := int(self.headers.get("Content-Length") or 0): - upstream.sendall(self.rfile.read(length)) + head = f"{self.command} {target} HTTP/1.1\r\n{headers}Connection: close\r\n\r\n".encode("latin-1") + body = self.rfile.read(length) if (length := int(self.headers.get("Content-Length") or 0)) else b"" self.close_connection = True - _pipe(self.connection, upstream) + self._relay(upstream, url.hostname, port, head + body) do_GET = do_POST = do_PUT = do_PATCH = do_DELETE = do_HEAD = do_OPTIONS = forward + def _open(self, host: str, port: int, method: str): + """A connection to an address ``host`` resolves to that the proxy may reach, or None once the browser has + its answer: a 403 for a host whose every address is refused, a 502 for one that doesn't resolve or answer.""" + try: + usable, refused = resolve(host, port) + except OSError as error: + log(refused={"method": method, "host": host, "port": port, "reason": f"doesn't resolve: {error}"}) + self._answer(502, f"{host} doesn't resolve\n") + return None + if not usable: + log(refused={"method": method, "host": host, "port": port, "reason": "internal address", + "addresses": refused}) + self._answer(403, f"{host} is an internal address, which the browser never reaches\n") + return None + for address in usable: + try: + upstream = socket.create_connection((address, port), timeout=CONNECT_TIMEOUT_S) + upstream.settimeout(None) + self.address = address + return upstream + except OSError: + continue + log(refused={"method": method, "host": host, "port": port, "reason": "no answer", "addresses": usable}) + self._answer(502, f"{host} didn't answer\n") + return None + + def _answer(self, status: int, body: str) -> None: + data = body.encode() + self.send_response(status) + self.send_header("Content-Type", "text/plain; charset=utf-8") + self.send_header("Content-Length", str(len(data))) + self.send_header("X-Agentenv-Site-Proxy", "failed") + self.send_header("Connection", "close") + self.end_headers() + self.wfile.write(data) + self.close_connection = True + + def _relay(self, upstream: socket.socket, host: str, port: int, first: bytes) -> None: + started, began = time.time(), time.monotonic() + sent, received = len(first), 0 + try: + if first: + upstream.sendall(first) + more, received = _pipe(self.connection, upstream) + sent += more + finally: + log(connection={"host": host, "port": port, "address": self.address, "bytes_sent": sent, + "bytes_received": received, + "started": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(started)), + "duration_s": round(time.monotonic() - began, 3)}) + def log_message(self, *args): pass -def _pipe(a: socket.socket, b: socket.socket) -> None: +def _pipe(client: socket.socket, upstream: socket.socket) -> tuple[int, int]: + """Relay both ways until either side closes or both are idle ``IDLE_S``; the bytes sent upstream and received.""" + sent = received = 0 try: while True: - ready, _, _ = select.select([a, b], [], [], IDLE_S) + ready, _, _ = select.select([client, upstream], [], [], IDLE_S) if not ready: - return + break for s in ready: data = s.recv(65536) if not data: - return - (b if s is a else a).sendall(data) + return sent, received + (upstream if s is client else client).sendall(data) + if s is client: + sent += len(data) + else: + received += len(data) except OSError: - return + pass finally: - b.close() + upstream.close() + return sent, received class Server(socketserver.ThreadingMixIn, http.server.HTTPServer): @@ -71,4 +174,5 @@ class Server(socketserver.ThreadingMixIn, http.server.HTTPServer): if __name__ == "__main__": + print(f"site proxy stand-in on 127.0.0.1:{PORT}", file=sys.stderr, flush=True) Server(("127.0.0.1", PORT), Proxy).serve_forever() diff --git a/src/agent_env/env/envs/computer/utils/site_proxy.py b/src/agent_env/env/envs/computer/utils/site_proxy.py index 6442dab12..f1d61c735 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -85,6 +85,7 @@ RESTART_DELAY_S = 1.0 SERVE_USER = "nobody" FAILED = (("X-Agentenv-Site-Proxy", "failed"),) +NAT64 = ipaddress.ip_network("64:ff9b::/96") class BadRequest(Exception): @@ -106,6 +107,26 @@ def matches(pattern: str, host: str) -> bool: return host == pattern +def allowed(address: str) -> bool: + """Whether the proxy may connect to ``address`` for a host outside the site table: a globally routable one, not + multicast, an IPv6 address that carries an IPv4 one (mapped, 6to4 or NAT64) judged as that IPv4 address. The base + computers' stand-in proxies judge addresses the same way.""" + ip = ipaddress.ip_address(address.split("%", 1)[0]) + if isinstance(ip, ipaddress.IPv6Address): + if ip.ipv4_mapped is not None: + ip = ip.ipv4_mapped + elif ip.sixtofour is not None: + ip = ip.sixtofour + elif ip in NAT64: + ip = ipaddress.IPv4Address(int(ip) & 0xFFFFFFFF) + return ip.is_global and not ip.is_multicast + + +def log_event(**fields) -> None: + """One JSON line in the proxy's log: a connection to a host outside the site table once it ends, or a refusal.""" + print(json.dumps(fields), file=sys.stderr, flush=True) + + @dataclass class Site: """One site as the proxy serves it: its names, the route on the gateway it forwards to, and its certificate and key @@ -380,10 +401,16 @@ async def relay_body(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, await relay_until_eof(reader, writer) -async def splice(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: - """One direction of an upgraded connection, closing the other side when it ends, which ends the other direction.""" +async def splice(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, counts: Optional[list[int]] = None, + index: int = 0) -> None: + """One direction of an upgraded connection, closing the other side when it ends, which ends the other direction; + the bytes it carried added to ``counts[index]`` when given.""" try: - await relay_until_eof(reader, writer) + while data := await reader.read(CHUNK): + writer.write(data) + await writer.drain() + if counts is not None: + counts[index] += len(data) except (ConnectionError, OSError, ssl.SSLError): pass finally: @@ -454,19 +481,49 @@ async def _tunnel(self, reader: asyncio.StreamReader, writer: asyncio.StreamWrit @staticmethod async def _direct(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, host: str, port: int, head: Optional[bytes] = None) -> None: - """``host``, a live site's, reached straight at ``port``: a tunnel, answered as established, for a ``CONNECT``, - else ``head`` sent on; then both ends relayed until either closes.""" + """``host``, a live site's, reached straight at ``port``, at an address the proxy resolved and checked itself + (``allowed``), never resolved again: a tunnel, answered as established, for a ``CONNECT``, else ``head`` sent + on; then both ends relayed until either closes, and the connection logged (``log_event``). A host whose every + address is internal is refused at once, one that doesn't resolve or answer is a 502.""" + method = "CONNECT" if head is None else head.split(b" ", 1)[0].decode("latin-1") try: - up_reader, up_writer = await asyncio.wait_for(asyncio.open_connection(host, port), UPSTREAM_TIMEOUT_S) - except (OSError, asyncio.TimeoutError) as e: - writer.write(simple_response("502 Bad Gateway", f"{host}:{port} didn't answer: {e}\n", FAILED)) + found = await asyncio.get_running_loop().getaddrinfo(host, port, type=socket.SOCK_STREAM) + except OSError as e: + log_event(refused={"method": method, "host": host, "port": port, "reason": f"doesn't resolve: {e}"}) + writer.write(simple_response("502 Bad Gateway", f"{host} doesn't resolve\n", FAILED)) + return + addresses = list(dict.fromkeys(info[4][0] for info in found)) + if not (usable := [address for address in addresses if allowed(address)]): + log_event(refused={"method": method, "host": host, "port": port, "reason": "internal address", + "addresses": addresses}) + writer.write(simple_response("403 Forbidden", f"{host} is an internal address, which the browser never " + "reaches\n", FAILED)) return + for address in usable: + try: + up_reader, up_writer = await asyncio.wait_for(asyncio.open_connection(address, port), + UPSTREAM_TIMEOUT_S) + break + except (OSError, asyncio.TimeoutError): + continue + else: + log_event(refused={"method": method, "host": host, "port": port, "reason": "no answer", + "addresses": usable}) + writer.write(simple_response("502 Bad Gateway", f"{host}:{port} didn't answer\n", FAILED)) + return + started, began, counts = time.time(), time.monotonic(), [len(head or b""), 0] if head is None: writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n") else: up_writer.write(head) - await asyncio.gather(writer.drain(), up_writer.drain()) - await asyncio.gather(splice(reader, up_writer), splice(up_reader, writer)) + try: + await asyncio.gather(writer.drain(), up_writer.drain()) + await asyncio.gather(splice(reader, up_writer, counts, 0), splice(up_reader, writer, counts, 1)) + finally: + log_event(connection={"host": host, "port": port, "address": address, "bytes_sent": counts[0], + "bytes_received": counts[1], + "started": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(started)), + "duration_s": round(time.monotonic() - began, 3)}) @staticmethod def _refuse(writer: asyncio.StreamWriter, method: str, host: str) -> None: diff --git a/tst/integration/computer/browser_computer_test.py b/tst/integration/computer/browser_computer_test.py index eeca7dba5..d1083f925 100644 --- a/tst/integration/computer/browser_computer_test.py +++ b/tst/integration/computer/browser_computer_test.py @@ -23,6 +23,7 @@ localhost:5000, and Node 20 or later and npm for the CDP clients.""" import asyncio +import itertools import json import platform import re @@ -38,6 +39,7 @@ from mcp import ClientSession from mcp.client.streamable_http import streamablehttp_client from PIL import Image +from websockets.asyncio.client import connect from agentenv_protocol import client as protocol_v1 from agentenv_protocol.computer import COMPUTER_PAGE, COMPUTER_READY @@ -104,6 +106,9 @@ """ PAGE_URL = "data:text/html," + urllib.parse.quote(PAGE) SITE = "http://127.0.0.1:8771/" +#: What a deploy sets so the browser reaches the pages these tests serve on its own loopback, which its proxy refuses +#: otherwise. +OWN_LOOPBACK = {"computer_settings": {"own_loopback": True}} # Serves, on loopback for the seconds it's given, a home page with a button that asks to confirm and a file input, each # saying in the title what came of it, and two more pages. SERVE_SITE = """import http.server, sys, threading, time @@ -167,7 +172,7 @@ def test_gate_3_makes_this_machines_arch_ready_with_the_page_driver_its_input_an async def test_its_tools_see_and_drive_the_page_that_is_its_screen(browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [PAGE_URL]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [PAGE_URL]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _state(deployed, lambda s: True) @@ -212,7 +217,7 @@ async def test_its_tools_see_and_drive_the_page_that_is_its_screen(browser_compu async def test_a_killed_app_server_or_driver_comes_back_and_a_killed_browser_fails_it_for_good(browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [PAGE_URL]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [PAGE_URL]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _kill(deployed, "[s]erve_app") @@ -244,7 +249,7 @@ async def test_an_agent_navigates_moves_in_history_answers_a_dialog_uses_tabs_an browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [SITE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: serving = asyncio.ensure_future(inside.run(deployed, ["python3", "-c", SERVE_SITE, "900"], timeout_s=960)) @@ -304,7 +309,7 @@ async def test_an_agent_navigates_moves_in_history_answers_a_dialog_uses_tabs_an @pytest.mark.asyncio async def test_its_cookies_local_storage_and_downloads_are_read_and_set_through_browser_v1(browser_computer): with namespace_routing(): - deployed = await Env.get(browser_computer.id).deploy() + deployed = await Env.get(browser_computer.id).deploy(env_params=OWN_LOOPBACK) context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -322,7 +327,7 @@ async def test_its_cookies_local_storage_and_downloads_are_read_and_set_through_ async def test_its_state_operations_hold_up_at_once_through_a_server_restart_and_fail_at_once_with_the_browser_gone( browser_computer): with namespace_routing(): - deployed = await Env.get(browser_computer.id).deploy() + deployed = await Env.get(browser_computer.id).deploy(env_params=OWN_LOOPBACK) context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -343,7 +348,7 @@ async def test_its_state_operations_hold_up_at_once_through_a_server_restart_and async def test_its_data_plane_signs_it_in_snapshots_it_resets_it_and_loads_a_snapshot_back_whole(browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [DATA_SITE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -369,7 +374,7 @@ async def test_its_task_steps_set_cookies_and_storage_open_a_tab_behind_and_acti browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [DATA_SITE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -428,7 +433,7 @@ async def test_each_start_tabs_page_sees_the_windows_size_from_its_first_script_ urls = [SIZED.format(index) for index in range(3)] with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": urls}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": urls}}}) context = TaskStepContext(deployed_envs=[deployed]) try: tabs = await tabs_of(deployed) @@ -447,7 +452,7 @@ async def test_each_start_tabs_page_sees_the_windows_size_from_its_first_script_ async def test_its_data_calls_take_turns_a_server_restart_takes_a_load_and_with_the_browser_gone_they_fail_at_once( browser_computer): with namespace_routing(): - deployed = await Env.get(browser_computer.id).deploy() + deployed = await Env.get(browser_computer.id).deploy(env_params=OWN_LOOPBACK) context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -535,11 +540,45 @@ async def _ready(deployed) -> dict: """ -async def _through_port(deployed, url: str) -> str: - done = await inside.run(deployed, ["python3", "-c", THROUGH_PROXY, url, "http://127.0.0.1:3129"], timeout_s=60) +async def _through_port(deployed, url: str, proxy: str = "http://127.0.0.1:3129") -> str: + done = await inside.run(deployed, ["python3", "-c", THROUGH_PROXY, url, proxy], timeout_s=60) return done["stdout"].strip() +#: What a CDP agent sees of the tab it navigates: its title, its first words, and whether it finished loading. +PAGE_SEEN = ("JSON.stringify({ready: document.readyState, title: document.title, " + "text: document.body ? document.body.innerText.slice(0, 200) : ''})") + + +async def _cdp_agent_loads(ws_url: str, urls: list[str]) -> dict: + """What a CDP agent at ``ws_url`` sees navigating a tab of its own to each of ``urls``, one after another: + ``Page.navigate``'s error, if any, and the page once it has loaded.""" + async with connect(ws_url, max_size=None, open_timeout=60) as ws: + ids = itertools.count(1) + + async def call(method: str, params: dict, session: str | None = None) -> dict: + message_id = next(ids) + await ws.send(json.dumps({"id": message_id, "method": method, "params": params, + **({"sessionId": session} if session else {})})) + while (reply := json.loads(await asyncio.wait_for(ws.recv(), 90))).get("id") != message_id: + pass + return reply + + target = (await call("Target.createTarget", {"url": "about:blank"}))["result"]["targetId"] + session = (await call("Target.attachToTarget", {"targetId": target, "flatten": True}))["result"]["sessionId"] + seen = {} + for url in urls: + navigated = await call("Page.navigate", {"url": url}, session) + page, deadline = {}, time.monotonic() + 30 + while page.get("ready") != "complete" and time.monotonic() < deadline: + await asyncio.sleep(1) + evaluated = await call("Runtime.evaluate", {"expression": PAGE_SEEN, "returnByValue": True}, session) + page = json.loads(evaluated.get("result", {}).get("result", {}).get("value") or "{}") + seen[url] = {"error": navigated.get("result", {}).get("errorText") or navigated.get("error", {}).get( + "message"), **page} + return seen + + async def _container_of(deployed) -> str: """The Docker container ``deployed``'s computer runs in, on this machine's local provider.""" return (await LocalSandboxProvider().get_sandbox(deployed.sandbox_id)).container_name @@ -649,6 +688,36 @@ async def test_routing_sites_beside_live_ones_its_forward_port_takes_the_live_ho assert not report.failed +@pytest.mark.asyncio +async def test_alone_on_the_open_web_it_loads_a_public_site_and_never_an_internal_address_a_page_or_an_agent_names( + browser_computer): + internal = ["http://169.254.169.254/latest/meta-data/", "https://10.0.0.1/", "http://127.0.0.1:18765/", + "https://[::1]/"] + with namespace_routing(): + deployed = await Env.get(browser_computer.id).deploy( + env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) + context = TaskStepContext(deployed_envs=[deployed]) + try: + through = {url: await _through_port(deployed, url, "http://127.0.0.1:3128") + for url in ["https://github.com/", *internal]} + seen = await _cdp_agent_loads((await _cdp(deployed, "ada"))["ws_url"], ["https://github.com/", *internal]) + logged = _as_root(await _container_of(deployed), "cat /var/log/agentenv/site-proxy.log") + finally: + report = await teardown_run(context) + + assert through["https://github.com/"].startswith("200 "), through + assert all(through[url].startswith("error") and "403" in through[url] for url in internal), through + assert seen["https://github.com/"]["title"].startswith("GitHub") and not seen["https://github.com/"]["error"], seen + for url in internal: + assert (seen[url]["error"] or "").startswith("net::ERR_TUNNEL_CONNECTION_FAILED") or ( + "is an internal address" in seen[url].get("text", "")), (url, seen[url]) + entries = [json.loads(line) for line in logged.splitlines() if line.startswith("{")] + assert any(entry.get("connection", {}).get("host") == "github.com" for entry in entries), logged[-2000:] + assert {entry["refused"]["host"] for entry in entries if "refused" in entry} >= { + "169.254.169.254", "10.0.0.1", "127.0.0.1", "::1"}, logged[-2000:] + assert not report.failed + + async def _kill(deployed, pattern: str, oldest: bool = False) -> None: """SIGKILL the newest, or oldest, process matching ``pattern``, a second after computer-exec has answered, since the driver that runs the command may be the one killed.""" @@ -671,7 +740,7 @@ async def test_playwright_and_puppeteer_drive_it_through_its_cdp_address_which_a browser_computer, cdp_clients): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [SITE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: serving = asyncio.ensure_future(inside.run(deployed, ["python3", "-c", SERVE_SITE, "900"], timeout_s=960)) @@ -723,7 +792,7 @@ async def test_browser_connect_cdp_hands_an_agent_its_address_with_the_tools_hid with namespace_routing(): agent = put_test_agent(f"cdp-agent-{uuid.uuid4().hex[:8]}") deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [SITE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) serving = None try: @@ -765,7 +834,7 @@ async def test_a_cdp_agent_keeps_its_address_through_an_app_server_and_a_driver_ with namespace_routing(): agent = put_test_agent(f"cdp-chaos-{uuid.uuid4().hex[:8]}") deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _connected(context, agent, browser_computer.id, "solver") @@ -789,7 +858,7 @@ async def test_two_cdp_agents_drive_one_browser_at_once_each_through_an_address_ with namespace_routing(): agent = put_test_agent(f"cdp-pair-{uuid.uuid4().hex[:8]}") deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: for name in ("solver", "buyer"): diff --git a/tst/integration/computer/browser_gateway_test.py b/tst/integration/computer/browser_gateway_test.py index d163f66b3..0201a6554 100644 --- a/tst/integration/computer/browser_gateway_test.py +++ b/tst/integration/computer/browser_gateway_test.py @@ -31,8 +31,8 @@ from agent_env.store.routing import namespace_routing from agent_env.task.teardown import teardown_run from agent_env.task_step.context import TaskStepContext -from tst.integration.computer.browser_computer_test import (DATA_PAGE, SERVE_SITE, SERVE_START_S, SITE, _clients, - _kill, _ready) +from tst.integration.computer.browser_computer_test import (DATA_PAGE, OWN_LOOPBACK, SERVE_SITE, SERVE_START_S, SITE, + _clients, _kill, _ready) from tst.integration.computer.browser_computer_test import _cdp as _driver_cdp pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] @@ -58,7 +58,7 @@ async def test_playwright_and_puppeteer_reach_the_browser_through_the_gateway_an browser_computer, cdp_clients, gateway_image): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [SITE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: serving = asyncio.ensure_future(inside.run(deployed, ["python3", "-c", SERVE_SITE, "900"], timeout_s=960)) @@ -93,7 +93,7 @@ async def test_a_gateway_restart_or_a_data_reset_closes_a_live_connection_with_a browser_computer, gateway_image): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: async with _gateway(gateway_image, deployed) as (gateway, container): @@ -126,7 +126,7 @@ async def test_behind_the_gateway_a_browser_down_at_first_is_found_once_up_and_a browser_computer, gateway_image): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) + env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: token = (await _driver_cdp(deployed, "ada"))["http_url"].split("/cdp/")[1].strip("/") diff --git a/tst/unit/env/envs/test_computer_open_web.py b/tst/unit/env/envs/test_computer_open_web.py new file mode 100644 index 000000000..69938d036 --- /dev/null +++ b/tst/unit/env/envs/test_computer_open_web.py @@ -0,0 +1,171 @@ +"""A browser alone on the open web: the headless base's stand-in proxy on 3128, its route out with no site routing, +forwards any host, HTTP and CONNECT alike, without decrypting, but only to an address it resolved and checked itself, +refusing loopback, private, link-local and the other addresses that aren't globally routable; and it logs each +connection and refusal as a JSON line. The shared site proxy judges the addresses of a live site's hosts the same way. +The proxy is a network program, so these tests open sockets, on loopback alone.""" + +from __future__ import annotations + +import contextlib +import importlib.util +import json +import socket +import threading +from pathlib import Path + +import pytest + +import agent_env +from agent_env.env.envs.computer.utils import site_proxy + +pytestmark = pytest.mark.usefixtures("socket_enabled") + +STAND_IN = Path(agent_env.__file__).parent / "computers" / "artifacts" / "ubuntu-24.04-headless" / "site-proxy.py" +GITHUB = "140.82.112.3" +REFUSED = ["127.0.0.1", "127.8.9.1", "10.0.0.1", "172.16.5.4", "192.168.1.1", "169.254.169.254", "100.64.0.1", + "0.0.0.0", "224.0.0.1", "255.255.255.255", "192.0.2.1", "::1", "::", "fe80::1", "fe80::1%eth0", "fc00::1", + "fd12:3456::1", "ff02::1", "::ffff:10.0.0.1", "::ffff:127.0.0.1", "64:ff9b::a00:1", "2002:a9fe:a9fe::1"] +ALLOWED = [GITHUB, "8.8.8.8", "2606:4700:4700::1111", "::ffff:140.82.112.3", "64:ff9b::8c52:7003"] + + +def _stand_in(): + spec = importlib.util.spec_from_file_location("headless_site_proxy", STAND_IN) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +@pytest.mark.parametrize("judge", ["stand-in", "site proxy"]) +def test_both_proxies_refuse_every_address_that_isnt_globally_routable_and_take_the_rest(judge): + allowed = _stand_in().allowed if judge == "stand-in" else site_proxy.allowed + + assert [address for address in REFUSED if allowed(address)] == [] + assert [address for address in ALLOWED if not allowed(address)] == [] + + +@pytest.fixture +def proxy(monkeypatch): + with _serving(monkeypatch, _stand_in()) as serving: + yield serving + + +@contextlib.contextmanager +def _serving(monkeypatch, module): + """``module``'s stand-in serving on a free port, with ``site.test`` resolving to one of GitHub's addresses and + ``rebound.test`` to an internal one, and connections to an address off loopback landing on a local upstream that + answers each request; yields the port, each address the proxy connected to, what the upstream read, and the + upstream's own port.""" + real_resolve, real_connect = socket.getaddrinfo, socket.create_connection + names = {"site.test": [GITHUB], "rebound.test": ["10.0.0.7"], "mixed.test": ["192.168.0.2", "140.82.112.4"]} + upstream, read, opened = socket.create_server(("127.0.0.1", 0)), [], [] + + def answer(): + while True: + try: + conn, _ = upstream.accept() + except OSError: + return + with conn: + read.append(conn.recv(65536)) + conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nhello") + + def getaddrinfo(host, port, *args, **kwargs): + if host in names: + return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, port)) for address in names[host]] + return real_resolve(host, port, *args, **kwargs) + + def create_connection(address, *args, **kwargs): + if address[0] in ("127.0.0.1", "localhost"): + return real_connect(address, *args, **kwargs) + opened.append(address) + return real_connect(upstream.getsockname(), *args, **kwargs) + + monkeypatch.setattr(module.socket, "getaddrinfo", getaddrinfo) + monkeypatch.setattr(module.socket, "create_connection", create_connection) + server = module.Server(("127.0.0.1", 0), module.Proxy) + threading.Thread(target=server.serve_forever, daemon=True).start() + threading.Thread(target=answer, daemon=True).start() + try: + yield server.server_address[1], opened, read, upstream.getsockname()[1] + finally: + server.shutdown() + upstream.close() + + +def _ask(port: int, request: bytes, then: bytes = b"") -> bytes: + """What the proxy answers ``request``, and in a tunnel it opened, ``then``.""" + with socket.create_connection(("127.0.0.1", port), timeout=10) as conn: + conn.sendall(request) + answer = conn.recv(65536) + if then and answer.startswith(b"HTTP/1.1 200"): + conn.sendall(then) + while chunk := conn.recv(65536): + answer += chunk + else: + while chunk := conn.recv(65536): + answer += chunk + return answer + + +def _logged(text: str, kind: str) -> list[dict]: + return [json.loads(line)[kind] for line in text.splitlines() if line.startswith(f'{{"{kind}"')] + + +@pytest.mark.parametrize("request_line", [ + b"CONNECT 127.0.0.1:18765 HTTP/1.1", b"CONNECT [::1]:443 HTTP/1.1", b"CONNECT 169.254.169.254:80 HTTP/1.1", + b"GET http://10.0.0.1/ HTTP/1.1", b"GET http://169.254.169.254/latest/meta-data/ HTTP/1.1", + b"GET http://rebound.test/ HTTP/1.1", b"CONNECT rebound.test:443 HTTP/1.1", b"GET http://localhost:18765/ HTTP/1.1", +], ids=["connect-loopback", "connect-ipv6-loopback", "connect-metadata", "url-private", "url-metadata", + "name-resolving-internal", "connect-name-resolving-internal", "url-localhost"]) +def test_the_stand_in_refuses_an_internal_address_at_once_whoever_names_it_and_logs_why(proxy, capsys, request_line): + port, opened, read, _ = proxy + + answer = _ask(port, request_line + b"\r\nHost: x\r\n\r\n") + + assert answer.startswith(b"HTTP/1.1 403 ") and b"X-Agentenv-Site-Proxy: failed" in answer, answer + assert b"is an internal address, which the browser never reaches" in answer + assert opened == [] and read == [] + [refused] = _logged(capsys.readouterr().out, "refused") + assert refused["reason"] == "internal address" and refused["addresses"] + + +def test_the_stand_in_tunnels_and_forwards_a_public_host_at_the_address_it_checked_and_logs_each_connection( + proxy, capsys): + port, opened, read, _ = proxy + + tunnelled = _ask(port, b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n", + then=b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n") + forwarded = _ask(port, b"GET http://mixed.test/a?b=1 HTTP/1.1\r\nHost: mixed.test\r\nProxy-Connection: keep-alive" + b"\r\n\r\n") + + assert tunnelled.startswith(b"HTTP/1.1 200 Connection established") and tunnelled.endswith(b"hello"), tunnelled + assert forwarded.startswith(b"HTTP/1.1 200 OK") and forwarded.endswith(b"hello"), forwarded + assert opened == [(GITHUB, 443), ("140.82.112.4", 80)] + assert read[0] == b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n" + assert read[1].startswith(b"GET /a?b=1 HTTP/1.1\r\n") and b"Host: mixed.test" in read[1] + assert b"Proxy-Connection" not in read[1] and b"Connection: close" in read[1] + logged = _logged(capsys.readouterr().out, "connection") + assert [(entry["host"], entry["port"], entry["address"]) for entry in logged] == [ + ("site.test", 443, GITHUB), ("mixed.test", 80, "140.82.112.4")] + assert all(entry["bytes_sent"] > 0 and entry["bytes_received"] > 0 for entry in logged), logged + assert all(set(entry) >= {"started", "duration_s"} for entry in logged) + + +def test_the_stand_in_answers_a_name_that_doesnt_resolve_at_once(proxy, capsys): + port, opened, _, _ = proxy + + answer = _ask(port, b"CONNECT nowhere.invalid:443 HTTP/1.1\r\nHost: nowhere.invalid:443\r\n\r\n") + + assert answer.startswith(b"HTTP/1.1 502 ") and b"nowhere.invalid doesn't resolve" in answer, answer + assert opened == [] and _logged(capsys.readouterr().out, "refused")[0]["reason"].startswith("doesn't resolve") + + +def test_with_its_own_loopback_let_in_the_stand_in_reaches_this_computers_servers_and_nothing_else_internal( + monkeypatch, capsys): + monkeypatch.setenv("AGENTENV_SETTING_OWN_LOOPBACK", "True") + with _serving(monkeypatch, _stand_in()) as (port, opened, read, upstream_port): + own = _ask(port, f"GET http://127.0.0.1:{upstream_port}/page HTTP/1.1\r\nHost: x\r\n\r\n".encode()) + private = _ask(port, b"GET http://10.0.0.1/ HTTP/1.1\r\nHost: x\r\n\r\n") + + assert own.startswith(b"HTTP/1.1 200 OK") and read[0].startswith(b"GET /page HTTP/1.1"), own + assert private.startswith(b"HTTP/1.1 403 "), private diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index a6e28d6b8..55b52ab01 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -9,6 +9,7 @@ import contextlib import email.message import io +import ipaddress import json import os import socket @@ -392,25 +393,42 @@ async def test_the_forward_port_refuses_any_other_host_at_once_and_logs_it(tmp_p assert f"site proxy: refused {method} {named}: no site of this computer" in capsys.readouterr().err +GITHUB = "140.82.112.3" + + @contextlib.contextmanager -def _live_hosts_at(monkeypatch, upstream_port): - """Connections the proxy opens to a live site's host landing on ``upstream_port`` on loopback, and each host it - opened one to recorded.""" - opened, real = [], asyncio.open_connection +def _live_hosts_at(monkeypatch, upstream_port, resolves=None): + """Each name resolving to the addresses ``resolves`` gives it, else to one of GitHub's, and connections the proxy + opens to an address off loopback landing on ``upstream_port``; each address it opened one to recorded.""" + opened, real_open, real_resolve = [], asyncio.open_connection, socket.getaddrinfo + + def getaddrinfo(host, port, *args, **kwargs): + try: + ipaddress.ip_address(host) + return real_resolve(host, port, *args, **kwargs) + except ValueError: + return [(socket.AF_INET6 if ":" in address else socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, port)) + for address in (resolves or {}).get(host, [GITHUB])] async def open_connection(host, port, **kwargs): if host == "127.0.0.1": - return await real(host, port, **kwargs) + return await real_open(host, port, **kwargs) opened.append((host, port)) - return await real("127.0.0.1", upstream_port, **kwargs) + return await real_open("127.0.0.1", upstream_port, **kwargs) + monkeypatch.setattr(site_proxy.socket, "getaddrinfo", getaddrinfo) monkeypatch.setattr(site_proxy.asyncio, "open_connection", open_connection) yield opened +def _logged(err: str, kind: str) -> list[dict]: + """The proxy's JSON log lines of ``kind`` (``connection`` or ``refused``).""" + return [json.loads(line)[kind] for line in err.splitlines() if line.startswith(f'{{"{kind}"')] + + @pytest.mark.asyncio async def test_the_forward_port_tunnels_a_live_sites_host_straight_to_it_and_sends_its_url_on_as_a_path( - tmp_path, monkeypatch): + tmp_path, monkeypatch, capsys): async with Upstream() as upstream, Upstream() as live: config, _ = _config(tmp_path, upstream.port) config.direct = ["github.com", "*.githubassets.com"] @@ -425,12 +443,44 @@ async def test_the_forward_port_tunnels_a_live_sites_host_straight_to_it_and_sen plain = await _exchange(port, b"GET http://github.com/a?b=1 HTTP/1.1\r\nHost: github.com\r\n" b"Proxy-Connection: keep-alive\r\n\r\n") plain[3][1].close() + await asyncio.sleep(0.3) # each connection is logged as it ends - assert established.startswith(b"HTTP/1.1 200 ") and opened == [("github.githubassets.com", 8443), ("github.com", 80)] + assert established.startswith(b"HTTP/1.1 200 ") and opened == [(GITHUB, 8443), (GITHUB, 80)] assert (tunnelled[0], tunnelled[2], plain[0], plain[2]) == ("HTTP/1.1 200 OK", b"hello", "HTTP/1.1 200 OK", b"hello") assert [request[0] for request in live.requests] == ["GET /x HTTP/1.1", "GET /a?b=1 HTTP/1.1"] assert live.requests[1][1]["connection"] == "close" and "proxy-connection" not in live.requests[1][1] - assert upstream.requests == [] + assert live.requests[1][1]["host"] == "github.com" and upstream.requests == [] + logged = _logged(capsys.readouterr().err, "connection") + assert [(entry["host"], entry["port"], entry["address"]) for entry in logged] == [ + ("github.githubassets.com", 8443, GITHUB), ("github.com", 80, GITHUB)] + assert all(entry["bytes_sent"] > 0 and entry["bytes_received"] > 0 and entry["duration_s"] >= 0 for entry in logged) + + +@pytest.mark.asyncio +async def test_a_live_host_resolving_to_an_internal_address_is_refused_and_one_resolving_both_ways_reaches_the_public_one( + tmp_path, monkeypatch, capsys): + resolves = {"github.com": ["10.0.0.1"], "api.github.com": ["169.254.169.254", "::1"], + "mixed.github.com": ["192.168.1.9", "140.82.112.4"]} + async with Upstream() as upstream, Upstream() as live: + config, _ = _config(tmp_path, upstream.port) + config.direct = ["github.com", "*.github.com"] + with _live_hosts_at(monkeypatch, live.port, resolves) as opened: + async with _forward_port(config) as port: + refused = [await _exchange(port, line + b"\r\n\r\n") for line in ( + b"CONNECT github.com:443 HTTP/1.1\r\nHost: github.com:443", + b"GET http://api.github.com/meta HTTP/1.1\r\nHost: api.github.com")] + reader, writer = await asyncio.open_connection("127.0.0.1", port) + writer.write(b"CONNECT mixed.github.com:443 HTTP/1.1\r\nHost: mixed.github.com:443\r\n\r\n") + established = await reader.readuntil(b"\r\n\r\n") + writer.close() + + assert [(answer[0], answer[1]["x-agentenv-site-proxy"]) for answer in refused] == [ + ("HTTP/1.1 403 Forbidden", "failed")] * 2 + assert refused[0][2] == b"github.com is an internal address, which the browser never reaches\n" + assert established.startswith(b"HTTP/1.1 200 ") and opened == [("140.82.112.4", 443)] + assert [(entry["host"], entry["reason"], entry["addresses"]) for entry in _logged(capsys.readouterr().err, "refused")] == [ + ("github.com", "internal address", ["10.0.0.1"]), ("api.github.com", "internal address", ["169.254.169.254", "::1"])] + assert live.requests == [] @pytest.mark.asyncio From d4ea94e34949412ea534aea1b12ceee29efe4257 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 19:06:38 -0800 Subject: [PATCH 2/6] fix(computer): a probe lets a browser alone reach the conformance fixture on its own loopback The probe serves its conformance fixture on the computer's loopback, which the headless base's stand-in now refuses, so gate 3 left P3 to P9 unverified on every browser computer. A probe boots with the base's own_loopback on, where the base declares it; a deploy still boots at the base's default, off. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/computers/README.md | 2 +- .../env/envs/computer/utils/probing.py | 14 ++++++++---- tst/unit/env/envs/test_computer_env.py | 22 ++++++++++++++++++- 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/agent_env/computers/README.md b/src/agent_env/computers/README.md index 68a39ba53..6629461ef 100644 --- a/src/agent_env/computers/README.md +++ b/src/agent_env/computers/README.md @@ -19,7 +19,7 @@ address it resolved and checked itself: one that isn't globally routable (loopba metadata service, their IPv6 equivalents) is refused at once with a 403, whether a page or an agent driving the browser names it, and each connection and refusal is a JSON line in `/var/log/agentenv/site-proxy.log`. The sandbox's network policy still applies beneath it. Its `own_loopback` setting (default `false`) lets the browser reach the computer's own -loopback, as a test that serves its pages inside the computer needs. A computer that routes sites names the shared site proxy's forward port, `127.0.0.1:3129`, to the +loopback, as a test that serves its pages inside the computer needs; a probe boots with it on, since the conformance fixture it serves is on that loopback, and so must a conformance run against a deployed browser computer. A computer that routes sites names the shared site proxy's forward port, `127.0.0.1:3129`, to the browser instead: it serves the run's sites, refuses every other host and runs out of the agent's reach. It has a container form only. Name it from another bundle as `ubuntu-24.04-headless`, or by its id, `@local/agentenv-framework/computers/ubuntu-24.04-headless`. diff --git a/src/agent_env/env/envs/computer/utils/probing.py b/src/agent_env/env/envs/computer/utils/probing.py index 56c931137..62ddbe701 100644 --- a/src/agent_env/env/envs/computer/utils/probing.py +++ b/src/agent_env/env/envs/computer/utils/probing.py @@ -32,6 +32,9 @@ _PROBE_TTL_S = 1800 #: How long the probe at full may take on a computer that booted: about ten seconds on a healthy one. _PROBE_TIMEOUT_S = 120 +#: Base settings a probe boots with, where the base declares them: a browser alone reaches its computer's own loopback +#: only with ``own_loopback``, and the probe serves its conformance fixture there. A probe runs no agent. +_PROBE_SETTINGS = {"own_loopback": True} #: How many probes, each on a fresh boot, a build gets when each ends with the connection to its driver dropped, refused #: or timed out, which says nothing of the build, before the last one's result is recorded. _PROBE_ATTEMPTS = 3 @@ -152,13 +155,16 @@ def _attempted(attempts: list[dict], result: dict) -> dict: async def _probe(links: list[SandboxProvider], env: ComputerEnv, driver: ComputerDriverEnv, image: str, form: str = "container") -> tuple[ProbeReport, dict, str]: """``image``, of ``form``, booted at the env's settings and its apps' defaults with a control token of its own, as - a deploy with no ``env_params`` boots it, on the first of ``links`` that boots it, probed at ``full`` with that - token within ``_PROBE_TIMEOUT_S`` and torn down; the report, the driver's card and the provider's name. Raises - Unprobed, booting nothing, when a setting it boots with doesn't resolve here.""" + a deploy with no ``env_params`` boots it, but for the base's ``_PROBE_SETTINGS``, on the first of ``links`` that + boots it, probed at ``full`` with that token within ``_PROBE_TIMEOUT_S`` and torn down; the report, the driver's + card and the provider's name. Raises Unprobed, booting nothing, when a setting it boots with doesn't resolve + here.""" token = boot.control_token() + values = {**env.computer_values(), **{name: value for name, value in _PROBE_SETTINGS.items() + if name in env.computer.config}} try: environment = { - **boot.settings_environment(env.computer, env.computer_values(), environment_name=env.computer_name), + **boot.settings_environment(env.computer, values, environment_name=env.computer_name), **boot.app_settings_environment(env.composed_apps(), env.app_defaults(), platform_of(env.computer.os))} except ConfigError as e: # an env: or secret: reference that doesn't resolve here, or no secret store to resolve it raise Unprobed(f"a setting it boots with didn't resolve here: {e}") from e diff --git a/tst/unit/env/envs/test_computer_env.py b/tst/unit/env/envs/test_computer_env.py index 3679af1f1..4f3a30036 100644 --- a/tst/unit/env/envs/test_computer_env.py +++ b/tst/unit/env/envs/test_computer_env.py @@ -30,7 +30,7 @@ from agentenv_protocol.website_browser import BROWSER from click.testing import CliRunner -from agent_env.artifact.artifacts.computer import ComputerArtifact +from agent_env.artifact.artifacts.computer import ComputerArtifact, Setting from agent_env.artifact.artifacts.docker_image import DockerImageArtifact from agent_env.artifact.artifacts.environment import EnvironmentArtifact from agent_env.artifact.artifacts.environment_universe import EnvironmentUniverseArtifact @@ -361,6 +361,26 @@ async def fake_boot(link, **kwargs): probed.assert_awaited_once_with("http://computer", "full", token=booted[0]["token"]) +def test_the_probe_lets_a_browser_alone_reach_its_own_loopback_where_the_base_declares_it_as_its_fixture_is_there( + base, driver, docker, monkeypatch): + env = _put(base, driver, compose=False) + monkeypatch.setitem(env.computer.config, "own_loopback", Setting(default=False, phase="deploy")) + booted = [] + + async def fake_boot(link, **kwargs): + booted.append(kwargs) + return boot.Booted(sandbox=None, url="http://computer", card=CARD) + + monkeypatch.setattr(boot, "boot", fake_boot) + monkeypatch.setattr(boot, "discard", AsyncMock()) + monkeypatch.setattr(probing, "run_probe", AsyncMock(return_value="report")) + + asyncio.run(_probe([SimpleNamespace()], env, driver, "img")) + + assert booted[0]["environment"]["AGENTENV_SETTING_OWN_LOOPBACK"] == "True" + assert "own_loopback" not in env.computer_values() # a deploy still boots at the base's default + + def test_a_build_the_probe_cant_boot_for_a_setting_that_doesnt_resolve_stays_unverified_until_a_compose_where_it_does( base, driver, docker, tmp_path, monkeypatch): monkeypatch.delenv("MAIL_TOKEN", raising=False) From a1ff622cf6f4186bbfd287453f77aef6f2c98ce3 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 19:31:27 -0800 Subject: [PATCH 3/6] fix(computer): the stand-in proxy relays a socket numbered past 1023 It waited on its two sockets with select(), which refuses a descriptor past 1023, so a proxy with that many files open dropped every connection it opened after. It waits with a selector now, epoll on Linux. Co-Authored-By: Claude Opus 5.5 --- .../ubuntu-24.04-headless/site-proxy.py | 14 +++++++---- tst/unit/env/envs/test_computer_open_web.py | 23 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py index 37db13ebd..676e4aa96 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py @@ -9,7 +9,7 @@ import ipaddress import json import os -import select +import selectors import socket import socketserver import sys @@ -145,14 +145,19 @@ def log_message(self, *args): def _pipe(client: socket.socket, upstream: socket.socket) -> tuple[int, int]: - """Relay both ways until either side closes or both are idle ``IDLE_S``; the bytes sent upstream and received.""" + """Relay both ways until either side closes or both are idle ``IDLE_S``; the bytes sent upstream and received. + It waits with a selector (epoll on Linux), not ``select()``, which refuses a socket numbered past 1023.""" sent = received = 0 + waiting = selectors.DefaultSelector() try: + waiting.register(client, selectors.EVENT_READ) + waiting.register(upstream, selectors.EVENT_READ) while True: - ready, _, _ = select.select([client, upstream], [], [], IDLE_S) + ready = waiting.select(IDLE_S) if not ready: break - for s in ready: + for key, _ in ready: + s = key.fileobj data = s.recv(65536) if not data: return sent, received @@ -164,6 +169,7 @@ def _pipe(client: socket.socket, upstream: socket.socket) -> tuple[int, int]: except OSError: pass finally: + waiting.close() upstream.close() return sent, received diff --git a/tst/unit/env/envs/test_computer_open_web.py b/tst/unit/env/envs/test_computer_open_web.py index 69938d036..83c22513a 100644 --- a/tst/unit/env/envs/test_computer_open_web.py +++ b/tst/unit/env/envs/test_computer_open_web.py @@ -9,6 +9,8 @@ import contextlib import importlib.util import json +import os +import resource import socket import threading from pathlib import Path @@ -151,6 +153,27 @@ def test_the_stand_in_tunnels_and_forwards_a_public_host_at_the_address_it_check assert all(set(entry) >= {"started", "duration_s"} for entry in logged) +def test_the_stand_in_relays_a_connection_whose_sockets_are_numbered_past_1023(proxy): + port, opened, read, _ = proxy + soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) + if hard != resource.RLIM_INFINITY and hard < 1400: + pytest.skip(f"this machine allows {hard} open files, too few to number a socket past 1023") + resource.setrlimit(resource.RLIMIT_NOFILE, (max(soft, 1400) if hard == resource.RLIM_INFINITY else + min(max(soft, 1400), hard), hard)) + held = [os.pipe() for _ in range(520)] + try: + answer = _ask(port, b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n", + then=b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n") + finally: + for pair in held: + os.close(pair[0]) + os.close(pair[1]) + resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard)) + + assert answer.startswith(b"HTTP/1.1 200 Connection established") and answer.endswith(b"hello"), answer + assert opened == [(GITHUB, 443)] + + def test_the_stand_in_answers_a_name_that_doesnt_resolve_at_once(proxy, capsys): port, opened, _, _ = proxy From 61ac64dcd53a182176bea98225acc9c4a8c4eb08 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 19:58:45 -0800 Subject: [PATCH 4/6] fix(computer): the open-web proxies bound every wait: a stuck write, and one deadline to connect - The stand-in cleared its upstream socket's timeout once connected, so a write to a server that stopped reading could wait for ever. Both of a connection's sockets keep IDLE_S now, so that write ends and the tunnel closes. - Each proxy gave every address its own connect timeout, and the shared one resolved outside any: a host of three addresses that never answer took three times as long to fail. The lookup and every attempt now share one deadline. Co-Authored-By: Claude Opus 5.5 --- .../ubuntu-24.04-headless/site-proxy.py | 14 +++++--- .../env/envs/computer/utils/site_proxy.py | 19 +++++++---- tst/unit/env/envs/test_computer_open_web.py | 33 +++++++++++++++++++ tst/unit/env/envs/test_computer_site_proxy.py | 29 ++++++++++++++++ 4 files changed, 84 insertions(+), 11 deletions(-) diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py index 676e4aa96..c1f32c3e4 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py @@ -90,8 +90,11 @@ def forward(self): do_GET = do_POST = do_PUT = do_PATCH = do_DELETE = do_HEAD = do_OPTIONS = forward def _open(self, host: str, port: int, method: str): - """A connection to an address ``host`` resolves to that the proxy may reach, or None once the browser has - its answer: a 403 for a host whose every address is refused, a 502 for one that doesn't resolve or answer.""" + """A connection to an address ``host`` resolves to that the proxy may reach, tried in turn within one + ``CONNECT_TIMEOUT_S`` for them all, or None once the browser has its answer: a 403 for a host whose every + address is refused, a 502 for one that doesn't resolve or answer. The connection keeps an ``IDLE_S`` timeout, + so a write to a peer that stopped reading ends too.""" + deadline = time.monotonic() + CONNECT_TIMEOUT_S try: usable, refused = resolve(host, port) except OSError as error: @@ -104,9 +107,11 @@ def _open(self, host: str, port: int, method: str): self._answer(403, f"{host} is an internal address, which the browser never reaches\n") return None for address in usable: + if (left := deadline - time.monotonic()) <= 0: + break try: - upstream = socket.create_connection((address, port), timeout=CONNECT_TIMEOUT_S) - upstream.settimeout(None) + upstream = socket.create_connection((address, port), timeout=left) + upstream.settimeout(IDLE_S) self.address = address return upstream except OSError: @@ -129,6 +134,7 @@ def _answer(self, status: int, body: str) -> None: def _relay(self, upstream: socket.socket, host: str, port: int, first: bytes) -> None: started, began = time.time(), time.monotonic() sent, received = len(first), 0 + self.connection.settimeout(IDLE_S) try: if first: upstream.sendall(first) diff --git a/src/agent_env/env/envs/computer/utils/site_proxy.py b/src/agent_env/env/envs/computer/utils/site_proxy.py index f1d61c735..6866b09c2 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -483,13 +483,17 @@ async def _direct(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, ho head: Optional[bytes] = None) -> None: """``host``, a live site's, reached straight at ``port``, at an address the proxy resolved and checked itself (``allowed``), never resolved again: a tunnel, answered as established, for a ``CONNECT``, else ``head`` sent - on; then both ends relayed until either closes, and the connection logged (``log_event``). A host whose every - address is internal is refused at once, one that doesn't resolve or answer is a 502.""" + on; then both ends relayed until either closes, and the connection logged (``log_event``). The lookup and every + address's connection attempt share one ``UPSTREAM_TIMEOUT_S``. A host whose every address is internal is + refused at once, one that doesn't resolve or answer in time is a 502.""" method = "CONNECT" if head is None else head.split(b" ", 1)[0].decode("latin-1") + loop = asyncio.get_running_loop() + deadline = loop.time() + UPSTREAM_TIMEOUT_S try: - found = await asyncio.get_running_loop().getaddrinfo(host, port, type=socket.SOCK_STREAM) - except OSError as e: - log_event(refused={"method": method, "host": host, "port": port, "reason": f"doesn't resolve: {e}"}) + found = await asyncio.wait_for(loop.getaddrinfo(host, port, type=socket.SOCK_STREAM), UPSTREAM_TIMEOUT_S) + except (OSError, asyncio.TimeoutError) as e: + log_event(refused={"method": method, "host": host, "port": port, + "reason": f"doesn't resolve: {e or type(e).__name__}"}) writer.write(simple_response("502 Bad Gateway", f"{host} doesn't resolve\n", FAILED)) return addresses = list(dict.fromkeys(info[4][0] for info in found)) @@ -500,9 +504,10 @@ async def _direct(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, ho "reaches\n", FAILED)) return for address in usable: + if (left := deadline - loop.time()) <= 0: + continue try: - up_reader, up_writer = await asyncio.wait_for(asyncio.open_connection(address, port), - UPSTREAM_TIMEOUT_S) + up_reader, up_writer = await asyncio.wait_for(asyncio.open_connection(address, port), left) break except (OSError, asyncio.TimeoutError): continue diff --git a/tst/unit/env/envs/test_computer_open_web.py b/tst/unit/env/envs/test_computer_open_web.py index 83c22513a..091d45415 100644 --- a/tst/unit/env/envs/test_computer_open_web.py +++ b/tst/unit/env/envs/test_computer_open_web.py @@ -13,6 +13,7 @@ import resource import socket import threading +import time from pathlib import Path import pytest @@ -174,6 +175,38 @@ def test_the_stand_in_relays_a_connection_whose_sockets_are_numbered_past_1023(p assert opened == [(GITHUB, 443)] +def test_the_stand_in_ends_a_tunnel_whose_server_stopped_reading_instead_of_waiting_on_it_for_ever(monkeypatch, capsys): + module = _stand_in() + monkeypatch.setattr(module, "IDLE_S", 1) + sink, accepted = socket.create_server(("127.0.0.1", 0)), [] + threading.Thread(target=lambda: accepted.append(sink.accept()), daemon=True).start() # never reads + real_resolve, real_connect = socket.getaddrinfo, socket.create_connection + monkeypatch.setattr(module.socket, "getaddrinfo", lambda host, port, *a, **k: [ + (socket.AF_INET, socket.SOCK_STREAM, 6, "", (GITHUB, port))] if host == "site.test" else real_resolve( + host, port, *a, **k)) + monkeypatch.setattr(module.socket, "create_connection", lambda address, *a, **k: real_connect( + sink.getsockname() if address[0] == GITHUB else address, *a, **k)) + server = module.Server(("127.0.0.1", 0), module.Proxy) + threading.Thread(target=server.serve_forever, daemon=True).start() + try: + with socket.create_connection(server.server_address, timeout=20) as conn: + conn.sendall(b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n") + assert conn.recv(4096).startswith(b"HTTP/1.1 200") + began, ended = time.monotonic(), None + try: + while time.monotonic() - began < 20: + conn.sendall(b"x" * 65536) + except OSError: + ended = time.monotonic() - began + finally: + server.shutdown() + sink.close() + + assert ended is not None and ended < 15, ended + [logged] = _logged(capsys.readouterr().out, "connection") + assert logged["host"] == "site.test" and logged["bytes_sent"] > 0 + + def test_the_stand_in_answers_a_name_that_doesnt_resolve_at_once(proxy, capsys): port, opened, _, _ = proxy diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index 55b52ab01..a31e8ecbb 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -17,6 +17,7 @@ import struct import subprocess import sys +import time import urllib.error from datetime import timedelta from pathlib import Path @@ -483,6 +484,34 @@ async def test_a_live_host_resolving_to_an_internal_address_is_refused_and_one_r assert live.requests == [] +@pytest.mark.asyncio +async def test_a_live_host_whose_every_address_hangs_fails_within_one_deadline_not_one_per_address( + tmp_path, monkeypatch, capsys): + monkeypatch.setattr(site_proxy, "UPSTREAM_TIMEOUT_S", 0.5) + tried, real_open = [], asyncio.open_connection + + async def hangs(host, port, **kwargs): + if host == "127.0.0.1": + return await real_open(host, port, **kwargs) + tried.append(host) + await asyncio.sleep(60) + + async with Upstream() as upstream, Upstream() as live: + config, _ = _config(tmp_path, upstream.port) + config.direct = ["github.com"] + with _live_hosts_at(monkeypatch, live.port, {"github.com": ["140.82.112.3", "140.82.112.4", "140.82.112.5"]}): + monkeypatch.setattr(site_proxy.asyncio, "open_connection", hangs) + async with _forward_port(config) as port: + began = time.monotonic() + answer = await _exchange(port, b"CONNECT github.com:443 HTTP/1.1\r\nHost: github.com:443\r\n\r\n") + took = time.monotonic() - began + + assert answer[0] == "HTTP/1.1 502 Bad Gateway" and took < 1.2, (answer[0], took) + assert tried and tried[0] == "140.82.112.3" + [refused] = _logged(capsys.readouterr().err, "refused") + assert refused["reason"] == "no answer" + + @pytest.mark.asyncio async def test_a_sites_name_within_a_live_hosts_wildcard_is_the_sites_and_no_address_is_ever_taken_straight( tmp_path, monkeypatch, capsys): From 59bb599bc40d5bf929ef230e9e974ba81492b458 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 21:23:28 -0800 Subject: [PATCH 5/6] refactor(computer): a browser alone's proxies refuse nothing, as Harbor's public network does The sandbox's network policy is what limits a browser alone, as it did before: the stand-in proxy and the shared proxy's direct tunnel go back to taking any host, the computer's own loopback and internal addresses included, so the `own_loopback` setting and the probe's override for it go too. What stays: the stand-in's selector relay, its one connect deadline and 300 s idle timeout, and each proxy's JSON line for every connection and every one that fails. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 2 +- src/agent_env/computers/README.md | 11 +- .../ubuntu-24.04-headless/artifact.toml | 4 - .../ubuntu-24.04-headless/site-proxy.py | 62 ++----- .../env/envs/computer/utils/probing.py | 14 +- .../env/envs/computer/utils/site_proxy.py | 59 ++----- .../computer/browser_computer_test.py | 53 +++--- .../computer/browser_gateway_test.py | 10 +- tst/unit/env/envs/test_computer_env.py | 22 +-- tst/unit/env/envs/test_computer_open_web.py | 154 +++++++++--------- tst/unit/env/envs/test_computer_site_proxy.py | 86 +++------- 11 files changed, 160 insertions(+), 317 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 0c8822f16..d9787b95f 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -121,7 +121,7 @@ and deserialized through a registry. | Package | Contents | |---|---| -| `env/` | `Env` base class: a subclass declares `type`, implements `from_dict` and `async deploy(...) -> DeployedEnv`, and may override `reset`, `close` and `load_file_artifact_universe`; teardown closes each env a run deployed before terminating its sandboxes by id. Built-ins in `env/envs/`: `MCPServerEnv` (`mcp_server`), `MultiEnv` (`multi`, MCP servers plus websites), `ServiceDBEnv` (`service_db`, PostgreSQL), `WebsiteEnv` (`website`, one site: a server image, `backend_image` in its env.toml, serving the pages and API, ready when its card's `website/v1` `ready` answers a `WebsiteReadiness` that says so, as `agentenv_protocol.website` (`website_readiness`, `env_readiness`) defines a website env's health for the deploy and `DeployedEnv.readiness()` alike, or, for a card without `ready`, when its `health` path answers 200, which is deprecated; the gateway container's `env/gateway/constants.py` mirrors the URI and op, a test keeping them equal; a separate frontend image is deprecated, warned of at put and optional in a bundle, and a stored `backend_docker_image_artifact` reads as the server image; a live one, `live = true`, is a real site whose server serves none of its pages, its `live`, `domains` and `allow_hosts` read at put from its server's `@website(...)` (`utils/card_naming.py` `WebsiteSource`) unless given, its `settings`' secrets only as references the gateway resolves as it writes the site's container (`WEBSITE_SETTINGS_ENV`); the gateway gives it no site route, a computer's site table leaves it out, a computer whose network is an allowlist is let reach its names (`MultiEnv.live_hosts`), a browser alone's forward port takes them straight to the real site (`site_proxy.py` `direct`), as the headless base's stand-in proxy takes any host, each only to an address it resolved and checked itself (`allowed`: never loopback, private, link-local or the metadata service), and a run that deploys one is marked live, `reproducible: false`), `GatewayEnv` (`gateway_server`), `ComputerDriverEnv` (`computer_driver`, `env/envs/computer/driver_env.py`: a computer driver's files and what its class declares for each platform it supports, read at put in a process of its own, `agentenv_protocol.computer._declare`, where the driver's own dependencies needn't be installed, with the `port` it serves on and its install by OS, `[install.]`, or for one distribution `[install."/"]`, which replaces its OS type's there, as its `known_failures` do (`env/envs/computer/utils/install.py`): files, apt `packages` with extra `sources`, a `python` requirements file for a venv, `downloads` by URL and sha256, `run` scripts, the services the base's init runs, and `ports`; it deploys only as a computer env's driver), `ComputerEnv` (`computer`, in `env/envs/computer/`: a base computer and a driver, matched at put by gate 1 before anything is stored, then composed by `composer.py` into an image for each form and arch on a builder of the put's sandbox provider, by default the env's (`utils/builder.py`; `--sandbox` on `env build`, `bundle register` and `run`): this machine's Docker for `local`, else a VM of the provider, which fetches the put's stored files by signed URL, takes the rest inline and pushes to the image store itself, so it builds only with stores a remote VM reaches; each arch goes to the first link that runs it natively, else one that emulates it (a VM registers QEMU; `--no-emulate` leaves it `skipped` instead, for a later put), and its pair records the provider that built it (`builder`); a run or register that reuses a computer env composes what it has left (`pending`), a run only the forms its sandbox's deploys boot (`booted_forms`: on each link, the first form it boots natively, by the deploy's own rule, `boot.first_native`; a compose of a disk adds its container), and a deploy composes first what it boots that no compose started, and a container it runs natively that was built but not probed, as one whose probe a setting that didn't resolve stopped, booting it only once ready (`ComputerEnv.compose_unstarted`); tagged `agentenv-computers:` in the image store by a key of the image's Dockerfile, build context and lock so any env with the same inputs reuses it; every layer's packages, requirements and downloads are resolved at put in a container of the base for each arch (`utils/packages.py`, `utils/resolve_in_base.py`), stored in the object store by sha256 under `agentenv-packages/`, and listed in a lock stored by a digest of its inputs, which any env with the same inputs reuses, and builds install only the lock's files, every install step `RUN --network=none`; each pair's status goes into its metadata, `composed`, the arches built are written as its computer, a `computer` artifact over one index, and `agent-env env build` resumes what a put left; gate 3 (`utils/probing.py`) then boots each native build on the put's sandbox provider and probes it at `full`, making it `ready` or `failed`, and a build no provider runs natively stays `unverified`. Its deploy, through the `computer` env provider (`providers/env_providers/env_computer_provider.py`), boots its first `ready` build on a sandbox provider that runs its arch natively (`native_platforms()`), with no gateway in front and the driver's card as the env card, its `/dev/shm` the most its apps' Linux installs ask for (`shm_mb`, in MiB, which its composed computer and the deploy record keep; `--shm-size` on a container, `AGENTENV_SHM_MB` read by a Linux disk's init, passed only to a provider whose `SHARED_MEMORY` says it gives that much, else the sandbox's default, which the deploy logs), then checks its `display`; `deploy_env` `env_params` take a `form`, `computer_settings` (deploy-phase values of the base's settings; a secret one, here or on the env, only as a bare `secret:`/`env:` reference) and `driver_settings` (the driver's `ComputerSettings`, such as its default coordinate space); a `network` that allows nothing runs it on local Docker's internal network behind a relay (`providers/sandbox_providers/local_docker/relay/`); `reset` and `close` have the driver end its recordings first (`computer-recording/v1` `end_all`, each uploading what's left and its manifest under the grant it started with), and `reset` then boots a fresh container in its place (`reset_container`), keeping the computer's address but not an agent's MCP session to the old driver; its `load_file_artifact_universe` writes files under the session user's home through the driver's `data/add`, `Downloads` by default, each file's bytes through a read grant when the store's grants reach it and its network allows everything, else inline (`env/envs/computer/utils/inside.py`)), `ComputerAppEnv` (`computer_app`, `env/envs/computer/app_env.py`: an app on a computer, a `ComputerAppEnvironment` class its env.toml's `module` names, read at put by the same `_declare`, which declares each OS with its own decorator (`@linux(...)`, or `@install(os_type, spec)` for any OS type; `@linux(distribution=...)` or `@install("/", spec)` for one distribution, which replaces its OS type's there) holding its install, its launch and, optionally, a skill and settings and build settings that add to the class's there, and install steps and ready checks for every OS or those their `os=` names; everything that reads its settings, launch or steps asks for the computer's OS type; deployed alone it is a one-app computer env, a `ComputerEnv` of the computer and driver its env.toml names, the composer installing it under `/opt/agentenv/apps//` with its services as supervisord programs beside its own server (`agentenv_protocol.computer.serve_app`, in a venv with the protocol's `app` extra, on `.agentenv/server.sock` in its folder), which adds its launcher, launches a `boot` app once per boot and says through `computer-app/v1` whether it's ready, as the driver's `computer-ready` asks, its card named after it, and `env_params` `app_settings` handing it values of its `Settings` in `AGENTENV_APP_SETTINGS`, a secret one only as a bare reference, over its env.toml's `[settings]` and a computer env's `settings` beside it in `apps`, each by top-level field (`params.app_settings`; checked at put, a required one left to the deploy; the deploy record keeps the merged values as `app_settings`, secrets withheld); its env.toml's `build_settings`, values of its `BuildSettings` checked at put, are written to `.agentenv/build_settings.json` in its folder, so they're part of the image's key, its install steps read them, and a deploy can't change them). An env type whose services take environment data defines `load_environment_artifact` and `load_environment_universe_artifact`; `load_artifact` refuses an environment artifact aimed at a type that doesn't, at save and before it loads. An env built on the protocol SDK can serve agents a skill (`@agent_skill`, `skill/v1`), which `add_skills` `env_ids` installs under its environment name (behind a gateway, each server's under that server's name); a skill with files beside its SKILL.md goes as a bundle stored once per content under `agentenv-env-skills/` in the object store. `env/gateway/gateway.py` is the env-side gateway server, container-only code. Its site route, `/website//`, never forwards a site's data plane or card (`/agentenv/…`, `/.well-known/agent-env.json`), which agentenv reaches through `/svc//`; a site with no frontend image is served by its server image there. Its CDP route, `/browser//cdp//` (`env/gateway/browser_proxy.py`), reaches the website browser behind the `/svc` child ``, found from that child's card (it, or an app among its children, offering `browser/v1` `cdp` with `/cdp/` in its `computer-app/v1` `agent_routes`): it relays the browser's discovery and WebSocket both ways, each leg pinged every 20 s, and every CDP address the browser hands out, in `json/version`, `json/list` and its `cdp` answer through `/svc//`, comes back at the gateway, everything before `/cdp//` becoming the address the caller reached the gateway at and `/browser/`; each connection is a `browser_connection` trajectory event, without its token. While its clock is armed it serves agents `get_time` (`env/gateway/get_time.py`), which answers in UTC and, given an IANA `time_zone`, also in that zone; executors' working `hours` (`env/gateway/triggers.py`) take a fixed `utc_offset` or an IANA `time_zone`. | +| `env/` | `Env` base class: a subclass declares `type`, implements `from_dict` and `async deploy(...) -> DeployedEnv`, and may override `reset`, `close` and `load_file_artifact_universe`; teardown closes each env a run deployed before terminating its sandboxes by id. Built-ins in `env/envs/`: `MCPServerEnv` (`mcp_server`), `MultiEnv` (`multi`, MCP servers plus websites), `ServiceDBEnv` (`service_db`, PostgreSQL), `WebsiteEnv` (`website`, one site: a server image, `backend_image` in its env.toml, serving the pages and API, ready when its card's `website/v1` `ready` answers a `WebsiteReadiness` that says so, as `agentenv_protocol.website` (`website_readiness`, `env_readiness`) defines a website env's health for the deploy and `DeployedEnv.readiness()` alike, or, for a card without `ready`, when its `health` path answers 200, which is deprecated; the gateway container's `env/gateway/constants.py` mirrors the URI and op, a test keeping them equal; a separate frontend image is deprecated, warned of at put and optional in a bundle, and a stored `backend_docker_image_artifact` reads as the server image; a live one, `live = true`, is a real site whose server serves none of its pages, its `live`, `domains` and `allow_hosts` read at put from its server's `@website(...)` (`utils/card_naming.py` `WebsiteSource`) unless given, its `settings`' secrets only as references the gateway resolves as it writes the site's container (`WEBSITE_SETTINGS_ENV`); the gateway gives it no site route, a computer's site table leaves it out, a computer whose network is an allowlist is let reach its names (`MultiEnv.live_hosts`), a browser alone's forward port takes them straight to the real site (`site_proxy.py` `direct`), as the headless base's stand-in proxy takes any host, refusing nothing, each proxy logging each such connection as a JSON line, and a run that deploys one is marked live, `reproducible: false`), `GatewayEnv` (`gateway_server`), `ComputerDriverEnv` (`computer_driver`, `env/envs/computer/driver_env.py`: a computer driver's files and what its class declares for each platform it supports, read at put in a process of its own, `agentenv_protocol.computer._declare`, where the driver's own dependencies needn't be installed, with the `port` it serves on and its install by OS, `[install.]`, or for one distribution `[install."/"]`, which replaces its OS type's there, as its `known_failures` do (`env/envs/computer/utils/install.py`): files, apt `packages` with extra `sources`, a `python` requirements file for a venv, `downloads` by URL and sha256, `run` scripts, the services the base's init runs, and `ports`; it deploys only as a computer env's driver), `ComputerEnv` (`computer`, in `env/envs/computer/`: a base computer and a driver, matched at put by gate 1 before anything is stored, then composed by `composer.py` into an image for each form and arch on a builder of the put's sandbox provider, by default the env's (`utils/builder.py`; `--sandbox` on `env build`, `bundle register` and `run`): this machine's Docker for `local`, else a VM of the provider, which fetches the put's stored files by signed URL, takes the rest inline and pushes to the image store itself, so it builds only with stores a remote VM reaches; each arch goes to the first link that runs it natively, else one that emulates it (a VM registers QEMU; `--no-emulate` leaves it `skipped` instead, for a later put), and its pair records the provider that built it (`builder`); a run or register that reuses a computer env composes what it has left (`pending`), a run only the forms its sandbox's deploys boot (`booted_forms`: on each link, the first form it boots natively, by the deploy's own rule, `boot.first_native`; a compose of a disk adds its container), and a deploy composes first what it boots that no compose started, and a container it runs natively that was built but not probed, as one whose probe a setting that didn't resolve stopped, booting it only once ready (`ComputerEnv.compose_unstarted`); tagged `agentenv-computers:` in the image store by a key of the image's Dockerfile, build context and lock so any env with the same inputs reuses it; every layer's packages, requirements and downloads are resolved at put in a container of the base for each arch (`utils/packages.py`, `utils/resolve_in_base.py`), stored in the object store by sha256 under `agentenv-packages/`, and listed in a lock stored by a digest of its inputs, which any env with the same inputs reuses, and builds install only the lock's files, every install step `RUN --network=none`; each pair's status goes into its metadata, `composed`, the arches built are written as its computer, a `computer` artifact over one index, and `agent-env env build` resumes what a put left; gate 3 (`utils/probing.py`) then boots each native build on the put's sandbox provider and probes it at `full`, making it `ready` or `failed`, and a build no provider runs natively stays `unverified`. Its deploy, through the `computer` env provider (`providers/env_providers/env_computer_provider.py`), boots its first `ready` build on a sandbox provider that runs its arch natively (`native_platforms()`), with no gateway in front and the driver's card as the env card, its `/dev/shm` the most its apps' Linux installs ask for (`shm_mb`, in MiB, which its composed computer and the deploy record keep; `--shm-size` on a container, `AGENTENV_SHM_MB` read by a Linux disk's init, passed only to a provider whose `SHARED_MEMORY` says it gives that much, else the sandbox's default, which the deploy logs), then checks its `display`; `deploy_env` `env_params` take a `form`, `computer_settings` (deploy-phase values of the base's settings; a secret one, here or on the env, only as a bare `secret:`/`env:` reference) and `driver_settings` (the driver's `ComputerSettings`, such as its default coordinate space); a `network` that allows nothing runs it on local Docker's internal network behind a relay (`providers/sandbox_providers/local_docker/relay/`); `reset` and `close` have the driver end its recordings first (`computer-recording/v1` `end_all`, each uploading what's left and its manifest under the grant it started with), and `reset` then boots a fresh container in its place (`reset_container`), keeping the computer's address but not an agent's MCP session to the old driver; its `load_file_artifact_universe` writes files under the session user's home through the driver's `data/add`, `Downloads` by default, each file's bytes through a read grant when the store's grants reach it and its network allows everything, else inline (`env/envs/computer/utils/inside.py`)), `ComputerAppEnv` (`computer_app`, `env/envs/computer/app_env.py`: an app on a computer, a `ComputerAppEnvironment` class its env.toml's `module` names, read at put by the same `_declare`, which declares each OS with its own decorator (`@linux(...)`, or `@install(os_type, spec)` for any OS type; `@linux(distribution=...)` or `@install("/", spec)` for one distribution, which replaces its OS type's there) holding its install, its launch and, optionally, a skill and settings and build settings that add to the class's there, and install steps and ready checks for every OS or those their `os=` names; everything that reads its settings, launch or steps asks for the computer's OS type; deployed alone it is a one-app computer env, a `ComputerEnv` of the computer and driver its env.toml names, the composer installing it under `/opt/agentenv/apps//` with its services as supervisord programs beside its own server (`agentenv_protocol.computer.serve_app`, in a venv with the protocol's `app` extra, on `.agentenv/server.sock` in its folder), which adds its launcher, launches a `boot` app once per boot and says through `computer-app/v1` whether it's ready, as the driver's `computer-ready` asks, its card named after it, and `env_params` `app_settings` handing it values of its `Settings` in `AGENTENV_APP_SETTINGS`, a secret one only as a bare reference, over its env.toml's `[settings]` and a computer env's `settings` beside it in `apps`, each by top-level field (`params.app_settings`; checked at put, a required one left to the deploy; the deploy record keeps the merged values as `app_settings`, secrets withheld); its env.toml's `build_settings`, values of its `BuildSettings` checked at put, are written to `.agentenv/build_settings.json` in its folder, so they're part of the image's key, its install steps read them, and a deploy can't change them). An env type whose services take environment data defines `load_environment_artifact` and `load_environment_universe_artifact`; `load_artifact` refuses an environment artifact aimed at a type that doesn't, at save and before it loads. An env built on the protocol SDK can serve agents a skill (`@agent_skill`, `skill/v1`), which `add_skills` `env_ids` installs under its environment name (behind a gateway, each server's under that server's name); a skill with files beside its SKILL.md goes as a bundle stored once per content under `agentenv-env-skills/` in the object store. `env/gateway/gateway.py` is the env-side gateway server, container-only code. Its site route, `/website//`, never forwards a site's data plane or card (`/agentenv/…`, `/.well-known/agent-env.json`), which agentenv reaches through `/svc//`; a site with no frontend image is served by its server image there. Its CDP route, `/browser//cdp//` (`env/gateway/browser_proxy.py`), reaches the website browser behind the `/svc` child ``, found from that child's card (it, or an app among its children, offering `browser/v1` `cdp` with `/cdp/` in its `computer-app/v1` `agent_routes`): it relays the browser's discovery and WebSocket both ways, each leg pinged every 20 s, and every CDP address the browser hands out, in `json/version`, `json/list` and its `cdp` answer through `/svc//`, comes back at the gateway, everything before `/cdp//` becoming the address the caller reached the gateway at and `/browser/`; each connection is a `browser_connection` trajectory event, without its token. While its clock is armed it serves agents `get_time` (`env/gateway/get_time.py`), which answers in UTC and, given an IANA `time_zone`, also in that zone; executors' working `hours` (`env/gateway/triggers.py`) take a fixed `utc_offset` or an IANA `time_zone`. | | `artifact/` | Immutable versioned artifacts; every `put()` creates a new version. The document goes to the document store, binary payloads to the object store. Every artifact but an environment universe, whose `metadata` key holds its legacy metadata file ids, carries a `metadata` dict outside its versioned fields, changed by compare-and-swap (`merge_metadata`). Built-ins: `CliArtifact` (`cli`), `ComputerArtifact` (`computer`, a base computer: its architectures, OS, session user, settings and container form, whose image `put` reads from its registry into `metadata.validated_images`), `FileArtifact` (`file`), `FileArtifactUniverse` (`file_artifact_universe`), `DockerImageArtifact` (`docker_image`), `EnvironmentArtifact` (`environment`: its environment's files by path, stored as the one wrapped `file_artifact_ref` every environment artifact had when it has one file at its filename, else as `file_artifact_refs`; `get_file_artifacts()` reads either, and a load hands each file to the env's `data/add` with its path, which a pre-v1 load, taking one file, refuses), `EnvironmentUniverseArtifact` (`environment_universe`), `SkillArtifact` (`skill`); `[artifacts] type_aliases` maps another stored spelling of a type onto one of them. A `DockerImageArtifact` holds a tar.gz of its image, or, written by `put_ref` (`--image-ref` on `env mcp-server put` and `a2a-agent put`), only a registry reference with its tag pinned to a digest, which sandboxes pull, or a published image's tag with its `image_digest` and its build context, which sandboxes pull by the digest and build from the context when they can't (the infra images, and a bundle agent built from a shipped agent's sources, `Plan.published`), or, written by `put_context`, only its build context, which a VM sandbox builds before running it (`VmSandbox.load_docker_images`) and Modal builds before creating its container (`ModalSandboxProvider.prepare_image`, outside a fallback chain's deadline); anything else that runs images by name (a container on the local provider, a lone server on any provider but Modal) refuses such an image. A build context (`utils/docker/context.py`) is what `docker build` would send, `.dockerignore` applied, written so equal files give equal bytes and an equal `source_digest`. | | `task/`, `task_step/` | A `Task` holds its `TaskStep`s inline; `Task.run()` executes them as a DAG. `depends_on` (None means all prior steps) gates a step, independent steps run concurrently, `fail_task_on_error` makes a failure fatal or tolerated, `retry_config` rolls a failed span back through the step journal and re-dispatches it. Built-in steps live in `task_step/task_steps/` (`deploy_env`, `deploy_agent`, `prompt_agent`, the verifiers under `verifiers/`, among them `start_computer_recording` and `end_computer_recording`, and more). On an env whose card serves computer-exec/v1 (a computer), `run_code` with `env_id` and `run_container_unit_tests_verifier` with `env_id` run inside it as its session user through the driver: files staged with `data/add` and read with `data/get`, commands run with `run`; `env_outcome_verifier` hands a `verify` that takes an `env_url` keyword the env's address. A computer's framework-only routes (everything but MCP and the card) take the per-deploy control token its provider boots it with (`DeployedEnv.control_token`, `AGENTENV_CONTROL_TOKEN`); framework calls send `deployed.control_headers`, `Task.run` runs every step under `protocol_v1.authorized` with each deployed env's (a `verify` included), and a `run_code` script inside the computer gets `input["env"]["headers"]`. `teardown_run` and the `teardown_sandboxes` step share one core (`task/teardown.py` `tear_down`): the recordings the run left open end with fresh grants, each env still up closes (`Env.close`; a computer env ends any recording still open), what stayed open is registered from its namespace, then every sandbox is terminated by id. `task/episode/` runs a task with some of its agents played from outside, as a training loop does (`EpisodeSpec`). | | `store/` | Four store ABCs with local and cloud implementations: `DocumentStore` (SQLite, MongoDB, DynamoDB, Firestore with MongoDB compatibility), `ObjectStore` (filesystem, S3, Cloud Storage), `ImageStore` (local OCI registry, ECR), `SecretStore` (env vars or file, AWS Secrets Manager, Google Cloud Secret Manager). `VersionedEntityStore` implements the shared versioned get/put logic, `QueryBuilder` is the immutable chained query API, `store/base.py` holds the error types. `store/routing.py` is the local namespaces, `@local` and `@hf` (`is_local_id`), each with a documents file of its own: while the CLI or a `build_app` server runs, a local id and what a local task's run records go to local stores beside the configured ones, and that run's writes to the configured stores are refused. A new backend must pass the conformance kits in `tst/store/`. | diff --git a/src/agent_env/computers/README.md b/src/agent_env/computers/README.md index 6629461ef..c0614ac3c 100644 --- a/src/agent_env/computers/README.md +++ b/src/agent_env/computers/README.md @@ -14,13 +14,10 @@ building it. Name it from another bundle as `ubuntu-24.04`, the alias `aliases.t (`display = "none"`), for one website browser, which runs headless there, and the `page` driver, whose screen is the browser's page: a browser computer, which agentenv's `chromium` deploys alone as. supervisord runs every program as the session user, among them a stand-in site proxy on `127.0.0.1:3128`, the browser's only route out when the computer -routes no sites. It forwards a request or a `CONNECT` tunnel to any host, never decrypting a tunnel, but only to an -address it resolved and checked itself: one that isn't globally routable (loopback, private, link-local and the -metadata service, their IPv6 equivalents) is refused at once with a 403, whether a page or an agent driving the browser -names it, and each connection and refusal is a JSON line in `/var/log/agentenv/site-proxy.log`. The sandbox's network -policy still applies beneath it. Its `own_loopback` setting (default `false`) lets the browser reach the computer's own -loopback, as a test that serves its pages inside the computer needs; a probe boots with it on, since the conformance fixture it serves is on that loopback, and so must a conformance run against a deployed browser computer. A computer that routes sites names the shared site proxy's forward port, `127.0.0.1:3129`, to the -browser instead: it serves the run's sites, refuses every other host and runs out of the agent's reach. It has a +routes no sites. It forwards a request or a `CONNECT` tunnel to any host, never decrypting a tunnel and refusing +nothing: the sandbox's network policy is all that limits it. Each connection, and each one that fails, is a JSON line in +`/var/log/agentenv/site-proxy.log`. A computer that routes sites names the shared site proxy's forward port, +`127.0.0.1:3129`, to the browser instead: it serves the run's sites, refuses every other host and runs out of the agent's reach. It has a container form only. Name it from another bundle as `ubuntu-24.04-headless`, or by its id, `@local/agentenv-framework/computers/ubuntu-24.04-headless`. diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/artifact.toml b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/artifact.toml index 7896fe4c8..dfcd4d670 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/artifact.toml +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/artifact.toml @@ -12,7 +12,3 @@ input_channel = "env" [config.timezone] default = "UTC" phase = "deploy" - -[config.own_loopback] -default = false -phase = "deploy" diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py index c1f32c3e4..09bbcc391 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py @@ -1,14 +1,9 @@ """A stand-in for the shared site proxy in this base, on 127.0.0.1:3128: it forwards every request the browser sends -it, absolute-form HTTP and CONNECT tunnels alike, to the host it names, never decrypting a tunnel. It resolves each -name itself and connects only to an address it checked: one that isn't globally routable (loopback, private, -link-local, the metadata service, their IPv6 equivalents) is refused at once, so neither a page nor an agent driving -the browser reaches the computer's own network. The computer's own loopback is reached only when its deploy sets the -base's `own_loopback` setting, as a test serving its pages inside the computer does. Each connection is logged as a -JSON line when it ends, and each refusal with its reason.""" +it, absolute-form HTTP and CONNECT tunnels alike, to the host it names, refusing nothing and never decrypting a tunnel. +The sandbox's own network policy still decides where a connection can go. Each connection is logged as a JSON line +when it ends, and each one that fails with its reason.""" import http.server -import ipaddress import json -import os import selectors import socket import socketserver @@ -20,36 +15,6 @@ CONNECT_TIMEOUT_S = 30 IDLE_S = 300 HOP = {"connection", "keep-alive", "proxy-connection", "proxy-authorization"} -NAT64 = ipaddress.ip_network("64:ff9b::/96") -#: Whether the deploy lets the browser reach this computer's own loopback (the base's `own_loopback` setting). -OWN_LOOPBACK = os.environ.get("AGENTENV_SETTING_OWN_LOOPBACK", "").lower() in ("true", "1") - - -def allowed(address: str) -> bool: - """Whether the proxy may connect to ``address``: a globally routable one, not multicast, an IPv6 address that - carries an IPv4 one (mapped, 6to4 or NAT64) judged as that IPv4 address.""" - ip = ipaddress.ip_address(address.split("%", 1)[0]) - if isinstance(ip, ipaddress.IPv6Address): - if ip.ipv4_mapped is not None: - ip = ip.ipv4_mapped - elif ip.sixtofour is not None: - ip = ip.sixtofour - elif ip in NAT64: - ip = ipaddress.IPv4Address(int(ip) & 0xFFFFFFFF) - return ip.is_global and not ip.is_multicast - - -def _loopback(address: str) -> bool: - ip = ipaddress.ip_address(address.split("%", 1)[0]) - return ip.is_loopback or isinstance(ip, ipaddress.IPv6Address) and bool(ip.ipv4_mapped and ip.ipv4_mapped.is_loopback) - - -def resolve(host: str, port: int) -> tuple[list[str], list[str]]: - """The addresses ``host`` resolves to that the proxy may connect to, in the resolver's order, and those it may - not; raises OSError when it doesn't resolve.""" - found = list(dict.fromkeys(info[4][0] for info in socket.getaddrinfo(host, port, type=socket.SOCK_STREAM))) - usable = [address for address in found if allowed(address) or OWN_LOOPBACK and _loopback(address)] - return usable, [address for address in found if address not in usable] def log(**fields) -> None: @@ -90,23 +55,18 @@ def forward(self): do_GET = do_POST = do_PUT = do_PATCH = do_DELETE = do_HEAD = do_OPTIONS = forward def _open(self, host: str, port: int, method: str): - """A connection to an address ``host`` resolves to that the proxy may reach, tried in turn within one - ``CONNECT_TIMEOUT_S`` for them all, or None once the browser has its answer: a 403 for a host whose every - address is refused, a 502 for one that doesn't resolve or answer. The connection keeps an ``IDLE_S`` timeout, - so a write to a peer that stopped reading ends too.""" + """A connection to an address ``host`` resolves to, tried in turn within one ``CONNECT_TIMEOUT_S`` for them + all, or None once the browser has its answer: a 502 for a host that doesn't resolve or answer. The connection + keeps an ``IDLE_S`` timeout, so a write to a peer that stopped reading ends too.""" deadline = time.monotonic() + CONNECT_TIMEOUT_S try: - usable, refused = resolve(host, port) + found = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM) + addresses = list(dict.fromkeys(info[4][0] for info in found)) except OSError as error: - log(refused={"method": method, "host": host, "port": port, "reason": f"doesn't resolve: {error}"}) + log(failed={"method": method, "host": host, "port": port, "reason": f"doesn't resolve: {error}"}) self._answer(502, f"{host} doesn't resolve\n") return None - if not usable: - log(refused={"method": method, "host": host, "port": port, "reason": "internal address", - "addresses": refused}) - self._answer(403, f"{host} is an internal address, which the browser never reaches\n") - return None - for address in usable: + for address in addresses: if (left := deadline - time.monotonic()) <= 0: break try: @@ -116,7 +76,7 @@ def _open(self, host: str, port: int, method: str): return upstream except OSError: continue - log(refused={"method": method, "host": host, "port": port, "reason": "no answer", "addresses": usable}) + log(failed={"method": method, "host": host, "port": port, "reason": "no answer", "addresses": addresses}) self._answer(502, f"{host} didn't answer\n") return None diff --git a/src/agent_env/env/envs/computer/utils/probing.py b/src/agent_env/env/envs/computer/utils/probing.py index 62ddbe701..56c931137 100644 --- a/src/agent_env/env/envs/computer/utils/probing.py +++ b/src/agent_env/env/envs/computer/utils/probing.py @@ -32,9 +32,6 @@ _PROBE_TTL_S = 1800 #: How long the probe at full may take on a computer that booted: about ten seconds on a healthy one. _PROBE_TIMEOUT_S = 120 -#: Base settings a probe boots with, where the base declares them: a browser alone reaches its computer's own loopback -#: only with ``own_loopback``, and the probe serves its conformance fixture there. A probe runs no agent. -_PROBE_SETTINGS = {"own_loopback": True} #: How many probes, each on a fresh boot, a build gets when each ends with the connection to its driver dropped, refused #: or timed out, which says nothing of the build, before the last one's result is recorded. _PROBE_ATTEMPTS = 3 @@ -155,16 +152,13 @@ def _attempted(attempts: list[dict], result: dict) -> dict: async def _probe(links: list[SandboxProvider], env: ComputerEnv, driver: ComputerDriverEnv, image: str, form: str = "container") -> tuple[ProbeReport, dict, str]: """``image``, of ``form``, booted at the env's settings and its apps' defaults with a control token of its own, as - a deploy with no ``env_params`` boots it, but for the base's ``_PROBE_SETTINGS``, on the first of ``links`` that - boots it, probed at ``full`` with that token within ``_PROBE_TIMEOUT_S`` and torn down; the report, the driver's - card and the provider's name. Raises Unprobed, booting nothing, when a setting it boots with doesn't resolve - here.""" + a deploy with no ``env_params`` boots it, on the first of ``links`` that boots it, probed at ``full`` with that + token within ``_PROBE_TIMEOUT_S`` and torn down; the report, the driver's card and the provider's name. Raises + Unprobed, booting nothing, when a setting it boots with doesn't resolve here.""" token = boot.control_token() - values = {**env.computer_values(), **{name: value for name, value in _PROBE_SETTINGS.items() - if name in env.computer.config}} try: environment = { - **boot.settings_environment(env.computer, values, environment_name=env.computer_name), + **boot.settings_environment(env.computer, env.computer_values(), environment_name=env.computer_name), **boot.app_settings_environment(env.composed_apps(), env.app_defaults(), platform_of(env.computer.os))} except ConfigError as e: # an env: or secret: reference that doesn't resolve here, or no secret store to resolve it raise Unprobed(f"a setting it boots with didn't resolve here: {e}") from e diff --git a/src/agent_env/env/envs/computer/utils/site_proxy.py b/src/agent_env/env/envs/computer/utils/site_proxy.py index 6866b09c2..085b615ab 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -85,7 +85,6 @@ RESTART_DELAY_S = 1.0 SERVE_USER = "nobody" FAILED = (("X-Agentenv-Site-Proxy", "failed"),) -NAT64 = ipaddress.ip_network("64:ff9b::/96") class BadRequest(Exception): @@ -107,23 +106,8 @@ def matches(pattern: str, host: str) -> bool: return host == pattern -def allowed(address: str) -> bool: - """Whether the proxy may connect to ``address`` for a host outside the site table: a globally routable one, not - multicast, an IPv6 address that carries an IPv4 one (mapped, 6to4 or NAT64) judged as that IPv4 address. The base - computers' stand-in proxies judge addresses the same way.""" - ip = ipaddress.ip_address(address.split("%", 1)[0]) - if isinstance(ip, ipaddress.IPv6Address): - if ip.ipv4_mapped is not None: - ip = ip.ipv4_mapped - elif ip.sixtofour is not None: - ip = ip.sixtofour - elif ip in NAT64: - ip = ipaddress.IPv4Address(int(ip) & 0xFFFFFFFF) - return ip.is_global and not ip.is_multicast - - def log_event(**fields) -> None: - """One JSON line in the proxy's log: a connection to a host outside the site table once it ends, or a refusal.""" + """One JSON line in the proxy's log: a connection to a live site's host once it ends, or one that failed.""" print(json.dumps(fields), file=sys.stderr, flush=True) @@ -481,41 +465,18 @@ async def _tunnel(self, reader: asyncio.StreamReader, writer: asyncio.StreamWrit @staticmethod async def _direct(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, host: str, port: int, head: Optional[bytes] = None) -> None: - """``host``, a live site's, reached straight at ``port``, at an address the proxy resolved and checked itself - (``allowed``), never resolved again: a tunnel, answered as established, for a ``CONNECT``, else ``head`` sent - on; then both ends relayed until either closes, and the connection logged (``log_event``). The lookup and every - address's connection attempt share one ``UPSTREAM_TIMEOUT_S``. A host whose every address is internal is - refused at once, one that doesn't resolve or answer in time is a 502.""" - method = "CONNECT" if head is None else head.split(b" ", 1)[0].decode("latin-1") - loop = asyncio.get_running_loop() - deadline = loop.time() + UPSTREAM_TIMEOUT_S + """``host``, a live site's, reached straight at ``port``: a tunnel, answered as established, for a ``CONNECT``, + else ``head`` sent on; then both ends relayed until either closes, and the connection logged (``log_event``). + One that doesn't resolve or answer within ``UPSTREAM_TIMEOUT_S``, the lookup and every address's attempt + together, is a 502, logged too.""" try: - found = await asyncio.wait_for(loop.getaddrinfo(host, port, type=socket.SOCK_STREAM), UPSTREAM_TIMEOUT_S) + up_reader, up_writer = await asyncio.wait_for(asyncio.open_connection(host, port), UPSTREAM_TIMEOUT_S) except (OSError, asyncio.TimeoutError) as e: - log_event(refused={"method": method, "host": host, "port": port, - "reason": f"doesn't resolve: {e or type(e).__name__}"}) - writer.write(simple_response("502 Bad Gateway", f"{host} doesn't resolve\n", FAILED)) - return - addresses = list(dict.fromkeys(info[4][0] for info in found)) - if not (usable := [address for address in addresses if allowed(address)]): - log_event(refused={"method": method, "host": host, "port": port, "reason": "internal address", - "addresses": addresses}) - writer.write(simple_response("403 Forbidden", f"{host} is an internal address, which the browser never " - "reaches\n", FAILED)) - return - for address in usable: - if (left := deadline - loop.time()) <= 0: - continue - try: - up_reader, up_writer = await asyncio.wait_for(asyncio.open_connection(address, port), left) - break - except (OSError, asyncio.TimeoutError): - continue - else: - log_event(refused={"method": method, "host": host, "port": port, "reason": "no answer", - "addresses": usable}) - writer.write(simple_response("502 Bad Gateway", f"{host}:{port} didn't answer\n", FAILED)) + log_event(failed={"method": "CONNECT" if head is None else head.split(b" ", 1)[0].decode("latin-1"), + "host": host, "port": port, "reason": str(e) or type(e).__name__}) + writer.write(simple_response("502 Bad Gateway", f"{host}:{port} didn't answer: {e}\n", FAILED)) return + address = (up_writer.get_extra_info("peername") or ("",))[0] started, began, counts = time.time(), time.monotonic(), [len(head or b""), 0] if head is None: writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n") diff --git a/tst/integration/computer/browser_computer_test.py b/tst/integration/computer/browser_computer_test.py index d1083f925..a28d3ffec 100644 --- a/tst/integration/computer/browser_computer_test.py +++ b/tst/integration/computer/browser_computer_test.py @@ -106,9 +106,6 @@ """ PAGE_URL = "data:text/html," + urllib.parse.quote(PAGE) SITE = "http://127.0.0.1:8771/" -#: What a deploy sets so the browser reaches the pages these tests serve on its own loopback, which its proxy refuses -#: otherwise. -OWN_LOOPBACK = {"computer_settings": {"own_loopback": True}} # Serves, on loopback for the seconds it's given, a home page with a button that asks to confirm and a file input, each # saying in the title what came of it, and two more pages. SERVE_SITE = """import http.server, sys, threading, time @@ -172,7 +169,7 @@ def test_gate_3_makes_this_machines_arch_ready_with_the_page_driver_its_input_an async def test_its_tools_see_and_drive_the_page_that_is_its_screen(browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [PAGE_URL]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [PAGE_URL]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _state(deployed, lambda s: True) @@ -217,7 +214,7 @@ async def test_its_tools_see_and_drive_the_page_that_is_its_screen(browser_compu async def test_a_killed_app_server_or_driver_comes_back_and_a_killed_browser_fails_it_for_good(browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [PAGE_URL]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [PAGE_URL]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _kill(deployed, "[s]erve_app") @@ -249,7 +246,7 @@ async def test_an_agent_navigates_moves_in_history_answers_a_dialog_uses_tabs_an browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [SITE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: serving = asyncio.ensure_future(inside.run(deployed, ["python3", "-c", SERVE_SITE, "900"], timeout_s=960)) @@ -309,7 +306,7 @@ async def test_an_agent_navigates_moves_in_history_answers_a_dialog_uses_tabs_an @pytest.mark.asyncio async def test_its_cookies_local_storage_and_downloads_are_read_and_set_through_browser_v1(browser_computer): with namespace_routing(): - deployed = await Env.get(browser_computer.id).deploy(env_params=OWN_LOOPBACK) + deployed = await Env.get(browser_computer.id).deploy() context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -327,7 +324,7 @@ async def test_its_cookies_local_storage_and_downloads_are_read_and_set_through_ async def test_its_state_operations_hold_up_at_once_through_a_server_restart_and_fail_at_once_with_the_browser_gone( browser_computer): with namespace_routing(): - deployed = await Env.get(browser_computer.id).deploy(env_params=OWN_LOOPBACK) + deployed = await Env.get(browser_computer.id).deploy() context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -348,7 +345,7 @@ async def test_its_state_operations_hold_up_at_once_through_a_server_restart_and async def test_its_data_plane_signs_it_in_snapshots_it_resets_it_and_loads_a_snapshot_back_whole(browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_SITE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [DATA_SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -374,7 +371,7 @@ async def test_its_task_steps_set_cookies_and_storage_open_a_tab_behind_and_acti browser_computer): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_SITE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [DATA_SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -433,7 +430,7 @@ async def test_each_start_tabs_page_sees_the_windows_size_from_its_first_script_ urls = [SIZED.format(index) for index in range(3)] with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": urls}}}) + env_params={"app_settings": {"chromium": {"start_urls": urls}}}) context = TaskStepContext(deployed_envs=[deployed]) try: tabs = await tabs_of(deployed) @@ -452,7 +449,7 @@ async def test_each_start_tabs_page_sees_the_windows_size_from_its_first_script_ async def test_its_data_calls_take_turns_a_server_restart_takes_a_load_and_with_the_browser_gone_they_fail_at_once( browser_computer): with namespace_routing(): - deployed = await Env.get(browser_computer.id).deploy(env_params=OWN_LOOPBACK) + deployed = await Env.get(browser_computer.id).deploy() context = TaskStepContext(deployed_envs=[deployed]) try: await _ready(deployed) @@ -689,32 +686,26 @@ async def test_routing_sites_beside_live_ones_its_forward_port_takes_the_live_ho @pytest.mark.asyncio -async def test_alone_on_the_open_web_it_loads_a_public_site_and_never_an_internal_address_a_page_or_an_agent_names( +async def test_alone_on_the_open_web_it_loads_a_public_site_through_its_proxy_and_an_agents_tab_and_logs_each_connection( browser_computer): - internal = ["http://169.254.169.254/latest/meta-data/", "https://10.0.0.1/", "http://127.0.0.1:18765/", - "https://[::1]/"] + public, nowhere = "https://github.com/", "https://agentenv-no-such-host.invalid/" with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: - through = {url: await _through_port(deployed, url, "http://127.0.0.1:3128") - for url in ["https://github.com/", *internal]} - seen = await _cdp_agent_loads((await _cdp(deployed, "ada"))["ws_url"], ["https://github.com/", *internal]) + through = {url: await _through_port(deployed, url, "http://127.0.0.1:3128") for url in (public, nowhere)} + seen = await _cdp_agent_loads((await _cdp(deployed, "ada"))["ws_url"], [public]) logged = _as_root(await _container_of(deployed), "cat /var/log/agentenv/site-proxy.log") finally: report = await teardown_run(context) - assert through["https://github.com/"].startswith("200 "), through - assert all(through[url].startswith("error") and "403" in through[url] for url in internal), through - assert seen["https://github.com/"]["title"].startswith("GitHub") and not seen["https://github.com/"]["error"], seen - for url in internal: - assert (seen[url]["error"] or "").startswith("net::ERR_TUNNEL_CONNECTION_FAILED") or ( - "is an internal address" in seen[url].get("text", "")), (url, seen[url]) + assert through[public].startswith("200 ") and "502" in through[nowhere], through + assert seen[public]["title"].startswith("GitHub") and not seen[public]["error"], seen entries = [json.loads(line) for line in logged.splitlines() if line.startswith("{")] - assert any(entry.get("connection", {}).get("host") == "github.com" for entry in entries), logged[-2000:] - assert {entry["refused"]["host"] for entry in entries if "refused" in entry} >= { - "169.254.169.254", "10.0.0.1", "127.0.0.1", "::1"}, logged[-2000:] + github = [entry["connection"] for entry in entries if entry.get("connection", {}).get("host") == "github.com"] + assert github and all(entry["bytes_received"] > 0 for entry in github), logged[-2000:] + assert any(entry.get("failed", {}).get("host") == "agentenv-no-such-host.invalid" for entry in entries), logged[-2000:] assert not report.failed @@ -740,7 +731,7 @@ async def test_playwright_and_puppeteer_drive_it_through_its_cdp_address_which_a browser_computer, cdp_clients): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [SITE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: serving = asyncio.ensure_future(inside.run(deployed, ["python3", "-c", SERVE_SITE, "900"], timeout_s=960)) @@ -792,7 +783,7 @@ async def test_browser_connect_cdp_hands_an_agent_its_address_with_the_tools_hid with namespace_routing(): agent = put_test_agent(f"cdp-agent-{uuid.uuid4().hex[:8]}") deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [SITE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) serving = None try: @@ -834,7 +825,7 @@ async def test_a_cdp_agent_keeps_its_address_through_an_app_server_and_a_driver_ with namespace_routing(): agent = put_test_agent(f"cdp-chaos-{uuid.uuid4().hex[:8]}") deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: await _connected(context, agent, browser_computer.id, "solver") @@ -858,7 +849,7 @@ async def test_two_cdp_agents_drive_one_browser_at_once_each_through_an_address_ with namespace_routing(): agent = put_test_agent(f"cdp-pair-{uuid.uuid4().hex[:8]}") deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: for name in ("solver", "buyer"): diff --git a/tst/integration/computer/browser_gateway_test.py b/tst/integration/computer/browser_gateway_test.py index 0201a6554..d163f66b3 100644 --- a/tst/integration/computer/browser_gateway_test.py +++ b/tst/integration/computer/browser_gateway_test.py @@ -31,8 +31,8 @@ from agent_env.store.routing import namespace_routing from agent_env.task.teardown import teardown_run from agent_env.task_step.context import TaskStepContext -from tst.integration.computer.browser_computer_test import (DATA_PAGE, OWN_LOOPBACK, SERVE_SITE, SERVE_START_S, SITE, - _clients, _kill, _ready) +from tst.integration.computer.browser_computer_test import (DATA_PAGE, SERVE_SITE, SERVE_START_S, SITE, _clients, + _kill, _ready) from tst.integration.computer.browser_computer_test import _cdp as _driver_cdp pytestmark = [pytest.mark.integration, pytest.mark.int_test_slow] @@ -58,7 +58,7 @@ async def test_playwright_and_puppeteer_reach_the_browser_through_the_gateway_an browser_computer, cdp_clients, gateway_image): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [SITE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [SITE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: serving = asyncio.ensure_future(inside.run(deployed, ["python3", "-c", SERVE_SITE, "900"], timeout_s=960)) @@ -93,7 +93,7 @@ async def test_a_gateway_restart_or_a_data_reset_closes_a_live_connection_with_a browser_computer, gateway_image): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: async with _gateway(gateway_image, deployed) as (gateway, container): @@ -126,7 +126,7 @@ async def test_behind_the_gateway_a_browser_down_at_first_is_found_once_up_and_a browser_computer, gateway_image): with namespace_routing(): deployed = await Env.get(browser_computer.id).deploy( - env_params={**OWN_LOOPBACK, "app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) + env_params={"app_settings": {"chromium": {"start_urls": [DATA_PAGE]}}}) context = TaskStepContext(deployed_envs=[deployed]) try: token = (await _driver_cdp(deployed, "ada"))["http_url"].split("/cdp/")[1].strip("/") diff --git a/tst/unit/env/envs/test_computer_env.py b/tst/unit/env/envs/test_computer_env.py index 4f3a30036..3679af1f1 100644 --- a/tst/unit/env/envs/test_computer_env.py +++ b/tst/unit/env/envs/test_computer_env.py @@ -30,7 +30,7 @@ from agentenv_protocol.website_browser import BROWSER from click.testing import CliRunner -from agent_env.artifact.artifacts.computer import ComputerArtifact, Setting +from agent_env.artifact.artifacts.computer import ComputerArtifact from agent_env.artifact.artifacts.docker_image import DockerImageArtifact from agent_env.artifact.artifacts.environment import EnvironmentArtifact from agent_env.artifact.artifacts.environment_universe import EnvironmentUniverseArtifact @@ -361,26 +361,6 @@ async def fake_boot(link, **kwargs): probed.assert_awaited_once_with("http://computer", "full", token=booted[0]["token"]) -def test_the_probe_lets_a_browser_alone_reach_its_own_loopback_where_the_base_declares_it_as_its_fixture_is_there( - base, driver, docker, monkeypatch): - env = _put(base, driver, compose=False) - monkeypatch.setitem(env.computer.config, "own_loopback", Setting(default=False, phase="deploy")) - booted = [] - - async def fake_boot(link, **kwargs): - booted.append(kwargs) - return boot.Booted(sandbox=None, url="http://computer", card=CARD) - - monkeypatch.setattr(boot, "boot", fake_boot) - monkeypatch.setattr(boot, "discard", AsyncMock()) - monkeypatch.setattr(probing, "run_probe", AsyncMock(return_value="report")) - - asyncio.run(_probe([SimpleNamespace()], env, driver, "img")) - - assert booted[0]["environment"]["AGENTENV_SETTING_OWN_LOOPBACK"] == "True" - assert "own_loopback" not in env.computer_values() # a deploy still boots at the base's default - - def test_a_build_the_probe_cant_boot_for_a_setting_that_doesnt_resolve_stays_unverified_until_a_compose_where_it_does( base, driver, docker, tmp_path, monkeypatch): monkeypatch.delenv("MAIL_TOKEN", raising=False) diff --git a/tst/unit/env/envs/test_computer_open_web.py b/tst/unit/env/envs/test_computer_open_web.py index 091d45415..37ddd4f70 100644 --- a/tst/unit/env/envs/test_computer_open_web.py +++ b/tst/unit/env/envs/test_computer_open_web.py @@ -1,8 +1,7 @@ """A browser alone on the open web: the headless base's stand-in proxy on 3128, its route out with no site routing, -forwards any host, HTTP and CONNECT alike, without decrypting, but only to an address it resolved and checked itself, -refusing loopback, private, link-local and the other addresses that aren't globally routable; and it logs each -connection and refusal as a JSON line. The shared site proxy judges the addresses of a live site's hosts the same way. -The proxy is a network program, so these tests open sockets, on loopback alone.""" +forwards any host, HTTP and CONNECT alike, without decrypting and refusing nothing, the sandbox's network policy +deciding where it can go; it bounds every wait, and logs each connection, and each one that fails, as a JSON line. The +proxy is a network program, so these tests open sockets, on loopback alone.""" from __future__ import annotations @@ -19,16 +18,11 @@ import pytest import agent_env -from agent_env.env.envs.computer.utils import site_proxy pytestmark = pytest.mark.usefixtures("socket_enabled") STAND_IN = Path(agent_env.__file__).parent / "computers" / "artifacts" / "ubuntu-24.04-headless" / "site-proxy.py" GITHUB = "140.82.112.3" -REFUSED = ["127.0.0.1", "127.8.9.1", "10.0.0.1", "172.16.5.4", "192.168.1.1", "169.254.169.254", "100.64.0.1", - "0.0.0.0", "224.0.0.1", "255.255.255.255", "192.0.2.1", "::1", "::", "fe80::1", "fe80::1%eth0", "fc00::1", - "fd12:3456::1", "ff02::1", "::ffff:10.0.0.1", "::ffff:127.0.0.1", "64:ff9b::a00:1", "2002:a9fe:a9fe::1"] -ALLOWED = [GITHUB, "8.8.8.8", "2606:4700:4700::1111", "::ffff:140.82.112.3", "64:ff9b::8c52:7003"] def _stand_in(): @@ -38,14 +32,6 @@ def _stand_in(): return module -@pytest.mark.parametrize("judge", ["stand-in", "site proxy"]) -def test_both_proxies_refuse_every_address_that_isnt_globally_routable_and_take_the_rest(judge): - allowed = _stand_in().allowed if judge == "stand-in" else site_proxy.allowed - - assert [address for address in REFUSED if allowed(address)] == [] - assert [address for address in ALLOWED if not allowed(address)] == [] - - @pytest.fixture def proxy(monkeypatch): with _serving(monkeypatch, _stand_in()) as serving: @@ -54,12 +40,10 @@ def proxy(monkeypatch): @contextlib.contextmanager def _serving(monkeypatch, module): - """``module``'s stand-in serving on a free port, with ``site.test`` resolving to one of GitHub's addresses and - ``rebound.test`` to an internal one, and connections to an address off loopback landing on a local upstream that - answers each request; yields the port, each address the proxy connected to, what the upstream read, and the - upstream's own port.""" + """``module``'s stand-in serving on a free port, with ``site.test`` resolving to one of GitHub's addresses, and + connections to an address off loopback landing on a local upstream that answers each request; yields the port, + each address the proxy connected to, what the upstream read, and the upstream's own port.""" real_resolve, real_connect = socket.getaddrinfo, socket.create_connection - names = {"site.test": [GITHUB], "rebound.test": ["10.0.0.7"], "mixed.test": ["192.168.0.2", "140.82.112.4"]} upstream, read, opened = socket.create_server(("127.0.0.1", 0)), [], [] def answer(): @@ -73,8 +57,8 @@ def answer(): conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nhello") def getaddrinfo(host, port, *args, **kwargs): - if host in names: - return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, port)) for address in names[host]] + if host == "site.test": + return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (GITHUB, port))] return real_resolve(host, port, *args, **kwargs) def create_connection(address, *args, **kwargs): @@ -102,11 +86,8 @@ def _ask(port: int, request: bytes, then: bytes = b"") -> bytes: answer = conn.recv(65536) if then and answer.startswith(b"HTTP/1.1 200"): conn.sendall(then) - while chunk := conn.recv(65536): - answer += chunk - else: - while chunk := conn.recv(65536): - answer += chunk + while chunk := conn.recv(65536): + answer += chunk return answer @@ -114,46 +95,93 @@ def _logged(text: str, kind: str) -> list[dict]: return [json.loads(line)[kind] for line in text.splitlines() if line.startswith(f'{{"{kind}"')] -@pytest.mark.parametrize("request_line", [ - b"CONNECT 127.0.0.1:18765 HTTP/1.1", b"CONNECT [::1]:443 HTTP/1.1", b"CONNECT 169.254.169.254:80 HTTP/1.1", - b"GET http://10.0.0.1/ HTTP/1.1", b"GET http://169.254.169.254/latest/meta-data/ HTTP/1.1", - b"GET http://rebound.test/ HTTP/1.1", b"CONNECT rebound.test:443 HTTP/1.1", b"GET http://localhost:18765/ HTTP/1.1", -], ids=["connect-loopback", "connect-ipv6-loopback", "connect-metadata", "url-private", "url-metadata", - "name-resolving-internal", "connect-name-resolving-internal", "url-localhost"]) -def test_the_stand_in_refuses_an_internal_address_at_once_whoever_names_it_and_logs_why(proxy, capsys, request_line): - port, opened, read, _ = proxy - - answer = _ask(port, request_line + b"\r\nHost: x\r\n\r\n") - - assert answer.startswith(b"HTTP/1.1 403 ") and b"X-Agentenv-Site-Proxy: failed" in answer, answer - assert b"is an internal address, which the browser never reaches" in answer - assert opened == [] and read == [] - [refused] = _logged(capsys.readouterr().out, "refused") - assert refused["reason"] == "internal address" and refused["addresses"] - - -def test_the_stand_in_tunnels_and_forwards_a_public_host_at_the_address_it_checked_and_logs_each_connection( - proxy, capsys): +def test_the_stand_in_tunnels_and_forwards_a_public_host_and_logs_each_connection(proxy, capsys): port, opened, read, _ = proxy tunnelled = _ask(port, b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n", then=b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n") - forwarded = _ask(port, b"GET http://mixed.test/a?b=1 HTTP/1.1\r\nHost: mixed.test\r\nProxy-Connection: keep-alive" + forwarded = _ask(port, b"GET http://site.test/a?b=1 HTTP/1.1\r\nHost: site.test\r\nProxy-Connection: keep-alive" b"\r\n\r\n") assert tunnelled.startswith(b"HTTP/1.1 200 Connection established") and tunnelled.endswith(b"hello"), tunnelled assert forwarded.startswith(b"HTTP/1.1 200 OK") and forwarded.endswith(b"hello"), forwarded - assert opened == [(GITHUB, 443), ("140.82.112.4", 80)] + assert opened == [(GITHUB, 443), (GITHUB, 80)] assert read[0] == b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n" - assert read[1].startswith(b"GET /a?b=1 HTTP/1.1\r\n") and b"Host: mixed.test" in read[1] + assert read[1].startswith(b"GET /a?b=1 HTTP/1.1\r\n") and b"Host: site.test" in read[1] assert b"Proxy-Connection" not in read[1] and b"Connection: close" in read[1] logged = _logged(capsys.readouterr().out, "connection") assert [(entry["host"], entry["port"], entry["address"]) for entry in logged] == [ - ("site.test", 443, GITHUB), ("mixed.test", 80, "140.82.112.4")] + ("site.test", 443, GITHUB), ("site.test", 80, GITHUB)] assert all(entry["bytes_sent"] > 0 and entry["bytes_received"] > 0 for entry in logged), logged assert all(set(entry) >= {"started", "duration_s"} for entry in logged) +def test_the_stand_in_refuses_nothing_it_reaches_the_computers_own_loopback_and_an_internal_address_alike(proxy): + port, opened, read, upstream_port = proxy + + own = _ask(port, f"GET http://127.0.0.1:{upstream_port}/page HTTP/1.1\r\nHost: x\r\n\r\n".encode()) + private = _ask(port, b"GET http://10.0.0.1/ HTTP/1.1\r\nHost: x\r\n\r\n") + + assert own.startswith(b"HTTP/1.1 200 OK") and private.startswith(b"HTTP/1.1 200 OK"), (own, private) + assert read[0].startswith(b"GET /page HTTP/1.1") and opened == [("10.0.0.1", 80)] + + +def test_the_stand_in_answers_a_name_that_doesnt_resolve_at_once_and_logs_it(proxy, capsys): + port, opened, _, _ = proxy + + answer = _ask(port, b"CONNECT nowhere.invalid:443 HTTP/1.1\r\nHost: nowhere.invalid:443\r\n\r\n") + + assert answer.startswith(b"HTTP/1.1 502 ") and b"nowhere.invalid doesn't resolve" in answer, answer + assert b"X-Agentenv-Site-Proxy: failed" in answer and opened == [] + assert _logged(capsys.readouterr().out, "failed")[0]["reason"].startswith("doesn't resolve") + + +@pytest.mark.parametrize("first", ["refuses", "hangs"]) +def test_the_stand_in_tries_a_names_addresses_in_turn_within_one_deadline_for_them_all(monkeypatch, capsys, first): + module = _stand_in() + monkeypatch.setattr(module, "CONNECT_TIMEOUT_S", 0.5) + real_resolve, real_connect = socket.getaddrinfo, socket.create_connection + upstream, tried = socket.create_server(("127.0.0.1", 0)), [] + + def answer_once(): + with contextlib.suppress(OSError), upstream.accept()[0] as conn: + conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") + + threading.Thread(target=answer_once, daemon=True).start() + + def create_connection(address, timeout=None, *args, **kwargs): + if address[0] == "127.0.0.1": + return real_connect(address, timeout, *args, **kwargs) + tried.append(address[0]) + if address[0] == GITHUB: + if first == "refuses": + raise ConnectionRefusedError(address) + time.sleep(timeout) + raise TimeoutError(address) + return real_connect(upstream.getsockname(), timeout, *args, **kwargs) + + monkeypatch.setattr(module.socket, "getaddrinfo", lambda host, port, *a, **k: [ + (socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, port)) for address in (GITHUB, "140.82.112.4") + ] if host == "site.test" else real_resolve(host, port, *a, **k)) + monkeypatch.setattr(module.socket, "create_connection", create_connection) + server = module.Server(("127.0.0.1", 0), module.Proxy) + threading.Thread(target=server.serve_forever, daemon=True).start() + try: + began = time.monotonic() + answer = _ask(server.server_address[1], b"GET http://site.test/ HTTP/1.1\r\nHost: site.test\r\n\r\n") + took = time.monotonic() - began + finally: + server.shutdown() + upstream.close() + + if first == "refuses": + assert answer.startswith(b"HTTP/1.1 200 OK") and tried == [GITHUB, "140.82.112.4"], (answer, tried) + else: + assert answer.startswith(b"HTTP/1.1 502 ") and tried == [GITHUB] and took < 1.5, (answer, tried, took) + [failed] = _logged(capsys.readouterr().out, "failed") + assert failed["reason"] == "no answer" and failed["addresses"] == [GITHUB, "140.82.112.4"] + + def test_the_stand_in_relays_a_connection_whose_sockets_are_numbered_past_1023(proxy): port, opened, read, _ = proxy soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) @@ -205,23 +233,3 @@ def test_the_stand_in_ends_a_tunnel_whose_server_stopped_reading_instead_of_wait assert ended is not None and ended < 15, ended [logged] = _logged(capsys.readouterr().out, "connection") assert logged["host"] == "site.test" and logged["bytes_sent"] > 0 - - -def test_the_stand_in_answers_a_name_that_doesnt_resolve_at_once(proxy, capsys): - port, opened, _, _ = proxy - - answer = _ask(port, b"CONNECT nowhere.invalid:443 HTTP/1.1\r\nHost: nowhere.invalid:443\r\n\r\n") - - assert answer.startswith(b"HTTP/1.1 502 ") and b"nowhere.invalid doesn't resolve" in answer, answer - assert opened == [] and _logged(capsys.readouterr().out, "refused")[0]["reason"].startswith("doesn't resolve") - - -def test_with_its_own_loopback_let_in_the_stand_in_reaches_this_computers_servers_and_nothing_else_internal( - monkeypatch, capsys): - monkeypatch.setenv("AGENTENV_SETTING_OWN_LOOPBACK", "True") - with _serving(monkeypatch, _stand_in()) as (port, opened, read, upstream_port): - own = _ask(port, f"GET http://127.0.0.1:{upstream_port}/page HTTP/1.1\r\nHost: x\r\n\r\n".encode()) - private = _ask(port, b"GET http://10.0.0.1/ HTTP/1.1\r\nHost: x\r\n\r\n") - - assert own.startswith(b"HTTP/1.1 200 OK") and read[0].startswith(b"GET /page HTTP/1.1"), own - assert private.startswith(b"HTTP/1.1 403 "), private diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index a31e8ecbb..8cbc50f9d 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -9,7 +9,6 @@ import contextlib import email.message import io -import ipaddress import json import os import socket @@ -394,36 +393,24 @@ async def test_the_forward_port_refuses_any_other_host_at_once_and_logs_it(tmp_p assert f"site proxy: refused {method} {named}: no site of this computer" in capsys.readouterr().err -GITHUB = "140.82.112.3" - - @contextlib.contextmanager -def _live_hosts_at(monkeypatch, upstream_port, resolves=None): - """Each name resolving to the addresses ``resolves`` gives it, else to one of GitHub's, and connections the proxy - opens to an address off loopback landing on ``upstream_port``; each address it opened one to recorded.""" - opened, real_open, real_resolve = [], asyncio.open_connection, socket.getaddrinfo - - def getaddrinfo(host, port, *args, **kwargs): - try: - ipaddress.ip_address(host) - return real_resolve(host, port, *args, **kwargs) - except ValueError: - return [(socket.AF_INET6 if ":" in address else socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, port)) - for address in (resolves or {}).get(host, [GITHUB])] +def _live_hosts_at(monkeypatch, upstream_port): + """Connections the proxy opens to a live site's host landing on ``upstream_port`` on loopback, and each host it + opened one to recorded.""" + opened, real = [], asyncio.open_connection async def open_connection(host, port, **kwargs): if host == "127.0.0.1": - return await real_open(host, port, **kwargs) + return await real(host, port, **kwargs) opened.append((host, port)) - return await real_open("127.0.0.1", upstream_port, **kwargs) + return await real("127.0.0.1", upstream_port, **kwargs) - monkeypatch.setattr(site_proxy.socket, "getaddrinfo", getaddrinfo) monkeypatch.setattr(site_proxy.asyncio, "open_connection", open_connection) yield opened def _logged(err: str, kind: str) -> list[dict]: - """The proxy's JSON log lines of ``kind`` (``connection`` or ``refused``).""" + """The proxy's JSON log lines of ``kind`` (``connection`` or ``failed``).""" return [json.loads(line)[kind] for line in err.splitlines() if line.startswith(f'{{"{kind}"')] @@ -446,70 +433,39 @@ async def test_the_forward_port_tunnels_a_live_sites_host_straight_to_it_and_sen plain[3][1].close() await asyncio.sleep(0.3) # each connection is logged as it ends - assert established.startswith(b"HTTP/1.1 200 ") and opened == [(GITHUB, 8443), (GITHUB, 80)] + assert established.startswith(b"HTTP/1.1 200 ") and opened == [("github.githubassets.com", 8443), ("github.com", 80)] assert (tunnelled[0], tunnelled[2], plain[0], plain[2]) == ("HTTP/1.1 200 OK", b"hello", "HTTP/1.1 200 OK", b"hello") assert [request[0] for request in live.requests] == ["GET /x HTTP/1.1", "GET /a?b=1 HTTP/1.1"] assert live.requests[1][1]["connection"] == "close" and "proxy-connection" not in live.requests[1][1] assert live.requests[1][1]["host"] == "github.com" and upstream.requests == [] logged = _logged(capsys.readouterr().err, "connection") assert [(entry["host"], entry["port"], entry["address"]) for entry in logged] == [ - ("github.githubassets.com", 8443, GITHUB), ("github.com", 80, GITHUB)] + ("github.githubassets.com", 8443, "127.0.0.1"), ("github.com", 80, "127.0.0.1")] assert all(entry["bytes_sent"] > 0 and entry["bytes_received"] > 0 and entry["duration_s"] >= 0 for entry in logged) @pytest.mark.asyncio -async def test_a_live_host_resolving_to_an_internal_address_is_refused_and_one_resolving_both_ways_reaches_the_public_one( - tmp_path, monkeypatch, capsys): - resolves = {"github.com": ["10.0.0.1"], "api.github.com": ["169.254.169.254", "::1"], - "mixed.github.com": ["192.168.1.9", "140.82.112.4"]} - async with Upstream() as upstream, Upstream() as live: - config, _ = _config(tmp_path, upstream.port) - config.direct = ["github.com", "*.github.com"] - with _live_hosts_at(monkeypatch, live.port, resolves) as opened: - async with _forward_port(config) as port: - refused = [await _exchange(port, line + b"\r\n\r\n") for line in ( - b"CONNECT github.com:443 HTTP/1.1\r\nHost: github.com:443", - b"GET http://api.github.com/meta HTTP/1.1\r\nHost: api.github.com")] - reader, writer = await asyncio.open_connection("127.0.0.1", port) - writer.write(b"CONNECT mixed.github.com:443 HTTP/1.1\r\nHost: mixed.github.com:443\r\n\r\n") - established = await reader.readuntil(b"\r\n\r\n") - writer.close() - - assert [(answer[0], answer[1]["x-agentenv-site-proxy"]) for answer in refused] == [ - ("HTTP/1.1 403 Forbidden", "failed")] * 2 - assert refused[0][2] == b"github.com is an internal address, which the browser never reaches\n" - assert established.startswith(b"HTTP/1.1 200 ") and opened == [("140.82.112.4", 443)] - assert [(entry["host"], entry["reason"], entry["addresses"]) for entry in _logged(capsys.readouterr().err, "refused")] == [ - ("github.com", "internal address", ["10.0.0.1"]), ("api.github.com", "internal address", ["169.254.169.254", "::1"])] - assert live.requests == [] - - -@pytest.mark.asyncio -async def test_a_live_host_whose_every_address_hangs_fails_within_one_deadline_not_one_per_address( - tmp_path, monkeypatch, capsys): +async def test_a_live_host_that_doesnt_answer_in_time_is_a_502_and_logged(tmp_path, monkeypatch, capsys): monkeypatch.setattr(site_proxy, "UPSTREAM_TIMEOUT_S", 0.5) - tried, real_open = [], asyncio.open_connection + real_open = asyncio.open_connection async def hangs(host, port, **kwargs): if host == "127.0.0.1": return await real_open(host, port, **kwargs) - tried.append(host) await asyncio.sleep(60) - async with Upstream() as upstream, Upstream() as live: + async with Upstream() as upstream: config, _ = _config(tmp_path, upstream.port) config.direct = ["github.com"] - with _live_hosts_at(monkeypatch, live.port, {"github.com": ["140.82.112.3", "140.82.112.4", "140.82.112.5"]}): - monkeypatch.setattr(site_proxy.asyncio, "open_connection", hangs) - async with _forward_port(config) as port: - began = time.monotonic() - answer = await _exchange(port, b"CONNECT github.com:443 HTTP/1.1\r\nHost: github.com:443\r\n\r\n") - took = time.monotonic() - began - - assert answer[0] == "HTTP/1.1 502 Bad Gateway" and took < 1.2, (answer[0], took) - assert tried and tried[0] == "140.82.112.3" - [refused] = _logged(capsys.readouterr().err, "refused") - assert refused["reason"] == "no answer" + monkeypatch.setattr(site_proxy.asyncio, "open_connection", hangs) + async with _forward_port(config) as port: + began = time.monotonic() + answer = await _exchange(port, b"CONNECT github.com:443 HTTP/1.1\r\nHost: github.com:443\r\n\r\n") + took = time.monotonic() - began + + assert answer[0] == "HTTP/1.1 502 Bad Gateway" and answer[1]["x-agentenv-site-proxy"] == "failed" and took < 1.2 + [failed] = _logged(capsys.readouterr().err, "failed") + assert (failed["method"], failed["host"], failed["port"]) == ("CONNECT", "github.com", 443) @pytest.mark.asyncio From 80601db4cce6ee0d802ed9046c4235f378a526b3 Mon Sep 17 00:00:00 2001 From: Edgar Arakelyan Date: Mon, 9 Nov 2026 22:48:15 -0800 Subject: [PATCH 6/6] refactor(computer): both browser-alone proxies log the same JSON lines The stand-in and the shared site proxy's live-host tunnel now write a failed connection the same way (reason "doesn't resolve" or "no answer", plus the error), and every line either proxy writes is JSON: the stand-in's startup line, the shared proxy's refusals, unexpected connection errors and keep's restarts included. One test sends the same success and failures through both and checks the records match, so the two can't drift again. Also: the shared proxy counts relayed bytes with a small Count instead of a list and an index, through one relay loop; the stand-in's _open returns the address it connected to instead of leaving it on the handler; the stand-in's tests share one fake-network fixture and are renamed for what they cover. Co-Authored-By: Claude Opus 5.5 --- src/agent_env/computers/README.md | 4 +- .../ubuntu-24.04-headless/site-proxy.py | 51 ++-- .../env/envs/computer/utils/site_proxy.py | 54 ++-- .../computer/browser_computer_test.py | 5 +- tst/unit/env/envs/test_computer_open_web.py | 235 ------------------ tst/unit/env/envs/test_computer_site_proxy.py | 62 ++++- .../env/envs/test_computer_stand_in_proxy.py | 216 ++++++++++++++++ 7 files changed, 339 insertions(+), 288 deletions(-) delete mode 100644 tst/unit/env/envs/test_computer_open_web.py create mode 100644 tst/unit/env/envs/test_computer_stand_in_proxy.py diff --git a/src/agent_env/computers/README.md b/src/agent_env/computers/README.md index c0614ac3c..4ebc98282 100644 --- a/src/agent_env/computers/README.md +++ b/src/agent_env/computers/README.md @@ -15,8 +15,8 @@ building it. Name it from another bundle as `ubuntu-24.04`, the alias `aliases.t browser's page: a browser computer, which agentenv's `chromium` deploys alone as. supervisord runs every program as the session user, among them a stand-in site proxy on `127.0.0.1:3128`, the browser's only route out when the computer routes no sites. It forwards a request or a `CONNECT` tunnel to any host, never decrypting a tunnel and refusing -nothing: the sandbox's network policy is all that limits it. Each connection, and each one that fails, is a JSON line in -`/var/log/agentenv/site-proxy.log`. A computer that routes sites names the shared site proxy's forward port, +nothing: the sandbox's network policy is all that limits it. Every line it writes to `/var/log/agentenv/site-proxy.log` +is JSON, one per connection and one per connection that fails, in the same shapes as the shared site proxy's. A computer that routes sites names the shared site proxy's forward port, `127.0.0.1:3129`, to the browser instead: it serves the run's sites, refuses every other host and runs out of the agent's reach. It has a container form only. Name it from another bundle as `ubuntu-24.04-headless`, or by its id, `@local/agentenv-framework/computers/ubuntu-24.04-headless`. diff --git a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py index 09bbcc391..a874525ae 100644 --- a/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py +++ b/src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py @@ -1,13 +1,14 @@ """A stand-in for the shared site proxy in this base, on 127.0.0.1:3128: it forwards every request the browser sends it, absolute-form HTTP and CONNECT tunnels alike, to the host it names, refusing nothing and never decrypting a tunnel. -The sandbox's own network policy still decides where a connection can go. Each connection is logged as a JSON line -when it ends, and each one that fails with its reason.""" +The sandbox's own network policy still decides where a connection can go. Every line it logs is JSON, in the same +shapes as the shared site proxy's: ``connection`` (host, port, address, bytes each way, start, duration) when one +ends, and ``failed`` (method, host, port, ``reason`` "doesn't resolve" or "no answer", and the error) for one that +couldn't open.""" import http.server import json import selectors import socket import socketserver -import sys import time import urllib.parse @@ -21,6 +22,10 @@ def log(**fields) -> None: print(json.dumps(fields), flush=True) +def _error(error: BaseException) -> str: + return f"{type(error).__name__}: {error}".removesuffix(": ") + + class Proxy(http.server.BaseHTTPRequestHandler): protocol_version = "HTTP/1.1" @@ -30,12 +35,12 @@ def do_CONNECT(self): if not port.isdigit(): self.send_error(400) return - if (upstream := self._open(host, int(port), "CONNECT")) is None: + if (opened := self._open(host, int(port), "CONNECT")) is None: return self.send_response(200, "Connection established") self.end_headers() self.close_connection = True - self._relay(upstream, host, int(port), b"") + self._relay(*opened, host, int(port), b"") def forward(self): url = urllib.parse.urlsplit(self.path) @@ -43,41 +48,43 @@ def forward(self): self.send_error(400) return port = url.port or 80 - if (upstream := self._open(url.hostname, port, self.command)) is None: + if (opened := self._open(url.hostname, port, self.command)) is None: return target = (url.path or "/") + (f"?{url.query}" if url.query else "") headers = "".join(f"{k}: {v}\r\n" for k, v in self.headers.items() if k.lower() not in HOP) head = f"{self.command} {target} HTTP/1.1\r\n{headers}Connection: close\r\n\r\n".encode("latin-1") body = self.rfile.read(length) if (length := int(self.headers.get("Content-Length") or 0)) else b"" self.close_connection = True - self._relay(upstream, url.hostname, port, head + body) + self._relay(*opened, url.hostname, port, head + body) do_GET = do_POST = do_PUT = do_PATCH = do_DELETE = do_HEAD = do_OPTIONS = forward - def _open(self, host: str, port: int, method: str): + def _open(self, host: str, port: int, method: str) -> tuple[socket.socket, str] | None: """A connection to an address ``host`` resolves to, tried in turn within one ``CONNECT_TIMEOUT_S`` for them - all, or None once the browser has its answer: a 502 for a host that doesn't resolve or answer. The connection - keeps an ``IDLE_S`` timeout, so a write to a peer that stopped reading ends too.""" + all, and that address; or None once the browser has its answer, a 502 for a host that doesn't resolve or + answer. The connection keeps an ``IDLE_S`` timeout, so a write to a peer that stopped reading ends too.""" deadline = time.monotonic() + CONNECT_TIMEOUT_S try: found = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM) - addresses = list(dict.fromkeys(info[4][0] for info in found)) except OSError as error: - log(failed={"method": method, "host": host, "port": port, "reason": f"doesn't resolve: {error}"}) + log(failed={"method": method, "host": host, "port": port, "reason": "doesn't resolve", + "error": _error(error)}) self._answer(502, f"{host} doesn't resolve\n") return None - for address in addresses: + last = None + for address in dict.fromkeys(info[4][0] for info in found): if (left := deadline - time.monotonic()) <= 0: break try: upstream = socket.create_connection((address, port), timeout=left) - upstream.settimeout(IDLE_S) - self.address = address - return upstream - except OSError: + except OSError as error: + last = error continue - log(failed={"method": method, "host": host, "port": port, "reason": "no answer", "addresses": addresses}) - self._answer(502, f"{host} didn't answer\n") + upstream.settimeout(IDLE_S) + return upstream, address + log(failed={"method": method, "host": host, "port": port, "reason": "no answer", + "error": _error(last) if last else f"none within {CONNECT_TIMEOUT_S} s"}) + self._answer(502, f"{host}:{port} didn't answer\n") return None def _answer(self, status: int, body: str) -> None: @@ -91,7 +98,7 @@ def _answer(self, status: int, body: str) -> None: self.wfile.write(data) self.close_connection = True - def _relay(self, upstream: socket.socket, host: str, port: int, first: bytes) -> None: + def _relay(self, upstream: socket.socket, address: str, host: str, port: int, first: bytes) -> None: started, began = time.time(), time.monotonic() sent, received = len(first), 0 self.connection.settimeout(IDLE_S) @@ -101,7 +108,7 @@ def _relay(self, upstream: socket.socket, host: str, port: int, first: bytes) -> more, received = _pipe(self.connection, upstream) sent += more finally: - log(connection={"host": host, "port": port, "address": self.address, "bytes_sent": sent, + log(connection={"host": host, "port": port, "address": address, "bytes_sent": sent, "bytes_received": received, "started": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(started)), "duration_s": round(time.monotonic() - began, 3)}) @@ -146,5 +153,5 @@ class Server(socketserver.ThreadingMixIn, http.server.HTTPServer): if __name__ == "__main__": - print(f"site proxy stand-in on 127.0.0.1:{PORT}", file=sys.stderr, flush=True) + log(listening={"address": "127.0.0.1", "port": PORT}) Server(("127.0.0.1", PORT), Proxy).serve_forever() diff --git a/src/agent_env/env/envs/computer/utils/site_proxy.py b/src/agent_env/env/envs/computer/utils/site_proxy.py index 085b615ab..eddacff70 100644 --- a/src/agent_env/env/envs/computer/utils/site_proxy.py +++ b/src/agent_env/env/envs/computer/utils/site_proxy.py @@ -107,10 +107,25 @@ def matches(pattern: str, host: str) -> bool: def log_event(**fields) -> None: - """One JSON line in the proxy's log: a connection to a live site's host once it ends, or one that failed.""" + """One JSON line in the proxy's log, every line it writes being one: ``connection`` (host, port, address, bytes each + way, start, duration) when a connection to a live site's host ends, ``failed`` (method, host, port, ``reason`` + "doesn't resolve" or "no answer", and the error) for one that couldn't open, both in the same shapes as the + headless base's stand-in proxy's; ``refused`` for a host that's no site, ``error`` for a connection that failed + unexpectedly, and ``restarted`` when ``keep`` starts the proxy again.""" print(json.dumps(fields), file=sys.stderr, flush=True) +def described(error: BaseException) -> str: + return f"{type(error).__name__}: {error}".removesuffix(": ") + + +class Count: + """The bytes a relay has carried one way, counted as it goes, so a connection that ends on an error keeps them.""" + + def __init__(self, start: int = 0): + self.bytes = start + + @dataclass class Site: """One site as the proxy serves it: its names, the route on the gateway it forwards to, and its certificate and key @@ -366,10 +381,13 @@ async def relay_chunked(reader: asyncio.StreamReader, writer: asyncio.StreamWrit await relay_exact(reader, writer, size + 2) -async def relay_until_eof(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: +async def relay_until_eof(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, + count: Optional[Count] = None) -> None: while data := await reader.read(CHUNK): writer.write(data) await writer.drain() + if count is not None: + count.bytes += len(data) async def relay_body(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, headers: list[tuple[str, str]], @@ -385,16 +403,11 @@ async def relay_body(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, await relay_until_eof(reader, writer) -async def splice(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, counts: Optional[list[int]] = None, - index: int = 0) -> None: +async def splice(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, count: Optional[Count] = None) -> None: """One direction of an upgraded connection, closing the other side when it ends, which ends the other direction; - the bytes it carried added to ``counts[index]`` when given.""" + the bytes it carries counted in ``count`` when given.""" try: - while data := await reader.read(CHUNK): - writer.write(data) - await writer.drain() - if counts is not None: - counts[index] += len(data) + await relay_until_eof(reader, writer, count) except (ConnectionError, OSError, ssl.SSLError): pass finally: @@ -472,28 +485,31 @@ async def _direct(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, ho try: up_reader, up_writer = await asyncio.wait_for(asyncio.open_connection(host, port), UPSTREAM_TIMEOUT_S) except (OSError, asyncio.TimeoutError) as e: + unresolved = isinstance(e, socket.gaierror) log_event(failed={"method": "CONNECT" if head is None else head.split(b" ", 1)[0].decode("latin-1"), - "host": host, "port": port, "reason": str(e) or type(e).__name__}) - writer.write(simple_response("502 Bad Gateway", f"{host}:{port} didn't answer: {e}\n", FAILED)) + "host": host, "port": port, "reason": "doesn't resolve" if unresolved else "no answer", + "error": described(e)}) + writer.write(simple_response("502 Bad Gateway", f"{host} doesn't resolve\n" if unresolved else + f"{host}:{port} didn't answer\n", FAILED)) return address = (up_writer.get_extra_info("peername") or ("",))[0] - started, began, counts = time.time(), time.monotonic(), [len(head or b""), 0] + started, began, sent, received = time.time(), time.monotonic(), Count(len(head or b"")), Count() if head is None: writer.write(b"HTTP/1.1 200 Connection established\r\n\r\n") else: up_writer.write(head) try: await asyncio.gather(writer.drain(), up_writer.drain()) - await asyncio.gather(splice(reader, up_writer, counts, 0), splice(up_reader, writer, counts, 1)) + await asyncio.gather(splice(reader, up_writer, sent), splice(up_reader, writer, received)) finally: - log_event(connection={"host": host, "port": port, "address": address, "bytes_sent": counts[0], - "bytes_received": counts[1], + log_event(connection={"host": host, "port": port, "address": address, "bytes_sent": sent.bytes, + "bytes_received": received.bytes, "started": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(started)), "duration_s": round(time.monotonic() - began, 3)}) @staticmethod def _refuse(writer: asyncio.StreamWriter, method: str, host: str) -> None: - print(f"site proxy: refused {method} {host}: no site of this computer", file=sys.stderr, flush=True) + log_event(refused={"method": method, "host": host, "reason": "no site of this computer"}) writer.write(simple_response("403 Forbidden", f"{host} is no site of this computer, and the browser reaches " "its sites alone\n", FAILED)) @@ -506,7 +522,7 @@ async def _serving(writer: asyncio.StreamWriter, label: str, serving: Awaitable[ except (ConnectionError, asyncio.IncompleteReadError, ssl.SSLError, OSError): pass except Exception as e: - print(f"site proxy: a {label} connection failed: {type(e).__name__}: {e}", file=sys.stderr, flush=True) + log_event(error={"connection": label, "error": described(e)}) finally: try: await writer.drain() @@ -1124,7 +1140,7 @@ def keep(serve_path: str) -> None: while True: ended = subprocess.run([sys.executable, os.path.abspath(__file__), "serve", serve_path], stdin=subprocess.DEVNULL).returncode - print(f"the site proxy ended ({ended}); starting it again", flush=True) + print(json.dumps({"restarted": {"exit_code": ended}}), flush=True) time.sleep(RESTART_DELAY_S) diff --git a/tst/integration/computer/browser_computer_test.py b/tst/integration/computer/browser_computer_test.py index a28d3ffec..490c62d40 100644 --- a/tst/integration/computer/browser_computer_test.py +++ b/tst/integration/computer/browser_computer_test.py @@ -646,8 +646,9 @@ async def test_routing_sites_it_goes_out_only_through_the_forward_port_which_the assert (status, line) == ("200", "data: first") and int(seconds) < 15, pages assert all(pages[url].startswith("error") and "403" in pages[url] for url in ( "https://example.com/", "http://127.0.0.1:18765/", "https://127.0.0.1:18765/")), pages - assert "site proxy: refused CONNECT example.com:443: no site of this computer" in logged - assert "site proxy: refused GET 127.0.0.1:18765: no site of this computer" in logged + refused = [json.loads(line)["refused"] for line in logged.splitlines() if line.startswith('{"refused"')] + assert {"method": "CONNECT", "host": "example.com:443", "reason": "no site of this computer"} in refused, logged + assert {"method": "GET", "host": "127.0.0.1:18765", "reason": "no site of this computer"} in refused, logged assert stopped["exit_code"] != 0 and still == serving and after_stop.startswith("200 "), (stopped, after_stop) assert back.startswith("200 ") and restarted and restarted != serving, (back, restarted) assert after_reset.startswith("200 ") and after_reset.endswith("alpha"), after_reset diff --git a/tst/unit/env/envs/test_computer_open_web.py b/tst/unit/env/envs/test_computer_open_web.py deleted file mode 100644 index 37ddd4f70..000000000 --- a/tst/unit/env/envs/test_computer_open_web.py +++ /dev/null @@ -1,235 +0,0 @@ -"""A browser alone on the open web: the headless base's stand-in proxy on 3128, its route out with no site routing, -forwards any host, HTTP and CONNECT alike, without decrypting and refusing nothing, the sandbox's network policy -deciding where it can go; it bounds every wait, and logs each connection, and each one that fails, as a JSON line. The -proxy is a network program, so these tests open sockets, on loopback alone.""" - -from __future__ import annotations - -import contextlib -import importlib.util -import json -import os -import resource -import socket -import threading -import time -from pathlib import Path - -import pytest - -import agent_env - -pytestmark = pytest.mark.usefixtures("socket_enabled") - -STAND_IN = Path(agent_env.__file__).parent / "computers" / "artifacts" / "ubuntu-24.04-headless" / "site-proxy.py" -GITHUB = "140.82.112.3" - - -def _stand_in(): - spec = importlib.util.spec_from_file_location("headless_site_proxy", STAND_IN) - module = importlib.util.module_from_spec(spec) - spec.loader.exec_module(module) - return module - - -@pytest.fixture -def proxy(monkeypatch): - with _serving(monkeypatch, _stand_in()) as serving: - yield serving - - -@contextlib.contextmanager -def _serving(monkeypatch, module): - """``module``'s stand-in serving on a free port, with ``site.test`` resolving to one of GitHub's addresses, and - connections to an address off loopback landing on a local upstream that answers each request; yields the port, - each address the proxy connected to, what the upstream read, and the upstream's own port.""" - real_resolve, real_connect = socket.getaddrinfo, socket.create_connection - upstream, read, opened = socket.create_server(("127.0.0.1", 0)), [], [] - - def answer(): - while True: - try: - conn, _ = upstream.accept() - except OSError: - return - with conn: - read.append(conn.recv(65536)) - conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nhello") - - def getaddrinfo(host, port, *args, **kwargs): - if host == "site.test": - return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (GITHUB, port))] - return real_resolve(host, port, *args, **kwargs) - - def create_connection(address, *args, **kwargs): - if address[0] in ("127.0.0.1", "localhost"): - return real_connect(address, *args, **kwargs) - opened.append(address) - return real_connect(upstream.getsockname(), *args, **kwargs) - - monkeypatch.setattr(module.socket, "getaddrinfo", getaddrinfo) - monkeypatch.setattr(module.socket, "create_connection", create_connection) - server = module.Server(("127.0.0.1", 0), module.Proxy) - threading.Thread(target=server.serve_forever, daemon=True).start() - threading.Thread(target=answer, daemon=True).start() - try: - yield server.server_address[1], opened, read, upstream.getsockname()[1] - finally: - server.shutdown() - upstream.close() - - -def _ask(port: int, request: bytes, then: bytes = b"") -> bytes: - """What the proxy answers ``request``, and in a tunnel it opened, ``then``.""" - with socket.create_connection(("127.0.0.1", port), timeout=10) as conn: - conn.sendall(request) - answer = conn.recv(65536) - if then and answer.startswith(b"HTTP/1.1 200"): - conn.sendall(then) - while chunk := conn.recv(65536): - answer += chunk - return answer - - -def _logged(text: str, kind: str) -> list[dict]: - return [json.loads(line)[kind] for line in text.splitlines() if line.startswith(f'{{"{kind}"')] - - -def test_the_stand_in_tunnels_and_forwards_a_public_host_and_logs_each_connection(proxy, capsys): - port, opened, read, _ = proxy - - tunnelled = _ask(port, b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n", - then=b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n") - forwarded = _ask(port, b"GET http://site.test/a?b=1 HTTP/1.1\r\nHost: site.test\r\nProxy-Connection: keep-alive" - b"\r\n\r\n") - - assert tunnelled.startswith(b"HTTP/1.1 200 Connection established") and tunnelled.endswith(b"hello"), tunnelled - assert forwarded.startswith(b"HTTP/1.1 200 OK") and forwarded.endswith(b"hello"), forwarded - assert opened == [(GITHUB, 443), (GITHUB, 80)] - assert read[0] == b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n" - assert read[1].startswith(b"GET /a?b=1 HTTP/1.1\r\n") and b"Host: site.test" in read[1] - assert b"Proxy-Connection" not in read[1] and b"Connection: close" in read[1] - logged = _logged(capsys.readouterr().out, "connection") - assert [(entry["host"], entry["port"], entry["address"]) for entry in logged] == [ - ("site.test", 443, GITHUB), ("site.test", 80, GITHUB)] - assert all(entry["bytes_sent"] > 0 and entry["bytes_received"] > 0 for entry in logged), logged - assert all(set(entry) >= {"started", "duration_s"} for entry in logged) - - -def test_the_stand_in_refuses_nothing_it_reaches_the_computers_own_loopback_and_an_internal_address_alike(proxy): - port, opened, read, upstream_port = proxy - - own = _ask(port, f"GET http://127.0.0.1:{upstream_port}/page HTTP/1.1\r\nHost: x\r\n\r\n".encode()) - private = _ask(port, b"GET http://10.0.0.1/ HTTP/1.1\r\nHost: x\r\n\r\n") - - assert own.startswith(b"HTTP/1.1 200 OK") and private.startswith(b"HTTP/1.1 200 OK"), (own, private) - assert read[0].startswith(b"GET /page HTTP/1.1") and opened == [("10.0.0.1", 80)] - - -def test_the_stand_in_answers_a_name_that_doesnt_resolve_at_once_and_logs_it(proxy, capsys): - port, opened, _, _ = proxy - - answer = _ask(port, b"CONNECT nowhere.invalid:443 HTTP/1.1\r\nHost: nowhere.invalid:443\r\n\r\n") - - assert answer.startswith(b"HTTP/1.1 502 ") and b"nowhere.invalid doesn't resolve" in answer, answer - assert b"X-Agentenv-Site-Proxy: failed" in answer and opened == [] - assert _logged(capsys.readouterr().out, "failed")[0]["reason"].startswith("doesn't resolve") - - -@pytest.mark.parametrize("first", ["refuses", "hangs"]) -def test_the_stand_in_tries_a_names_addresses_in_turn_within_one_deadline_for_them_all(monkeypatch, capsys, first): - module = _stand_in() - monkeypatch.setattr(module, "CONNECT_TIMEOUT_S", 0.5) - real_resolve, real_connect = socket.getaddrinfo, socket.create_connection - upstream, tried = socket.create_server(("127.0.0.1", 0)), [] - - def answer_once(): - with contextlib.suppress(OSError), upstream.accept()[0] as conn: - conn.sendall(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n") - - threading.Thread(target=answer_once, daemon=True).start() - - def create_connection(address, timeout=None, *args, **kwargs): - if address[0] == "127.0.0.1": - return real_connect(address, timeout, *args, **kwargs) - tried.append(address[0]) - if address[0] == GITHUB: - if first == "refuses": - raise ConnectionRefusedError(address) - time.sleep(timeout) - raise TimeoutError(address) - return real_connect(upstream.getsockname(), timeout, *args, **kwargs) - - monkeypatch.setattr(module.socket, "getaddrinfo", lambda host, port, *a, **k: [ - (socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, port)) for address in (GITHUB, "140.82.112.4") - ] if host == "site.test" else real_resolve(host, port, *a, **k)) - monkeypatch.setattr(module.socket, "create_connection", create_connection) - server = module.Server(("127.0.0.1", 0), module.Proxy) - threading.Thread(target=server.serve_forever, daemon=True).start() - try: - began = time.monotonic() - answer = _ask(server.server_address[1], b"GET http://site.test/ HTTP/1.1\r\nHost: site.test\r\n\r\n") - took = time.monotonic() - began - finally: - server.shutdown() - upstream.close() - - if first == "refuses": - assert answer.startswith(b"HTTP/1.1 200 OK") and tried == [GITHUB, "140.82.112.4"], (answer, tried) - else: - assert answer.startswith(b"HTTP/1.1 502 ") and tried == [GITHUB] and took < 1.5, (answer, tried, took) - [failed] = _logged(capsys.readouterr().out, "failed") - assert failed["reason"] == "no answer" and failed["addresses"] == [GITHUB, "140.82.112.4"] - - -def test_the_stand_in_relays_a_connection_whose_sockets_are_numbered_past_1023(proxy): - port, opened, read, _ = proxy - soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) - if hard != resource.RLIM_INFINITY and hard < 1400: - pytest.skip(f"this machine allows {hard} open files, too few to number a socket past 1023") - resource.setrlimit(resource.RLIMIT_NOFILE, (max(soft, 1400) if hard == resource.RLIM_INFINITY else - min(max(soft, 1400), hard), hard)) - held = [os.pipe() for _ in range(520)] - try: - answer = _ask(port, b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n", - then=b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n") - finally: - for pair in held: - os.close(pair[0]) - os.close(pair[1]) - resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard)) - - assert answer.startswith(b"HTTP/1.1 200 Connection established") and answer.endswith(b"hello"), answer - assert opened == [(GITHUB, 443)] - - -def test_the_stand_in_ends_a_tunnel_whose_server_stopped_reading_instead_of_waiting_on_it_for_ever(monkeypatch, capsys): - module = _stand_in() - monkeypatch.setattr(module, "IDLE_S", 1) - sink, accepted = socket.create_server(("127.0.0.1", 0)), [] - threading.Thread(target=lambda: accepted.append(sink.accept()), daemon=True).start() # never reads - real_resolve, real_connect = socket.getaddrinfo, socket.create_connection - monkeypatch.setattr(module.socket, "getaddrinfo", lambda host, port, *a, **k: [ - (socket.AF_INET, socket.SOCK_STREAM, 6, "", (GITHUB, port))] if host == "site.test" else real_resolve( - host, port, *a, **k)) - monkeypatch.setattr(module.socket, "create_connection", lambda address, *a, **k: real_connect( - sink.getsockname() if address[0] == GITHUB else address, *a, **k)) - server = module.Server(("127.0.0.1", 0), module.Proxy) - threading.Thread(target=server.serve_forever, daemon=True).start() - try: - with socket.create_connection(server.server_address, timeout=20) as conn: - conn.sendall(b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n") - assert conn.recv(4096).startswith(b"HTTP/1.1 200") - began, ended = time.monotonic(), None - try: - while time.monotonic() - began < 20: - conn.sendall(b"x" * 65536) - except OSError: - ended = time.monotonic() - began - finally: - server.shutdown() - sink.close() - - assert ended is not None and ended < 15, ended - [logged] = _logged(capsys.readouterr().out, "connection") - assert logged["host"] == "site.test" and logged["bytes_sent"] > 0 diff --git a/tst/unit/env/envs/test_computer_site_proxy.py b/tst/unit/env/envs/test_computer_site_proxy.py index 8cbc50f9d..a25492f71 100644 --- a/tst/unit/env/envs/test_computer_site_proxy.py +++ b/tst/unit/env/envs/test_computer_site_proxy.py @@ -33,6 +33,8 @@ from agent_env.env.envs.computer.utils.site_proxy import Config, Site, SiteProxy from agent_env.env.gateway.constants import AGENT_ENV_SITE_ROOT_HEADER from agent_env.env.gateway.gateway import Gateway +from tst.unit.env.envs.test_computer_stand_in_proxy import _ask, _logged +from tst.unit.env.envs.test_computer_stand_in_proxy import _serving as _stand_in_serving from tst.unit.env.gateway.website_websocket_test import EchoSite pytestmark = pytest.mark.usefixtures("socket_enabled") @@ -352,7 +354,8 @@ async def test_a_request_in_a_sites_tunnel_naming_another_host_is_refused_and_lo assert start == "HTTP/1.1 403 Forbidden" and headers["x-agentenv-site-proxy"] == "failed" assert upstream.requests == [] - assert "site proxy: refused GET evil.test: no site of this computer" in capsys.readouterr().err + assert _logged(capsys.readouterr().err, "refused") == [ + {"method": "GET", "host": "evil.test", "reason": "no site of this computer"}] @pytest.mark.asyncio @@ -390,7 +393,8 @@ async def test_the_forward_port_refuses_any_other_host_at_once_and_logs_it(tmp_p assert body == f"{named} is no site of this computer, and the browser reaches its sites alone\n".encode() assert upstream.requests == [] method = request_line.split(b" ")[0].decode() - assert f"site proxy: refused {method} {named}: no site of this computer" in capsys.readouterr().err + assert _logged(capsys.readouterr().err, "refused") == [ + {"method": method, "host": named, "reason": "no site of this computer"}] @contextlib.contextmanager @@ -409,11 +413,6 @@ async def open_connection(host, port, **kwargs): yield opened -def _logged(err: str, kind: str) -> list[dict]: - """The proxy's JSON log lines of ``kind`` (``connection`` or ``failed``).""" - return [json.loads(line)[kind] for line in err.splitlines() if line.startswith(f'{{"{kind}"')] - - @pytest.mark.asyncio async def test_the_forward_port_tunnels_a_live_sites_host_straight_to_it_and_sends_its_url_on_as_a_path( tmp_path, monkeypatch, capsys): @@ -465,7 +464,54 @@ async def hangs(host, port, **kwargs): assert answer[0] == "HTTP/1.1 502 Bad Gateway" and answer[1]["x-agentenv-site-proxy"] == "failed" and took < 1.2 [failed] = _logged(capsys.readouterr().err, "failed") - assert (failed["method"], failed["host"], failed["port"]) == ("CONNECT", "github.com", 443) + assert failed == {"method": "CONNECT", "host": "github.com", "port": 443, "reason": "no answer", + "error": "TimeoutError"} + assert answer[2] == b"github.com:443 didn't answer\n" + + +@pytest.mark.asyncio +async def test_both_proxies_log_a_connection_and_each_way_one_fails_in_the_same_json_lines(tmp_path, monkeypatch, + capsys): + """The stand-in a browser alone uses and the forward port's tunnel to a live site's hosts write the same records: + each line JSON, a ``connection`` with the same fields, and a ``failed`` with the same fields and reason, whichever + proxy a run took.""" + requests = [b"GET http://site.test/ HTTP/1.1\r\nHost: site.test\r\n\r\n", + b"CONNECT nowhere.test:443 HTTP/1.1\r\nHost: nowhere.test:443\r\n\r\n", + b"CONNECT slow.test:443 HTTP/1.1\r\nHost: slow.test:443\r\n\r\n"] + with _stand_in_serving(monkeypatch, names={"nowhere.test": [], "slow.test": ["140.82.112.9"]}, + acts={"140.82.112.9": "hangs"}, CONNECT_TIMEOUT_S=0.3) as stand_in: + for request in requests: + _ask(stand_in.port, request) + stand_in_log = capsys.readouterr().out + + monkeypatch.setattr(site_proxy, "UPSTREAM_TIMEOUT_S", 0.3) + real_open = asyncio.open_connection + + async def open_connection(host, port, **kwargs): + if host == "nowhere.test": + raise socket.gaierror(-2, "Name or service not known") + if host == "slow.test": + await asyncio.sleep(60) + return await real_open("127.0.0.1", port if host == "127.0.0.1" else live.port, **kwargs) + + async with Upstream() as upstream, Upstream() as live: + config, _ = _config(tmp_path, upstream.port) + config.direct = ["site.test", "nowhere.test", "slow.test"] + monkeypatch.setattr(site_proxy.asyncio, "open_connection", open_connection) + async with _forward_port(config) as port: + for request in requests: + answer = await _exchange(port, request) + answer[3][1].close() + await asyncio.sleep(0.3) # each connection is logged as it ends + shared_log = capsys.readouterr().err + + def shape(log: str) -> list: + return [(kind, sorted(record), record.get("reason")) for entry in _logged(log) for kind, record in entry.items()] + + assert sorted(shape(stand_in_log)) == sorted(shape(shared_log)) == [ + ("connection", ["address", "bytes_received", "bytes_sent", "duration_s", "host", "port", "started"], None), + ("failed", ["error", "host", "method", "port", "reason"], "doesn't resolve"), + ("failed", ["error", "host", "method", "port", "reason"], "no answer")] @pytest.mark.asyncio diff --git a/tst/unit/env/envs/test_computer_stand_in_proxy.py b/tst/unit/env/envs/test_computer_stand_in_proxy.py new file mode 100644 index 000000000..7e7728ec5 --- /dev/null +++ b/tst/unit/env/envs/test_computer_stand_in_proxy.py @@ -0,0 +1,216 @@ +"""The headless base's stand-in proxy on 3128, a browser alone's route out with no site routing: it forwards any +host, HTTP and CONNECT alike, without decrypting and refusing nothing, the sandbox's network policy deciding where it +can go; it bounds every wait, and logs each connection, and each one that fails, as a JSON line. The proxy is a +network program, so these tests open sockets, on loopback alone.""" + +from __future__ import annotations + +import contextlib +import importlib.util +import json +import os +import resource +import socket +import threading +import time +from pathlib import Path +from types import SimpleNamespace + +import pytest + +import agent_env + +pytestmark = pytest.mark.usefixtures("socket_enabled") + +STAND_IN = Path(agent_env.__file__).parent / "computers" / "artifacts" / "ubuntu-24.04-headless" / "site-proxy.py" +GITHUB = "140.82.112.3" +OK = b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nhello" + + +def _stand_in(): + spec = importlib.util.spec_from_file_location("headless_site_proxy", STAND_IN) + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +def _accepting(server: socket.socket, each) -> None: + """``each`` run on every connection ``server`` accepts, on a thread of its own, until it closes.""" + def run(): + while True: + try: + conn, _ = server.accept() + except OSError: + return + each(conn) + + threading.Thread(target=run, daemon=True).start() + + +@contextlib.contextmanager +def _serving(monkeypatch, names: dict | None = None, acts: dict | None = None, **settings): + """The stand-in serving on a free port, with its module's ``settings`` set first. Each of ``names`` resolves to the + addresses it gives (none: it doesn't resolve), ``site.test`` to one of GitHub's unless given. A connection to an + address off loopback acts as ``acts`` says, ``"refuses"``, ``"hangs"`` (for its whole timeout) or ``"never + reads"``, else lands on a local upstream that answers each request with ``OK``. Yields the port, each address and + port the proxy connected to, what the upstream read, and the upstream's own port.""" + module = _stand_in() + for name, value in settings.items(): + monkeypatch.setattr(module, name, value) + names, acts = {"site.test": [GITHUB], **(names or {})}, acts or {} + real_resolve, real_connect = socket.getaddrinfo, socket.create_connection + upstream, sink = socket.create_server(("127.0.0.1", 0)), socket.create_server(("127.0.0.1", 0)) + read, opened, held = [], [], [] + + def answer(conn): + with conn: + read.append(conn.recv(65536)) + conn.sendall(OK) + + def getaddrinfo(host, port, *args, **kwargs): + if host not in names: + return real_resolve(host, port, *args, **kwargs) + if not names[host]: + raise socket.gaierror(-2, "Name or service not known") + return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (address, port)) for address in names[host]] + + def create_connection(address, *args, **kwargs): + if address[0] in ("127.0.0.1", "localhost"): + return real_connect(address, *args, **kwargs) + opened.append(address) + if (act := acts.get(address[0])) == "refuses": + raise ConnectionRefusedError(f"{address[0]} refused") + if act == "hangs": + time.sleep(kwargs.get("timeout") or 0) + raise TimeoutError("timed out") + return real_connect((sink if act == "never reads" else upstream).getsockname(), *args, **kwargs) + + monkeypatch.setattr(module.socket, "getaddrinfo", getaddrinfo) + monkeypatch.setattr(module.socket, "create_connection", create_connection) + _accepting(upstream, answer) + _accepting(sink, held.append) + server = module.Server(("127.0.0.1", 0), module.Proxy) + threading.Thread(target=server.serve_forever, daemon=True).start() + try: + yield SimpleNamespace(port=server.server_address[1], opened=opened, read=read, + upstream_port=upstream.getsockname()[1]) + finally: + server.shutdown() + upstream.close() + sink.close() + + +@pytest.fixture +def proxy(monkeypatch): + with _serving(monkeypatch) as serving: + yield serving + + +def _ask(port: int, request: bytes, then: bytes = b"") -> bytes: + """What the proxy answers ``request``, and in a tunnel it opened, ``then``.""" + with socket.create_connection(("127.0.0.1", port), timeout=10) as conn: + conn.sendall(request) + answer = conn.recv(65536) + if then and answer.startswith(b"HTTP/1.1 200"): + conn.sendall(then) + while chunk := conn.recv(65536): + answer += chunk + return answer + + +def _logged(text: str, kind: str | None = None) -> list[dict]: + """A proxy's log, every line of which is JSON: each line's record, or, given ``kind`` (``connection``, + ``failed``, ...), the records of that kind.""" + entries = [json.loads(line) for line in text.splitlines() if line.strip()] + return entries if kind is None else [entry[kind] for entry in entries if kind in entry] + + +def test_the_stand_in_tunnels_and_forwards_a_public_host_and_logs_each_connection(proxy, capsys): + tunnelled = _ask(proxy.port, b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n", + then=b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n") + forwarded = _ask(proxy.port, b"GET http://site.test/a?b=1 HTTP/1.1\r\nHost: site.test\r\n" + b"Proxy-Connection: keep-alive\r\n\r\n") + + assert tunnelled.startswith(b"HTTP/1.1 200 Connection established") and tunnelled.endswith(b"hello"), tunnelled + assert forwarded.startswith(b"HTTP/1.1 200 OK") and forwarded.endswith(b"hello"), forwarded + assert proxy.opened == [(GITHUB, 443), (GITHUB, 80)] + assert proxy.read[0] == b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n" + assert proxy.read[1].startswith(b"GET /a?b=1 HTTP/1.1\r\n") and b"Host: site.test" in proxy.read[1] + assert b"Proxy-Connection" not in proxy.read[1] and b"Connection: close" in proxy.read[1] + logged = _logged(capsys.readouterr().out, "connection") + assert [(entry["host"], entry["port"], entry["address"]) for entry in logged] == [ + ("site.test", 443, GITHUB), ("site.test", 80, GITHUB)] + assert all(entry["bytes_sent"] > 0 and entry["bytes_received"] > 0 for entry in logged), logged + assert all(set(entry) >= {"started", "duration_s"} for entry in logged) + + +def test_the_stand_in_refuses_nothing_it_reaches_the_computers_own_loopback_and_an_internal_address_alike(proxy): + own = _ask(proxy.port, f"GET http://127.0.0.1:{proxy.upstream_port}/page HTTP/1.1\r\nHost: x\r\n\r\n".encode()) + private = _ask(proxy.port, b"GET http://10.0.0.1/ HTTP/1.1\r\nHost: x\r\n\r\n") + + assert own.startswith(b"HTTP/1.1 200 OK") and private.startswith(b"HTTP/1.1 200 OK"), (own, private) + assert proxy.read[0].startswith(b"GET /page HTTP/1.1") and proxy.opened == [("10.0.0.1", 80)] + + +def test_the_stand_in_answers_a_name_that_doesnt_resolve_at_once_and_logs_it(monkeypatch, capsys): + with _serving(monkeypatch, names={"nowhere.test": []}) as proxy: + answer = _ask(proxy.port, b"CONNECT nowhere.test:443 HTTP/1.1\r\nHost: nowhere.test:443\r\n\r\n") + + assert answer.startswith(b"HTTP/1.1 502 ") and b"nowhere.test doesn't resolve" in answer, answer + assert b"X-Agentenv-Site-Proxy: failed" in answer and proxy.opened == [] + [failed] = _logged(capsys.readouterr().out, "failed") + assert (failed["reason"], failed["error"]) == ("doesn't resolve", "gaierror: [Errno -2] Name or service not known") + + +@pytest.mark.parametrize("first", ["refuses", "hangs"]) +def test_the_stand_in_tries_a_names_addresses_in_turn_within_one_deadline_for_them_all(monkeypatch, capsys, first): + with _serving(monkeypatch, names={"site.test": [GITHUB, "140.82.112.4"]}, acts={GITHUB: first}, + CONNECT_TIMEOUT_S=0.5) as proxy: + began = time.monotonic() + answer = _ask(proxy.port, b"GET http://site.test/ HTTP/1.1\r\nHost: site.test\r\n\r\n") + took = time.monotonic() - began + + if first == "refuses": + assert answer.startswith(b"HTTP/1.1 200 OK") and proxy.opened == [(GITHUB, 80), ("140.82.112.4", 80)], answer + else: + assert answer.startswith(b"HTTP/1.1 502 ") and b"site.test:80 didn't answer" in answer, answer + assert proxy.opened == [(GITHUB, 80)] and took < 1.5, (proxy.opened, took) + [failed] = _logged(capsys.readouterr().out, "failed") + assert (failed["reason"], failed["error"]) == ("no answer", "TimeoutError: timed out") + + +def test_the_stand_in_relays_a_connection_whose_sockets_are_numbered_past_1023(proxy): + soft, hard = resource.getrlimit(resource.RLIMIT_NOFILE) + if hard != resource.RLIM_INFINITY and hard < 1400: + pytest.skip(f"this machine allows {hard} open files, too few to number a socket past 1023") + resource.setrlimit(resource.RLIMIT_NOFILE, (max(soft, 1400) if hard == resource.RLIM_INFINITY else + min(max(soft, 1400), hard), hard)) + held = [os.pipe() for _ in range(520)] + try: + answer = _ask(proxy.port, b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n", + then=b"GET / HTTP/1.1\r\nHost: site.test\r\n\r\n") + finally: + for pair in held: + os.close(pair[0]) + os.close(pair[1]) + resource.setrlimit(resource.RLIMIT_NOFILE, (soft, hard)) + + assert answer.startswith(b"HTTP/1.1 200 Connection established") and answer.endswith(b"hello"), answer + assert proxy.opened == [(GITHUB, 443)] + + +def test_the_stand_in_ends_a_tunnel_whose_server_stopped_reading_instead_of_waiting_on_it_for_ever(monkeypatch, capsys): + with _serving(monkeypatch, acts={GITHUB: "never reads"}, IDLE_S=1) as proxy: + with socket.create_connection(("127.0.0.1", proxy.port), timeout=20) as conn: + conn.sendall(b"CONNECT site.test:443 HTTP/1.1\r\nHost: site.test:443\r\n\r\n") + assert conn.recv(4096).startswith(b"HTTP/1.1 200") + began, ended = time.monotonic(), None + try: + while time.monotonic() - began < 20: + conn.sendall(b"x" * 65536) + except OSError: + ended = time.monotonic() - began + + assert ended is not None and ended < 15, ended + [logged] = _logged(capsys.readouterr().out, "connection") + assert logged["host"] == "site.test" and logged["bytes_sent"] > 0