Skip to content

The agent that grows with you

test(computer-use): cover CUA signature security paths

fangliquanflq164db901e870
diff --git a/tests/tools/test_computer_use_cua_macos_identity.py b/tests/tools/test_computer_use_cua_macos_identity.py
index 1ac47bc7823..f623e998c14 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)
+
+
[email protected](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",
+ ]