Skip to content

Commit cfb5d65

Browse files
feat(browsers): chromium makes no background requests of its own (FD-5113, M8.52) (#596)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 68c7de8 commit cfb5d65

8 files changed

Lines changed: 130 additions & 25 deletions

File tree

‎src/agent_env/browsers/README.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ display, and the `page` driver, whose screen is the browser's active tab, headle
77
build Playwright 1.64.1 pins, fetched by sha256 when a computer env that lists it is put and unpacked into
88
`/opt/agentenv/apps/chromium/` by its install step. It's a website browser
99
(`agentenv_protocol.website_browser.WebsiteBrowserEnvironment`): agentenv launches it on a debugging pipe with its own
10-
flags, and writes managed policies to `/etc/opt/chrome_for_testing/policies/managed/` for unattended use. It runs
10+
flags, and writes managed policies to `/etc/opt/chrome_for_testing/policies/managed/` for unattended use. Flags and policies of its own
11+
(`QUIET_FLAGS`, `DISABLED_FEATURES` and `QUIET_POLICIES` in `chromium.py`, found against this build, to check again
12+
when its version moves) stop the requests Chrome makes of its own: component and Safe Browsing updates, the network
13+
time, a spellcheck dictionary, the AI Mode check, autofill predictions and its search engine's preconnect. Its proxy
14+
sees only its pages' requests, but for one Google account check at launch. It runs
1115
without Chromium's sandbox, which a container can't give it. Its env.toml spells out its settings and their defaults
1216
under `[settings]`: `start_urls`, opened at boot in order, the first tab active, each waited on to load; `window`,
1317
alone only; `locale`; `time_zone`; and

‎src/agent_env/browsers/envs/chromium/chromium.py‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""agentenv's chromium app: Chrome for Testing at the build Playwright 1.64.1 pins, for amd64 and arm64 (Google ships
2-
no Linux arm64 Chrome), a website browser whose flags, policies and settings agentenv's base class gives it. Its data is
2+
no Linux arm64 Chrome), a website browser whose flags, policies and settings agentenv's base class gives it, with the
3+
flags and policies of its own that stop the requests this build makes of its own. Its data is
34
its profile, which the image has none of, so every deploy and every reset starts with a clean one."""
45
import subprocess
56
from pathlib import Path
@@ -25,18 +26,43 @@
2526
PROFILE = ".config/chromium"
2627
#: The shared memory, in MiB, Chrome's renderers get in /dev/shm, so a heavy page doesn't fill Docker's 64 MiB.
2728
SHM_MB = 1024
29+
# What stops the requests this build makes of its own, found against VERSION behind a browser alone's proxy: check them
30+
# again when VERSION moves. One is left, a Google account check at launch, which none of them stops.
31+
#: Flags: Safe Browsing's list updates, among its other background services; and the component updater's checks, at
32+
#: launch and a minute later, pointed at a file it can't fetch, since ComponentUpdatesEnabled leaves the components
33+
#: Chrome deems critical, such as its CRLSets, checking in.
34+
QUIET_FLAGS = ["--disable-background-networking", "--component-updater=url-source=file:///dev/null"]
35+
#: Features off: the AI Mode eligibility check at launch, which no policy stops; and the preconnect to its search engine
36+
#: at launch, which NetworkPredictionOptions would stop only by turning off the preconnects and prefetches it makes for
37+
#: pages too.
38+
DISABLED_FEATURES = ["AimEnabled", "PreconnectToSearch"]
39+
#: Every language Chromium has a spellcheck dictionary for, from its spellcheck_common.cc: blocklisted, so it fetches
40+
#: none for the deploy's locale at launch. SpellcheckEnabled false would void the blocklist and still fetch it.
41+
SPELLCHECK_LANGUAGES = ["af", "bg", "ca", "cs", "cy", "da", "de", "de-DE", "el", "en", "en-AU", "en-CA", "en-GB",
42+
"en-GB-oxendict", "en-US", "es", "es-419", "es-AR", "es-ES", "es-MX", "es-US", "et", "fa", "fo",
43+
"fr", "fr-FR", "gl", "he", "hi", "hr", "hu", "hy", "id", "it", "it-IT", "ko", "lt", "lv", "nb",
44+
"nl", "pl", "pt", "pt-BR", "pt-PT", "ro", "ru", "sh", "sk", "sl", "sq", "sr", "sv", "ta", "tg",
45+
"tr", "uk", "vi"]
46+
#: Policies, over agentenv's: the network time query, spellcheck dictionaries, and the autofill server's field
47+
#: predictions for a page's forms, which agentenv's AutofillSettings leaves on in this build.
48+
QUIET_POLICIES = {"BrowserNetworkTimeQueriesEnabled": False, "SpellcheckLanguageBlocklist": SPELLCHECK_LANGUAGES,
49+
"AutofillAddressEnabled": False, "AutofillCreditCardEnabled": False}
50+
COMMAND = ["chromium/chrome/chrome", "--no-sandbox", f"--class={WINDOW_CLASS}", *QUIET_FLAGS,
51+
f"--disable-features={','.join(DISABLED_FEATURES)}"]
2852

2953

3054
@computer_app(supports=[{"os": {"type": "linux", "display": ["x11", "none"]}, "arch": ["amd64", "arm64"]}])
3155
@linux(packages=Packages(apt=PACKAGES), downloads=DOWNLOADS, paths=[PROFILE], shm_mb=SHM_MB,
32-
launch=Launch(at="boot", command=["chromium/chrome/chrome", "--no-sandbox", f"--class={WINDOW_CLASS}"],
33-
window={"class": WINDOW_CLASS}, ready=Ready(window=True)))
56+
launch=Launch(at="boot", command=COMMAND, window={"class": WINDOW_CLASS}, ready=Ready(window=True)))
3457
class Chromium(WebsiteBrowserEnvironment):
35-
"""Chrome for Testing without its sandbox, which a container can't give it."""
58+
"""Chrome for Testing without its sandbox, which a container can't give it, and with none of the requests it makes
59+
of its own but one: a subclass that declares its own launch keeps ``QUIET_FLAGS`` and ``DISABLED_FEATURES`` in its
60+
command."""
3661

