From 6662b3618d22f03102cecf203772e1238465306d Mon Sep 17 00:00:00 2001 From: fangliquanflq Date: Thu, 27 Aug 2026 20:21:34 +0800 Subject: [PATCH 1/2] fix(computer-use): accept current notarised CUA Driver --- hermes_cli/config_defaults.py | 4 +- .../test_computer_use_cua_macos_identity.py | 85 +++++++++++++++++++ tools/computer_use/cua_backend.py | 17 ++-- 3 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 tests/tools/test_computer_use_cua_macos_identity.py diff --git a/hermes_cli/config_defaults.py b/hermes_cli/config_defaults.py index c9bf9ad6e2..5a954ed0a0 100644 --- a/hermes_cli/config_defaults.py +++ b/hermes_cli/config_defaults.py @@ -3688,8 +3688,8 @@ DEFAULT_CONFIG = { # macOS only: allow launching an UNSIGNED (ad-hoc / TeamIdentifier # not set) CuaDriver.app for the private-session daemon. The default # (false) fails closed unless the bundle is signed with the official - # cua-driver identity (com.trycua.driver / team 4YEC26S9KF). Enable - # only when developing the driver locally from source. + # cua-driver identity (com.trycua.driver / an official signing team). + # Enable only when developing the driver locally from source. "allow_unsigned_driver": False, # Pre-authorize existing-profile browser attachment in standard mode # (cua-driver's trusted-launcher `--grant existing-profile`). When diff --git a/tests/tools/test_computer_use_cua_macos_identity.py b/tests/tools/test_computer_use_cua_macos_identity.py new file mode 100644 index 0000000000..1ac47bc782 --- /dev/null +++ b/tests/tools/test_computer_use_cua_macos_identity.py @@ -0,0 +1,85 @@ +"""macOS CuaDriver.app path and signing identity contracts.""" + +from __future__ import annotations + +import subprocess + +import pytest + +from tools.computer_use import cua_backend + + +def _codesign_proc( + *, + team_id: str, + identifier: str = "com.trycua.driver", +) -> subprocess.CompletedProcess[str]: + return subprocess.CompletedProcess( + ["codesign"], + 0, + stdout="", + stderr=( + f"Identifier={identifier}\n" + f"TeamIdentifier={team_id}\n" + ), + ) + + +def test_resolve_app_path_follows_standard_driver_symlink(monkeypatch): + symlink = "/Users/test/.local/bin/cua-driver" + executable = "/Applications/CuaDriver.app/Contents/MacOS/cua-driver" + + monkeypatch.setattr(cua_backend.os.path, "realpath", lambda path: executable) + monkeypatch.setattr(cua_backend.os.path, "isfile", lambda path: True) + monkeypatch.setattr(cua_backend.os, "access", lambda path, mode: True) + + assert cua_backend._resolve_cua_driver_app_path(symlink) == "/Applications/CuaDriver.app" + + +def test_resolve_app_path_does_not_fall_back_to_an_unrelated_bundle(monkeypatch): + monkeypatch.setattr( + cua_backend.os.path, + "realpath", + lambda path: "/usr/local/bin/cua-driver", + ) + + assert cua_backend._resolve_cua_driver_app_path("cua-driver") is None + + +@pytest.mark.parametrize("team_id", ["4YEC26S9KF", "YCK386LBJ7"]) +def test_driver_signature_accepts_official_team_ids(monkeypatch, team_id): + monkeypatch.setattr(cua_backend.shutil, "which", lambda name: "/usr/bin/codesign") + monkeypatch.setattr( + cua_backend.subprocess, + "run", + lambda *args, **kwargs: _codesign_proc(team_id=team_id), + ) + + cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") + + +def test_driver_signature_still_rejects_unrecognised_team(monkeypatch): + monkeypatch.setattr(cua_backend.shutil, "which", lambda name: "/usr/bin/codesign") + monkeypatch.setattr( + cua_backend.subprocess, + "run", + lambda *args, **kwargs: _codesign_proc(team_id="EVIL000000"), + ) + + with pytest.raises(RuntimeError, match="signed by team"): + cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") + + +def test_driver_signature_still_requires_exact_bundle_identifier(monkeypatch): + monkeypatch.setattr(cua_backend.shutil, "which", lambda name: "/usr/bin/codesign") + monkeypatch.setattr( + cua_backend.subprocess, + "run", + lambda *args, **kwargs: _codesign_proc( + team_id="YCK386LBJ7", + identifier="com.trycua.driver.evil", + ), + ) + + with pytest.raises(RuntimeError, match="has identifier"): + cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") diff --git a/tools/computer_use/cua_backend.py b/tools/computer_use/cua_backend.py index 3e70607c67..4982426eef 100644 --- a/tools/computer_use/cua_backend.py +++ b/tools/computer_use/cua_backend.py @@ -601,11 +601,12 @@ def _resolve_cua_driver_app_path(driver_cmd: str) -> Optional[str]: chain never validated. If the resolved driver does not live inside an app bundle, the caller fails closed with install guidance. """ + resolved_driver_cmd = os.path.realpath(driver_cmd) marker = ".app/Contents/MacOS/" - marker_index = driver_cmd.find(marker) + marker_index = resolved_driver_cmd.find(marker) if marker_index < 0: return None - candidate = driver_cmd[: marker_index + len(".app")] + candidate = resolved_driver_cmd[: marker_index + len(".app")] executable = os.path.join(candidate, "Contents", "MacOS", "cua-driver") if os.path.isfile(executable) and os.access(executable, os.X_OK): return candidate @@ -613,11 +614,11 @@ def _resolve_cua_driver_app_path(driver_cmd: str) -> Optional[str]: # The only bundle identity the private daemon may launch through, and the -# team that signs official cua-driver releases. Exact matches only: a +# teams that sign official cua-driver releases. Exact matches only: a # suffixed identifier ("com.trycua.driver.evil") or a different non-empty # team is an impostor bundle, not a variant. _CUA_DRIVER_BUNDLE_ID = "com.trycua.driver" -_CUA_DRIVER_TEAM_ID = "4YEC26S9KF" +_CUA_DRIVER_TEAM_IDS = ("4YEC26S9KF", "YCK386LBJ7") def _validate_cua_driver_app_signature(app_path: str) -> None: @@ -626,7 +627,7 @@ def _validate_cua_driver_app_signature(app_path: str) -> None: Launching via ``/usr/bin/open`` hands LaunchServices whatever bundle sits at the path, so the TCC-identity fix must not become a launcher for arbitrary apps: require ``codesign -dv`` to report EXACTLY - ``Identifier=com.trycua.driver`` and the expected TeamIdentifier. + ``Identifier=com.trycua.driver`` and an expected TeamIdentifier. ``TeamIdentifier=not set`` (unsigned/ad-hoc dev builds) is allowed only when ``computer_use.allow_unsigned_driver: true`` is set in config.yaml — the escape hatch for local driver development, never the default. Raises @@ -664,13 +665,13 @@ def _validate_cua_driver_app_signature(app_path: str) -> None: f"CuaDriver.app at {app_path} has identifier {identifier!r}, " f"expected {_CUA_DRIVER_BUNDLE_ID!r}; refusing to launch it." ) - if team == _CUA_DRIVER_TEAM_ID: + if team in _CUA_DRIVER_TEAM_IDS: return if team in ("", "not set") and _computer_use_cfg().get("allow_unsigned_driver") is True: return raise RuntimeError( - f"CuaDriver.app at {app_path} is signed by team {team!r}, expected " - f"{_CUA_DRIVER_TEAM_ID!r}; refusing to launch it. (Set " + f"CuaDriver.app at {app_path} is signed by team {team!r}, expected one of " + f"{_CUA_DRIVER_TEAM_IDS!r}; refusing to launch it. (Set " "computer_use.allow_unsigned_driver: true in config.yaml only for " "local unsigned driver builds.)" ) From 164db901e8704f7370261c67981c88b968781e86 Mon Sep 17 00:00:00 2001 From: fangliquanflq Date: Thu, 27 Aug 2026 22:43:03 +0800 Subject: [PATCH 2/2] test(computer-use): cover CUA signature security paths Co-authored-by: Finn763 <165816600+Finn763@users.noreply.github.com> --- .../test_computer_use_cua_macos_identity.py | 129 +++++++++++++++--- 1 file changed, 111 insertions(+), 18 deletions(-) diff --git a/tests/tools/test_computer_use_cua_macos_identity.py b/tests/tools/test_computer_use_cua_macos_identity.py index 1ac47bc782..f623e998c1 100644 --- a/tests/tools/test_computer_use_cua_macos_identity.py +++ b/tests/tools/test_computer_use_cua_macos_identity.py @@ -2,6 +2,7 @@ from __future__ import annotations +import os import subprocess import pytest @@ -13,10 +14,11 @@ def _codesign_proc( *, team_id: str, identifier: str = "com.trycua.driver", + returncode: int = 0, ) -> subprocess.CompletedProcess[str]: return subprocess.CompletedProcess( ["codesign"], - 0, + returncode, stdout="", stderr=( f"Identifier={identifier}\n" @@ -25,6 +27,28 @@ def _codesign_proc( ) +def _patch_codesign(monkeypatch, proc): + monkeypatch.setattr(cua_backend.shutil, "which", lambda name: "/usr/bin/codesign") + monkeypatch.setattr(cua_backend.subprocess, "run", lambda *args, **kwargs: proc) + + +@pytest.mark.skipif(os.name == "nt", reason="macOS bundle paths use POSIX separators") +def test_resolve_app_path_follows_real_symlink_and_is_idempotent(tmp_path): + app = tmp_path / "CuaDriver.app" + executable = app / "Contents" / "MacOS" / "cua-driver" + executable.parent.mkdir(parents=True) + executable.write_text("#!/bin/sh\nexit 0\n", encoding="utf-8") + executable.chmod(0o755) + shim = tmp_path / "cua-driver" + try: + shim.symlink_to(executable) + except (OSError, NotImplementedError): + pytest.skip("symlinks are unavailable on this host") + + assert cua_backend._resolve_cua_driver_app_path(str(shim)) == str(app) + assert cua_backend._resolve_cua_driver_app_path(str(executable)) == str(app) + + def test_resolve_app_path_follows_standard_driver_symlink(monkeypatch): symlink = "/Users/test/.local/bin/cua-driver" executable = "/Applications/CuaDriver.app/Contents/MacOS/cua-driver" @@ -48,34 +72,22 @@ def test_resolve_app_path_does_not_fall_back_to_an_unrelated_bundle(monkeypatch) @pytest.mark.parametrize("team_id", ["4YEC26S9KF", "YCK386LBJ7"]) def test_driver_signature_accepts_official_team_ids(monkeypatch, team_id): - monkeypatch.setattr(cua_backend.shutil, "which", lambda name: "/usr/bin/codesign") - monkeypatch.setattr( - cua_backend.subprocess, - "run", - lambda *args, **kwargs: _codesign_proc(team_id=team_id), - ) + _patch_codesign(monkeypatch, _codesign_proc(team_id=team_id)) cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") def test_driver_signature_still_rejects_unrecognised_team(monkeypatch): - monkeypatch.setattr(cua_backend.shutil, "which", lambda name: "/usr/bin/codesign") - monkeypatch.setattr( - cua_backend.subprocess, - "run", - lambda *args, **kwargs: _codesign_proc(team_id="EVIL000000"), - ) + _patch_codesign(monkeypatch, _codesign_proc(team_id="EVIL000000")) with pytest.raises(RuntimeError, match="signed by team"): cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") def test_driver_signature_still_requires_exact_bundle_identifier(monkeypatch): - monkeypatch.setattr(cua_backend.shutil, "which", lambda name: "/usr/bin/codesign") - monkeypatch.setattr( - cua_backend.subprocess, - "run", - lambda *args, **kwargs: _codesign_proc( + _patch_codesign( + monkeypatch, + _codesign_proc( team_id="YCK386LBJ7", identifier="com.trycua.driver.evil", ), @@ -83,3 +95,84 @@ def test_driver_signature_still_requires_exact_bundle_identifier(monkeypatch): with pytest.raises(RuntimeError, match="has identifier"): cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") + + +def test_driver_signature_rejects_unsigned_by_default(monkeypatch): + _patch_codesign(monkeypatch, _codesign_proc(team_id="not set")) + monkeypatch.setattr(cua_backend, "_computer_use_cfg", lambda: {}) + + with pytest.raises(RuntimeError, match="signed by team"): + cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") + + +def test_driver_signature_allows_unsigned_only_with_opt_in(monkeypatch): + _patch_codesign(monkeypatch, _codesign_proc(team_id="not set")) + monkeypatch.setattr( + cua_backend, + "_computer_use_cfg", + lambda: {"allow_unsigned_driver": True}, + ) + + cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") + + +def test_unsigned_opt_in_still_requires_exact_bundle_identifier(monkeypatch): + _patch_codesign( + monkeypatch, + _codesign_proc( + team_id="not set", + identifier="com.trycua.driver.evil", + ), + ) + monkeypatch.setattr( + cua_backend, + "_computer_use_cfg", + lambda: {"allow_unsigned_driver": True}, + ) + + with pytest.raises(RuntimeError, match="has identifier"): + cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") + + +def test_driver_signature_requires_codesign(monkeypatch): + monkeypatch.setattr(cua_backend.shutil, "which", lambda name: None) + + with pytest.raises(RuntimeError, match="codesign is required"): + cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") + + +def test_driver_signature_rejects_codesign_failure(monkeypatch): + _patch_codesign( + monkeypatch, + _codesign_proc(team_id="", returncode=1), + ) + + with pytest.raises(RuntimeError, match="not code-signed"): + cua_backend._validate_cua_driver_app_signature("/Applications/CuaDriver.app") + + +def test_embedded_spawn_resolves_shim_and_accepts_current_team(monkeypatch): + executable = "/Applications/CuaDriver.app/Contents/MacOS/cua-driver" + monkeypatch.setattr(cua_backend.os.path, "realpath", lambda path: executable) + monkeypatch.setattr(cua_backend.os.path, "isfile", lambda path: True) + monkeypatch.setattr(cua_backend.os, "access", lambda path, mode: True) + _patch_codesign(monkeypatch, _codesign_proc(team_id="YCK386LBJ7")) + + command = cua_backend._embedded_daemon_spawn_command( + "/Users/test/.local/bin/cua-driver", + ["serve", "--embedded", "--socket", "/tmp/private.sock"], + platform="darwin", + ) + + assert command == [ + "/usr/bin/open", + "-n", + "-g", + "-a", + "/Applications/CuaDriver.app", + "--args", + "serve", + "--embedded", + "--socket", + "/tmp/private.sock", + ]