Merge pull request #96341 from fangliquanflq/fix/computer-use-notarised-cua-paths
fix(computer-use): launch notarised CUA Driver from standard macOS installs
This commit is contained in:
@@ -3725,8 +3725,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
|
||||
|
||||
@@ -0,0 +1,178 @@
|
||||
"""macOS CuaDriver.app path and signing identity contracts."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
import pytest
|
||||
|
||||
from tools.computer_use import cua_backend
|
||||
|
||||
|
||||
def _codesign_proc(
|
||||
*,
|
||||
team_id: str,
|
||||
identifier: str = "com.trycua.driver",
|
||||
returncode: int = 0,
|
||||
) -> subprocess.CompletedProcess[str]:
|
||||
return subprocess.CompletedProcess(
|
||||
["codesign"],
|
||||
returncode,
|
||||
stdout="",
|
||||
stderr=(
|
||||
f"Identifier={identifier}\n"
|
||||
f"TeamIdentifier={team_id}\n"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
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"
|
||||
|
||||
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):
|
||||
_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):
|
||||
_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):
|
||||
_patch_codesign(
|
||||
monkeypatch,
|
||||
_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")
|
||||
|
||||
|
||||
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",
|
||||
]
|
||||
@@ -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.)"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user