Skip to content

The agent that grows with you

fix(browser): never start the user's real browser without its sandbox

Tekniuma8f4727bd02f
diff --git a/tests/tools/test_bot_desktop_browser.py b/tests/tools/test_bot_desktop_browser.py
index 124b3b38966..da6429acd6c 100644
--- a/tests/tools/test_bot_desktop_browser.py
+++ b/tests/tools/test_bot_desktop_browser.py
@@ -227,7 +227,8 @@ def _agent_sandbox_flags(exe: str) -> set:
"""The sandbox-bypass flags the agent's launch of ``exe`` on the shared profile carries."""
from tools.browser_launcher import CHROMIUM_SANDBOX_BYPASS_ARGS, build_chromium_argv
- argv = build_chromium_argv(exe, "/p/dir", packaged=False, headed=True, extra_args=browser.SHARED_PROFILE_ARGS)
+ argv = build_chromium_argv(exe, "/p/dir", packaged=False, headed=True, sandbox_bypass=True,
+ extra_args=browser.SHARED_PROFILE_ARGS)
return set(argv) & set(CHROMIUM_SANDBOX_BYPASS_ARGS)
diff --git a/tests/tools/test_browser_launcher.py b/tests/tools/test_browser_launcher.py
index e78bdc125b8..5644285101c 100644
--- a/tests/tools/test_browser_launcher.py
+++ b/tests/tools/test_browser_launcher.py
@@ -203,10 +203,12 @@ def test_headed_only_when_configured_and_a_display_exists(headed, display, expec
def test_packaged_only_flags_never_reach_an_explicit_binary(monkeypatch):
monkeypatch.setattr(launcher, "needs_chromium_sandbox_bypass", lambda: True)
monkeypatch.setattr("tools.browser_session_cloud.chromium_args", lambda: ["--lang=de"])
- packaged = launcher.build_chromium_argv("/c", "/p", packaged=True, headed=False)
- real = launcher.build_chromium_argv("/c", "/p", packaged=False, headed=True)
+ packaged = launcher.build_chromium_argv("/c", "/p", packaged=True, headed=False, sandbox_bypass=True)
+ dock = launcher.build_chromium_argv("/c", "/p", packaged=False, headed=True, sandbox_bypass=True)
+ real = launcher.build_chromium_argv("/c", "/p", packaged=False, headed=True, sandbox_bypass=False)
assert "--password-store=basic" in packaged and "--password-store=basic" not in real
assert "--headless=new" in packaged and "--headless=new" not in real
- for argv in (packaged, real):
- assert set(launcher.CHROMIUM_SANDBOX_BYPASS_ARGS) <= set(argv)
+ assert set(launcher.CHROMIUM_SANDBOX_BYPASS_ARGS) <= set(packaged) and set(launcher.CHROMIUM_SANDBOX_BYPASS_ARGS) <= set(dock)
+ assert not set(launcher.CHROMIUM_SANDBOX_BYPASS_ARGS) & set(real) # the user's own browser keeps its sandbox
+ for argv in (packaged, dock, real):
assert "--lang=de" in argv and argv[-1] == "about:blank"
diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py
index 5e4c1ce6ed9..60e856faf63 100644
--- a/tests/tools/test_browser_real_profile.py
+++ b/tests/tools/test_browser_real_profile.py
@@ -239,7 +239,9 @@ class TestRealProfileCdpLaunch:
``--headless=new`` shares the profile's normal cookie store (unlike legacy ``--headless``), and
cookie decryption is unaffected by headless — the drop we avoid comes from ``--use-mock-keychain`` /
``--password-store=basic``, so neither may reach this launch: the copied auth/login state still
- loads. The copy is persistent: the launch must never wipe it like an ephemeral profile.
+ loads. The copy is persistent: the launch must never wipe it like an ephemeral profile. The user's
+ own browser is never started without its sandbox, even where the packaged Chromium needs the
+ bypass (root / Docker / AppArmor userns).
"""
copy_dir = tmp_path / "copy"
(copy_dir / "Default").mkdir(parents=True)
@@ -249,7 +251,7 @@ class TestRealProfileCdpLaunch:
monkeypatch.setattr(launcher, "_records_dir", lambda: records)
monkeypatch.setattr(launcher, "_start_time", lambda pid: None)
monkeypatch.setattr("tools.browser_session_cloud.chromium_args", list)
- monkeypatch.setattr(launcher, "needs_chromium_sandbox_bypass", lambda: False)
+ monkeypatch.setattr(launcher, "needs_chromium_sandbox_bypass", lambda: True)
captured = {}
class FakeChrome:
@@ -281,6 +283,7 @@ class TestRealProfileCdpLaunch:
# The chrome launch itself is headless (no window, no focus steal).
assert "--headless=new" in argv
assert "--password-store=basic" not in argv and "--use-mock-keychain" not in argv
+ assert not set(launcher.CHROMIUM_SANDBOX_BYPASS_ARGS) & set(argv)
assert (copy_dir / "Default" / "Cookies").read_text(encoding="utf-8") == "copied-cookies"
running = launcher.get_browser(rp.REAL_PROFILE_KEY)
assert running is not None and running.owned and not running.ephemeral
diff --git a/tools/browser_launcher.py b/tools/browser_launcher.py
index 9506188e4de..e5d48a0a234 100644
--- a/tools/browser_launcher.py
+++ b/tools/browser_launcher.py
@@ -201,8 +201,12 @@ def _configured_chromium_args() -> list[str]:
def build_chromium_argv(executable: str, user_data_dir: str, *, packaged: bool, headed: bool,
- extra_args=()) -> list[str]:
- """The launch argv (pure in its inputs + host sandbox facts + ``browser.chromium_args``)."""
+ sandbox_bypass: bool, extra_args=()) -> list[str]:
+ """The launch argv (pure in its inputs + host sandbox facts + ``browser.chromium_args``).
+
+ ``sandbox_bypass`` lets the host policy (root, Docker, AppArmor userns) add
+ ``CHROMIUM_SANDBOX_BYPASS_ARGS``; it is False for the user's own browser binary, which is never
+ started without its sandbox."""
argv = [executable, "--remote-debugging-port=0", f"--user-data-dir={user_data_dir}", *_BASE_FLAGS]
if packaged:
argv += _PACKAGED_ONLY_FLAGS
@@ -211,7 +215,7 @@ def build_chromium_argv(executable: str, user_data_dir: str, *, packaged: bool,
if not headed:
argv.append("--headless=new")
configured = _configured_chromium_args()
- if needs_chromium_sandbox_bypass():
+ if sandbox_bypass and needs_chromium_sandbox_bypass():
argv += [flag for flag in CHROMIUM_SANDBOX_BYPASS_ARGS if flag not in configured]
argv += [str(a) for a in extra_args]
argv += configured
@@ -359,14 +363,15 @@ def _attach_running(key: str, user_data_dir: str, ephemeral: bool, home_key: str
def launch_chromium(key: str, *, executable: Optional[str] = None, user_data_dir: Optional[str] = None,
ephemeral: bool = True, headed: Optional[bool] = None, extra_args=(),
- env: Optional[dict] = None, timeout: Optional[float] = None,
+ env: Optional[dict] = None, timeout: Optional[float] = None, sandbox_bypass: bool = True,
) -> tuple[Optional[LaunchedBrowser], Optional[str]]:
"""Start (or reuse) the Chromium for ``key``; ``(browser, None)`` or ``(None, error)``.
``executable=None`` resolves the packaged Chromium (``chromium_ready``); an explicit executable (the
Bot Desktop's, the user's real browser) is used as given. ``user_data_dir=None`` is a fresh
ephemeral profile under ``<HERMES_HOME>/cache/browser/chromium/``; a persistent dir that already has
- a live Chromium on it is ATTACHED (``owned=False``, never killed)."""
+ a live Chromium on it is ATTACHED (``owned=False``, never killed). ``sandbox_bypass=False`` (the
+ user's real browser) never gets the host's sandbox-bypass flags."""
from hermes_constants import hermes_home_key
with _key_lock(key): # one launch per key; other keys launch concurrently
existing = get_browser(key)
@@ -386,7 +391,8 @@ def launch_chromium(key: str, *, executable: Optional[str] = None, user_data_dir
_browsers[key] = attached
return attached, None
return _launch_owned(key, executable, udd, ephemeral=ephemeral, packaged=packaged, headed=headed,
- extra_args=extra_args, env=env, timeout=timeout, home_key=home_key)
+ extra_args=extra_args, env=env, timeout=timeout, home_key=home_key,
+ sandbox_bypass=sandbox_bypass)
def _key_lock(key: str) -> threading.Lock:
@@ -395,7 +401,8 @@ def _key_lock(key: str) -> threading.Lock:
def _launch_owned(key: str, executable: str, udd: str, *, ephemeral: bool, packaged: bool, headed,
- extra_args, env, timeout, home_key: str) -> tuple[Optional[LaunchedBrowser], Optional[str]]:
+ extra_args, env, timeout, home_key: str, sandbox_bypass: bool,
+ ) -> tuple[Optional[LaunchedBrowser], Optional[str]]:
if env is None:
from tools.browser_session import build_browser_env
env = build_browser_env()
@@ -404,7 +411,7 @@ def _launch_owned(key: str, executable: str, udd: str, *, ephemeral: bool, packa
except OSError as exc:
return None, f"Could not create the browser profile directory {udd}: {exc}"
argv = build_chromium_argv(executable, udd, packaged=packaged, headed=_resolve_headed(headed, env),
- extra_args=extra_args)
+ sandbox_bypass=sandbox_bypass, extra_args=extra_args)
try:
proc = _spawn(argv, env, udd)
except (OSError, subprocess.SubprocessError) as exc:
diff --git a/tools/browser_session_real_profile.py b/tools/browser_session_real_profile.py
index 420c33088fe..f986c5ec242 100644
--- a/tools/browser_session_real_profile.py
+++ b/tools/browser_session_real_profile.py
@@ -91,7 +91,8 @@ def _launch(browser_name: str) -> tuple[Optional[str], Optional[str]]:
return None, f"{_RP}the real browser binary for '{browser_name}' could not be found. Reinstall it or turn the toggle off."
ensure_screen_for_headed_chromium()
launched, err = launch_chromium(REAL_PROFILE_KEY, executable=real_binary, user_data_dir=copy_dir,
- ephemeral=False, extra_args=_REAL_PROFILE_EXTRA_FLAGS, timeout=30.0)
+ ephemeral=False, extra_args=_REAL_PROFILE_EXTRA_FLAGS, timeout=30.0,
+ sandbox_bypass=False)
if launched is None:
return None, f"{_RP}the real-profile browser failed to start: {err}"
logger.info("real-profile browser ready for %s at %s (%s)", browser_name, launched.http_url, copy_dir)
diff --git a/tools/browser_session_sandbox.py b/tools/browser_session_sandbox.py
index a69141704f1..3484aadccf5 100644
--- a/tools/browser_session_sandbox.py
+++ b/tools/browser_session_sandbox.py
@@ -45,9 +45,9 @@ def _start_chromium(env, user, exe: str, udd: str, child_env: dict) -> int:
from tools.bot_desktop.browser import SHARED_PROFILE_ARGS
from tools.browser_launcher import CHROMIUM_SANDBOX_BYPASS_ARGS, build_chromium_argv
from tools.environments import streams
- # Container: no user namespaces for Chromium's own sandbox, so the bypass flags always apply; the
- # shared-profile flags match the dock's icon on this same profile.
- argv = build_chromium_argv(exe, udd, packaged=False, headed=True,
+ # Container: no user namespaces for Chromium's own sandbox, so the bypass flags always apply (this
+ # host's facts say nothing about the sandbox); the shared-profile flags match the dock's icon.
+ argv = build_chromium_argv(exe, udd, packaged=False, headed=True, sandbox_bypass=False,
extra_args=(*SHARED_PROFILE_ARGS, *CHROMIUM_SANDBOX_BYPASS_ARGS))
argv = list(dict.fromkeys(argv))
script = (f"mkdir -p {shlex.quote(udd)} && rm -f {shlex.quote(udd)}/DevToolsActivePort && "