3762
version = VERSION
3863
#: Where Chrome for Testing reads managed policies; it reads neither Chromium's folder nor Chrome's.
3964
policy_dir = Path("/etc/opt/chrome_for_testing/policies/managed")
65+
policies = QUIET_POLICIES
4066
profile = PROFILE
4167

4268
@install_step(scope="system", os="linux")

‎src/agent_env/computers/artifacts/ubuntu-24.04-headless/site-proxy.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
it, absolute-form HTTP and CONNECT tunnels alike, to the host it names, refusing nothing and never decrypting a tunnel.
33
The sandbox's own network policy still decides where a connection can go. Every line it logs is JSON, in the same
44
shapes as the shared site proxy's: ``connection`` (host, port, address, bytes each way, start, duration) when one
5-
ends, and ``failed`` (method, host, port, ``reason`` "doesn't resolve" or "no answer", and the error) for one that
6-
couldn't open."""
5+
ends, and ``failed`` (method, host, port, ``reason`` "doesn't resolve" or "no answer", the error, and when it was
6+
asked for) for one that couldn't open."""
77
import http.server
88
import json
99
import selectors
@@ -22,6 +22,10 @@ def log(**fields) -> None:
2222
print(json.dumps(fields), flush=True)
2323

2424

25+
def _when(at: float) -> str:
26+
return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(at))
27+
28+
2529
def _error(error: BaseException) -> str:
2630
return f"{type(error).__name__}: {error}".removesuffix(": ")
2731

@@ -63,12 +67,12 @@ def _open(self, host: str, port: int, method: str) -> tuple[socket.socket, str]
6367
"""A connection to an address ``host`` resolves to, tried in turn within one ``CONNECT_TIMEOUT_S`` for them
6468
all, and that address; or None once the browser has its answer, a 502 for a host that doesn't resolve or
6569
answer. The connection keeps an ``IDLE_S`` timeout, so a write to a peer that stopped reading ends too."""
66-
deadline = time.monotonic() + CONNECT_TIMEOUT_S
70+
asked, deadline = time.time(), time.monotonic() + CONNECT_TIMEOUT_S
6771
try:
6872
found = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM)
6973
except OSError as error:
7074
log(failed={"method": method, "host": host, "port": port, "reason": "doesn't resolve",
71-
"error": _error(error)})
75+
"error": _error(error), "started": _when(asked)})
7276
self._answer(502, f"{host} doesn't resolve\n")
7377
return None
7478
last = None
@@ -83,7 +87,7 @@ def _open(self, host: str, port: int, method: str) -> tuple[socket.socket, str]
8387
upstream.settimeout(IDLE_S)
8488
return upstream, address
8589
log(failed={"method": method, "host": host, "port": port, "reason": "no answer",
86-
"error": _error(last) if last else f"none within {CONNECT_TIMEOUT_S} s"})
90+
"error": _error(last) if last else f"none within {CONNECT_TIMEOUT_S} s", "started": _when(asked)})
8791
self._answer(502, f"{host}:{port} didn't answer\n")
8892
return None
8993

