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/apps/desktop/src/app/settings/constants.ts b/apps/desktop/src/app/settings/constants.ts index 03c19496fd732..ee841452ea1f1 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 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.', 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/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 af1b04eaee0a0..b8a5b38502320 100644 --- a/hermes_cli/browser_connect.py +++ b/hermes_cli/browser_connect.py @@ -2,15 +2,19 @@ from __future__ import annotations +import json import logging +import ntpath import os import platform import posixpath +import re import shlex import shutil import subprocess import time from dataclasses import dataclass, field +from pathlib import Path from hermes_constants import get_hermes_home @@ -76,6 +80,597 @@ _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 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"), + ("bravehtml", "brave"), + ("chromiumhtm", "chromium"), +) + +# 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. 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"), + ("chromium", "chromium"), + ("brave", "brave"), + ("microsoft-edge", "edge"), + ("com.microsoft.edge", "edge"), + ("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", + "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 → 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"), + ("com.brave.browser", "brave"), + ("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).""" + 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. 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 + 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") + 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: + """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() + # 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 + 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 + # 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 + return None + + +def _detect_default_darwin() -> str | None: + 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 + 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 b == frag: + return browser + # 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 + + +def _detect_default_linux() -> str | None: + try: + out = subprocess.run( + ["xdg-settings", "get", "default-web-browser"], + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + timeout=5, + ).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 + 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() + + +# --------------------------------------------------------------------------- +# 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 _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 = [] + 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 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. + + 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: + 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: + 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) + # 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: + 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 d3c36844a4d81..eee52189bc338 100644 --- a/hermes_cli/config_defaults.py +++ b/hermes_cli/config_defaults.py @@ -575,6 +575,20 @@ "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 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=...) # CDP supervisor — dialog + frame detection via a persistent WebSocket. 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..576d5b984a7d8 --- /dev/null +++ b/tests/hermes_cli/test_browser_connect_default_chromium.py @@ -0,0 +1,204 @@ +"""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 _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" + 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" + + 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): + 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 + + +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" 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) diff --git a/tests/tools/test_browser_real_profile.py b/tests/tools/test_browser_real_profile.py new file mode 100644 index 0000000000000..aac41a4a59458 --- /dev/null +++ b/tests/tools/test_browser_real_profile.py @@ -0,0 +1,678 @@ +"""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 + +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 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 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.""" + + def _reset(self): + import tools.browser_tool as bt + 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): + 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): + cdp, err = bt._real_profile_cdp() + 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() + 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=(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 cdp == "http://127.0.0.1:41000" + self._reset() + + 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.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() + + def test_cdp_on_data_dir_matches_devtoolsactiveport(self, tmp_path): + import tools.browser_tool as bt + (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_consent_read_from_config(self, tmp_path, monkeypatch): + import tools.browser_tool as bt + 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_consent_default_off(self): + import tools.browser_tool as bt + with patch("hermes_cli.config.read_raw_config", return_value={}): + assert bt._use_real_profile() is False + + def test_consent_revocation_takes_effect_immediately(self): + """No process-lifetime caching: consent is a per-use read.""" + import tools.browser_tool as bt + 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 + + +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_") + + 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): + 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 + 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, "_auto_local_for_private_urls", return_value=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_public_url_stays_on_cloud(self): + import tools.browser_tool as bt + 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): + 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 + + 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 diff --git a/tools/browser_tool.py b/tools/browser_tool.py index 363a68607ea89..25b2b4617c901 100644 --- a/tools/browser_tool.py +++ b/tools/browser_tool.py @@ -1427,6 +1427,250 @@ 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) 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. + """ + try: + from hermes_cli.config import read_raw_config + cfg = read_raw_config() + browser_cfg = cfg.get("browser", {}) + 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 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 _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. + + 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, 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, + snapshot_real_profile, + ) + + 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." + ) + 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}" + + # 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: """Return True when the URL's host resolves to a private/LAN/loopback address. @@ -1503,7 +1747,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. + continues to serve public URLs. With ``browser.use_real_profile`` consent, + 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" @@ -2341,8 +2590,33 @@ 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 + # 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. + # + # ``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: + 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) @@ -2432,7 +2706,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: @@ -3473,6 +3752,13 @@ def browser_navigate(url: str, task_id: Optional[str] = None) -> str: "url": final_url, "title": title } + # 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. 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.