diff --git a/cli.py b/cli.py index 50e7a8c8ce9..241d41e9fcd 100644 --- a/cli.py +++ b/cli.py @@ -11736,11 +11736,13 @@ class HermesCLI: # Ensure tirith security scanner is available (downloads if needed). # Warn the user if tirith is enabled in config but not available, - # so they know command security scanning is degraded. + # so they know command security scanning is degraded. Suppressed + # on platforms where tirith ships no binary (Windows etc.) — the + # user can't act on it and pattern-matching guards still run. try: - from tools.tirith_security import ensure_installed + from tools.tirith_security import ensure_installed, is_platform_supported tirith_path = ensure_installed(log_failures=False) - if tirith_path is None: + if tirith_path is None and is_platform_supported(): security_cfg = self.config.get("security", {}) or {} tirith_enabled = security_cfg.get("tirith_enabled", True) if tirith_enabled: diff --git a/tests/tools/test_tirith_security.py b/tests/tools/test_tirith_security.py index ecaf4f4e639..afeb14f9458 100644 --- a/tests/tools/test_tirith_security.py +++ b/tests/tools/test_tirith_security.py @@ -333,6 +333,103 @@ class TestEnsureInstalled: _tirith_mod._resolved_path = None +# --------------------------------------------------------------------------- +# Unsupported platform (Windows etc.) — silent fast-path everywhere +# --------------------------------------------------------------------------- + +class TestUnsupportedPlatform: + """When _detect_target() returns None (no tirith binary for this OS+arch), + the entire subsystem must stay silent: no PATH probes, no download thread, + no disk failure marker, no spawn attempts, no CLI banner. Pattern-matching + guards still cover the gap; tirith content scanning is just absent.""" + + def test_is_platform_supported_true_on_linux_x86_64(self): + with patch("tools.tirith_security.platform.system", return_value="Linux"), \ + patch("tools.tirith_security.platform.machine", return_value="x86_64"): + assert _tirith_mod.is_platform_supported() is True + + def test_is_platform_supported_true_on_darwin_arm64(self): + with patch("tools.tirith_security.platform.system", return_value="Darwin"), \ + patch("tools.tirith_security.platform.machine", return_value="arm64"): + assert _tirith_mod.is_platform_supported() is True + + def test_is_platform_supported_false_on_windows(self): + with patch("tools.tirith_security.platform.system", return_value="Windows"), \ + patch("tools.tirith_security.platform.machine", return_value="AMD64"): + assert _tirith_mod.is_platform_supported() is False + + def test_is_platform_supported_false_on_unknown_arch(self): + with patch("tools.tirith_security.platform.system", return_value="Linux"), \ + patch("tools.tirith_security.platform.machine", return_value="riscv64"): + assert _tirith_mod.is_platform_supported() is False + + @patch("tools.tirith_security._load_security_config") + def test_ensure_installed_unsupported_returns_none_no_thread(self, mock_cfg): + """Windows: don't start a background install thread, don't write a + failure marker — just cache the verdict and return None.""" + mock_cfg.return_value = {"tirith_enabled": True, "tirith_path": "tirith", + "tirith_timeout": 5, "tirith_fail_open": True} + _tirith_mod._resolved_path = None + with patch("tools.tirith_security.is_platform_supported", return_value=False), \ + patch("tools.tirith_security.threading.Thread") as MockThread, \ + patch("tools.tirith_security._mark_install_failed") as mock_mark, \ + patch("tools.tirith_security.shutil.which") as mock_which: + result = ensure_installed() + assert result is None + MockThread.assert_not_called() + mock_mark.assert_not_called() + mock_which.assert_not_called() + assert _tirith_mod._resolved_path is _tirith_mod._INSTALL_FAILED + assert _tirith_mod._install_failure_reason == "unsupported_platform" + + @patch("tools.tirith_security._load_security_config") + def test_check_command_security_unsupported_allows_silently(self, mock_cfg): + """Windows: skip the resolver and spawn entirely — return allow with + an empty summary so callers can't accidentally surface 'tirith + unavailable' messaging to the user.""" + mock_cfg.return_value = {"tirith_enabled": True, "tirith_path": "tirith", + "tirith_timeout": 5, "tirith_fail_open": True} + with patch("tools.tirith_security.is_platform_supported", return_value=False), \ + patch("tools.tirith_security.subprocess.run") as mock_run, \ + patch("tools.tirith_security._resolve_tirith_path") as mock_resolve: + result = check_command_security("rm -rf /") + assert result == {"action": "allow", "findings": [], "summary": ""} + mock_run.assert_not_called() + mock_resolve.assert_not_called() + + @patch("tools.tirith_security._load_security_config") + def test_resolve_path_unsupported_caches_failure_without_probing(self, mock_cfg): + """The per-command resolver must also short-circuit on Windows so + long-running gateways don't churn through `shutil.which` and disk + I/O for every scanned command.""" + mock_cfg.return_value = {"tirith_enabled": True, "tirith_path": "tirith", + "tirith_timeout": 5, "tirith_fail_open": True} + _tirith_mod._resolved_path = None + with patch("tools.tirith_security.is_platform_supported", return_value=False), \ + patch("tools.tirith_security.shutil.which") as mock_which: + result = _tirith_mod._resolve_tirith_path("tirith") + assert result == "tirith" + mock_which.assert_not_called() + assert _tirith_mod._resolved_path is _tirith_mod._INSTALL_FAILED + assert _tirith_mod._install_failure_reason == "unsupported_platform" + + @patch("tools.tirith_security._load_security_config") + def test_explicit_path_still_honored_on_unsupported_platform(self, mock_cfg): + """If a user explicitly configured a tirith_path (e.g. they built it + themselves under WSL), the unsupported-platform short-circuit must + NOT override that — explicit config wins.""" + mock_cfg.return_value = {"tirith_enabled": True, + "tirith_path": "/opt/custom/tirith", + "tirith_timeout": 5, "tirith_fail_open": True} + _tirith_mod._resolved_path = None + with patch("tools.tirith_security.is_platform_supported", return_value=False), \ + patch("os.path.isfile", return_value=True), \ + patch("os.access", return_value=True): + result = _tirith_mod._resolve_tirith_path("/opt/custom/tirith") + assert result == "/opt/custom/tirith" + assert _tirith_mod._resolved_path == "/opt/custom/tirith" + + # --------------------------------------------------------------------------- # Failed download caches the miss (Finding #1) # --------------------------------------------------------------------------- diff --git a/tools/tirith_security.py b/tools/tirith_security.py index 1c79892f424..b45d7d29213 100644 --- a/tools/tirith_security.py +++ b/tools/tirith_security.py @@ -214,7 +214,12 @@ def _hermes_bin_dir() -> str: def _detect_target() -> str | None: - """Return the Rust target triple for the current platform, or None.""" + """Return the Rust target triple for the current platform, or None. + + Windows is intentionally unsupported — tirith does not ship a Windows + build. Callers should treat `None` as "this platform will never have + tirith" and silently fall back to pattern-matching guards. + """ system = platform.system() machine = platform.machine().lower() @@ -236,6 +241,16 @@ def _detect_target() -> str | None: return f"{arch}-{plat}" +def is_platform_supported() -> bool: + """True when tirith ships a prebuilt binary for this OS+arch. + + Used by callers (CLI banner, etc.) to distinguish "tirith failed to + install" from "tirith was never going to install here" — the latter + is silent because there is nothing the user can do about it. + """ + return _detect_target() is not None + + def _download_file(url: str, dest: str, timeout: int = 10): """Download a URL to a local file.""" req = urllib.request.Request(url) @@ -448,6 +463,15 @@ def _resolve_tirith_path(configured_path: str) -> str: explicit = _is_explicit_path(configured_path) install_failed = _resolved_path is _INSTALL_FAILED + # Platform has no tirith build (Windows etc.). Cache the verdict and + # return the unexpanded configured path — the spawn loop will fail-open + # via the dedupe'd OSError handler, but only after the first call; on + # subsequent calls the fast-path above short-circuits before spawning. + if not explicit and not is_platform_supported(): + _resolved_path = _INSTALL_FAILED + _install_failure_reason = "unsupported_platform" + return expanded + # Explicit path: check it and stop. Never auto-download a replacement. if explicit: if os.path.isfile(expanded) and os.access(expanded, os.X_OK): @@ -574,6 +598,14 @@ def ensure_installed(*, log_failures: bool = True): return path return None + # Platform has no tirith build (e.g. Windows) — don't probe PATH, + # don't start a download thread, don't write a disk failure marker. + # Pattern-matching guards still run; this path stays silent. + if not is_platform_supported(): + _resolved_path = _INSTALL_FAILED + _install_failure_reason = "unsupported_platform" + return None + configured_path = cfg["tirith_path"] explicit = _is_explicit_path(configured_path) expanded = os.path.expanduser(configured_path) @@ -659,6 +691,12 @@ def check_command_security(command: str) -> dict: if not cfg["tirith_enabled"]: return {"action": "allow", "findings": [], "summary": ""} + # Unsupported platform (Windows etc.) — tirith has no binary here and + # never will. Skip the resolver entirely so we don't even try to spawn. + # Pattern-matching guards still run via the rest of approval.py. + if not is_platform_supported(): + return {"action": "allow", "findings": [], "summary": ""} + tirith_path = _resolve_tirith_path(cfg["tirith_path"]) timeout = cfg["tirith_timeout"] fail_open = cfg["tirith_fail_open"] diff --git a/website/docs/user-guide/security.md b/website/docs/user-guide/security.md index fca8a99a248..2a48deb2448 100644 --- a/website/docs/user-guide/security.md +++ b/website/docs/user-guide/security.md @@ -537,6 +537,8 @@ security: When `tirith_fail_open` is `true` (default), commands proceed if tirith is not installed or times out. Set to `false` in high-security environments to block commands when tirith is unavailable. +Tirith ships prebuilt binaries for Linux (x86_64 / aarch64) and macOS (x86_64 / arm64). On platforms with no prebuilt binary (Windows, etc.), tirith is silently skipped — pattern-matching guards still run, and the CLI does not surface an "unavailable" banner. To use tirith on Windows, run Hermes under WSL. + Tirith's verdict integrates with the approval flow: safe commands pass through, while both suspicious and blocked commands trigger user approval with the full tirith findings (severity, title, description, safer alternatives). Users can approve or deny — the default choice is deny to keep unattended scenarios secure. ### Context File Injection Protection