The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(desktop): pin macOS signing-downgrade guard and no-ad-hoc-fallback contract
Yuan Licf0291065c0b
diff --git a/tests/hermes_cli/test_desktop_install_after_update.py b/tests/hermes_cli/test_desktop_install_after_update.pyindex be57ed6bccd..b7994928f83 100644--- a/tests/hermes_cli/test_desktop_install_after_update.py+++ b/tests/hermes_cli/test_desktop_install_after_update.py@@ -58,6 +58,105 @@ def test_stale_and_missing_bundles_are_installed_current_and_running_are_left_alassert len(problems) == 1 and str(running) in problems[0]+def _patch_sigs(monkeypatch, summaries):+ """Stub the codesign probe: ``summaries(app) -> dict | None``."""+ monkeypatch.setattr(+ main_desktop.shutil, "which",+ lambda name: "/usr/bin/codesign" if name == "codesign" else None)+ monkeypatch.setattr(main_desktop, "_macos_signature_summary", lambda codesign, app: summaries(app))+++def _publisher_sig(app, team="TEAM0123", ident="com.nousresearch.hermes", verified=True):+ return {"team": team, "identifier": ident, "verified": verified}+++def test_publisher_signed_install_is_never_replaced_by_local_build(rebuilt, tmp_path, monkeypatch):+ """#123748: a Team-ID-signed installation must survive an ad-hoc local rebuild.++ Swapping a publisher-signed app for a locally signed build invalidates the+ code-hash-bound keychain ACLs safeStorage credentials live under — the+ update must retain the working app and report it instead.+ """+ stale = _bundle(tmp_path / "Applications", b"stale")+ _patch_sigs(monkeypatch, lambda app: None if app == rebuilt else _publisher_sig(stale))++ installed, problems = main_desktop._install_rebuilt_macos_bundles(rebuilt, [stale], running=set())++ assert installed == []+ assert len(problems) == 1+ assert "publisher-signed app (Team ID TEAM0123)" in problems[0]+ assert "kept the existing app" in problems[0]+ assert _asar(stale) == b"stale"+ assert not (stale.parent / "Hermes.app.hermes-update-new").exists()+ assert not (stale.parent / "Hermes.app.hermes-update-old").exists()+++def test_team_id_mismatch_refuses_swap(rebuilt, tmp_path, monkeypatch):+ stale = _bundle(tmp_path / "Applications", b"stale")+ sigs = {rebuilt: _publisher_sig(rebuilt, team="TEAM9999"), stale: _publisher_sig(stale)}+ _patch_sigs(monkeypatch, lambda app: sigs.get(app))++ installed, problems = main_desktop._install_rebuilt_macos_bundles(rebuilt, [stale], running=set())++ assert installed == [] and len(problems) == 1+ assert "Team ID TEAM0123 does not match rebuilt TEAM9999" in problems[0]+ assert _asar(stale) == b"stale"+++def test_bundle_identifier_mismatch_refuses_swap(rebuilt, tmp_path, monkeypatch):+ stale = _bundle(tmp_path / "Applications", b"stale")+ sigs = {+ rebuilt: _publisher_sig(rebuilt, ident="com.other.hermes"),+ stale: _publisher_sig(stale),+ }+ _patch_sigs(monkeypatch, lambda app: sigs.get(app))++ installed, problems = main_desktop._install_rebuilt_macos_bundles(rebuilt, [stale], running=set())++ assert installed == [] and len(problems) == 1+ assert "bundle identifier" in problems[0]+ assert _asar(stale) == b"stale"+++def test_unverifiable_rebuilt_refuses_swap(rebuilt, tmp_path, monkeypatch):+ stale = _bundle(tmp_path / "Applications", b"stale")+ sigs = {+ rebuilt: _publisher_sig(rebuilt, verified=False),+ stale: _publisher_sig(stale),+ }+ _patch_sigs(monkeypatch, lambda app: sigs.get(app))++ installed, problems = main_desktop._install_rebuilt_macos_bundles(rebuilt, [stale], running=set())++ assert installed == [] and len(problems) == 1+ assert "strict signature verification" in problems[0]+ assert _asar(stale) == b"stale"+++def test_matching_publisher_signing_swaps(rebuilt, tmp_path, monkeypatch):+ """A rebuilt bundle with the SAME publisher identity is a legitimate update: swap."""+ stale = _bundle(tmp_path / "Applications", b"stale")+ sigs = {rebuilt: _publisher_sig(rebuilt), stale: _publisher_sig(stale)}+ _patch_sigs(monkeypatch, lambda app: sigs.get(app))++ installed, problems = main_desktop._install_rebuilt_macos_bundles(rebuilt, [stale], running=set())++ assert installed == [stale] and problems == []+ assert _asar(stale) == b"rebuilt"+++def test_adhoc_installed_still_swaps(rebuilt, tmp_path, monkeypatch):+ """Ad-hoc installs (the local development flow) keep swapping regardless of rebuild sig."""+ stale = _bundle(tmp_path / "Applications", b"stale")+ sigs = {stale: {"team": None, "identifier": "com.nousresearch.hermes", "verified": True}}+ _patch_sigs(monkeypatch, lambda app: sigs.get(app))++ installed, problems = main_desktop._install_rebuilt_macos_bundles(rebuilt, [stale], running=set())++ assert installed == [stale] and problems == []+ assert _asar(stale) == b"rebuilt"++def test_failed_swap_keeps_the_previous_bundle_launchable(rebuilt, tmp_path, monkeypatch):stale = _bundle(tmp_path / "Applications", b"stale")real_rename = Path.renamediff --git a/tests/hermes_cli/test_gui_command.py b/tests/hermes_cli/test_gui_command.pyindex b5066c52237..a12581702fd 100644--- a/tests/hermes_cli/test_gui_command.py+++ b/tests/hermes_cli/test_gui_command.py@@ -715,6 +715,81 @@ def test_relaunchable_fixup_stable_identity_never_touches_keychain(tmp_path, mon[email protected]("macos")+def test_relaunchable_fixup_configured_identity_failure_never_falls_back_to_adhoc(tmp_path, monkeypatch):+ """A configured signing identity that fails must NOT degrade to ad-hoc (#123748).++ Falling back to ad-hoc swaps the signature anchor the keychain ACLs are+ bound against, orphaning safeStorage credentials. The fixup keeps the+ existing signature and reports the failure instead.++ ``platforms("macos")``: the fixup no-ops on non-macOS (sys.platform guard), and+ the subject is codesign against a real ``.app`` bundle layout.+ """+ root = _make_desktop_tree(tmp_path)+ desktop_dir = root / "apps" / "desktop"+ monkeypatch.setattr(cli_main, "PROJECT_ROOT", root)+ monkeypatch.delenv("CSC_LINK", raising=False)+ monkeypatch.delenv("APPLE_SIGNING_IDENTITY", raising=False)+ exe = _make_packaged_executable(root, monkeypatch)+ app = exe.parents[2]++ calls: list[list[str]] = []++ def fake_run(cmd, **kwargs):+ calls.append(list(cmd))+ return subprocess.CompletedProcess(cmd, 0)++ monkeypatch.setattr(+ cli_main.shutil, "which", lambda name: "/usr/bin/codesign" if name == "codesign" else None+ )+ monkeypatch.setattr(cli_main.subprocess, "run", fake_run)+ monkeypatch.setattr(main_desktop, "_desktop_macos_has_valid_real_signature", lambda a: False)+ monkeypatch.setattr(main_desktop, "_desktop_macos_local_signing_identity", lambda: "Hermes Local Signing")++ def boom(*a, **kw):+ raise subprocess.CalledProcessError(1, ["codesign"])++ monkeypatch.setattr(main_desktop, "_desktop_macos_local_codesign", boom)++ assert cli_main._desktop_macos_relaunchable_fixup(desktop_dir) is False+ # The old behavior fell through to the legacy deep ad-hoc re-sign — must not happen.+ assert not any("--deep" in c for c in calls)+ assert not any("delete-generic-password" in c for c in calls)+ # Only the quarantine-xattr hygiene ran; the bundle's signature is untouched.+ assert ["xattr", "-cr", str(app)] in calls++[email protected]("macos")+def test_relaunchable_fixup_configured_identity_success_still_signs(tmp_path, monkeypatch):+ """A configured identity that signs successfully keeps the working path (#123748)."""+ root = _make_desktop_tree(tmp_path)+ desktop_dir = root / "apps" / "desktop"+ monkeypatch.setattr(cli_main, "PROJECT_ROOT", root)+ monkeypatch.delenv("CSC_LINK", raising=False)+ monkeypatch.delenv("APPLE_SIGNING_IDENTITY", raising=False)+ _make_packaged_executable(root, monkeypatch)++ calls: list[list[str]] = []+ monkeypatch.setattr(main_desktop, "_desktop_macos_has_valid_real_signature", lambda a: False)+ monkeypatch.setattr(main_desktop, "_desktop_macos_local_signing_identity", lambda: "Hermes Local Signing")++ def fake_local_codesign(app, *, desktop_dir, identity):+ calls.append(["local-codesign", identity])+ return True++ monkeypatch.setattr(main_desktop, "_desktop_macos_local_codesign", fake_local_codesign)+ monkeypatch.setattr(+ cli_main.subprocess, "run",+ lambda cmd, **kw: calls.append(list(cmd)) or subprocess.CompletedProcess(cmd, 0),+ )++ assert cli_main._desktop_macos_relaunchable_fixup(desktop_dir) is True+ assert ["local-codesign", "Hermes Local Signing"] in calls+ assert not any("--deep" in c for c in calls)+ assert not any("delete-generic-password" in c for c in calls)++@pytest.mark.platforms("macos")def test_relaunchable_fixup_legacy_adhoc_failure_never_touches_keychain(tmp_path, monkeypatch):"""A failed fallback re-sign must preserve the keychain item (no deletion).