@@ -110,7 +114,7 @@ def _relay(self, upstream: socket.socket, address: str, host: str, port: int, fi
110114
finally:
111115
log(connection={"host": host, "port": port, "address": address, "bytes_sent": sent,
112116
"bytes_received": received,
113-
"started": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(started)),
117+
"started": _when(started),
114118
"duration_s": round(time.monotonic() - began, 3)})
115119

116120
def log_message(self, *args):

‎src/agent_env/env/envs/computer/utils/site_proxy.py‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,17 @@ def matches(pattern: str, host: str) -> bool:
109109
def log_event(**fields) -> None:
110110
"""One JSON line in the proxy's log, every line it writes being one: ``connection`` (host, port, address, bytes each
111111
way, start, duration) when a connection to a live site's host ends, ``failed`` (method, host, port, ``reason``
112-
"doesn't resolve" or "no answer", and the error) for one that couldn't open, both in the same shapes as the
112+
"doesn't resolve" or "no answer", the error, and when it was asked for) for one that couldn't open, both in the same shapes as the
113113
headless base's stand-in proxy's; ``refused`` for a host that's no site, ``error`` for a connection that failed
114114
unexpectedly, and ``restarted`` when ``keep`` starts the proxy again."""
115115
print(json.dumps(fields), file=sys.stderr, flush=True)
116116

117117

118+
def when(at: float) -> str:
119+
"""A log line's time, to the second, in UTC."""
120+
return time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(at))
121+
122+
118123
def described(error: BaseException) -> str:
119124
return f"{type(error).__name__}: {error}".removesuffix(": ")
120125

