From f5f6da9f6a669bfd7265979aa9e41f2228390101 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 26 Aug 2026 05:00:20 -0700 Subject: [PATCH 01/12] feat(browser): consent-gated real default-Chromium profile for local browsing + local_browser arg --- apps/desktop/src/app/settings/constants.ts | 20 +- hermes_cli/browser_connect.py | 215 +++++++++++++++++++++ hermes_cli/config_defaults.py | 11 ++ tests/tools/test_browser_real_profile.py | 122 ++++++++++++ tools/browser_tool.py | 114 ++++++++++- 5 files changed, 475 insertions(+), 7 deletions(-) create mode 100644 tests/tools/test_browser_real_profile.py diff --git a/apps/desktop/src/app/settings/constants.ts b/apps/desktop/src/app/settings/constants.ts index 03c19496fd732..13d71393445a7 100644 --- a/apps/desktop/src/app/settings/constants.ts +++ b/apps/desktop/src/app/settings/constants.ts @@ -1,6 +1,7 @@ import { Box, Brain, + Globe, type IconComponent, Lock, MessageCircle, @@ -426,7 +427,8 @@ export const FIELD_LABELS: Record = defineFieldCopy({ }, browser: { allowPrivateUrls: 'Browser Private URLs', - autoLocalForPrivateUrls: 'Local Browser For Private URLs' + autoLocalForPrivateUrls: 'Local Browser For Private URLs', + useRealProfile: 'Use My Real Browser Profile' }, checkpoints: { enabled: 'File Checkpoints', @@ -554,6 +556,10 @@ export const FIELD_DESCRIPTIONS: Record = defineFieldCopy({ repoScanExcludePaths: 'Folders and their descendants to skip during repository discovery.' }, timezone: 'IANA timezone identifier. Blank uses the system timezone.', + browser: { + useRealProfile: + "Local browser use drives your real default browser profile — its logins, cookies, and history. Backend-independent: applies whenever a local Chromium is used, and lets the agent choose a local real-profile session even under a cloud backend. Only Chromium browsers (Chrome, Edge, Brave, Chromium) are supported; a non-Chromium default fails with a clear message. Off by default." + }, agent: { imageInputMode: 'Controls how image attachments are sent to the model.', maxTurns: 'Upper bound for tool-calling turns before Hermes stops a run.' @@ -667,11 +673,19 @@ export const SECTIONS: DesktopConfigSection[] = [ 'command_allowlist', 'security.redact_secrets', 'security.allow_private_urls', - 'browser.allow_private_urls', - 'browser.auto_local_for_private_urls', 'checkpoints.enabled' ] }, + { + id: 'browser', + label: 'Browser', + icon: Globe, + keys: [ + 'browser.use_real_profile', + 'browser.allow_private_urls', + 'browser.auto_local_for_private_urls' + ] + }, { id: 'memory', label: 'Memory & Context', diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py index af1b04eaee0a0..c3f2657d2bb4a 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -3,6 +3,7 @@ from __future__ import annotations import logging +import ntpath import os import platform import posixpath @@ -11,6 +12,7 @@ import subprocess import time from dataclasses import dataclass, field +from pathlib import Path from hermes_constants import get_hermes_home @@ -76,6 +78,219 @@ _LINUX_INSTALL_PATHS = tuple(path for _, paths in _LINUX_BROWSER_GROUPS for path in paths) +# --------------------------------------------------------------------------- +# Real-profile (default Chromium) resolution +# +# Used by the browser tool's ``browser.use_real_profile`` consent path: when a +# local Chromium is launched, point agent-browser at the user's REAL default +# browser profile (``--profile `` + ``--executable-path``) so +# their live logins/cookies are available. Only Chromium-family browsers are +# supported; a non-Chromium default (Firefox, Safari) resolves to None and the +# caller fails closed with a clear message. +# --------------------------------------------------------------------------- + +# Canonical Chromium browser keys we support for real-profile driving. +_CHROMIUM_BROWSERS = ("chrome", "edge", "brave", "chromium") + +# Windows UserChoice ProgId prefixes → canonical browser key. Matched +# case-insensitively by prefix so channel/version suffixes (e.g. +# ``ChromeHTML.X``, ``MSEdgeHTM``) still resolve. +_WINDOWS_PROGID_MAP = ( + ("chromehtml", "chrome"), + ("msedgehtm", "edge"), + ("bravehtml", "brave"), + ("chromiumhtm", "chromium"), +) + +# Linux xdg default-web-browser .desktop name fragments → canonical key. +_LINUX_DESKTOP_MAP = ( + ("google-chrome", "chrome"), + ("chromium", "chromium"), + ("brave", "brave"), + ("microsoft-edge", "edge"), + ("msedge", "edge"), +) + +# macOS LaunchServices bundle-id fragments → canonical key. +_DARWIN_BUNDLE_MAP = ( + ("com.google.chrome", "chrome"), + ("com.microsoft.edgemac", "edge"), + ("com.brave.browser", "brave"), + ("org.chromium.chromium", "chromium"), +) + + +def _real_profile_relparts(browser: str) -> tuple: + """(mac_support_subdir, windows_localappdata_parts, linux_config_name).""" + return { + "chrome": ( + ("Google", "Chrome"), + ("Google", "Chrome", "User Data"), + "google-chrome", + ), + "edge": ( + ("Microsoft Edge",), + ("Microsoft", "Edge", "User Data"), + "microsoft-edge", + ), + "brave": ( + ("BraveSoftware", "Brave-Browser"), + ("BraveSoftware", "Brave-Browser", "User Data"), + "BraveSoftware/Brave-Browser", + ), + "chromium": ( + ("Chromium",), + ("Chromium", "User Data"), + "chromium", + ), + }[browser] + + +def real_profile_data_dir(browser: str, system: str | None = None) -> str | None: + """Return the default user-data-dir for a Chromium ``browser`` on ``system``. + + Returns None for unknown browsers. Does not check existence — callers that + need that should stat the result. Paths are built with the TARGET system's + separator (posix for Darwin/Linux, backslash for Windows) so an explicit + ``system`` argument resolves correctly regardless of the host OS. + """ + if browser not in _CHROMIUM_BROWSERS: + return None + system = system or platform.system() + mac_parts, win_parts, linux_name = _real_profile_relparts(browser) + home = os.path.expanduser("~") + if system == "Darwin": + return posixpath.join(home, "Library", "Application Support", *mac_parts) + if system == "Windows": + local = os.environ.get("LOCALAPPDATA") or ntpath.join(home, "AppData", "Local") + return ntpath.join(local, *win_parts) + # Linux / other POSIX + config = os.environ.get("XDG_CONFIG_HOME") or posixpath.join(home, ".config") + return posixpath.join(config, *linux_name.split("/")) + + +def chromium_executable(browser: str, system: str | None = None) -> str | None: + """Return the first present executable for a Chromium ``browser``.""" + if browser not in _CHROMIUM_BROWSERS: + return None + system = system or platform.system() + + def first_present(paths: tuple) -> str | None: + for p in paths: + if p and os.path.isfile(p): + return p + return None + + if system == "Darwin": + app = { + "chrome": "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", + "chromium": "/Applications/Chromium.app/Contents/MacOS/Chromium", + "brave": "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser", + "edge": "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge", + }[browser] + return app if os.path.isfile(app) else None + if system == "Windows": + groups = { + "chrome": (("Google", "Chrome", "Application", "chrome.exe"),), + "chromium": (("Chromium", "Application", "chrome.exe"), ("Chromium", "Application", "chromium.exe")), + "brave": (("BraveSoftware", "Brave-Browser", "Application", "brave.exe"),), + "edge": (("Microsoft", "Edge", "Application", "msedge.exe"),), + }[browser] + bases = [ + os.environ.get("PROGRAMFILES", r"C:\Program Files"), + os.environ.get("PROGRAMFILES(X86)", r"C:\Program Files (x86)"), + os.environ.get("LOCALAPPDATA", str(Path.home() / "AppData" / "Local")), + ] + cands = tuple(os.path.join(base, *parts) for base in bases for parts in groups) + return first_present(cands) + # Linux + linux = { + "chrome": ("google-chrome", "google-chrome-stable"), + "chromium": ("chromium-browser", "chromium"), + "brave": ("brave-browser", "brave-browser-stable", "brave"), + "edge": ("microsoft-edge", "microsoft-edge-stable"), + }[browser] + for name in linux: + found = shutil.which(name) + if found: + return found + # fall back to the known absolute paths from the launch tables + for names, paths in _LINUX_BROWSER_GROUPS: + if any(n in linux for n in names): + hit = first_present(tuple(paths)) + if hit: + return hit + return None + + +def _detect_default_windows() -> str | None: + try: + import winreg # type: ignore + except Exception: + return None + try: + key = winreg.OpenKey( + winreg.HKEY_CURRENT_USER, + r"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\https\UserChoice", + ) + prog_id, _ = winreg.QueryValueEx(key, "ProgId") + winreg.CloseKey(key) + except Exception: + return None + low = str(prog_id or "").lower() + for prefix, browser in _WINDOWS_PROGID_MAP: + if low.startswith(prefix): + return browser + return None + + +def _detect_default_darwin() -> str | None: + # LaunchServices handler for the https scheme. + for reader in ( + ["defaults", "read", "com.apple.LaunchServices/com.apple.launchservices.secure", "LSHandlers"], + ): + try: + out = subprocess.run(reader, capture_output=True, text=True, timeout=5).stdout.lower() + except Exception: + out = "" + for frag, browser in _DARWIN_BUNDLE_MAP: + if frag in out and "https" in out: + return browser + # Fallback: first installed Chromium app wins. + for browser in _CHROMIUM_BROWSERS: + if chromium_executable(browser, "Darwin"): + return browser + return None + + +def _detect_default_linux() -> str | None: + try: + out = subprocess.run( + ["xdg-settings", "get", "default-web-browser"], + capture_output=True, text=True, timeout=5, + ).stdout.strip().lower() + except Exception: + out = "" + for frag, browser in _LINUX_DESKTOP_MAP: + if frag in out: + return browser + return None + + +def detect_default_chromium(system: str | None = None) -> str | None: + """Return the canonical key of the default Chromium browser, or None. + + None means the default browser is non-Chromium (Firefox, Safari) or could + not be determined — the caller fails closed rather than guessing. + """ + system = system or platform.system() + if system == "Windows": + return _detect_default_windows() + if system == "Darwin": + return _detect_default_darwin() + return _detect_default_linux() + + def get_chrome_debug_candidates(system: str) -> list[str]: candidates: list[str] = [] seen: set[str] = set() diff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.py index d3c36844a4d81..f09068eca09ac 100644 --- a/hermes_cli/config_defaults.py +++ b/hermes_cli/config_defaults.py @@ -575,6 +575,17 @@ "engine": "auto", "auto_local_for_private_urls": True, # When a cloud provider is set, auto-spawn local Chromium for LAN/localhost URLs instead of sending them to the cloud "cdp_url": "", # Optional persistent CDP endpoint for attaching to an existing Chromium/Chrome + # Consent to drive the user's REAL default-Chromium profile in local + # mode. When true, any local Chromium launch (built-in tools, or a + # local sidecar spawned under a cloud backend) uses the default + # browser's real user-data-dir — exposing that profile's live logins, + # cookies, and history to automation. Backend-independent. Only + # Chromium-family default browsers are supported (Chrome, Edge, Brave, + # Chromium); a non-Chromium default (e.g. Firefox) fails closed with a + # clear message. Default false. Also gates the browser tools' + # ``local_browser`` argument. Toggle in the desktop Settings → Browser + # section. + "use_real_profile": False, "allow_unsafe_evaluate": False, # Legacy override: when true, browser_console(expression=...) bypasses the restrict_evaluate denylist entirely "restrict_evaluate": False, # Opt-in denylist blocking sensitive JS primitives (cookies/storage/clipboard/network/form values) in browser_console(expression=...) # CDP supervisor — dialog + frame detection via a persistent WebSocket. diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py new file mode 100644 index 0000000000000..6c8e22d62dba8 --- /dev/null +++ b/tests/tools/test_browser_real_profile.py @@ -0,0 +1,122 @@ +"""Tests for real-profile local browser resolution + routing.""" +import os +import ntpath +from unittest.mock import patch + +import pytest + + +class TestRealProfileResolvers: + def test_data_dir_windows(self): + import hermes_cli.browser_connect as bc + with patch.dict(os.environ, {"LOCALAPPDATA": r"C:\Users\T\AppData\Local"}, clear=False): + got = bc.real_profile_data_dir("chrome", "Windows") + # Use ntpath basename checks so this passes on Linux CI too. + assert got.endswith(ntpath.join("Google", "Chrome", "User Data")) or got.endswith( + "Google\\Chrome\\User Data" + ) + + def test_data_dir_linux_edge(self): + import hermes_cli.browser_connect as bc + with patch.dict(os.environ, {"XDG_CONFIG_HOME": "/home/t/.config"}, clear=False): + got = bc.real_profile_data_dir("edge", "Linux") + assert got == "/home/t/.config/microsoft-edge" + + def test_data_dir_unknown_browser_is_none(self): + import hermes_cli.browser_connect as bc + assert bc.real_profile_data_dir("firefox", "Windows") is None + + def test_detect_default_windows_progid_maps(self): + import hermes_cli.browser_connect as bc + # Non-Windows host: _detect_default_windows short-circuits via winreg + # ImportError → None. Assert the ProgId map itself is correct instead. + m = dict(bc._WINDOWS_PROGID_MAP) + assert m["chromehtml"] == "chrome" + assert m["msedgehtm"] == "edge" + assert m["bravehtml"] == "brave" + + def test_detect_default_non_chromium_is_none(self): + import hermes_cli.browser_connect as bc + with patch.object(bc, "_detect_default_linux", return_value=None): + assert bc.detect_default_chromium("Linux") is None + + +class TestRealProfileLaunchArgs: + def _reset(self): + import tools.browser_tool as bt + bt._use_real_profile_resolved = False + bt._cached_use_real_profile = False + + def test_consent_off_is_noop(self): + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_use_real_profile", return_value=False): + args, err = bt._real_profile_launch_args() + assert args == [] and err is None + + def test_non_chromium_default_fails_closed(self): + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_use_real_profile", return_value=True), \ + patch("hermes_cli.browser_connect.detect_default_chromium", return_value=None): + args, err = bt._real_profile_launch_args() + assert args == [] + assert err and "not a supported Chromium" in err + + def test_chromium_default_injects_profile(self, tmp_path): + import tools.browser_tool as bt + self._reset() + data_dir = tmp_path / "chrome-user-data" + data_dir.mkdir() + with patch.object(bt, "_use_real_profile", return_value=True), \ + patch("hermes_cli.browser_connect.detect_default_chromium", return_value="chrome"), \ + patch("hermes_cli.browser_connect.real_profile_data_dir", return_value=str(data_dir)), \ + patch("hermes_cli.browser_connect.chromium_executable", return_value="/usr/bin/google-chrome"): + args, err = bt._real_profile_launch_args() + assert err is None + assert "--profile" in args and str(data_dir) in args + assert "--executable-path" in args and "/usr/bin/google-chrome" in args + + def test_missing_profile_dir_fails_closed(self, tmp_path): + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_use_real_profile", return_value=True), \ + patch("hermes_cli.browser_connect.detect_default_chromium", return_value="chrome"), \ + patch("hermes_cli.browser_connect.real_profile_data_dir", return_value=str(tmp_path / "nope")): + args, err = bt._real_profile_launch_args() + assert args == [] + assert err and "profile directory was not found" in err + + +class TestLocalBrowserRouting: + def _reset(self): + import tools.browser_tool as bt + bt._use_real_profile_resolved = False + bt._cached_use_real_profile = False + + def test_local_browser_forces_sidecar_with_consent(self): + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ + patch.object(bt, "_is_camofox_mode", return_value=False), \ + patch.object(bt, "_use_real_profile", return_value=True): + key = bt._navigation_session_key("t1", "https://example.com", local_browser=True) + assert key == "t1::local" + + def test_local_browser_ignored_without_consent(self): + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ + patch.object(bt, "_is_camofox_mode", return_value=False), \ + patch.object(bt, "_use_real_profile", return_value=False), \ + patch.object(bt, "_get_cloud_provider", return_value=None): + key = bt._navigation_session_key("t1", "https://example.com", local_browser=True) + assert key == "t1" + + def test_cdp_override_still_wins_over_local_browser(self): + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_get_cdp_override_raw", return_value="ws://x"), \ + patch.object(bt, "_use_real_profile", return_value=True): + key = bt._navigation_session_key("t1", "https://example.com", local_browser=True) + assert key == "t1" diff --git a/tools/browser_tool.py b/tools/browser_tool.py index 363a68607ea89..f1c3a4fabaa59 100644 --- a/tools/browser_tool.py +++ b/tools/browser_tool.py @@ -1025,6 +1025,9 @@ def _is_local_backend() -> bool: _auto_local_for_private_urls_resolved = False _cached_auto_local_for_private_urls: bool = True +_use_real_profile_resolved = False +_cached_use_real_profile: bool = False + def _get_browser_engine() -> str: """Return the configured browser engine (``auto``, ``lightpanda``, or ``chrome``). @@ -1427,6 +1430,70 @@ def _auto_local_for_private_urls() -> bool: return _cached_auto_local_for_private_urls +def _use_real_profile() -> bool: + """Return whether the user consented to real-profile local browsing. + + Reads ``browser.use_real_profile`` (default False) once and caches it. When + True, local Chromium launches point agent-browser at the user's REAL + default-browser profile, and the browser tools' ``local_browser`` argument + is honored. + """ + global _use_real_profile_resolved, _cached_use_real_profile + if _use_real_profile_resolved: + return _cached_use_real_profile + + _use_real_profile_resolved = True + try: + from hermes_cli.config import read_raw_config + cfg = read_raw_config() + browser_cfg = cfg.get("browser", {}) + if isinstance(browser_cfg, dict) and "use_real_profile" in browser_cfg: + _cached_use_real_profile = bool(browser_cfg.get("use_real_profile")) + except Exception as e: + logger.debug("Could not read use_real_profile from config: %s", e) + return _cached_use_real_profile + + +def _real_profile_launch_args() -> tuple: + """Resolve (profile_args, error) for a real-profile local launch. + + Returns ``(["--profile", , "--executable-path", ], None)`` + when consent is on and the default browser is a resolvable Chromium. + Returns ``([], )`` when the default browser is non-Chromium or its + profile/binary cannot be located, so the caller can fail closed with a + clear reason. Returns ``([], None)`` when consent is off (no-op). + """ + if not _use_real_profile(): + return [], None + from hermes_cli.browser_connect import ( + chromium_executable, + detect_default_chromium, + real_profile_data_dir, + ) + + browser = detect_default_chromium() + if browser is None: + return [], ( + "browser.use_real_profile is on, but your default browser is not a " + "supported Chromium browser (Chrome, Edge, Brave, Chromium). " + "Real-profile browsing requires a Chromium default; set one or turn " + "the toggle off." + ) + data_dir = real_profile_data_dir(browser) + if not data_dir or not os.path.isdir(data_dir): + return [], ( + f"browser.use_real_profile is on and your default browser is " + f"'{browser}', but its profile directory was not found " + f"({data_dir!r}). Launch that browser at least once, or turn the " + "toggle off." + ) + args = ["--profile", data_dir] + exe = chromium_executable(browser) + if exe: + args += ["--executable-path", exe] + return args, None + + def _url_is_private(url: str) -> bool: """Return True when the URL's host resolves to a private/LAN/loopback address. @@ -1490,7 +1557,7 @@ def _url_is_private(url: str) -> bool: return False -def _navigation_session_key(task_id: str, url: str) -> str: +def _navigation_session_key(task_id: str, url: str, local_browser: bool = False) -> str: """Pick the session key that should handle ``url`` for ``task_id``. Returns the bare task_id unless ALL of these are true: @@ -1504,6 +1571,12 @@ def _navigation_session_key(task_id: str, url: str) -> str: When all are true, returns ``f"{task_id}::local"`` so the hybrid-routing path spawns a local Chromium sidecar while the cloud session (if any) continues to serve public URLs. + + ``local_browser=True`` (the model-facing arg) forces the ``::local`` sidecar + regardless of the URL — but ONLY when the user consented to real-profile + browsing (``browser.use_real_profile``). Without consent the flag is ignored + (the caller surfaces a note); it never silently routes to the real profile. + A CDP override or Camofox mode still owns the session and is not overridden. """ if task_id is None: task_id = "default" @@ -1511,6 +1584,8 @@ def _navigation_session_key(task_id: str, url: str) -> str: return task_id if _is_camofox_mode(): return task_id + if local_browser and _use_real_profile(): + return f"{task_id}{_LOCAL_SUFFIX}" if _get_cloud_provider() is None: return task_id if not _auto_local_for_private_urls(): @@ -2197,6 +2272,11 @@ def _update_session_activity(task_id: str): "url": { "type": "string", "description": "The URL to navigate to (e.g., 'https://example.com')" + }, + "local_browser": { + "type": "boolean", + "description": "Use the user's real local browser profile (their logins/cookies) via a local Chromium session, even if a cloud backend is configured. Only honored when the user has consented (browser.use_real_profile); otherwise ignored.", + "default": False } }, "required": ["url"] @@ -2912,6 +2992,15 @@ def _run_browser_command( backend_args = ["--session", session_info["session_name"]] if _is_headed_mode(): backend_args.append("--headed") + # Real-profile consent: point agent-browser at the user's real default + # Chromium profile so their live logins/cookies are available. Fails + # closed (returns an error) if the default browser is non-Chromium or + # its profile can't be located, rather than silently using a throwaway. + _profile_args, _profile_err = _real_profile_launch_args() + if _profile_err: + return {"success": False, "error": _profile_err} + if _profile_args: + backend_args += _profile_args # Lightpanda engine injection (local mode only, agent-browser v0.25.3+). # Use the resolved session backend rather than global cloud-provider state: @@ -3307,13 +3396,17 @@ def evaluate_url_safety(url: str) -> Optional[dict]: return None -def browser_navigate(url: str, task_id: Optional[str] = None) -> str: +def browser_navigate(url: str, task_id: Optional[str] = None, local_browser: bool = False) -> str: """ Navigate to a URL in the browser. Args: url: The URL to navigate to task_id: Task identifier for session isolation + local_browser: Force a local Chromium session using the user's real + default-browser profile, even when a cloud backend is configured. + Honored only if the user consented via ``browser.use_real_profile``; + ignored (with a note) otherwise. Returns: JSON string with navigation result (includes stealth features info on first nav) @@ -3349,7 +3442,18 @@ def browser_navigate(url: str, task_id: Optional[str] = None) -> str: # cloud provider never sees the URL in that case. Can also be opted # out globally via ``browser.allow_private_urls`` in config. effective_task_id = task_id or "default" - nav_session_key = _navigation_session_key(effective_task_id, url) + # local_browser is honored only with real-profile consent; note when the + # agent asked for it but consent is off so the behavior isn't silently lost. + real_profile_note = None + if local_browser and not _use_real_profile(): + real_profile_note = ( + "local_browser was requested but browser.use_real_profile is off; " + "using the default browser backend. Enable real-profile browsing in " + "Settings → Browser to honor this." + ) + nav_session_key = _navigation_session_key( + effective_task_id, url, local_browser=local_browser + ) auto_local_this_nav = _is_local_sidecar_key(nav_session_key) sensitive_query_key = _sensitive_query_param_name(url) @@ -3473,6 +3577,8 @@ def browser_navigate(url: str, task_id: Optional[str] = None) -> str: "url": final_url, "title": title } + if real_profile_note: + response["note"] = real_profile_note # Remember only a successful, non-blocked navigation as the task owner. # Failed opens and blocked redirects must not retarget follow-up clicks # or snapshots to a newly-created but irrelevant session. @@ -5475,7 +5581,7 @@ def check_browser_press_requirements() -> bool: handler=lambda args, **kw: routed_browser_handler( "browser_navigate", args, - fallback=lambda: browser_navigate(url=args.get("url", ""), task_id=kw.get("task_id")), + fallback=lambda: browser_navigate(url=args.get("url", ""), task_id=kw.get("task_id"), local_browser=bool(args.get("local_browser", False))), **_browser_router_kw(kw), ), check_fn=check_browser_navigate_requirements, From 6c91e49bbfd3f183e75477e276d2d3ec4532ee42 Mon Sep 17 00:00:00 2001 From: Jan-Stefan Janetzky Date: Wed, 26 Aug 2026 14:49:08 +0200 Subject: [PATCH 02/12] fix(browser): pass encoding to detector subprocesses and accept local_browser in test spies The two default-browser detectors call subprocess.run(text=True) without an explicit encoding, which the Windows-footgun linter (and its full-repo test, tests/scripts/test_footgun_subprocess_encoding.py) rejects. Pass encoding='utf-8', errors='replace' like the rest of the tree. Two existing tests replace browser_navigate / _navigation_session_key with positional-only lambdas; both callables now receive local_browser= from the registry handler and browser_navigate, so the spies raised TypeError. Accept the keyword with its default. Fixes the three CI failures on the PR head (Windows footguns lint, test_browser_extension_router_wiring x2, test_browser_open_timeout). --- hermes_cli/browser_connect.py | 15 +++++++++++++-- .../tools/test_browser_extension_router_wiring.py | 6 +++++- tests/tools/test_browser_open_timeout.py | 4 +++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py index c3f2657d2bb4a..25e4a5d91b6d5 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -250,7 +250,14 @@ def _detect_default_darwin() -> str | None: ["defaults", "read", "com.apple.LaunchServices/com.apple.launchservices.secure", "LSHandlers"], ): try: - out = subprocess.run(reader, capture_output=True, text=True, timeout=5).stdout.lower() + out = subprocess.run( + reader, + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + timeout=5, + ).stdout.lower() except Exception: out = "" for frag, browser in _DARWIN_BUNDLE_MAP: @@ -267,7 +274,11 @@ def _detect_default_linux() -> str | None: try: out = subprocess.run( ["xdg-settings", "get", "default-web-browser"], - capture_output=True, text=True, timeout=5, + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + timeout=5, ).stdout.strip().lower() except Exception: out = "" diff --git a/tests/tools/test_browser_extension_router_wiring.py b/tests/tools/test_browser_extension_router_wiring.py index bbdd4bf1bec7e..8fb7e62869298 100644 --- a/tests/tools/test_browser_extension_router_wiring.py +++ b/tests/tools/test_browser_extension_router_wiring.py @@ -36,7 +36,11 @@ def spy(action, args, *, fallback, task_id=None, session_id=None, tool_call_id=N monkeypatch.setattr(browser_tool, "routed_browser_handler", spy) monkeypatch.setattr(browser_cdp_tool, "routed_browser_handler", spy) - monkeypatch.setattr(browser_tool, "browser_navigate", lambda url="", task_id=None: "legacy-nav") + monkeypatch.setattr( + browser_tool, + "browser_navigate", + lambda url="", task_id=None, local_browser=False: "legacy-nav", + ) monkeypatch.setattr(browser_cdp_tool, "browser_cdp", lambda *a, **k: "legacy-cdp") return calls diff --git a/tests/tools/test_browser_open_timeout.py b/tests/tools/test_browser_open_timeout.py index 6d791ee28232a..570f8f9b6ac33 100644 --- a/tests/tools/test_browser_open_timeout.py +++ b/tests/tools/test_browser_open_timeout.py @@ -96,7 +96,9 @@ def fake_run(task_id, command, args, timeout=None): monkeypatch.setattr(bt, "_is_camofox_mode", lambda: False) monkeypatch.setattr(bt, "_is_local_backend", lambda: True) monkeypatch.setattr(bt, "_is_local_sidecar_key", lambda key: False) - monkeypatch.setattr(bt, "_navigation_session_key", lambda task_id, url: task_id) + monkeypatch.setattr( + bt, "_navigation_session_key", lambda task_id, url, local_browser=False: task_id + ) monkeypatch.setattr(bt, "_maybe_start_recording", lambda *a, **kw: None) monkeypatch.setattr(bt, "check_website_access", lambda url: None) From 99adf4be9dbd3f004af3630c3f4afc26a6341617 Mon Sep 17 00:00:00 2001 From: Jan-Stefan Janetzky Date: Wed, 26 Aug 2026 14:53:33 +0200 Subject: [PATCH 03/12] fix(browser): read the macOS https handler per entry and drop the installed-browser fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _detect_default_darwin matched a Chromium bundle id and the literal 'https' anywhere in the whole LSHandlers dump, so a browser registered for ftp or a content type was reported as the https default, and map order decided ties. When nothing matched it fell back to the first installed Chromium app — with Safari or Firefox as the actual default that drove a browser the user never consented to, contradicting the docstring, the config comment and the desktop copy ('a non-Chromium default fails with a clear message'). Parse the dump entry by entry, take the LSHandlerRoleAll/Viewer of the entry whose LSHandlerURLScheme is https, and fail closed on anything else — an empty handler list is what macOS stores while Safari is still the implicit default. Tests feed real 'defaults read' output shapes instead of patching the detector (reviewer fixture from the PR discussion: Safari on https, Chrome on ftp). --- hermes_cli/browser_connect.py | 88 +++++++++++---- .../test_browser_connect_default_chromium.py | 106 ++++++++++++++++++ 2 files changed, 173 insertions(+), 21 deletions(-) create mode 100644 tests/hermes_cli/test_browser_connect_default_chromium.py diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py index 25e4a5d91b6d5..12b4565d1dbc7 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -7,6 +7,7 @@ import os import platform import posixpath +import re import shlex import shutil import subprocess @@ -244,29 +245,74 @@ def _detect_default_windows() -> str | None: return None +_LS_HANDLERS_READER = ( + "defaults", + "read", + "com.apple.LaunchServices/com.apple.launchservices.secure", + "LSHandlers", +) + + +def _launchservices_https_handler(dump: str) -> str | None: + """Return the bundle id registered for the ``https`` URL scheme. + + ``dump`` is the ``defaults read … LSHandlers`` output: an array of + ``{ … }`` dictionaries, one per handler. Only the entry whose + ``LSHandlerURLScheme`` is ``https`` counts — a browser registered for + another scheme or a file type must not be mistaken for the default. + Returns None when no https handler is recorded, which is what macOS + stores while Safari (the implicit default) has never been replaced. + """ + entries: list[str] = [] + depth = 0 + buf: list[str] = [] + for ch in dump: + if ch == "{": + depth += 1 + if depth == 1: + buf = [] + continue + elif ch == "}": + depth -= 1 + if depth == 0: + entries.append("".join(buf)) + continue + if depth >= 1: + buf.append(ch) + for entry in entries: + low = entry.lower() + if not re.search(r'lshandlerurlscheme\s*=\s*"?https"?\s*;', low): + continue + # The nested LSHandlerPreferredVersions block carries "-" placeholders + # under the same key names; the real bundle id is the first non-"-". + for role in re.findall(r'lshandlerrole(?:all|viewer)\s*=\s*"?([a-z0-9.\-]+)"?\s*;', low): + if role != "-": + return role + return None + return None + + def _detect_default_darwin() -> str | None: - # LaunchServices handler for the https scheme. - for reader in ( - ["defaults", "read", "com.apple.LaunchServices/com.apple.launchservices.secure", "LSHandlers"], - ): - try: - out = subprocess.run( - reader, - capture_output=True, - text=True, - encoding="utf-8", - errors="replace", - timeout=5, - ).stdout.lower() - except Exception: - out = "" - for frag, browser in _DARWIN_BUNDLE_MAP: - if frag in out and "https" in out: - return browser - # Fallback: first installed Chromium app wins. - for browser in _CHROMIUM_BROWSERS: - if chromium_executable(browser, "Darwin"): + try: + out = subprocess.run( + list(_LS_HANDLERS_READER), + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + timeout=5, + ).stdout + except Exception: + return None + bundle = _launchservices_https_handler(out) + if not bundle: + return None + for frag, browser in _DARWIN_BUNDLE_MAP: + if bundle.startswith(frag): return browser + # A non-Chromium https handler (Safari, Firefox, Arc, …): fail closed. + # No "first installed Chromium wins" fallback — that would drive a + # browser the user never made their default. return None diff --git a/tests/hermes_cli/test_browser_connect_default_chromium.py b/tests/hermes_cli/test_browser_connect_default_chromium.py new file mode 100644 index 0000000000000..a47f248b31405 --- /dev/null +++ b/tests/hermes_cli/test_browser_connect_default_chromium.py @@ -0,0 +1,106 @@ +"""Default-Chromium detection and profile-dir resolution (hermes_cli.browser_connect). + +These exercise the parsers with real command output shapes instead of +patching the detectors themselves, so a change in what macOS / xdg report is +caught here rather than in a user's browser session. +""" +from unittest.mock import patch + +import pytest + +import hermes_cli.browser_connect as bc + + +def _ls_dump(*entries: str) -> str: + return "(\n" + ",\n".join(entries) + "\n)\n" + + +def _handler(scheme: str, bundle: str) -> str: + return ( + " {\n" + " LSHandlerPreferredVersions = {\n" + ' LSHandlerRoleAll = "-";\n' + " };\n" + f' LSHandlerRoleAll = "{bundle}";\n' + f" LSHandlerURLScheme = {scheme};\n" + " }" + ) + + +def _content_type_handler(uti: str, bundle: str) -> str: + return ( + " {\n" + f' LSHandlerContentType = "{uti}";\n' + f' LSHandlerRoleViewer = "{bundle}";\n' + " }" + ) + + +class TestLaunchServicesHttpsHandler: + def test_https_entry_wins_over_other_schemes(self): + dump = _ls_dump( + _handler("ftp", "com.google.chrome"), + _handler("https", "com.apple.safari"), + ) + assert bc._launchservices_https_handler(dump) == "com.apple.safari" + + def test_content_type_registration_is_not_an_https_handler(self): + dump = _ls_dump(_content_type_handler("public.html", "com.google.chrome")) + assert bc._launchservices_https_handler(dump) is None + + def test_no_entries_means_no_recorded_handler(self): + assert bc._launchservices_https_handler("(\n)\n") is None + assert bc._launchservices_https_handler("") is None + + def test_nested_dictionary_does_not_split_the_entry(self): + dump = _ls_dump(_handler("https", "com.microsoft.edgemac")) + assert bc._launchservices_https_handler(dump) == "com.microsoft.edgemac" + + +class TestDetectDefaultDarwin: + def _run_with(self, dump: str): + class _Proc: + stdout = dump + + return patch.object(bc.subprocess, "run", return_value=_Proc()) + + def test_chrome_as_https_handler(self): + with self._run_with(_ls_dump(_handler("https", "com.google.chrome"))): + assert bc._detect_default_darwin() == "chrome" + + def test_safari_default_with_chrome_installed_fails_closed(self): + """The old fallback returned the first installed Chromium app; a + non-Chromium default must resolve to None even when Chrome exists.""" + dump = _ls_dump( + _handler("https", "com.apple.safari"), + _handler("ftp", "com.google.chrome"), + ) + with self._run_with(dump), \ + patch.object(bc, "chromium_executable", return_value="/Applications/Google Chrome.app/x"): + assert bc._detect_default_darwin() is None + + def test_no_handler_recorded_fails_closed(self): + with self._run_with("(\n)\n"), \ + patch.object(bc, "chromium_executable", return_value="/Applications/Google Chrome.app/x"): + assert bc._detect_default_darwin() is None + + def test_firefox_default_fails_closed(self): + with self._run_with(_ls_dump(_handler("https", "org.mozilla.firefox"))): + assert bc._detect_default_darwin() is None + + def test_reader_failure_fails_closed(self): + with patch.object(bc.subprocess, "run", side_effect=OSError("no defaults")): + assert bc._detect_default_darwin() is None + + @pytest.mark.parametrize( + "bundle,expected", + [ + ("com.google.Chrome", "chrome"), + ("com.brave.Browser", "brave"), + ("com.microsoft.edgemac", "edge"), + ("org.chromium.Chromium", "chromium"), + ], + ) + def test_bundle_map(self, bundle, expected): + with self._run_with(_ls_dump(_handler("https", bundle))): + assert bc._detect_default_darwin() == expected From edb97ad1c56be4430f14f5cac8834f96c7e5e997 Mon Sep 17 00:00:00 2001 From: Jan-Stefan Janetzky Date: Wed, 26 Aug 2026 14:55:06 +0200 Subject: [PATCH 04/12] fix(browser): resolve snap and Flatpak Chromium profiles on Linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit real_profile_data_dir hard-wired Linux to $XDG_CONFIG_HOME/, and the xdg fragment map only knew the native package names. Ubuntu's default snap Chromium (xdg reports chromium_chromium.desktop, profile under ~/snap/chromium/common/chromium) and Flatpak builds (~/.var/app//config/…) therefore ended in 'profile directory was not found' for a browser the user runs every day, and Flatpak Chrome (com.google.Chrome.desktop) was reported as 'not a supported Chromium browser'. Try the native, snap and Flatpak locations and return the first that exists; fall back to the native path so the error message still names a concrete directory. Map the Flatpak application ids in the xdg lookup. Tests cover the xdg names for all four browsers in native and Flatpak form, and the directory preference order with a temp HOME. --- hermes_cli/browser_connect.py | 40 +++++++++-- .../test_browser_connect_default_chromium.py | 66 +++++++++++++++++++ 2 files changed, 101 insertions(+), 5 deletions(-) diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py index 12b4565d1dbc7..0d0b786ad6de9 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -104,14 +104,30 @@ ) # Linux xdg default-web-browser .desktop name fragments → canonical key. +# Includes the Flatpak application ids (``com.google.Chrome.desktop`` etc.), +# which share none of the native package name fragments. _LINUX_DESKTOP_MAP = ( ("google-chrome", "chrome"), + ("com.google.chrome", "chrome"), ("chromium", "chromium"), ("brave", "brave"), ("microsoft-edge", "edge"), + ("com.microsoft.edge", "edge"), ("msedge", "edge"), ) +# Where sandboxed Linux packages keep the profile instead of $XDG_CONFIG_HOME. +_LINUX_FLATPAK_IDS = { + "chrome": "com.google.Chrome", + "chromium": "org.chromium.Chromium", + "brave": "com.brave.Browser", + "edge": "com.microsoft.Edge", +} +_LINUX_SNAP_PROFILE_PARTS = { + "chromium": ("snap", "chromium", "common", "chromium"), + "brave": ("snap", "brave", "current", ".config", "BraveSoftware", "Brave-Browser"), +} + # macOS LaunchServices bundle-id fragments → canonical key. _DARWIN_BUNDLE_MAP = ( ("com.google.chrome", "chrome"), @@ -150,10 +166,12 @@ def _real_profile_relparts(browser: str) -> tuple: def real_profile_data_dir(browser: str, system: str | None = None) -> str | None: """Return the default user-data-dir for a Chromium ``browser`` on ``system``. - Returns None for unknown browsers. Does not check existence — callers that - need that should stat the result. Paths are built with the TARGET system's - separator (posix for Darwin/Linux, backslash for Windows) so an explicit - ``system`` argument resolves correctly regardless of the host OS. + Returns None for unknown browsers. On Linux the native ($XDG_CONFIG_HOME), + snap and Flatpak locations are tried and the first existing one wins; the + native path is returned when none exists so the caller's error names it. + Darwin/Windows paths are not stat'ed. Paths are built with the TARGET + system's separator (posix for Darwin/Linux, backslash for Windows) so an + explicit ``system`` argument resolves correctly regardless of the host OS. """ if browser not in _CHROMIUM_BROWSERS: return None @@ -167,7 +185,19 @@ def real_profile_data_dir(browser: str, system: str | None = None) -> str | None return ntpath.join(local, *win_parts) # Linux / other POSIX config = os.environ.get("XDG_CONFIG_HOME") or posixpath.join(home, ".config") - return posixpath.join(config, *linux_name.split("/")) + candidates = [posixpath.join(config, *linux_name.split("/"))] + snap_parts = _LINUX_SNAP_PROFILE_PARTS.get(browser) + if snap_parts: + candidates.append(posixpath.join(home, *snap_parts)) + flatpak_id = _LINUX_FLATPAK_IDS.get(browser) + if flatpak_id: + candidates.append( + posixpath.join(home, ".var", "app", flatpak_id, "config", *linux_name.split("/")) + ) + for candidate in candidates: + if os.path.isdir(candidate): + return candidate + return candidates[0] def chromium_executable(browser: str, system: str | None = None) -> str | None: diff --git a/tests/hermes_cli/test_browser_connect_default_chromium.py b/tests/hermes_cli/test_browser_connect_default_chromium.py index a47f248b31405..4a654d8e66e63 100644 --- a/tests/hermes_cli/test_browser_connect_default_chromium.py +++ b/tests/hermes_cli/test_browser_connect_default_chromium.py @@ -104,3 +104,69 @@ def test_reader_failure_fails_closed(self): def test_bundle_map(self, bundle, expected): with self._run_with(_ls_dump(_handler("https", bundle))): assert bc._detect_default_darwin() == expected + + +class TestDetectDefaultLinux: + def _run_with(self, output: str): + class _Proc: + stdout = output + + return patch.object(bc.subprocess, "run", return_value=_Proc()) + + @pytest.mark.parametrize( + "desktop,expected", + [ + ("google-chrome.desktop", "chrome"), + ("com.google.Chrome.desktop", "chrome"), + ("chromium_chromium.desktop", "chromium"), + ("org.chromium.Chromium.desktop", "chromium"), + ("brave-browser.desktop", "brave"), + ("com.brave.Browser.desktop", "brave"), + ("microsoft-edge.desktop", "edge"), + ("com.microsoft.Edge.desktop", "edge"), + ("firefox.desktop", None), + ("org.mozilla.firefox.desktop", None), + ("", None), + ], + ) + def test_xdg_desktop_names(self, desktop, expected): + with self._run_with(desktop + "\n"): + assert bc._detect_default_linux() == expected + + def test_missing_xdg_settings_fails_closed(self): + with patch.object(bc.subprocess, "run", side_effect=FileNotFoundError("xdg-settings")): + assert bc._detect_default_linux() is None + + +class TestLinuxProfileDir: + def _env(self, monkeypatch, home): + monkeypatch.setenv("HOME", str(home)) + monkeypatch.delenv("XDG_CONFIG_HOME", raising=False) + + def test_native_path_when_nothing_exists(self, tmp_path, monkeypatch): + self._env(monkeypatch, tmp_path) + assert bc.real_profile_data_dir("chromium", "Linux") == str(tmp_path / ".config" / "chromium") + + def test_snap_chromium_profile_is_found(self, tmp_path, monkeypatch): + self._env(monkeypatch, tmp_path) + snap = tmp_path / "snap" / "chromium" / "common" / "chromium" + snap.mkdir(parents=True) + assert bc.real_profile_data_dir("chromium", "Linux") == str(snap) + + def test_flatpak_chrome_profile_is_found(self, tmp_path, monkeypatch): + self._env(monkeypatch, tmp_path) + flatpak = tmp_path / ".var" / "app" / "com.google.Chrome" / "config" / "google-chrome" + flatpak.mkdir(parents=True) + assert bc.real_profile_data_dir("chrome", "Linux") == str(flatpak) + + def test_native_profile_wins_when_present(self, tmp_path, monkeypatch): + self._env(monkeypatch, tmp_path) + native = tmp_path / ".config" / "BraveSoftware" / "Brave-Browser" + native.mkdir(parents=True) + (tmp_path / ".var" / "app" / "com.brave.Browser" / "config" / "BraveSoftware" / "Brave-Browser").mkdir(parents=True) + assert bc.real_profile_data_dir("brave", "Linux") == str(native) + + def test_xdg_config_home_is_honoured(self, tmp_path, monkeypatch): + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.setenv("XDG_CONFIG_HOME", "/home/t/.config") + assert bc.real_profile_data_dir("edge", "Linux") == "/home/t/.config/microsoft-edge" From 38402d1fc19d93115db5ebd7972c89a5693ee9f5 Mon Sep 17 00:00:00 2001 From: Jan-Stefan Janetzky Date: Wed, 26 Aug 2026 14:53:35 +0200 Subject: [PATCH 05/12] fix(browser): keep local_browser inside the private-URL policy and the existing local session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _navigation_session_key returned the ::local sidecar key for local_browser before the cloud-provider and auto_local_for_private_urls checks. Two consequences: - Every private-URL gate in browser_navigate (credential-bearing query, _is_safe_url pre-nav, post-redirect) is keyed off the sidecar key, so a model-supplied browser_navigate(url, local_browser=True) opened LAN addresses in a host-side Chromium — with the real profile's cookies — even when the user had set browser.auto_local_for_private_urls: false. Consent to the profile is not consent to override the LAN routing opt-out; a private URL now follows auto_local_for_private_urls exactly as without the flag. - Without a cloud provider the bare session already is the local Chromium (and already carries the real profile when consented), so the flag created a second session for the same task on the same user-data-dir, which Chromium's process singleton refuses. The flag is now a no-op there. The existing consent/CDP-precedence tests keep their assertions; the sidecar test now states the cloud-provider precondition it silently relied on. --- tests/tools/test_browser_real_profile.py | 43 +++++++++++++++++++++++- tools/browser_tool.py | 20 ++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py index 6c8e22d62dba8..66f2f3fe18ab3 100644 --- a/tests/tools/test_browser_real_profile.py +++ b/tests/tools/test_browser_real_profile.py @@ -1,7 +1,7 @@ """Tests for real-profile local browser resolution + routing.""" import os import ntpath -from unittest.mock import patch +from unittest.mock import Mock, patch import pytest @@ -99,10 +99,51 @@ def test_local_browser_forces_sidecar_with_consent(self): self._reset() with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ patch.object(bt, "_is_camofox_mode", return_value=False), \ + patch.object(bt, "_get_cloud_provider", return_value=Mock()), \ + patch.object(bt, "_url_is_private", return_value=False), \ patch.object(bt, "_use_real_profile", return_value=True): key = bt._navigation_session_key("t1", "https://example.com", local_browser=True) assert key == "t1::local" + def test_local_browser_without_cloud_provider_keeps_bare_session(self): + """Pure local backend: the bare session already is the (real-profile) + local Chromium; a second ``::local`` session would fight it for the + same user-data-dir.""" + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ + patch.object(bt, "_is_camofox_mode", return_value=False), \ + patch.object(bt, "_get_cloud_provider", return_value=None), \ + patch.object(bt, "_use_real_profile", return_value=True): + key = bt._navigation_session_key("t1", "https://example.com", local_browser=True) + assert key == "t1" + + def test_local_browser_respects_private_url_opt_out(self): + """auto_local_for_private_urls: false is the user's LAN routing + decision; a model-supplied flag must not override it.""" + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ + patch.object(bt, "_is_camofox_mode", return_value=False), \ + patch.object(bt, "_get_cloud_provider", return_value=Mock()), \ + patch.object(bt, "_url_is_private", return_value=True), \ + patch.object(bt, "_auto_local_for_private_urls", return_value=False), \ + patch.object(bt, "_use_real_profile", return_value=True): + key = bt._navigation_session_key("t1", "http://192.168.1.1/admin", local_browser=True) + assert key == "t1" + + def test_local_browser_private_url_follows_auto_local_when_allowed(self): + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ + patch.object(bt, "_is_camofox_mode", return_value=False), \ + patch.object(bt, "_get_cloud_provider", return_value=Mock()), \ + patch.object(bt, "_url_is_private", return_value=True), \ + patch.object(bt, "_auto_local_for_private_urls", return_value=True), \ + patch.object(bt, "_use_real_profile", return_value=True): + key = bt._navigation_session_key("t1", "http://192.168.1.1/admin", local_browser=True) + assert key == "t1::local" + def test_local_browser_ignored_without_consent(self): import tools.browser_tool as bt self._reset() diff --git a/tools/browser_tool.py b/tools/browser_tool.py index f1c3a4fabaa59..0b6ce0b02045d 100644 --- a/tools/browser_tool.py +++ b/tools/browser_tool.py @@ -1573,10 +1573,14 @@ def _navigation_session_key(task_id: str, url: str, local_browser: bool = False) continues to serve public URLs. ``local_browser=True`` (the model-facing arg) forces the ``::local`` sidecar - regardless of the URL — but ONLY when the user consented to real-profile + for the backend choice — but ONLY when the user consented to real-profile browsing (``browser.use_real_profile``). Without consent the flag is ignored (the caller surfaces a note); it never silently routes to the real profile. - A CDP override or Camofox mode still owns the session and is not overridden. + It does not override the private-URL policy: a private URL is still routed + exactly as ``browser.auto_local_for_private_urls`` says. Without a cloud + provider the bare session is already local (and already carries the real + profile when consented), so the flag adds nothing there. A CDP override or + Camofox mode still owns the session and is not overridden. """ if task_id is None: task_id = "default" @@ -1584,10 +1588,18 @@ def _navigation_session_key(task_id: str, url: str, local_browser: bool = False) return task_id if _is_camofox_mode(): return task_id - if local_browser and _use_real_profile(): - return f"{task_id}{_LOCAL_SUFFIX}" if _get_cloud_provider() is None: + # Already local. A second ``::local`` session for the same task would + # contend with the bare one for the same user-data-dir. return task_id + if local_browser and _use_real_profile(): + # Every private-URL gate in browser_navigate keys off the sidecar + # key, so honouring the flag for a private URL while the user opted + # out of auto-local routing would turn a model-supplied argument + # into a LAN policy override. + if _url_is_private(url) and not _auto_local_for_private_urls(): + return task_id + return f"{task_id}{_LOCAL_SUFFIX}" if not _auto_local_for_private_urls(): return task_id if not _url_is_private(url): From 8bfa49da9294e57a04d6ae040cf1e52804ac6ec4 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 26 Aug 2026 07:31:54 -0700 Subject: [PATCH 06/12] feat(browser): real-profile browsing via agent-browser copy + browser-use CDP Copy the user's default-Chromium profile (auth state only) into a managed snapshot, launch Hermes' packaged Chromium on it via agent-browser, and hand the CDP endpoint to the Browser Use CLI (and built-in tools) to drive. The snapshot is a non-default dir, so it sidesteps Chrome 136+'s default-profile remote-debugging block and never contends with the user's running browser; launched without mock-keychain switches so keyring-encrypted cookies decrypt. - consent-gated browser_exec 'local' arg (schema only appears with consent) - fail-closed on non-Chromium default / snapshot failure - stale-session guard: reuse only when the live session is on our copy dir - snapshot excludes extensions/service-workers (renderer wedge) + caches --- apps/desktop/src/app/settings/constants.ts | 2 +- hermes_cli/browser_connect.py | 151 ++++++++ hermes_cli/config_defaults.py | 23 +- tests/tools/test_browser_real_profile.py | 379 ++++++++++++++++---- tools/browser_tool.py | 337 +++++++++++------ tools/browser_use_cli.py | 118 +++++- website/docs/user-guide/features/browser.md | 35 ++ 7 files changed, 856 insertions(+), 189 deletions(-) diff --git a/apps/desktop/src/app/settings/constants.ts b/apps/desktop/src/app/settings/constants.ts index 13d71393445a7..ee841452ea1f1 100644 --- a/apps/desktop/src/app/settings/constants.ts +++ b/apps/desktop/src/app/settings/constants.ts @@ -558,7 +558,7 @@ export const FIELD_DESCRIPTIONS: Record = defineFieldCopy({ timezone: 'IANA timezone identifier. Blank uses the system timezone.', browser: { useRealProfile: - "Local browser use drives your real default browser profile — its logins, cookies, and history. Backend-independent: applies whenever a local Chromium is used, and lets the agent choose a local real-profile session even under a cloud backend. Only Chromium browsers (Chrome, Edge, Brave, Chromium) are supported; a non-Chromium default fails with a clear message. Off by default." + "Local browsing uses your real logins. Hermes copies your default browser's profile (cookies, logins, preferences) into a managed snapshot and drives it with its packaged Chromium — your live profile is never opened directly, and the copy is refreshed from it on each run. Also lets the agent open a local real-profile session on request even when a cloud browser backend is configured. Only Chromium browsers (Chrome, Edge, Brave, Chromium) are supported; a non-Chromium default fails with a clear message. Off by default." }, agent: { imageInputMode: 'Controls how image attachments are sent to the model.', diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py index 0d0b786ad6de9..28720c4d622f0 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -378,6 +378,157 @@ def detect_default_chromium(system: str | None = None) -> str | None: return _detect_default_linux() +# --------------------------------------------------------------------------- +# Real-profile SNAPSHOT launch +# +# The consent path (``browser.use_real_profile``) never drives the live +# default user-data-dir. Chromium ≥136 (Google-branded builds) refuses +# remote debugging on the default dir no matter who launches it, and the +# live dir is usually held by the user's running browser (SingletonLock). +# Instead we snapshot the real profile into ``~/.hermes/browser-profile/`` +# — a non-default dir Chrome will happily debug, that never contends with +# the user's browser — launch the user's real binary on the copy with a +# devtools port, and hand the CDP URL to whichever browser lane is active +# (Browser Use CLI or the built-in tools). We launch the browser ourselves +# precisely so NO mock-keychain/basic-store switches are added: cookies +# encrypted with the OS keyring (gnome-keyring / kwallet / macOS Keychain) +# decrypt exactly like they do in the user's own browser. +# --------------------------------------------------------------------------- + +# Directory names excluded from the profile snapshot: caches/telemetry AND the +# heavy, replay-prone state that hangs a fresh Chromium's renderer (extensions +# and their service workers spin up on launch and wedge JS eval; IndexedDB / +# GPUCache add hundreds of MB for nothing). We keep ONLY auth/login state +# (cookies, Login Data, Web Data, Preferences, Local State) — the point of the +# feature — which turns a multi-hundred-MB profile into a few MB. +_SNAPSHOT_IGNORES = ( + "*Cache*", # Cache, Code Cache, GPUCache, GrShaderCache, ShaderCache, GraphiteDawnCache, component_crx_cache, ... + "Extensions", # wallets/etc.: 100s of MB, and hang the renderer headless + "Extension*", # Extension State, Extension Rules, Extension Scripts + "Local Extension Settings", + "Service Worker", # replays on launch → wedges the renderer + "IndexedDB", + "Crash Reports", + "Crashpad", + "BrowserMetrics*", + "Snapshots", + "OptimizationGuide*", + "optimization_guide_model_store", + "Safe Browsing", + "SafetyTips", + "OnDeviceHeadSuggestModel", + "segmentation_platform", + "Sync Data", + "Shared Dictionary", + "History*", # large; not needed for auth + "Favicons*", + "Singleton*", # live-instance symlinks; never valid in a copy + "RunningChromeVersion", + "SingletonSocket", + "*.tmp", + "BrowserMetrics-spare.pma", +) + +# Small, auth-bearing files re-synced from the live profile on EVERY consented +# launch (the full tree is only copied when the snapshot doesn't exist yet). +# Paths are relative to the user-data-dir; expands per profile dir. +_AUTH_REFRESH_FILES = ( + "Local State", + "/Cookies", + "/Cookies-journal", + "/Network/Cookies", + "/Network/Cookies-journal", + "/Login Data", + "/Login Data-journal", + "/Login Data For Account", + "/Web Data", + "/Web Data-journal", + "/Preferences", +) + +def real_profile_copy_dir(browser: str) -> str: + """Return the hermes-owned snapshot dir for ``browser``'s real profile.""" + return str(get_hermes_home() / "browser-profile" / browser) + + +def _profile_subdirs(src: str) -> list[str]: + """Names of per-profile dirs (Default, Profile 1, ...) inside a data dir.""" + out = [] + try: + for name in os.listdir(src): + if name == "Default" or name.startswith("Profile "): + if os.path.isdir(os.path.join(src, name)): + out.append(name) + except OSError: + pass + return out or ["Default"] + + +def snapshot_real_profile(browser: str, src: str | None = None) -> tuple[str | None, str | None]: + """Snapshot ``browser``'s real profile into the hermes copy dir. + + Full copy (minus caches) when the copy doesn't exist; otherwise only the + auth-bearing files (cookies, login data, preferences) are re-synced so + fresh logins from the user's own browsing show up in the agent session. + Locked-file copy errors are tolerated best-effort: SQLite journals make a + mid-write copy readable, and a stale cookie jar beats a failed launch. + + Returns ``(copy_dir, None)`` on success, ``(None, error)`` on failure. + """ + src = src or real_profile_data_dir(browser) + if not src or not os.path.isdir(src): + return None, ( + f"profile directory for '{browser}' was not found ({src!r}). " + "Launch that browser at least once, or turn browser.use_real_profile off." + ) + dst = real_profile_copy_dir(browser) + fresh = not os.path.isdir(os.path.join(dst, "Default")) + try: + if fresh: + os.makedirs(dst, exist_ok=True) + try: + shutil.copytree( + src, + dst, + dirs_exist_ok=True, + symlinks=False, + ignore=shutil.ignore_patterns(*_SNAPSHOT_IGNORES), + ignore_dangling_symlinks=True, + ) + except shutil.Error as multi: + # Per-file failures (browser mid-write) are non-fatal. + logger.info( + "real-profile snapshot: %d file(s) skipped while copying %s", + len(multi.args[0]) if multi.args else 0, src, + ) + else: + profiles = _profile_subdirs(src) + for rel in _AUTH_REFRESH_FILES: + rels = ( + [rel.replace("", p) for p in profiles] + if "" in rel else [rel] + ) + for r in rels: + s = os.path.join(src, r) + if not os.path.isfile(s): + continue + d = os.path.join(dst, r) + try: + os.makedirs(os.path.dirname(d), exist_ok=True) + shutil.copy2(s, d) + except OSError as e: + logger.debug("real-profile refresh: skipped %s: %s", r, e) + # Never carry live-instance leftovers into the copy. + for leftover in ("SingletonLock", "SingletonSocket", "SingletonCookie"): + try: + os.unlink(os.path.join(dst, leftover)) + except OSError: + pass + except OSError as e: + return None, f"could not snapshot the '{browser}' profile into {dst}: {e}" + return dst, None + + def get_chrome_debug_candidates(system: str) -> list[str]: candidates: list[str] = [] seen: set[str] = set() diff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.py index f09068eca09ac..eee52189bc338 100644 --- a/hermes_cli/config_defaults.py +++ b/hermes_cli/config_defaults.py @@ -575,16 +575,19 @@ "engine": "auto", "auto_local_for_private_urls": True, # When a cloud provider is set, auto-spawn local Chromium for LAN/localhost URLs instead of sending them to the cloud "cdp_url": "", # Optional persistent CDP endpoint for attaching to an existing Chromium/Chrome - # Consent to drive the user's REAL default-Chromium profile in local - # mode. When true, any local Chromium launch (built-in tools, or a - # local sidecar spawned under a cloud backend) uses the default - # browser's real user-data-dir — exposing that profile's live logins, - # cookies, and history to automation. Backend-independent. Only - # Chromium-family default browsers are supported (Chrome, Edge, Brave, - # Chromium); a non-Chromium default (e.g. Firefox) fails closed with a - # clear message. Default false. Also gates the browser tools' - # ``local_browser`` argument. Toggle in the desktop Settings → Browser - # section. + # Consent to browse with the user's REAL logins for local browsing. + # When true, local browsing (the Browser Use CLI, or the built-in + # browser tools) runs on a Hermes-managed SNAPSHOT of the user's + # default-Chromium profile — cookies, logins and preferences copied in, + # re-synced from the real profile on each launch — driven by Hermes' + # packaged Chromium. The snapshot is a non-default dir, so it sidesteps + # Chrome 136+'s block on debugging the default profile and never + # contends with the user's running browser. Only Chromium-family + # default browsers are supported (Chrome, Edge, Brave, Chromium); a + # non-Chromium default (e.g. Firefox) fails closed with a clear + # message. Default false. Also gates the browser_exec ``local`` + # argument, which forces a real-profile local session even under a + # cloud browser backend. Toggle in the desktop Settings → Browser section. "use_real_profile": False, "allow_unsafe_evaluate": False, # Legacy override: when true, browser_console(expression=...) bypasses the restrict_evaluate denylist entirely "restrict_evaluate": False, # Opt-in denylist blocking sensitive JS primitives (cookies/storage/clipboard/network/form values) in browser_console(expression=...) diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py index 66f2f3fe18ab3..5cf447027ac5e 100644 --- a/tests/tools/test_browser_real_profile.py +++ b/tests/tools/test_browser_real_profile.py @@ -1,4 +1,12 @@ -"""Tests for real-profile local browser resolution + routing.""" +"""Tests for real-profile browsing: resolvers, snapshot, launch routing, consent. + +The consent path never drives the live default profile: it snapshots into +``~/.hermes/browser-profile//`` and launches the user's real binary +on the copy with a devtools port (see hermes_cli.browser_connect). These tests +exercise the real functions with real file I/O wherever possible — the mocks +are limited to OS detection and process launch. +""" +import json import os import ntpath from unittest.mock import Mock, patch @@ -41,123 +49,344 @@ def test_detect_default_non_chromium_is_none(self): assert bc.detect_default_chromium("Linux") is None -class TestRealProfileLaunchArgs: +class TestSnapshotRealProfile: + """Real file I/O: the snapshot copier against a synthetic profile tree.""" + + def _make_profile(self, root): + """Build a minimal real-looking Chromium user-data-dir.""" + (root / "Default" / "Network").mkdir(parents=True) + (root / "Default" / "Cache" / "Cache_Data").mkdir(parents=True) + (root / "Code Cache" / "js").mkdir(parents=True) + (root / "Crashpad").mkdir() + (root / "Local State").write_text('{"os_crypt": {}}') + (root / "Default" / "Cookies").write_text("sqlite-cookies") + (root / "Default" / "Network" / "Cookies").write_text("sqlite-net-cookies") + (root / "Default" / "Login Data").write_text("sqlite-logins") + (root / "Default" / "Preferences").write_text("{}") + (root / "Default" / "Cache" / "Cache_Data" / "big").write_text("x" * 1000) + (root / "Code Cache" / "js" / "blob").write_text("y" * 1000) + (root / "Crashpad" / "dump").write_text("z") + # Live-instance leftovers that must never reach the copy + os.symlink("dead-target-1", root / "SingletonLock") + return root + + def test_fresh_snapshot_copies_auth_and_skips_caches(self, tmp_path, monkeypatch): + import hermes_cli.browser_connect as bc + src = self._make_profile(tmp_path / "real") + home = tmp_path / "hermes-home" + monkeypatch.setattr(bc, "get_hermes_home", lambda: home) + + dst, err = bc.snapshot_real_profile("chrome", src=str(src)) + assert err is None + assert dst == str(home / "browser-profile" / "chrome") + # Auth files present + assert (home / "browser-profile" / "chrome" / "Default" / "Cookies").read_text() == "sqlite-cookies" + assert (home / "browser-profile" / "chrome" / "Default" / "Network" / "Cookies").exists() + assert (home / "browser-profile" / "chrome" / "Default" / "Login Data").exists() + assert (home / "browser-profile" / "chrome" / "Local State").exists() + # Caches, crash dirs, singleton leftovers excluded + assert not (home / "browser-profile" / "chrome" / "Default" / "Cache").exists() + assert not (home / "browser-profile" / "chrome" / "Code Cache").exists() + assert not (home / "browser-profile" / "chrome" / "Crashpad").exists() + assert not (home / "browser-profile" / "chrome" / "SingletonLock").exists() + + def test_existing_snapshot_refreshes_auth_files_only(self, tmp_path, monkeypatch): + import hermes_cli.browser_connect as bc + src = self._make_profile(tmp_path / "real") + home = tmp_path / "hermes-home" + monkeypatch.setattr(bc, "get_hermes_home", lambda: home) + + dst, err = bc.snapshot_real_profile("chrome", src=str(src)) + assert err is None + # Simulate: user logs into a new site in their own browser, and the + # copy has drifted state that must survive (History not in refresh set). + (src / "Default" / "Cookies").write_text("sqlite-cookies-v2") + copy_history = home / "browser-profile" / "chrome" / "Default" / "History" + copy_history.write_text("agent-session-history") + + dst2, err2 = bc.snapshot_real_profile("chrome", src=str(src)) + assert err2 is None and dst2 == dst + assert (home / "browser-profile" / "chrome" / "Default" / "Cookies").read_text() == "sqlite-cookies-v2" + assert copy_history.read_text() == "agent-session-history" + + def test_missing_source_fails_closed(self, tmp_path, monkeypatch): + import hermes_cli.browser_connect as bc + monkeypatch.setattr(bc, "get_hermes_home", lambda: tmp_path / "hh") + dst, err = bc.snapshot_real_profile("chrome", src=str(tmp_path / "nope")) + assert dst is None + assert err and "was not found" in err + + +class TestRealProfileCdpLaunch: + """The agent-browser-based launcher in browser_tool._real_profile_cdp.""" + def _reset(self): import tools.browser_tool as bt - bt._use_real_profile_resolved = False - bt._cached_use_real_profile = False + bt._real_profile_cdp_cache.clear() def test_consent_off_is_noop(self): import tools.browser_tool as bt self._reset() with patch.object(bt, "_use_real_profile", return_value=False): - args, err = bt._real_profile_launch_args() - assert args == [] and err is None + cdp, err = bt._real_profile_cdp() + assert cdp is None and err is None def test_non_chromium_default_fails_closed(self): import tools.browser_tool as bt self._reset() with patch.object(bt, "_use_real_profile", return_value=True), \ patch("hermes_cli.browser_connect.detect_default_chromium", return_value=None): - args, err = bt._real_profile_launch_args() - assert args == [] + cdp, err = bt._real_profile_cdp() + assert cdp is None assert err and "not a supported Chromium" in err - def test_chromium_default_injects_profile(self, tmp_path): + def test_snapshot_failure_fails_closed(self): import tools.browser_tool as bt self._reset() - data_dir = tmp_path / "chrome-user-data" - data_dir.mkdir() with patch.object(bt, "_use_real_profile", return_value=True), \ patch("hermes_cli.browser_connect.detect_default_chromium", return_value="chrome"), \ - patch("hermes_cli.browser_connect.real_profile_data_dir", return_value=str(data_dir)), \ - patch("hermes_cli.browser_connect.chromium_executable", return_value="/usr/bin/google-chrome"): - args, err = bt._real_profile_launch_args() + patch("hermes_cli.browser_connect.snapshot_real_profile", return_value=(None, "boom")): + cdp, err = bt._real_profile_cdp() + assert cdp is None + assert err and "boom" in err + + def test_launch_returns_http_cdp(self, tmp_path): + import tools.browser_tool as bt + self._reset() + proc = Mock(returncode=0, stdout="", stderr="") + with patch.object(bt, "_use_real_profile", return_value=True), \ + patch("hermes_cli.browser_connect.detect_default_chromium", return_value="chrome"), \ + patch("hermes_cli.browser_connect.snapshot_real_profile", return_value=(str(tmp_path), None)), \ + patch.object(bt, "_agent_browser_get_cdp", + side_effect=[None, "http://127.0.0.1:41000"]), \ + patch.object(bt, "_find_agent_browser", return_value="/usr/bin/agent-browser"), \ + patch.object(bt.subprocess, "run", return_value=proc), \ + patch.object(bt, "_is_headed_mode", return_value=False): + cdp, err = bt._real_profile_cdp() assert err is None - assert "--profile" in args and str(data_dir) in args - assert "--executable-path" in args and "/usr/bin/google-chrome" in args + assert cdp == "http://127.0.0.1:41000" + self._reset() - def test_missing_profile_dir_fails_closed(self, tmp_path): + def test_launch_never_passes_headless(self, tmp_path): + """--headless would use a separate cookie store → 0 real cookies.""" import tools.browser_tool as bt self._reset() + proc = Mock(returncode=0, stdout="", stderr="") + captured = {} + + def fake_run(argv, **kw): + captured["argv"] = argv + return proc + with patch.object(bt, "_use_real_profile", return_value=True), \ patch("hermes_cli.browser_connect.detect_default_chromium", return_value="chrome"), \ - patch("hermes_cli.browser_connect.real_profile_data_dir", return_value=str(tmp_path / "nope")): - args, err = bt._real_profile_launch_args() - assert args == [] - assert err and "profile directory was not found" in err + patch("hermes_cli.browser_connect.snapshot_real_profile", return_value=(str(tmp_path), None)), \ + patch.object(bt, "_agent_browser_get_cdp", + side_effect=[None, "http://127.0.0.1:41000"]), \ + patch.object(bt, "_find_agent_browser", return_value="/usr/bin/agent-browser"), \ + patch.object(bt.subprocess, "run", side_effect=fake_run), \ + patch.object(bt, "_is_headed_mode", return_value=False): + bt._real_profile_cdp() + assert "--headless" not in captured["argv"] + assert "--profile" in captured["argv"] + assert str(tmp_path) in captured["argv"] + self._reset() + def test_reuses_only_session_on_our_copy_dir(self, tmp_path): + """A live session on a DIFFERENT dir (stale/throwaway) is closed, not reused.""" + import tools.browser_tool as bt + self._reset() + proc = Mock(returncode=0, stdout="", stderr="") + closed = {"n": 0} + with patch.object(bt, "_use_real_profile", return_value=True), \ + patch("hermes_cli.browser_connect.detect_default_chromium", return_value="chrome"), \ + patch("hermes_cli.browser_connect.snapshot_real_profile", return_value=(str(tmp_path), None)), \ + patch.object(bt, "_agent_browser_get_cdp", + side_effect=["http://127.0.0.1:5000", "http://127.0.0.1:41000"]), \ + patch.object(bt, "_cdp_http_ready", return_value=True), \ + patch.object(bt, "_cdp_on_data_dir", return_value=False), \ + patch.object(bt, "_agent_browser_close_session", + side_effect=lambda s: closed.__setitem__("n", closed["n"] + 1)), \ + patch.object(bt, "_find_agent_browser", return_value="/usr/bin/agent-browser"), \ + patch.object(bt.subprocess, "run", return_value=proc), \ + patch.object(bt, "_is_headed_mode", return_value=False): + cdp, err = bt._real_profile_cdp() + assert closed["n"] == 1 # stale wrong-dir session was closed + assert cdp == "http://127.0.0.1:41000" + self._reset() -class TestLocalBrowserRouting: - def _reset(self): + def test_cdp_on_data_dir_matches_devtoolsactiveport(self, tmp_path): import tools.browser_tool as bt - bt._use_real_profile_resolved = False - bt._cached_use_real_profile = False + (tmp_path / "DevToolsActivePort").write_text("41000\n/devtools/browser/x\n") + assert bt._cdp_on_data_dir("http://127.0.0.1:41000", str(tmp_path)) + assert not bt._cdp_on_data_dir("http://127.0.0.1:9999", str(tmp_path)) + + +class TestConsentConfigRead: + """Unmocked config read: _use_real_profile against a real config.yaml.""" - def test_local_browser_forces_sidecar_with_consent(self): + def test_consent_read_from_config(self, tmp_path, monkeypatch): import tools.browser_tool as bt - self._reset() - with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ - patch.object(bt, "_is_camofox_mode", return_value=False), \ - patch.object(bt, "_get_cloud_provider", return_value=Mock()), \ - patch.object(bt, "_url_is_private", return_value=False), \ - patch.object(bt, "_use_real_profile", return_value=True): - key = bt._navigation_session_key("t1", "https://example.com", local_browser=True) - assert key == "t1::local" + cfg = tmp_path / "config.yaml" + cfg.write_text("browser:\n use_real_profile: true\n") + with patch("hermes_cli.config.read_raw_config", + return_value={"browser": {"use_real_profile": True}}): + assert bt._use_real_profile() is True - def test_local_browser_without_cloud_provider_keeps_bare_session(self): - """Pure local backend: the bare session already is the (real-profile) - local Chromium; a second ``::local`` session would fight it for the - same user-data-dir.""" + def test_consent_default_off(self): import tools.browser_tool as bt - self._reset() - with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ - patch.object(bt, "_is_camofox_mode", return_value=False), \ - patch.object(bt, "_get_cloud_provider", return_value=None), \ - patch.object(bt, "_use_real_profile", return_value=True): - key = bt._navigation_session_key("t1", "https://example.com", local_browser=True) - assert key == "t1" + with patch("hermes_cli.config.read_raw_config", return_value={}): + assert bt._use_real_profile() is False - def test_local_browser_respects_private_url_opt_out(self): - """auto_local_for_private_urls: false is the user's LAN routing - decision; a model-supplied flag must not override it.""" + def test_consent_revocation_takes_effect_immediately(self): + """No process-lifetime caching: consent is a per-use read.""" import tools.browser_tool as bt - self._reset() - with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ - patch.object(bt, "_is_camofox_mode", return_value=False), \ - patch.object(bt, "_get_cloud_provider", return_value=Mock()), \ - patch.object(bt, "_url_is_private", return_value=True), \ - patch.object(bt, "_auto_local_for_private_urls", return_value=False), \ - patch.object(bt, "_use_real_profile", return_value=True): - key = bt._navigation_session_key("t1", "http://192.168.1.1/admin", local_browser=True) - assert key == "t1" + with patch("hermes_cli.config.read_raw_config", + return_value={"browser": {"use_real_profile": True}}): + assert bt._use_real_profile() is True + with patch("hermes_cli.config.read_raw_config", + return_value={"browser": {"use_real_profile": False}}): + assert bt._use_real_profile() is False - def test_local_browser_private_url_follows_auto_local_when_allowed(self): + +class TestLocalSessionRealProfile: + def test_local_session_attaches_to_real_profile_cdp(self): + import tools.browser_tool as bt + with patch.object(bt, "_real_profile_cdp", + return_value=("http://127.0.0.1:9251", None)), \ + patch.object(bt, "_resolve_cdp_override", side_effect=lambda u: u): + info = bt._create_local_session("t1") + assert info["cdp_url"] == "http://127.0.0.1:9251" + assert info["features"]["real_profile"] is True + assert info["session_name"].startswith("rp_") + + def test_local_session_fails_closed_on_error(self): + import tools.browser_tool as bt + with patch.object(bt, "_real_profile_cdp", return_value=(None, "no chromium")): + with pytest.raises(RuntimeError, match="no chromium"): + bt._create_local_session("t1") + + def test_local_session_without_consent_is_throwaway(self): + import tools.browser_tool as bt + with patch.object(bt, "_real_profile_cdp", return_value=(None, None)): + info = bt._create_local_session("t1") + assert info["cdp_url"] is None + assert "real_profile" not in info["features"] + assert info["session_name"].startswith("h_") + + +class TestBrowserExecLocalArg: + def _env(self): + return {} + + def test_local_forces_real_profile_under_cloud_backend(self): + import tools.browser_use_cli as bu + env = self._env() + with patch.object(bu, "_real_profile_consented", return_value=True), \ + patch("tools.browser_tool._get_cdp_override_raw", return_value=""), \ + patch("tools.browser_tool._get_cloud_provider", return_value=Mock()), \ + patch("tools.browser_tool._real_profile_cdp", + return_value=("http://127.0.0.1:9251", None)): + err = bu._resolve_real_profile_cdp(env, force_local=True) + assert err is None + assert env.get("BU_CDP_URL") == "http://127.0.0.1:9251" + + def test_no_force_keeps_cloud_backend(self): + import tools.browser_use_cli as bu + env = self._env() + with patch.object(bu, "_real_profile_consented", return_value=True), \ + patch("tools.browser_tool._get_cdp_override_raw", return_value=""), \ + patch("tools.browser_tool._get_cloud_provider", return_value=Mock()): + err = bu._resolve_real_profile_cdp(env, force_local=False) + assert err is None + assert "BU_CDP_URL" not in env and "BU_CDP_WS" not in env + + def test_local_backend_upgrades_without_force(self): + import tools.browser_use_cli as bu + env = self._env() + with patch.object(bu, "_real_profile_consented", return_value=True), \ + patch.object(bu, "_read_browser_cfg", return_value={}), \ + patch("tools.browser_tool._get_cdp_override_raw", return_value=""), \ + patch("tools.browser_tool._get_cloud_provider", return_value=None), \ + patch("tools.browser_tool._real_profile_cdp", + return_value=("http://127.0.0.1:9251", None)): + err = bu._resolve_real_profile_cdp(env, force_local=False) + assert err is None + assert env.get("BU_CDP_URL") == "http://127.0.0.1:9251" + + def test_consent_off_is_inert(self): + import tools.browser_use_cli as bu + env = self._env() + with patch.object(bu, "_real_profile_consented", return_value=False): + err = bu._resolve_real_profile_cdp(env, force_local=True) + assert err is None and env == {} + + def test_launch_failure_fails_closed(self): + import tools.browser_use_cli as bu + env = self._env() + with patch.object(bu, "_real_profile_consented", return_value=True), \ + patch("tools.browser_tool._get_cdp_override_raw", return_value=""), \ + patch("tools.browser_tool._real_profile_cdp", + return_value=(None, "chrome exited")): + err = bu._resolve_real_profile_cdp(env, force_local=True) + assert err == "chrome exited" + assert "BU_CDP_URL" not in env + + def test_explicit_bu_env_override_wins(self): + import tools.browser_use_cli as bu + env = {"BU_CDP_WS": "ws://operator-override"} + with patch.object(bu, "_real_profile_consented", return_value=True): + err = bu._resolve_real_profile_cdp(env, force_local=True) + assert err is None + assert env["BU_CDP_WS"] == "ws://operator-override" + assert "BU_CDP_URL" not in env + + def test_operator_cdp_override_wins(self): + import tools.browser_use_cli as bu + env = self._env() + with patch.object(bu, "_real_profile_consented", return_value=True), \ + patch("tools.browser_tool._get_cdp_override_raw", return_value="ws://connect"): + err = bu._resolve_real_profile_cdp(env, force_local=True) + assert err is None and env == {} + + +class TestBrowserExecSchemaGating: + def test_local_arg_absent_without_consent(self): + import tools.browser_use_cli as bu + with patch.object(bu, "_real_profile_consented", return_value=False): + overrides = bu._dynamic_schema_overrides() + assert "parameters" not in overrides + assert "local" not in bu.BROWSER_EXEC_SCHEMA["parameters"]["properties"] + + def test_local_arg_present_with_consent(self): + import tools.browser_use_cli as bu + with patch.object(bu, "_real_profile_consented", return_value=True): + overrides = bu._dynamic_schema_overrides() + props = overrides["parameters"]["properties"] + assert "local" in props + assert props["local"]["type"] == "boolean" + # Static schema must stay untouched (override is a copy). + assert "local" not in bu.BROWSER_EXEC_SCHEMA["parameters"]["properties"] + # 'local' must not be required — pure opt-in. + assert "local" not in overrides["parameters"].get("required", []) + + +class TestNavigationRouting: + def test_private_url_routing_unchanged(self): import tools.browser_tool as bt - self._reset() with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ patch.object(bt, "_is_camofox_mode", return_value=False), \ patch.object(bt, "_get_cloud_provider", return_value=Mock()), \ - patch.object(bt, "_url_is_private", return_value=True), \ patch.object(bt, "_auto_local_for_private_urls", return_value=True), \ - patch.object(bt, "_use_real_profile", return_value=True): - key = bt._navigation_session_key("t1", "http://192.168.1.1/admin", local_browser=True) + patch.object(bt, "_url_is_private", return_value=True): + key = bt._navigation_session_key("t1", "http://192.168.1.1/x") assert key == "t1::local" - def test_local_browser_ignored_without_consent(self): + def test_public_url_stays_on_cloud(self): import tools.browser_tool as bt - self._reset() with patch.object(bt, "_get_cdp_override_raw", return_value=""), \ patch.object(bt, "_is_camofox_mode", return_value=False), \ - patch.object(bt, "_use_real_profile", return_value=False), \ - patch.object(bt, "_get_cloud_provider", return_value=None): - key = bt._navigation_session_key("t1", "https://example.com", local_browser=True) - assert key == "t1" - - def test_cdp_override_still_wins_over_local_browser(self): - import tools.browser_tool as bt - self._reset() - with patch.object(bt, "_get_cdp_override_raw", return_value="ws://x"), \ - patch.object(bt, "_use_real_profile", return_value=True): - key = bt._navigation_session_key("t1", "https://example.com", local_browser=True) + patch.object(bt, "_get_cloud_provider", return_value=Mock()), \ + patch.object(bt, "_url_is_private", return_value=False): + key = bt._navigation_session_key("t1", "https://example.com") assert key == "t1" diff --git a/tools/browser_tool.py b/tools/browser_tool.py index 0b6ce0b02045d..98cb033452676 100644 --- a/tools/browser_tool.py +++ b/tools/browser_tool.py @@ -1025,9 +1025,6 @@ def _is_local_backend() -> bool: _auto_local_for_private_urls_resolved = False _cached_auto_local_for_private_urls: bool = True -_use_real_profile_resolved = False -_cached_use_real_profile: bool = False - def _get_browser_engine() -> str: """Return the configured browser engine (``auto``, ``lightpanda``, or ``chrome``). @@ -1433,65 +1430,220 @@ def _auto_local_for_private_urls() -> bool: def _use_real_profile() -> bool: """Return whether the user consented to real-profile local browsing. - Reads ``browser.use_real_profile`` (default False) once and caches it. When - True, local Chromium launches point agent-browser at the user's REAL - default-browser profile, and the browser tools' ``local_browser`` argument - is honored. + Reads ``browser.use_real_profile`` (default False) on EVERY call — it is a + consent switch, so flipping it off must take effect without a restart, and + in a multiplexed gateway each profile's config must decide for itself. + The read is one YAML load per local session creation (not per command), + so there is no hot-path cost to keeping it uncached. """ - global _use_real_profile_resolved, _cached_use_real_profile - if _use_real_profile_resolved: - return _cached_use_real_profile - - _use_real_profile_resolved = True try: from hermes_cli.config import read_raw_config cfg = read_raw_config() browser_cfg = cfg.get("browser", {}) - if isinstance(browser_cfg, dict) and "use_real_profile" in browser_cfg: - _cached_use_real_profile = bool(browser_cfg.get("use_real_profile")) + if isinstance(browser_cfg, dict): + return bool(browser_cfg.get("use_real_profile", False)) except Exception as e: logger.debug("Could not read use_real_profile from config: %s", e) - return _cached_use_real_profile + return False + + +# Session name for the single shared real-profile copy-browser. All consented +# local browsing attaches to this one agent-browser session so concurrent +# tasks reuse the same copy-browser instead of each launching a rival Chromium +# on the same copied user-data-dir. +_REAL_PROFILE_SESSION = "hermes-real-profile" +_real_profile_cdp_lock = threading.Lock() +_real_profile_cdp_cache: dict = {} + + +def _agent_browser_argv(browser_cmd: str) -> list: + """Command prefix to invoke agent-browser (binary or npx sentinel).""" + if _is_npx_agent_browser_sentinel(browser_cmd): + _npx_bin = _resolve_npx_bin() or "npx" + return [_npx_bin, "--ignore-scripts", "--prefer-offline", "-y", AGENT_BROWSER_NPX_SPEC] + return [browser_cmd] + + +def _cdp_http_ready(http_cdp: str) -> bool: + """True when an ``http://host:port`` CDP discovery root answers.""" + try: + from hermes_cli.browser_connect import is_browser_debug_ready + + return is_browser_debug_ready(http_cdp, timeout=1.0) + except Exception: + return False + + +def _agent_browser_get_cdp(session_name: str) -> Optional[str]: + """Return the HTTP CDP endpoint of an agent-browser session, or None. + + agent-browser prints ``ws://127.0.0.1:/devtools/browser/`` for + ``get cdp-url``; the browser-use harness wants the HTTP discovery root + (``http://127.0.0.1:``), so we convert. None when the session isn't + running or the port can't be parsed. + """ + try: + browser_cmd = _find_agent_browser() + except FileNotFoundError: + return None + try: + proc = subprocess.run( + [*_agent_browser_argv(browser_cmd), "--session", session_name, "get", "cdp-url"], + capture_output=True, text=True, timeout=15, env=_build_browser_env(), + ) + except (subprocess.SubprocessError, OSError) as e: + logger.debug("real-profile get cdp-url failed: %s", e) + return None + out = (proc.stdout or "").strip() + m = re.search(r"ws://127\.0\.0\.1:(\d+)/", out) + if not m: + return None + return f"http://127.0.0.1:{m.group(1)}" + + +def _cdp_on_data_dir(http_cdp: str, data_dir: str) -> bool: + """True when the CDP endpoint's browser is running on ``data_dir``. + + agent-browser's launched Chrome writes ``DevToolsActivePort`` into its + user-data-dir with the live debug port on the first line. Matching that + port against the CDP endpoint's port confirms the running browser is our + profile copy — not a throwaway temp dir a raced/stale launch fell back to. + """ + m = re.search(r":(\d+)", http_cdp or "") + if not m: + return False + try: + with open(os.path.join(data_dir, "DevToolsActivePort"), encoding="utf-8") as fh: + port_line = fh.readline().strip() + return port_line == m.group(1) + except OSError: + return False -def _real_profile_launch_args() -> tuple: - """Resolve (profile_args, error) for a real-profile local launch. +def _agent_browser_close_session(session_name: str) -> None: + """Best-effort close of an agent-browser session (stale/wrong-dir cleanup).""" + try: + browser_cmd = _find_agent_browser() + except FileNotFoundError: + return + try: + subprocess.run( + [*_agent_browser_argv(browser_cmd), "--session", session_name, "close"], + capture_output=True, text=True, timeout=15, env=_build_browser_env(), + ) + except (subprocess.SubprocessError, OSError) as e: + logger.debug("real-profile session close failed: %s", e) + + +def _real_profile_cdp() -> tuple: + """Resolve ``(cdp_url, error)`` for consented real-profile browsing. - Returns ``(["--profile", , "--executable-path", ], None)`` - when consent is on and the default browser is a resolvable Chromium. - Returns ``([], )`` when the default browser is non-Chromium or its - profile/binary cannot be located, so the caller can fail closed with a - clear reason. Returns ``([], None)`` when consent is off (no-op). + Snapshots the user's default-Chromium profile into a hermes-owned copy + (auth/login state only), then has agent-browser launch its packaged + Chromium on that copy and returns the HTTP CDP endpoint for the browser-use + harness to attach to. The copy is a non-default dir, so it sidesteps the + Chrome ≥136 default-profile remote-debugging block and never contends with + the user's running browser; agent-browser launches the packaged Chromium + with no mock-keychain switches, so keyring-encrypted cookies decrypt. + + A single shared agent-browser session is reused across calls (its CDP URL + is cached and re-validated). Returns ``(None, message)`` fail-closed when + the default browser is non-Chromium or the snapshot/launch fails; + ``(None, None)`` when consent is off. """ if not _use_real_profile(): - return [], None + return None, None + from hermes_cli.browser_connect import ( - chromium_executable, detect_default_chromium, - real_profile_data_dir, + snapshot_real_profile, ) - browser = detect_default_chromium() - if browser is None: - return [], ( - "browser.use_real_profile is on, but your default browser is not a " - "supported Chromium browser (Chrome, Edge, Brave, Chromium). " - "Real-profile browsing requires a Chromium default; set one or turn " - "the toggle off." - ) - data_dir = real_profile_data_dir(browser) - if not data_dir or not os.path.isdir(data_dir): - return [], ( - f"browser.use_real_profile is on and your default browser is " - f"'{browser}', but its profile directory was not found " - f"({data_dir!r}). Launch that browser at least once, or turn the " - "toggle off." - ) - args = ["--profile", data_dir] - exe = chromium_executable(browser) - if exe: - args += ["--executable-path", exe] - return args, None + with _real_profile_cdp_lock: + # Reuse a live copy-browser from an earlier call this process made. + cached = _real_profile_cdp_cache.get("cdp") + if cached and _cdp_http_ready(cached): + return cached, None + _real_profile_cdp_cache.pop("cdp", None) + + browser = detect_default_chromium() + if browser is None: + return None, ( + "browser.use_real_profile is on, but your default browser is not a " + "supported Chromium browser (Chrome, Edge, Brave, Chromium). " + "Real-profile browsing requires a Chromium default; set one or turn " + "the toggle off." + ) + copy_dir, err = snapshot_real_profile(browser) + if err or not copy_dir: + return None, f"browser.use_real_profile is on, but {err}" + + # A shared session may already be up from a previous hermes process, + # but ONLY reuse it when it is actually driving our copy dir — a stale + # session from an earlier/failed launch can be attached to a throwaway + # temp profile (agent-browser falls back to one when a launch races), + # which would serve zero real cookies. Verify, else close and relaunch. + existing = _agent_browser_get_cdp(_REAL_PROFILE_SESSION) + if existing and _cdp_http_ready(existing) and _cdp_on_data_dir(existing, copy_dir): + _real_profile_cdp_cache["cdp"] = existing + return existing, None + if existing: + _agent_browser_close_session(_REAL_PROFILE_SESSION) + + # Launch agent-browser's packaged Chromium on the profile COPY. This is + # the same launch path Hermes' built-in local browsing already uses, + # just pointed at the copied user-data-dir — no bespoke Chrome launch. + try: + browser_cmd = _find_agent_browser() + except FileNotFoundError as e: + return None, ( + "browser.use_real_profile is on, but the local browser engine " + f"(agent-browser) is not installed: {e}" + ) + argv = [ + *_agent_browser_argv(browser_cmd), + "--session", _REAL_PROFILE_SESSION, + "--profile", copy_dir, + ] + # Do NOT pass agent-browser's ``--headless``: it maps to Chrome's legacy + # headless mode, which uses a SEPARATE cookie store and loads none of the + # copied profile's cookies (verified: --headless → 0 cookies, default → + # full jar). agent-browser's default already runs windowless on a + # server (no DISPLAY) while reading the real cookie store, which is + # exactly what real-profile browsing needs. Headed mode is a superset + # (visible window) and equally fine, so no flag either way. + argv += ["open", "about:blank"] + try: + proc = subprocess.run( + argv, capture_output=True, text=True, + timeout=_get_open_command_timeout(first_open=True), + env=_build_browser_env(), + ) + except subprocess.TimeoutExpired: + return None, ( + "browser.use_real_profile is on, but the real-profile browser " + "took too long to start. Retry, or turn the toggle off." + ) + except (subprocess.SubprocessError, OSError) as e: + return None, f"browser.use_real_profile is on, but the launch failed: {e}" + if proc.returncode != 0: + tail = (proc.stderr or proc.stdout or "").strip().splitlines() + reason = tail[-1] if tail else f"exit {proc.returncode}" + return None, ( + f"browser.use_real_profile is on, but the real-profile browser " + f"failed to start: {reason}" + ) + + cdp = _agent_browser_get_cdp(_REAL_PROFILE_SESSION) + if not cdp: + return None, ( + "browser.use_real_profile is on, but the real-profile browser " + "started without exposing a devtools endpoint. Retry, or turn " + "the toggle off." + ) + _real_profile_cdp_cache["cdp"] = cdp + logger.info("real-profile browser ready for %s at %s (%s)", browser, cdp, copy_dir) + return cdp, None def _url_is_private(url: str) -> bool: @@ -1557,7 +1709,7 @@ def _url_is_private(url: str) -> bool: return False -def _navigation_session_key(task_id: str, url: str, local_browser: bool = False) -> str: +def _navigation_session_key(task_id: str, url: str) -> str: """Pick the session key that should handle ``url`` for ``task_id``. Returns the bare task_id unless ALL of these are true: @@ -1570,17 +1722,11 @@ def _navigation_session_key(task_id: str, url: str, local_browser: bool = False) When all are true, returns ``f"{task_id}::local"`` so the hybrid-routing path spawns a local Chromium sidecar while the cloud session (if any) - continues to serve public URLs. - - ``local_browser=True`` (the model-facing arg) forces the ``::local`` sidecar - for the backend choice — but ONLY when the user consented to real-profile - browsing (``browser.use_real_profile``). Without consent the flag is ignored - (the caller surfaces a note); it never silently routes to the real profile. - It does not override the private-URL policy: a private URL is still routed - exactly as ``browser.auto_local_for_private_urls`` says. Without a cloud - provider the bare session is already local (and already carries the real - profile when consented), so the flag adds nothing there. A CDP override or - Camofox mode still owns the session and is not overridden. + continues to serve public URLs. With ``browser.use_real_profile`` consent, + local sessions (bare or sidecar) attach to the user's real-profile + copy-browser via ``_create_local_session``; forcing a local session from + the model side is the Browser Use lane's ``local`` argument, not a + built-in-tools argument. """ if task_id is None: task_id = "default" @@ -1589,17 +1735,7 @@ def _navigation_session_key(task_id: str, url: str, local_browser: bool = False) if _is_camofox_mode(): return task_id if _get_cloud_provider() is None: - # Already local. A second ``::local`` session for the same task would - # contend with the bare one for the same user-data-dir. return task_id - if local_browser and _use_real_profile(): - # Every private-URL gate in browser_navigate keys off the sidecar - # key, so honouring the flag for a private URL while the user opted - # out of auto-local routing would turn a model-supplied argument - # into a LAN policy override. - if _url_is_private(url) and not _auto_local_for_private_urls(): - return task_id - return f"{task_id}{_LOCAL_SUFFIX}" if not _auto_local_for_private_urls(): return task_id if not _url_is_private(url): @@ -2284,11 +2420,6 @@ def _update_session_activity(task_id: str): "url": { "type": "string", "description": "The URL to navigate to (e.g., 'https://example.com')" - }, - "local_browser": { - "type": "boolean", - "description": "Use the user's real local browser profile (their logins/cookies) via a local Chromium session, even if a cloud backend is configured. Only honored when the user has consented (browser.use_real_profile); otherwise ignored.", - "default": False } }, "required": ["url"] @@ -2435,6 +2566,27 @@ def _update_session_activity(task_id: str): def _create_local_session(task_id: str) -> Dict[str, str]: import uuid + + # Real-profile consent: instead of an agent-browser-managed throwaway + # Chromium, attach this local session (via CDP) to the user's default + # browser running on a hermes-owned SNAPSHOT of their real profile — + # live logins/cookies included. Fail closed on resolver/launch errors: + # a consented user must never be silently downgraded to a throwaway. + cdp_url, err = _real_profile_cdp() + if err: + raise RuntimeError(err) + if cdp_url: + session_name = f"rp_{uuid.uuid4().hex[:10]}" + logger.info( + "Created real-profile local session %s for task %s", session_name, task_id + ) + return { + "session_name": session_name, + "bb_session_id": None, + "cdp_url": _resolve_cdp_override(cdp_url), + "features": {"local": True, "real_profile": True}, + } + session_name = f"h_{uuid.uuid4().hex[:10]}" logger.info("Created local browser session %s for task %s", session_name, task_id) @@ -3004,15 +3156,6 @@ def _run_browser_command( backend_args = ["--session", session_info["session_name"]] if _is_headed_mode(): backend_args.append("--headed") - # Real-profile consent: point agent-browser at the user's real default - # Chromium profile so their live logins/cookies are available. Fails - # closed (returns an error) if the default browser is non-Chromium or - # its profile can't be located, rather than silently using a throwaway. - _profile_args, _profile_err = _real_profile_launch_args() - if _profile_err: - return {"success": False, "error": _profile_err} - if _profile_args: - backend_args += _profile_args # Lightpanda engine injection (local mode only, agent-browser v0.25.3+). # Use the resolved session backend rather than global cloud-provider state: @@ -3408,17 +3551,13 @@ def evaluate_url_safety(url: str) -> Optional[dict]: return None -def browser_navigate(url: str, task_id: Optional[str] = None, local_browser: bool = False) -> str: +def browser_navigate(url: str, task_id: Optional[str] = None) -> str: """ Navigate to a URL in the browser. Args: url: The URL to navigate to task_id: Task identifier for session isolation - local_browser: Force a local Chromium session using the user's real - default-browser profile, even when a cloud backend is configured. - Honored only if the user consented via ``browser.use_real_profile``; - ignored (with a note) otherwise. Returns: JSON string with navigation result (includes stealth features info on first nav) @@ -3454,18 +3593,7 @@ def browser_navigate(url: str, task_id: Optional[str] = None, local_browser: boo # cloud provider never sees the URL in that case. Can also be opted # out globally via ``browser.allow_private_urls`` in config. effective_task_id = task_id or "default" - # local_browser is honored only with real-profile consent; note when the - # agent asked for it but consent is off so the behavior isn't silently lost. - real_profile_note = None - if local_browser and not _use_real_profile(): - real_profile_note = ( - "local_browser was requested but browser.use_real_profile is off; " - "using the default browser backend. Enable real-profile browsing in " - "Settings → Browser to honor this." - ) - nav_session_key = _navigation_session_key( - effective_task_id, url, local_browser=local_browser - ) + nav_session_key = _navigation_session_key(effective_task_id, url) auto_local_this_nav = _is_local_sidecar_key(nav_session_key) sensitive_query_key = _sensitive_query_param_name(url) @@ -3589,8 +3717,13 @@ def browser_navigate(url: str, task_id: Optional[str] = None, local_browser: boo "url": final_url, "title": title } - if real_profile_note: - response["note"] = real_profile_note + # Auditability: stamp navigations that ran on the user's real-profile + # copy-browser so usage is visible in the tool result. + try: + if (session_info.get("features") or {}).get("real_profile"): + response["used_real_profile"] = True + except Exception: + pass # Remember only a successful, non-blocked navigation as the task owner. # Failed opens and blocked redirects must not retarget follow-up clicks # or snapshots to a newly-created but irrelevant session. @@ -5593,7 +5726,7 @@ def check_browser_press_requirements() -> bool: handler=lambda args, **kw: routed_browser_handler( "browser_navigate", args, - fallback=lambda: browser_navigate(url=args.get("url", ""), task_id=kw.get("task_id"), local_browser=bool(args.get("local_browser", False))), + fallback=lambda: browser_navigate(url=args.get("url", ""), task_id=kw.get("task_id")), **_browser_router_kw(kw), ), check_fn=check_browser_navigate_requirements, diff --git a/tools/browser_use_cli.py b/tools/browser_use_cli.py index 257ba2c945b79..23dbd24bed220 100644 --- a/tools/browser_use_cli.py +++ b/tools/browser_use_cli.py @@ -599,11 +599,87 @@ def _resolve_backend_cdp( return None +def _real_profile_consented() -> bool: + """Whether the user opted in to real-profile local browsing (config read).""" + try: + from tools.browser_tool import _use_real_profile + + return _use_real_profile() + except Exception as e: # pragma: no cover — stubbed browser_tool in tests + logger.debug("real-profile consent lookup failed: %s", e) + return False + + +def _resolve_real_profile_cdp(env: dict, force_local: bool) -> Optional[str]: + """Point the harness at the user's real-profile copy-browser when consented. + + With ``browser.use_real_profile`` on, local browsing must mean the user's + default Chromium with their logins — a browser Hermes launches on a + SNAPSHOT of their real profile (see hermes_cli.browser_connect). Two ways + in: + + - the effective backend is already local (no cloud provider, no CDP + override, no legacy Browser Use cloud config): every local attach + upgrades to the real profile, silently — this is requirement one; or + - ``force_local`` (the consent-gated ``local`` tool arg): the model was + asked to drive the user's actual browser even though a cloud backend + is configured. The cloud backend keeps serving everything else. + + Explicit operator overrides (BU_CDP_WS/BU_CDP_URL env, /browser connect, + ``browser.cdp_url``) own the session either way, matching the built-in + lane's precedence. + + Sets BU_CDP_URL/BU_CDP_WS on success. Returns an error string when the + real-profile launch fails (fail closed — a consented user is never + silently downgraded to a throwaway browser), else None. + """ + if not _real_profile_consented(): + return None + if env.get("BU_CDP_WS") or env.get("BU_CDP_URL"): + return None + + try: + from tools.browser_tool import ( + _get_cdp_override_raw, + _get_cloud_provider, + _real_profile_cdp, + ) + except Exception as e: # pragma: no cover — stubbed browser_tool in tests + logger.debug("real-profile backend resolution unavailable: %s", e) + return None + + try: + if _get_cdp_override_raw(): + return None + except Exception: + pass + + if not force_local: + # Only auto-upgrade genuinely-local attaches; any cloud path (provider + # or legacy Browser Use cloud config) stays on its backend unless the + # model passes local=true. + try: + if _get_cloud_provider() is not None: + return None + except Exception: + return None + if is_legacy_browser_use_cloud_config(_read_browser_cfg()): + return None + + cdp, err = _real_profile_cdp() + if err: + return err + if cdp: + env["BU_CDP_URL" if cdp.startswith(("http://", "https://")) else "BU_CDP_WS"] = cdp + return None + + def browser_exec( code: str, session: str = "", timeout_s: int = _DEFAULT_TIMEOUT_S, task_id: Optional[str] = None, + local: bool = False, ): """Run Python code through the browser-use CLI, and return its output""" from tools.registry import tool_error, tool_result @@ -632,6 +708,24 @@ def browser_exec( "dashes, or underscores (e.g. 'r7k2')." ) env["BU_NAME"] = session + # Real-profile consent: on a local backend this upgrades the attach to + # the user's default browser (profile snapshot, logins included); with + # local=True it forces that even under a cloud backend. Runs BEFORE + # provider resolution so a real-profile hit short-circuits the cloud + # path via the BU_CDP_* env contract. + rp_err = _resolve_real_profile_cdp(env, force_local=bool(local)) + if rp_err: + return tool_error(rp_err) + if local and not (env.get("BU_CDP_URL") or env.get("BU_CDP_WS")): + # local=True is only served by the real-profile route; anything else + # (consent off — schema normally hidden, but be explicit; or an + # operator CDP override owning the session) must not pretend. + if not _real_profile_consented(): + return tool_error( + "local=true was requested but browser.use_real_profile is off. " + "Enable it in config.yaml (browser.use_real_profile: true) or " + "the desktop Settings → Browser section, then retry." + ) # Route through the configured browser backend (Browserbase, Firecrawl, # Nous gateway, CDP override, local Chrome, …). Named sessions compose # with the backend: BU_NAME namespaces the harness daemon (its IPC @@ -831,7 +925,28 @@ def _cli_skill_text() -> str: def _dynamic_schema_overrides() -> dict: - return {"description": _description_header() + _HELPERS_DIGEST} + overrides: dict = {"description": _description_header() + _HELPERS_DIGEST} + # The ``local`` argument exists ONLY when the user consented to + # real-profile browsing — everyone else's schema carries zero extra + # surface. get_definitions() applies this at schema-build time, and the + # caller memoizes on config.yaml mtime, so toggling consent changes the + # schema on the next session rather than mid-conversation. + if _real_profile_consented(): + props = dict(BROWSER_EXEC_SCHEMA["parameters"]["properties"]) + props["local"] = { + "type": "boolean", + "description": ( + "Drive the user's own local browser (a Hermes-managed copy of " + "their real default-Chromium profile, logins/cookies included) " + "instead of the configured cloud browser backend. Use when the " + "user asks to act as themselves — their accounts, their " + "sessions. No-op when the backend is already local. Default " + "false." + ), + "default": False, + } + overrides["parameters"] = {**BROWSER_EXEC_SCHEMA["parameters"], "properties": props} + return overrides BROWSER_EXEC_SCHEMA = { @@ -879,6 +994,7 @@ def _dynamic_schema_overrides() -> dict: session=args.get("session", "") or "", timeout_s=args.get("timeout_s", _DEFAULT_TIMEOUT_S), task_id=kw.get("task_id"), + local=bool(args.get("local", False)), ), check_fn=is_browser_use_cli_mode, dynamic_schema_overrides=_dynamic_schema_overrides, diff --git a/website/docs/user-guide/features/browser.md b/website/docs/user-guide/features/browser.md index 9ca1f368637f7..1403518ac353b 100644 --- a/website/docs/user-guide/features/browser.md +++ b/website/docs/user-guide/features/browser.md @@ -158,6 +158,41 @@ auto-installs it). Post-navigation redirects from a public URL onto a private address are still blocked (you can't use a redirect-to-internal trick to reach your LAN through the public path). +### Real profile browsing (use your own logins) + +By default, local browsing runs in a clean, throwaway profile — the agent is +logged into nothing. Turn on **real profile browsing** to let the agent browse +as *you*, with your existing logins and cookies: + +```yaml +# ~/.hermes/config.yaml +browser: + use_real_profile: true +``` + +When enabled, Hermes copies your default browser's profile — cookies, saved +logins, and preferences — into a managed snapshot under +`~/.hermes/browser-profile//`, then drives that snapshot with its +packaged Chromium. Your live browser profile is **never opened directly**: the +snapshot is a separate directory, so it doesn't fight your running browser for +the profile lock and it sidesteps Chrome 136+'s block on remote-debugging the +default profile directory. The auth files (cookies/logins/preferences) are +re-synced from your real profile on every launch, so logins you do in your own +browser show up in the agent's session. + +- **Supported browsers:** Chrome, Edge, Brave, Chromium (whichever is your OS + default). A non-Chromium default (e.g. Firefox) fails closed with a clear + message rather than guessing. +- **Works on any backend.** On a local backend it's automatic once the toggle + is on. Under a **cloud** browser backend, the agent can still open a + real-profile local session on demand via the `browser_exec` tool's `local` + argument (the tool only exposes that argument when this toggle is on) — the + cloud backend keeps serving everything else. +- **Security framing:** this is a consent-gated convenience, not an isolation + boundary. A page the agent visits runs with your real logins, so only enable + it when you want the agent acting as you. Off by default. +- **Desktop:** toggle it in **Settings → Browser → Use My Real Browser Profile**. + ### Camofox local mode [Camofox](https://github.com/jo-inc/camofox-browser) is a self-hosted Node.js server wrapping Camoufox (a Firefox fork with C++ fingerprint spoofing). It provides local anti-detection browsing without cloud dependencies. From dc5ba5d576034bf87318ef469d587fe656c24485 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 26 Aug 2026 08:03:30 -0700 Subject: [PATCH 07/12] fix(browser): real-profile snapshot is a first-class secret store + preserve channel identity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses two P1 review blockers (kshitij / @kxee) on the real-profile feature: Credential-store lifecycle for ~/.hermes/browser-profile/ (copied Cookies/ Login Data): - exclude the singular 'browser-profile' dir from backup AND import (_EXCLUDED_DIRS drives both) — was silently archiving cookies/logins - add a browser-profile/ directory-PREFIX read-deny to agent/file_safety.py, same class as auth.json / mcp-tokens - secure the snapshot dir through the canonical hermes_cli.config._secure_dir (honors managed/NixOS group-share + HERMES_UID/GID), not a bespoke chmod Channel identity (#95549 invariant — never normalize Beta/Dev/Canary to stable, which would drive a different account's profile): - detect recognized pre-release channels FIRST (Win ProgIds, macOS bundle ids, Linux .desktop) and return UNSUPPORTED_CHANNEL - macOS bundle match is now EXACT (was startswith); Linux/Win channel-before- stable ordering; real_profile_data_dir/chromium_executable reject the sentinel - _real_profile_cdp fails closed with a channel-specific message, never snapshots Tests: channel-not-normalized (linux/darwin/windows), wrong-principal fail-closed, backup exclusion, read-guard block/allow, snapshot dir secured. 187 browser + 222 backup/file_safety pass. Live re-verified: real Gmail inbox still loads. --- agent/file_safety.py | 28 ++++++ hermes_cli/backup.py | 6 ++ hermes_cli/browser_connect.py | 90 +++++++++++++++-- tests/tools/test_browser_real_profile.py | 118 +++++++++++++++++++++++ tools/browser_tool.py | 13 +++ 5 files changed, 246 insertions(+), 9 deletions(-) diff --git a/agent/file_safety.py b/agent/file_safety.py index 7547000fa46bb..fb469833dc509 100644 --- a/agent/file_safety.py +++ b/agent/file_safety.py @@ -374,6 +374,34 @@ def get_read_block_error(path: str) -> Optional[str]: "security boundary; the terminal tool can still bypass.)" ) + # browser-profile/: real-profile browsing snapshot (browser.use_real_profile). + # A copy of the user's Cookies / Login Data / Web Data lives here — the same + # credential class as auth.json, so it gets the same directory-prefix read + # deny. Prefix (not a finite filename list) so future Chromium files are + # covered too. + for hd in hermes_dirs: + try: + browser_profile = (hd / "browser-profile").resolve() + except Exception: + continue + if resolved == browser_profile: + return ( + f"Access denied: {path} is the Hermes real-profile browser " + "snapshot directory (copied cookies/logins) and cannot be read " + "directly. (Defense-in-depth — not a security boundary; the " + "terminal tool can still bypass.)" + ) + try: + resolved.relative_to(browser_profile) + except ValueError: + continue + return ( + f"Access denied: {path} is inside the Hermes real-profile browser " + "snapshot (copied cookies/logins) and cannot be read directly. " + "(Defense-in-depth — not a security boundary; the terminal tool " + "can still bypass.)" + ) + # Block common secret-bearing project-local .env files anywhere on disk. # The agent helping a user with their project rarely needs to read raw # .env contents — .env.example is the documented-shape substitute. The diff --git a/hermes_cli/backup.py b/hermes_cli/backup.py index 4a86594197503..c831ea7b78e93 100644 --- a/hermes_cli/backup.py +++ b/hermes_cli/backup.py @@ -82,6 +82,12 @@ # the busy timeout — a full backup hangs mid-archive on the first locked DB. # Profiles are regenerable (cache + re-login) and unsafe to snapshot live. "browser-profiles", + # Real-profile browsing snapshot (browser.use_real_profile). Holds copies of + # the user's Cookies / Login Data / Web Data — a credential-bearing store + # that must NOT enter a backup archive. It is regenerated from the user's + # live profile on the next consented launch. Singular, distinct from the + # ``browser-profiles`` CDP dir above; both are excluded. + "browser-profile", # Python dependency trees (plugin / MCP-server venvs under HERMES_HOME) — # regenerated by reinstalling; never irreplaceable state. ".venv", diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py index 28720c4d622f0..5f310d1511748 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -94,8 +94,11 @@ _CHROMIUM_BROWSERS = ("chrome", "edge", "brave", "chromium") # Windows UserChoice ProgId prefixes → canonical browser key. Matched -# case-insensitively by prefix so channel/version suffixes (e.g. -# ``ChromeHTML.X``, ``MSEdgeHTM``) still resolve. +# case-insensitively by prefix so version suffixes (e.g. ``ChromeHTML.X``) +# still resolve to STABLE. Pre-release channels have their own ProgIds and +# MUST be matched first (see _WINDOWS_CHANNEL_PROGIDS) so they are never +# swallowed into the stable family — driving the wrong profile is a +# wrong-principal bug (#95549 invariant). _WINDOWS_PROGID_MAP = ( ("chromehtml", "chrome"), ("msedgehtm", "edge"), @@ -103,9 +106,22 @@ ("chromiumhtm", "chromium"), ) -# Linux xdg default-web-browser .desktop name fragments → canonical key. +# Pre-release ProgId prefixes we recognize but do NOT support (their profiles +# live in channel-specific dirs the resolver tables don't carry). Matched +# BEFORE the stable map; a hit fails closed rather than resolving to stable. +# ``ChromeBHTML`` = Beta, ``ChromeDHTML`` = Dev, ``ChromeSSHTML`` = Canary +# (SxS); ``MSEdgeBHTML`` / ``MSEdgeDHTML`` / ``MSEdgeCHTML`` = Edge channels. +_WINDOWS_CHANNEL_PROGIDS = ( + "chromebhtml", "chromedhtml", "chromesshtml", "chromecanaryhtml", + "msedgebhtml", "msedgedhtml", "msedgechtml", + "bravebetahtml", "bravenightlyhtml", +) + +# Linux xdg default-web-browser .desktop name fragments → canonical STABLE key. # Includes the Flatpak application ids (``com.google.Chrome.desktop`` etc.), -# which share none of the native package name fragments. +# which share none of the native package name fragments. Anchored so a channel +# .desktop (``google-chrome-beta``, ``com.google.chrome.beta``) does NOT match +# the stable fragment — channels are caught by _LINUX_CHANNEL_FRAGMENTS first. _LINUX_DESKTOP_MAP = ( ("google-chrome", "chrome"), ("com.google.chrome", "chrome"), @@ -116,6 +132,15 @@ ("msedge", "edge"), ) +# Non-stable Linux channel .desktop fragments — recognized, unsupported. +# Checked before the stable map; a hit fails closed. +_LINUX_CHANNEL_FRAGMENTS = ( + "google-chrome-beta", "google-chrome-unstable", "google-chrome-canary", + "com.google.chrome.beta", "com.google.chrome.dev", "com.google.chrome.canary", + "microsoft-edge-beta", "microsoft-edge-dev", "microsoft-edge-canary", + "brave-browser-beta", "brave-browser-nightly", "brave-browser-dev", +) + # Where sandboxed Linux packages keep the profile instead of $XDG_CONFIG_HOME. _LINUX_FLATPAK_IDS = { "chrome": "com.google.Chrome", @@ -128,7 +153,8 @@ "brave": ("snap", "brave", "current", ".config", "BraveSoftware", "Brave-Browser"), } -# macOS LaunchServices bundle-id fragments → canonical key. +# macOS LaunchServices bundle-id → canonical STABLE key. EXACT match (not +# prefix): ``com.google.chrome.beta`` must not be read as ``com.google.chrome``. _DARWIN_BUNDLE_MAP = ( ("com.google.chrome", "chrome"), ("com.microsoft.edgemac", "edge"), @@ -136,6 +162,19 @@ ("org.chromium.chromium", "chromium"), ) +# Non-stable macOS channel bundle ids — recognized, unsupported. Checked first. +_DARWIN_CHANNEL_BUNDLES = ( + "com.google.chrome.beta", "com.google.chrome.dev", "com.google.chrome.canary", + "com.microsoft.edgemac.beta", "com.microsoft.edgemac.dev", "com.microsoft.edgemac.canary", + "com.brave.browser.beta", "com.brave.browser.nightly", +) + +# Sentinel returned when the OS default is a recognized-but-unsupported +# Chromium CHANNEL (Beta/Dev/Canary). Distinct from None (non-Chromium) so the +# caller fails closed with a channel-specific message instead of driving the +# stable profile of a different account. +UNSUPPORTED_CHANNEL = "__unsupported_channel__" + def _real_profile_relparts(browser: str) -> tuple: """(mac_support_subdir, windows_localappdata_parts, linux_config_name).""" @@ -269,6 +308,11 @@ def _detect_default_windows() -> str | None: except Exception: return None low = str(prog_id or "").lower() + # Channels first: a recognized Beta/Dev/Canary ProgId must fail closed, not + # fall through to a stable prefix match and drive the stable profile. + for chan in _WINDOWS_CHANNEL_PROGIDS: + if low.startswith(chan): + return UNSUPPORTED_CHANNEL for prefix, browser in _WINDOWS_PROGID_MAP: if low.startswith(prefix): return browser @@ -337,12 +381,16 @@ def _detect_default_darwin() -> str | None: bundle = _launchservices_https_handler(out) if not bundle: return None + b = bundle.lower() + # Channels first (exact): a Beta/Dev/Canary bundle must fail closed. + if b in _DARWIN_CHANNEL_BUNDLES: + return UNSUPPORTED_CHANNEL for frag, browser in _DARWIN_BUNDLE_MAP: - if bundle.startswith(frag): + if b == frag: return browser - # A non-Chromium https handler (Safari, Firefox, Arc, …): fail closed. - # No "first installed Chromium wins" fallback — that would drive a - # browser the user never made their default. + # A non-Chromium https handler (Safari, Firefox, Arc, …) or an unknown + # channel bundle: fail closed. No "first installed Chromium wins" fallback + # — that would drive a browser the user never made their default. return None @@ -358,6 +406,12 @@ def _detect_default_linux() -> str | None: ).stdout.strip().lower() except Exception: out = "" + # Channels first: ``google-chrome-beta.desktop`` contains the stable + # ``google-chrome`` fragment, so a substring match would drive stable. + # Catch recognized channels and fail closed instead. + for frag in _LINUX_CHANNEL_FRAGMENTS: + if frag in out: + return UNSUPPORTED_CHANNEL for frag, browser in _LINUX_DESKTOP_MAP: if frag in out: return browser @@ -451,6 +505,23 @@ def real_profile_copy_dir(browser: str) -> str: return str(get_hermes_home() / "browser-profile" / browser) +def _secure_snapshot_root(path: str) -> None: + """Lock down the snapshot dir through Hermes' canonical secret-store policy. + + The snapshot holds copies of the user's Cookies / Login Data, so it is a + credential store and must get the same owner-only permissions (and + managed-mode / NixOS group-share carve-out, HERMES_UID/GID ownership) as + every other Hermes secret dir — via ``hermes_cli.config._secure_dir``, + not a bespoke chmod. Deferred import avoids a config↔browser import cycle. + """ + try: + from hermes_cli.config import _secure_dir + + _secure_dir(path) + except Exception as e: # never block a launch on a permissions best-effort + logger.debug("could not secure real-profile snapshot dir %s: %s", path, e) + + def _profile_subdirs(src: str) -> list[str]: """Names of per-profile dirs (Default, Profile 1, ...) inside a data dir.""" out = [] @@ -486,6 +557,7 @@ def snapshot_real_profile(browser: str, src: str | None = None) -> tuple[str | N try: if fresh: os.makedirs(dst, exist_ok=True) + _secure_snapshot_root(dst) try: shutil.copytree( src, diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py index 5cf447027ac5e..d36ae21b1a4e5 100644 --- a/tests/tools/test_browser_real_profile.py +++ b/tests/tools/test_browser_real_profile.py @@ -390,3 +390,121 @@ def test_public_url_stays_on_cloud(self): patch.object(bt, "_url_is_private", return_value=False): key = bt._navigation_session_key("t1", "https://example.com") assert key == "t1" + + +class TestChannelIdentity: + """#95549 invariant: pre-release channels must NOT normalize to stable. + + Swallowing Beta/Dev/Canary into the stable family drives a different + profile/account — a wrong-principal bug. Detection must flag the channel + (UNSUPPORTED_CHANNEL) so the caller fails closed, never returning 'chrome' + for a Beta default. + """ + + def test_linux_beta_not_normalized_to_stable(self): + import hermes_cli.browser_connect as bc + with patch.object(bc.subprocess, "run", + return_value=Mock(stdout="google-chrome-beta.desktop\n")): + assert bc._detect_default_linux() == bc.UNSUPPORTED_CHANNEL + + def test_linux_stable_still_resolves(self): + import hermes_cli.browser_connect as bc + with patch.object(bc.subprocess, "run", + return_value=Mock(stdout="google-chrome.desktop\n")): + assert bc._detect_default_linux() == "chrome" + + def test_linux_flatpak_beta_not_stable(self): + import hermes_cli.browser_connect as bc + with patch.object(bc.subprocess, "run", + return_value=Mock(stdout="com.google.chrome.beta.desktop\n")): + assert bc._detect_default_linux() == bc.UNSUPPORTED_CHANNEL + + def test_darwin_canary_not_normalized(self): + import hermes_cli.browser_connect as bc + with patch.object(bc, "_launchservices_https_handler", + return_value="com.google.chrome.canary"): + with patch.object(bc.subprocess, "run", return_value=Mock(stdout="")): + assert bc._detect_default_darwin() == bc.UNSUPPORTED_CHANNEL + + def test_darwin_stable_exact_match(self): + import hermes_cli.browser_connect as bc + with patch.object(bc, "_launchservices_https_handler", + return_value="com.google.chrome"): + with patch.object(bc.subprocess, "run", return_value=Mock(stdout="")): + assert bc._detect_default_darwin() == "chrome" + + def test_windows_progid_maps(self): + import hermes_cli.browser_connect as bc + # Stable ProgIds → family; channel ProgIds are in the channel set. + assert dict(bc._WINDOWS_PROGID_MAP)["chromehtml"] == "chrome" + assert "chromebhtml" in bc._WINDOWS_CHANNEL_PROGIDS # Beta + assert "msedgebhtml" in bc._WINDOWS_CHANNEL_PROGIDS # Edge Beta + # A channel ProgId must not be a prefix hit for any stable entry. + for chan in bc._WINDOWS_CHANNEL_PROGIDS: + assert not any(chan.startswith(p) for p, _ in bc._WINDOWS_PROGID_MAP) + + def test_channel_sentinel_fails_closed_in_cdp(self): + """A channel default → _real_profile_cdp fails closed, never launches.""" + import tools.browser_tool as bt + import hermes_cli.browser_connect as bc + bt._real_profile_cdp_cache.clear() + with patch.object(bt, "_use_real_profile", return_value=True), \ + patch("hermes_cli.browser_connect.detect_default_chromium", + return_value=bc.UNSUPPORTED_CHANNEL), \ + patch("hermes_cli.browser_connect.snapshot_real_profile") as snap: + cdp, err = bt._real_profile_cdp() + assert cdp is None + assert err and "pre-release" in err.lower() + snap.assert_not_called() # never even snapshotted a stable profile + bt._real_profile_cdp_cache.clear() + + def test_data_dir_rejects_sentinel(self): + import hermes_cli.browser_connect as bc + assert bc.real_profile_data_dir(bc.UNSUPPORTED_CHANNEL, "Linux") is None + assert bc.chromium_executable(bc.UNSUPPORTED_CHANNEL, "Linux") is None + + +class TestSnapshotIsCredentialStore: + """The copied Cookies/Login Data must live inside Hermes' secret lifecycle.""" + + def test_excluded_from_backup(self): + import hermes_cli.backup as bk + # Exact-component match (both singular and plural browser dirs). + assert "browser-profile" in bk._EXCLUDED_DIRS + assert bk._should_exclude( + __import__("pathlib").Path("browser-profile/chrome/Default/Cookies") + ) + + def test_read_guard_blocks_snapshot(self, tmp_path, monkeypatch): + import agent.file_safety as fs + home = tmp_path / ".hermes" + (home / "browser-profile" / "chrome" / "Default").mkdir(parents=True) + cookies = home / "browser-profile" / "chrome" / "Default" / "Cookies" + cookies.write_text("secret-cookie-db") + monkeypatch.setenv("HERMES_HOME", str(home)) + err = fs.get_read_block_error(str(cookies)) + assert err and "snapshot" in err.lower() + + def test_read_guard_allows_normal_file(self, tmp_path, monkeypatch): + import agent.file_safety as fs + home = tmp_path / ".hermes" + home.mkdir(parents=True) + monkeypatch.setenv("HERMES_HOME", str(home)) + normal = tmp_path / "notes.txt" + normal.write_text("hello") + assert fs.get_read_block_error(str(normal)) is None + + def test_snapshot_dir_secured(self, tmp_path, monkeypatch): + """snapshot_real_profile locks the dir via the canonical _secure_dir.""" + import hermes_cli.browser_connect as bc + src = tmp_path / "real" / "Default" + src.mkdir(parents=True) + (tmp_path / "real" / "Local State").write_text("{}") + (src / "Cookies").write_text("db") + monkeypatch.setattr(bc, "get_hermes_home", lambda: tmp_path / "hh") + called = {} + with patch("hermes_cli.config._secure_dir", + side_effect=lambda p: called.__setitem__("p", p)): + dst, err = bc.snapshot_real_profile("chrome", src=str(tmp_path / "real")) + assert err is None + assert called.get("p") == dst # secured through the canonical owner diff --git a/tools/browser_tool.py b/tools/browser_tool.py index 98cb033452676..0d4bc5561662d 100644 --- a/tools/browser_tool.py +++ b/tools/browser_tool.py @@ -1555,6 +1555,7 @@ def _real_profile_cdp() -> tuple: return None, None from hermes_cli.browser_connect import ( + UNSUPPORTED_CHANNEL, detect_default_chromium, snapshot_real_profile, ) @@ -1574,6 +1575,18 @@ def _real_profile_cdp() -> tuple: "Real-profile browsing requires a Chromium default; set one or turn " "the toggle off." ) + if browser == UNSUPPORTED_CHANNEL: + # A recognized pre-release channel (Beta/Dev/Canary) is the OS + # default. Its profile lives in a channel-specific directory we + # don't resolve, and normalizing it to the stable family would + # drive a DIFFERENT profile/account — a wrong-principal bug. Fail + # closed rather than guess (#95549 invariant). + return None, ( + "browser.use_real_profile is on, but your default browser is a " + "pre-release Chromium channel (Beta / Dev / Canary), which " + "real-profile browsing does not support. Set your default to a " + "stable Chrome / Edge / Brave / Chromium, or turn the toggle off." + ) copy_dir, err = snapshot_real_profile(browser) if err or not copy_dir: return None, f"browser.use_real_profile is on, but {err}" From 489fcd15ac0773d230b6052a7cc87c80df9fb618 Mon Sep 17 00:00:00 2001 From: Jan-Stefan Janetzky Date: Wed, 26 Aug 2026 17:21:38 +0200 Subject: [PATCH 08/12] fix(browser): read the macOS https handler past LSHandlerPreferredVersions The entry scanner splits the LSHandlers dump at brace depth 1, so a handler entry still contains its nested LSHandlerPreferredVersions dictionary. That block repeats LSHandlerRoleAll one level down with a value that is not a bundle id, and the role scan took the first non-"-" match it found. Older macOS wrote "-" there, which the filter happened to skip. macOS 26 writes the real preferred version, so the scan returns "7559.97" instead of "com.google.chrome": detect_default_chromium("Darwin") yields None and real-profile browsing fails closed at consent resolution on a machine that does have a Chromium default. Flatten nested dictionaries out of the entry before reading the role, so only keys at the entry's own level are considered. The test helper only ever fed the "-" placeholder, which is why this shape was not covered; a versioned helper now exercises both the Chromium and the non-Chromium default. Reported by @kshitijk4poor on #95620 (macOS 26, Chrome 152). The regex came in with #95549, so the same defect is present there. --- hermes_cli/browser_connect.py | 14 ++++++-- .../test_browser_connect_default_chromium.py | 32 +++++++++++++++++++ 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py index 5f310d1511748..a3552e3407b0e 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -357,9 +357,17 @@ def _launchservices_https_handler(dump: str) -> str | None: low = entry.lower() if not re.search(r'lshandlerurlscheme\s*=\s*"?https"?\s*;', low): continue - # The nested LSHandlerPreferredVersions block carries "-" placeholders - # under the same key names; the real bundle id is the first non-"-". - for role in re.findall(r'lshandlerrole(?:all|viewer)\s*=\s*"?([a-z0-9.\-]+)"?\s*;', low): + # Strip nested dictionaries before reading the role. The entry still + # carries them (the scanner above only splits at depth 1), and + # LSHandlerPreferredVersions repeats the SAME key names one level + # down with a value that is not a bundle id. On macOS 26 that value + # is a real version string ("7559.97"), so a plain non-"-" scan + # returns the version instead of the handler and detection fails + # closed on a machine that does have a Chromium default. + flat = re.sub(r"\{[^{}]*\}", "", low) + while re.search(r"\{[^{}]*\}", flat): + flat = re.sub(r"\{[^{}]*\}", "", flat) + for role in re.findall(r'lshandlerrole(?:all|viewer)\s*=\s*"?([a-z0-9.\-]+)"?\s*;', flat): if role != "-": return role return None diff --git a/tests/hermes_cli/test_browser_connect_default_chromium.py b/tests/hermes_cli/test_browser_connect_default_chromium.py index 4a654d8e66e63..576d5b984a7d8 100644 --- a/tests/hermes_cli/test_browser_connect_default_chromium.py +++ b/tests/hermes_cli/test_browser_connect_default_chromium.py @@ -27,6 +27,24 @@ def _handler(scheme: str, bundle: str) -> str: ) +def _handler_versioned(scheme: str, bundle: str, version: str) -> str: + """A handler whose LSHandlerPreferredVersions carries a real version. + + macOS 26 records the preferred version instead of the "-" placeholder + older releases wrote, so the nested block holds a value that is not a + bundle id but sits under the same key name. + """ + return ( + " {\n" + " LSHandlerPreferredVersions = {\n" + f' LSHandlerRoleAll = "{version}";\n' + " };\n" + f' LSHandlerRoleAll = "{bundle}";\n' + f" LSHandlerURLScheme = {scheme};\n" + " }" + ) + + def _content_type_handler(uti: str, bundle: str) -> str: return ( " {\n" @@ -56,6 +74,20 @@ def test_nested_dictionary_does_not_split_the_entry(self): dump = _ls_dump(_handler("https", "com.microsoft.edgemac")) assert bc._launchservices_https_handler(dump) == "com.microsoft.edgemac" + def test_preferred_version_is_not_mistaken_for_the_bundle_id(self): + """macOS 26 writes a version string where older releases wrote "-". + + The nested LSHandlerPreferredVersions block repeats LSHandlerRoleAll, + so reading the first non-"-" value returns "7559.97" and detection + fails closed on a machine that does have Chrome as its default. + """ + dump = _ls_dump(_handler_versioned("https", "com.google.chrome", "7559.97")) + assert bc._launchservices_https_handler(dump) == "com.google.chrome" + + def test_preferred_version_block_on_a_non_chromium_default(self): + dump = _ls_dump(_handler_versioned("https", "com.apple.safari", "7559.97")) + assert bc._launchservices_https_handler(dump) == "com.apple.safari" + class TestDetectDefaultDarwin: def _run_with(self, dump: str): From 3d563e01e82ea70e4a88e341a3f77304303c6bf8 Mon Sep 17 00:00:00 2001 From: Jan-Stefan Janetzky Date: Wed, 26 Aug 2026 17:23:00 +0200 Subject: [PATCH 09/12] fix(browser): drive the profile the user actually browses in MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The snapshot reproduces the source layout, so a user whose session lives in "Profile 6" gets a copy whose Default is some other, usually signed-out profile. Chrome opens Default in a user-data-dir unless --profile-directory says otherwise, and agent-browser only emits that flag when --profile is a profile NAME (is_chrome_profile_name, no slash) — the real-profile path hands it a PATH, so the flag is never added. Result: the launch succeeds, cookies are present, and the agent is signed out anyway, because it is reading a profile the user does not use. Read profile.last_used from Local State and mirror that profile's auth files into the copy's Default slot, on both the full-copy and the refresh path. The original directories are left in place. Falls back to Default when Local State is missing, unparsable, or names a directory that is not there. Reported by @kshitijk4poor on #95620 (LinkedIn login page instead of the feed, session in a non-Default profile). --- hermes_cli/browser_connect.py | 55 +++++++++++++++++ tests/tools/test_browser_real_profile.py | 79 ++++++++++++++++++++++++ 2 files changed, 134 insertions(+) diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py index a3552e3407b0e..d47c71f7d5aad 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -2,6 +2,7 @@ from __future__ import annotations +import json import logging import ntpath import os @@ -543,6 +544,51 @@ def _profile_subdirs(src: str) -> list[str]: return out or ["Default"] +def active_profile_dir(src: str) -> str: + """Return the profile directory the user actually browses in. + + Chromium records it as ``profile.last_used`` in ``Local State``. Falls + back to ``Default`` when the file is missing/unparsable or names a + directory that isn't there. + """ + try: + with open(os.path.join(src, "Local State"), encoding="utf-8") as fh: + state = json.load(fh) + except (OSError, ValueError) as e: + logger.debug("real-profile: could not read Local State in %s: %s", src, e) + return "Default" + profile = state.get("profile") if isinstance(state, dict) else None + last_used = profile.get("last_used") if isinstance(profile, dict) else None + if isinstance(last_used, str) and last_used: + if os.path.isdir(os.path.join(src, last_used)): + return last_used + logger.debug("real-profile: Local State names missing profile %r", last_used) + return "Default" + + +def _mirror_active_profile_as_default(src: str, dst: str, active: str) -> None: + """Copy ``active``'s auth files into the snapshot's ``Default`` slot. + + Chrome opens ``Default`` inside a user-data-dir unless + ``--profile-directory`` says otherwise, and agent-browser only emits that + flag when ``--profile`` is a profile NAME — the real-profile path hands it + a PATH. So the copy's ``Default`` has to BE the profile the user browses + in, or the agent gets a signed-out profile that merely sorts first. + """ + for rel in _AUTH_REFRESH_FILES: + if "" not in rel: + continue + s = os.path.join(src, rel.replace("", active)) + if not os.path.isfile(s): + continue + d = os.path.join(dst, rel.replace("", "Default")) + try: + os.makedirs(os.path.dirname(d), exist_ok=True) + shutil.copy2(s, d) + except OSError as e: + logger.debug("real-profile: could not mirror %s as Default: %s", rel, e) + + def snapshot_real_profile(browser: str, src: str | None = None) -> tuple[str | None, str | None]: """Snapshot ``browser``'s real profile into the hermes copy dir. @@ -598,6 +644,15 @@ def snapshot_real_profile(browser: str, src: str | None = None) -> tuple[str | N shutil.copy2(s, d) except OSError as e: logger.debug("real-profile refresh: skipped %s: %s", r, e) + # Point the copy's Default slot at the profile the user browses in. + # Runs on both paths: the full copy reproduces the source layout (so + # "Profile 6" stays "Profile 6"), and the refresh re-syncs each profile + # in place — neither makes Default the ACTIVE one, which is what Chrome + # will open here. + active = active_profile_dir(src) + if active != "Default": + logger.info("real-profile: mirroring %r into the snapshot's Default", active) + _mirror_active_profile_as_default(src, dst, active) # Never carry live-instance leftovers into the copy. for leftover in ("SingletonLock", "SingletonSocket", "SingletonCookie"): try: diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py index d36ae21b1a4e5..a26e5f488ebb1 100644 --- a/tests/tools/test_browser_real_profile.py +++ b/tests/tools/test_browser_real_profile.py @@ -117,6 +117,85 @@ def test_missing_source_fails_closed(self, tmp_path, monkeypatch): assert err and "was not found" in err +class TestActiveProfileLandsInDefault: + """Chrome opens Default in a user-data-dir; that has to be the live one.""" + + def _make_multi_profile(self, root, last_used): + """A user-data-dir whose signed-in session is NOT in Default.""" + for name in ("Default", "Profile 6"): + (root / name / "Network").mkdir(parents=True) + (root / name / "Preferences").write_text("{}", encoding="utf-8") + (root / "Local State").write_text( + json.dumps({"profile": {"last_used": last_used}}), encoding="utf-8" + ) + # Default is a stale, signed-out profile; Profile 6 carries the session. + (root / "Default" / "Cookies").write_text("signed-out", encoding="utf-8") + (root / "Default" / "Network" / "Cookies").write_text("signed-out-net", encoding="utf-8") + (root / "Profile 6" / "Cookies").write_text("li_at-session", encoding="utf-8") + (root / "Profile 6" / "Network" / "Cookies").write_text("li_at-net", encoding="utf-8") + (root / "Profile 6" / "Login Data").write_text("real-logins", encoding="utf-8") + return root + + def test_active_profile_is_read_from_local_state(self, tmp_path): + import hermes_cli.browser_connect as bc + src = self._make_multi_profile(tmp_path / "real", "Profile 6") + assert bc.active_profile_dir(str(src)) == "Profile 6" + + def test_active_profile_falls_back_when_local_state_is_unusable(self, tmp_path): + import hermes_cli.browser_connect as bc + src = tmp_path / "real" + (src / "Default").mkdir(parents=True) + assert bc.active_profile_dir(str(src)) == "Default" + (src / "Local State").write_text("not json", encoding="utf-8") + assert bc.active_profile_dir(str(src)) == "Default" + (src / "Local State").write_text( + json.dumps({"profile": {"last_used": "Profile 9"}}), encoding="utf-8" + ) + # Names a directory that does not exist -> do not point Chrome at it. + assert bc.active_profile_dir(str(src)) == "Default" + + def test_snapshot_puts_the_active_profile_in_default(self, tmp_path, monkeypatch): + import hermes_cli.browser_connect as bc + src = self._make_multi_profile(tmp_path / "real", "Profile 6") + home = tmp_path / "hermes-home" + monkeypatch.setattr(bc, "get_hermes_home", lambda: home) + + dst, err = bc.snapshot_real_profile("chrome", src=str(src)) + assert err is None + copy = home / "browser-profile" / "chrome" + # Chrome will open Default here, so Default must hold the live session. + assert (copy / "Default" / "Cookies").read_text(encoding="utf-8") == "li_at-session" + assert (copy / "Default" / "Network" / "Cookies").read_text(encoding="utf-8") == "li_at-net" + assert (copy / "Default" / "Login Data").read_text(encoding="utf-8") == "real-logins" + # The original layout is still there for anything that addresses it. + assert (copy / "Profile 6" / "Cookies").read_text(encoding="utf-8") == "li_at-session" + + def test_refresh_keeps_default_pointed_at_the_active_profile(self, tmp_path, monkeypatch): + import hermes_cli.browser_connect as bc + src = self._make_multi_profile(tmp_path / "real", "Profile 6") + home = tmp_path / "hermes-home" + monkeypatch.setattr(bc, "get_hermes_home", lambda: home) + + bc.snapshot_real_profile("chrome", src=str(src)) + (src / "Profile 6" / "Cookies").write_text("li_at-session-v2", encoding="utf-8") + dst2, err2 = bc.snapshot_real_profile("chrome", src=str(src)) + + assert err2 is None + copy = home / "browser-profile" / "chrome" + assert (copy / "Default" / "Cookies").read_text(encoding="utf-8") == "li_at-session-v2" + + def test_default_is_left_alone_when_it_is_the_active_profile(self, tmp_path, monkeypatch): + import hermes_cli.browser_connect as bc + src = self._make_multi_profile(tmp_path / "real", "Default") + home = tmp_path / "hermes-home" + monkeypatch.setattr(bc, "get_hermes_home", lambda: home) + + dst, err = bc.snapshot_real_profile("chrome", src=str(src)) + assert err is None + copy = home / "browser-profile" / "chrome" + assert (copy / "Default" / "Cookies").read_text(encoding="utf-8") == "signed-out" + + class TestRealProfileCdpLaunch: """The agent-browser-based launcher in browser_tool._real_profile_cdp.""" From c4080acfd47ef2ca1ad23f5e79c562ba70c4ae54 Mon Sep 17 00:00:00 2001 From: Jan-Stefan Janetzky Date: Wed, 26 Aug 2026 17:23:41 +0200 Subject: [PATCH 10/12] fix(browser): re-secure the profile snapshot on every launch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _secure_snapshot_root() only ran inside the fresh-copy branch, so the permissions on a credential-bearing directory were asserted exactly once, at creation. _secure_dir is deliberately best-effort — the wrapper swallows and debug-logs — so a first attempt that failed left the copied Cookies / Login Data tree at whatever the umask gave it, permanently and silently. A snapshot dir created by an earlier build was never revisited either. Move the call ahead of the fresh/refresh split so it runs on every consented launch, and secure the browser-profile/ parent as well: makedirs creates it under the process umask, and it enumerates every browser Hermes holds cookies for. --- hermes_cli/browser_connect.py | 11 ++++++++-- tests/tools/test_browser_real_profile.py | 26 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/hermes_cli/browser_connect.py b/hermes_cli/browser_connect.py index d47c71f7d5aad..b8a5b38502320 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -609,9 +609,16 @@ def snapshot_real_profile(browser: str, src: str | None = None) -> tuple[str | N dst = real_profile_copy_dir(browser) fresh = not os.path.isdir(os.path.join(dst, "Default")) try: + os.makedirs(dst, exist_ok=True) + # Re-assert on EVERY launch, not just on creation. _secure_dir is + # best-effort (it swallows and debug-logs), so a first attempt that + # failed would otherwise leave a credential store world-readable for + # the rest of its life. Also covers a dir created by an older build. + # The parent is secured too: makedirs creates browser-profile/ under + # the process umask, and it names every browser we hold cookies for. + _secure_snapshot_root(os.path.dirname(dst)) + _secure_snapshot_root(dst) if fresh: - os.makedirs(dst, exist_ok=True) - _secure_snapshot_root(dst) try: shutil.copytree( src, diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py index a26e5f488ebb1..508fe34d72c3c 100644 --- a/tests/tools/test_browser_real_profile.py +++ b/tests/tools/test_browser_real_profile.py @@ -587,3 +587,29 @@ def test_snapshot_dir_secured(self, tmp_path, monkeypatch): dst, err = bc.snapshot_real_profile("chrome", src=str(tmp_path / "real")) assert err is None assert called.get("p") == dst # secured through the canonical owner + + def test_snapshot_dir_secured_on_every_launch(self, tmp_path, monkeypatch): + """Permissions are re-asserted on refresh, not only on creation. + + _secure_dir is best-effort, so a first attempt that failed must not + leave a credential store loose for the rest of its life. + """ + import hermes_cli.browser_connect as bc + src = tmp_path / "real" / "Default" + src.mkdir(parents=True) + (tmp_path / "real" / "Local State").write_text("{}", encoding="utf-8") + (src / "Cookies").write_text("db", encoding="utf-8") + monkeypatch.setattr(bc, "get_hermes_home", lambda: tmp_path / "hh") + + seen = [] + with patch("hermes_cli.config._secure_dir", side_effect=seen.append): + dst, _ = bc.snapshot_real_profile("chrome", src=str(tmp_path / "real")) + first = list(seen) + seen.clear() + # Second call takes the refresh path (the copy already exists). + bc.snapshot_real_profile("chrome", src=str(tmp_path / "real")) + + assert dst in first + assert dst in seen, "refresh path must re-secure the snapshot dir" + # The parent holds one entry per browser we keep cookies for. + assert os.path.dirname(dst) in seen From bd26e21c211fbbddb397037bb24177ce34bb9160 Mon Sep 17 00:00:00 2001 From: Jan-Stefan Janetzky Date: Wed, 26 Aug 2026 17:25:05 +0200 Subject: [PATCH 11/12] fix(browser): keep the private-URL sidecar off the real profile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _create_local_session() resolved the real-profile CDP unconditionally, so every local session got the user's cookie jar — including the hybrid sidecar that auto_local_for_private_urls creates. That sidecar exists for one reason: a URL resolved to a LAN/loopback host and must not be handed to the cloud backend. Routing it into the real-profile copy-browser instead means the agent visits an internal host — a router UI, a local dashboard, whatever the URL pointed at — with the user's full authenticated cookie jar attached. That is a strictly larger exposure than the routing rule was protecting against, and it happens without the user asking for a real-profile session on that URL. Pass allow_real_profile=False on the sidecar branch, where the caller already knows it is a sidecar (force_local). Bare local sessions are unchanged: consent still means the real profile there. A broken real-profile setup also no longer breaks private-URL routing, since the sidecar never calls the resolver. --- tests/tools/test_browser_real_profile.py | 42 ++++++++++++++++++++++++ tools/browser_tool.py | 24 ++++++++++---- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py index 508fe34d72c3c..55a88b4dc4823 100644 --- a/tests/tools/test_browser_real_profile.py +++ b/tests/tools/test_browser_real_profile.py @@ -353,6 +353,48 @@ def test_local_session_without_consent_is_throwaway(self): assert "real_profile" not in info["features"] assert info["session_name"].startswith("h_") + def test_private_url_sidecar_does_not_get_the_real_profile(self): + """The sidecar keeps a LAN host off the cloud backend. + + Handing it the user's logged-in cookie jar instead would be a larger + exposure than the routing rule was protecting against. + """ + import tools.browser_tool as bt + with patch.object(bt, "_real_profile_cdp", + return_value=("http://127.0.0.1:9251", None)) as rp: + info = bt._create_local_session("t1::local", allow_real_profile=False) + rp.assert_not_called() + assert info["cdp_url"] is None + assert "real_profile" not in info["features"] + assert info["session_name"].startswith("h_") + + def test_sidecar_is_not_blocked_by_a_real_profile_failure(self): + """A broken real-profile setup must not break private-URL routing.""" + import tools.browser_tool as bt + with patch.object(bt, "_real_profile_cdp", return_value=(None, "no chromium")): + info = bt._create_local_session("t1::local", allow_real_profile=False) + assert info["session_name"].startswith("h_") + + def test_session_creation_routes_the_sidecar_without_the_real_profile(self): + """End to end through _get_session_info: the ::local key opts out.""" + import tools.browser_tool as bt + seen = {} + + def fake_create(task_id, allow_real_profile=True): + seen[task_id] = allow_real_profile + return {"session_name": "h_x", "bb_session_id": None, + "cdp_url": None, "features": {"local": True}} + + with patch.object(bt, "_create_local_session", side_effect=fake_create), \ + patch.object(bt, "_get_cdp_override", return_value=None), \ + patch.object(bt, "_get_cloud_provider", return_value=None), \ + patch.dict(bt._active_sessions, {}, clear=True): + bt._get_session_info("t1::local") + bt._get_session_info("t2") + + assert seen["t1::local"] is False, "sidecar must opt out of the real profile" + assert seen["t2"] is True, "a bare local session keeps consented behaviour" + class TestBrowserExecLocalArg: def _env(self): diff --git a/tools/browser_tool.py b/tools/browser_tool.py index 0d4bc5561662d..524aab36eda56 100644 --- a/tools/browser_tool.py +++ b/tools/browser_tool.py @@ -1736,10 +1736,11 @@ def _navigation_session_key(task_id: str, url: str) -> str: When all are true, returns ``f"{task_id}::local"`` so the hybrid-routing path spawns a local Chromium sidecar while the cloud session (if any) continues to serve public URLs. With ``browser.use_real_profile`` consent, - local sessions (bare or sidecar) attach to the user's real-profile - copy-browser via ``_create_local_session``; forcing a local session from - the model side is the Browser Use lane's ``local`` argument, not a - built-in-tools argument. + a BARE local session attaches to the user's real-profile copy-browser via + ``_create_local_session``; the private-URL sidecar deliberately does not + (it exists to keep a LAN host off the cloud backend, not to hand it the + user's logins). Forcing a local session from the model side is the Browser + Use lane's ``local`` argument, not a built-in-tools argument. """ if task_id is None: task_id = "default" @@ -2577,7 +2578,7 @@ def _update_session_activity(task_id: str): # Utility Functions # ============================================================================ -def _create_local_session(task_id: str) -> Dict[str, str]: +def _create_local_session(task_id: str, allow_real_profile: bool = True) -> Dict[str, str]: import uuid # Real-profile consent: instead of an agent-browser-managed throwaway @@ -2585,7 +2586,11 @@ def _create_local_session(task_id: str) -> Dict[str, str]: # browser running on a hermes-owned SNAPSHOT of their real profile — # live logins/cookies included. Fail closed on resolver/launch errors: # a consented user must never be silently downgraded to a throwaway. - cdp_url, err = _real_profile_cdp() + # + # ``allow_real_profile=False`` is the hybrid private-URL sidecar. That + # session exists to keep a LAN/loopback URL AWAY from the cloud backend, + # not to carry the user's logged-in cookie jar to it. See the caller. + cdp_url, err = (_real_profile_cdp() if allow_real_profile else (None, None)) if err: raise RuntimeError(err) if cdp_url: @@ -2689,7 +2694,12 @@ def _get_session_info(task_id: Optional[str] = None) -> Dict[str, Any]: if cdp_override and not force_local: session_info = _create_cdp_session(task_id, cdp_override) elif force_local: - session_info = _create_local_session(task_id) + # Private-URL sidecar. It was created because auto_local_for_private_urls + # decided a LAN/loopback host must not be handed to the cloud backend; + # sending the user's real cookie jar there instead would be a strictly + # larger exposure than the routing rule was protecting against. A + # throwaway profile is what this session is for. + session_info = _create_local_session(task_id, allow_real_profile=False) else: provider = _get_cloud_provider() if provider is None: From 35297291df0eec8f96ec4bf8a716a56a0eeaa7c9 Mon Sep 17 00:00:00 2001 From: Jan-Stefan Janetzky Date: Wed, 26 Aug 2026 17:25:49 +0200 Subject: [PATCH 12/12] fix(browser): name the Lightpanda conflict instead of failing generically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit _real_profile_cdp() launches agent-browser with --profile, and agent-browser rejects that outright for the Lightpanda engine ("Profiles are not supported with Lightpanda", cli/src/native/browser.rs:86). A user with browser.engine: lightpanda and the consent toggle on therefore gets "the real-profile browser failed to start: " — a message that names neither the setting that caused it nor the fix. The in-place implementation caught this case explicitly; the copy-based redo does not, so the guard is restored here against the new entry point. Checked before detection so a machine with no default browser at all still reports the engine conflict, which is the actionable one. --- tests/tools/test_browser_real_profile.py | 21 +++++++++++++++++++++ tools/browser_tool.py | 12 ++++++++++++ 2 files changed, 33 insertions(+) diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py index 55a88b4dc4823..aac41a4a59458 100644 --- a/tests/tools/test_browser_real_profile.py +++ b/tests/tools/test_browser_real_profile.py @@ -219,6 +219,27 @@ def test_non_chromium_default_fails_closed(self): assert cdp is None assert err and "not a supported Chromium" in err + def test_lightpanda_engine_names_the_conflict(self): + """agent-browser rejects --profile for Lightpanda; say which setting.""" + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_use_real_profile", return_value=True), \ + patch.object(bt, "_using_lightpanda_engine", return_value=True), \ + patch("hermes_cli.browser_connect.detect_default_chromium") as detect: + cdp, err = bt._real_profile_cdp() + assert cdp is None + assert err and "lightpanda" in err.lower() + detect.assert_not_called() + + def test_chrome_engine_does_not_trip_the_lightpanda_guard(self): + import tools.browser_tool as bt + self._reset() + with patch.object(bt, "_use_real_profile", return_value=True), \ + patch.object(bt, "_using_lightpanda_engine", return_value=False), \ + patch("hermes_cli.browser_connect.detect_default_chromium", return_value=None): + cdp, err = bt._real_profile_cdp() + assert err and "lightpanda" not in err.lower() + def test_snapshot_failure_fails_closed(self): import tools.browser_tool as bt self._reset() diff --git a/tools/browser_tool.py b/tools/browser_tool.py index 524aab36eda56..25b2b4617c901 100644 --- a/tools/browser_tool.py +++ b/tools/browser_tool.py @@ -1554,6 +1554,18 @@ def _real_profile_cdp() -> tuple: if not _use_real_profile(): return None, None + if _using_lightpanda_engine(): + # agent-browser rejects --profile outright for the Lightpanda engine + # ("Profiles are not supported with Lightpanda", + # cli/src/native/browser.rs), and the launch below is a --profile + # launch. Name the conflicting setting instead of surfacing that as a + # generic start failure the user cannot act on. + return None, ( + "browser.use_real_profile needs the Chrome engine: agent-browser " + "does not support profiles with browser.engine: lightpanda. Set " + "browser.engine to auto or chrome, or turn the toggle off." + ) + from hermes_cli.browser_connect import ( UNSUPPORTED_CHANNEL, detect_default_chromium,