@@ -482,13 +487,14 @@ async def _direct(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, ho
482487
else ``head`` sent on; then both ends relayed until either closes, and the connection logged (``log_event``).
483488
One that doesn't resolve or answer within ``UPSTREAM_TIMEOUT_S``, the lookup and every address's attempt
484489
together, is a 502, logged too."""
490+
asked = time.time()
485491
try:
486492
up_reader, up_writer = await asyncio.wait_for(asyncio.open_connection(host, port), UPSTREAM_TIMEOUT_S)
487493
except (OSError, asyncio.TimeoutError) as e:
488494
unresolved = isinstance(e, socket.gaierror)
489495
log_event(failed={"method": "CONNECT" if head is None else head.split(b" ", 1)[0].decode("latin-1"),
490496
"host": host, "port": port, "reason": "doesn't resolve" if unresolved else "no answer",
491-
"error": described(e)})
497+
"error": described(e), "started": when(asked)})
492498
writer.write(simple_response("502 Bad Gateway", f"{host} doesn't resolve\n" if unresolved else
493499
f"{host}:{port} didn't answer\n", FAILED))
494500
return
@@ -504,7 +510,7 @@ async def _direct(reader: asyncio.StreamReader, writer: asyncio.StreamWriter, ho
504510
finally:
505511
log_event(connection={"host": host, "port": port, "address": address, "bytes_sent": sent.bytes,
506512
"bytes_received": received.bytes,
507-
"started": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(started)),
513+
"started": when(started),
508514
"duration_s": round(time.monotonic() - began, 3)})
509515

510516
@staticmethod

‎tst/integration/computer/browser_computer_test.py‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
snapshot back and holds up under chaos; the browser task steps set its cookies and storage and open, activate and
1616
close its tabs by URL; a task whose deploy_env pins a version of the browser, its latest one with none, passes
1717
preflight on the version pinned and runs its browser steps through task.run; and each start tab's page sees the
18-
window's size from its first script. Routing sites, it goes out only through the shared site proxy's forward port:
18+
window's size from its first script. Alone, it makes no request of its own, its proxy seeing only the page an agent
19+
loads for well over a minute after it's ready, but for one Google account check at launch. Routing sites, it goes out only through the shared site proxy's forward port:
1920
start tabs on a site over HTTPS and on a .localhost one load through it, an event stream comes through as it's sent,
2021
any other host and loopback are refused and logged, the session user can't stop the port, killed it comes back, and
2122
after a reset it serves the sites and refuses the rest again.
2223
Needs Docker and the local registry on
2324
localhost:5000, and Node 20 or later and npm for the CDP clients."""
2425

2526
import asyncio
27+
import calendar
2628
import itertools
2729
import json
2830
import platform
@@ -710,6 +712,47 @@ async def test_alone_on_the_open_web_it_loads_a_public_site_through_its_proxy_an
710712
assert not report.failed
711713

712714

715+
#: How long after the browser is ready its proxy log is read: past the component updater's check a minute after launch.
716+
QUIET_S = 75.0
717+
#: The one request the browser makes of its own that nothing stops: a Google account check at launch.
718+
ACCOUNT_CHECK = "accounts.google.com"
719+
720+
721+
@pytest.mark.asyncio
722+
async def test_alone_it_makes_no_request_of_its_own_its_proxy_seeing_only_its_page_until_well_after_it_is_ready(
723+
browser_computer):
724+
with namespace_routing():
725+
deployed = await Env.get(browser_computer.id).deploy()
726+
ready = time.time()
727+
context = TaskStepContext(deployed_envs=[deployed])
728+
try:
729+
serving = asyncio.ensure_future(inside.run(deployed, ["python3", "-c", SERVE_SITE, "150"], timeout_s=180))
730+
await asyncio.sleep(SERVE_START_S)
731+
seen = await _cdp_agent_loads((await _cdp(deployed, "ada"))["ws_url"], [SITE])
732+
await asyncio.sleep(max(0.0, ready + QUIET_S - time.time()))
733+
container = await _container_of(deployed)
734+
# Chromium ended ends each connection it holds open, which its proxy logs as it ends.
735+
_as_root(container, "pkill -f '[c]hromium/chrome/chrome'; sleep 3")
736+
logged = _as_root(container, "cat /var/log/agentenv/site-proxy.log")
737+
serving.cancel()
738+
finally:
739+
report = await teardown_run(context)
740+
741+
assert seen[SITE]["title"] == "Home" and not seen[SITE]["error"], seen
742+
entries = [(kind, entry) for line in logged.splitlines() if line.startswith("{")
743+
for kind, entry in json.loads(line).items() if kind in ("connection", "failed")]
744+
own = [(kind, entry) for kind, entry in entries if (entry["host"], entry["port"]) != ("127.0.0.1", 8771)]
745+
assert any(kind == "connection" and (entry["host"], entry["port"]) == ("127.0.0.1", 8771)
746+
for kind, entry in entries), logged
747+
assert all(entry["host"] == ACCOUNT_CHECK for _, entry in own), own
748+
assert all(_epoch(entry["started"]) <= ready for _, entry in own), (ready, own)
749+
assert not report.failed
750+
751+
752+
def _epoch(stamp: str) -> float:
753+
return calendar.timegm(time.strptime(stamp, "%Y-%m-%dT%H:%M:%SZ"))
754+
755+
713756
async def _kill(deployed, pattern: str, oldest: bool = False) -> None:
714757
"""SIGKILL the newest, or oldest, process matching ``pattern``, a second after computer-exec has answered, since the
715758
driver that runs the command may be the one killed."""

‎tst/integration/computer/chromium_app_test.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,11 @@ async def test_it_runs_chrome_itself_on_its_pipe_answers_prompts_as_the_deploy_s
190190

191191
argv = browser["argv"]
192192
assert argv[0].endswith("chromium/chrome/chrome") and argv[1:3] == ["--no-sandbox", "--class=Chromium"]
193-
assert argv[3].endswith("/.config/chromium") and argv[4].endswith("/.cache/chromium")
194-
assert argv[5:9] == ["--remote-debugging-pipe", "--no-first-run", "--no-default-browser-check", "--lang=en-US"]
195-
assert argv[9:] == ["about:blank"]
193+
assert argv[3:6] == ["--disable-background-networking", "--component-updater=url-source=file:///dev/null",
194+
"--disable-features=AimEnabled,PreconnectToSearch"]
195+
assert argv[6].endswith("/.config/chromium") and argv[7].endswith("/.cache/chromium")
196+
assert argv[8:12] == ["--remote-debugging-pipe", "--no-first-run", "--no-default-browser-check", "--lang=en-US"]
197+
assert argv[12:] == ["about:blank"]
196198
assert browser["listening"] == 0
197199
assert permissions == ["denied prompt - Google Chrome for Testing"]
198200
assert launched["exit_code"] == 0 and len(windows) == 2 and after["pid"] == browser["pid"]

‎tst/unit/env/envs/test_chromium_app.py‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""agentenv's chromium app as a put reads it: the installed ``browsers`` bundle names it ``chromium``; its class, a website browser, declares the pinned Chrome for Testing archive for each arch and its
22
packages, its profile as its data, 1 GiB of shared memory, a launch of Chrome itself matched by its window
3-
class, the base's settings, and its install step, which unpacks the archive keeping its execute bits, beside
3+
class with the flags that stop the requests this build makes of its own, and the policies that do too over
4+
agentenv's, the base's settings, and its install step, which unpacks the archive keeping its execute bits, beside
45
the base's, which writes the policies."""
6+
import json
57
import os
68
import sys
79
import zipfile
@@ -10,7 +12,7 @@
1012

1113
from agentenv_protocol.computer._load import load_class
1214
from agentenv_protocol.computer.app import ComputerAppEnvironment
13-
from agentenv_protocol.website_browser import BrowserSettings, WebsiteBrowserEnvironment
15+
from agentenv_protocol.website_browser import POLICIES, BrowserSettings, WebsiteBrowserEnvironment
1416

1517
from agent_env.bundle import parse_bundle
1618
from agent_env.bundle.installed import find_bundle
@@ -44,14 +46,32 @@ def test_chromium_is_a_website_browser_that_declares_its_archives_profile_launch
4446
assert all(f"/chrome-for-testing-public/{chromium.VERSION}/" in d["url"] for d in linux.install["downloads"])
4547
assert {"libnss3", "libasound2t64", "unzip"} <= set(linux.install["packages"]["apt"])
4648
assert linux.paths == [".config/chromium"] and linux.install["shm_mb"] == chromium.SHM_MB == 1024
47-
assert linux.launch.command == ["chromium/chrome/chrome", "--no-sandbox", "--class=Chromium"]
49+
assert linux.launch.command == ["chromium/chrome/chrome", "--no-sandbox", "--class=Chromium",
50+
"--disable-background-networking",
51+
"--component-updater=url-source=file:///dev/null",
52+
"--disable-features=AimEnabled,PreconnectToSearch"]
4853
assert linux.launch.at == "boot" and linux.launch.window == {"class": "Chromium"} and linux.launch.ready.window
4954
assert {(step.name, step.scope) for step in declared.install_steps} == {("chromium", "system"),
5055
("write_policies", "system")}
5156
assert declared.settings == BrowserSettings.model_json_schema()
5257
assert declared.supports[0]["arch"] == ["amd64", "arm64"]
5358

5459

60+
def test_its_policies_stop_what_this_build_asks_for_of_its_own_beside_agentenvs_and_spellcheck_stays_blocklisted(
61+
chromium, monkeypatch, tmp_path):
62+
monkeypatch.setattr(chromium.Chromium, "policy_dir", tmp_path / "policies")
63+
64+
chromium.Chromium.__new__(chromium.Chromium).write_policies()
65+
66+
written = json.loads((tmp_path / "policies" / "agentenv.json").read_text())
67+
assert written == {**POLICIES, **chromium.QUIET_POLICIES}
68+
assert written["BrowserNetworkTimeQueriesEnabled"] is False and written["AutofillAddressEnabled"] is False
69+
assert {"en-US", "fr-FR", "de", "es-419", "pt-BR"} <= set(written["SpellcheckLanguageBlocklist"])
70+
# SpellcheckEnabled false would void the blocklist and still fetch a dictionary; NetworkPredictionOptions would
71+
# turn off Chrome's preconnects for pages, where a feature stops its search engine's alone.
72+
assert "SpellcheckEnabled" not in written and "NetworkPredictionOptions" not in written
73+
74+
5575
def test_its_install_unpacks_the_archive_with_its_execute_bits_in_its_place(chromium, tmp_path):
5676
(tmp_path / "chromium").mkdir()
5777
with zipfile.ZipFile(tmp_path / "chromium" / "chrome-amd64.zip", "w") as archive:

0 commit comments

Comments
 (0)