fix(computer-use): sanitize subprocess env in cua-driver CLI fallback transport

_CuaDriverSession._call_tool_via_cli() (the EAGAIN/silent-empty MCP
fallback transport) invokes `cua-driver call <tool> <json>` via
subprocess.run() with no env= argument, so the third-party cua-driver
binary inherits the full, unsanitized parent environment. The primary
MCP spawn site (_lifecycle_coro) already applies
_sanitize_subprocess_env(cua_driver_child_env()) before opening the
stdio client, per the same policy #53503/#55709 established for other
subprocess spawn points — this fallback path, added alongside the
EAGAIN/silent-empty-capture hardening, missed it.

Fix: apply the same env=_sanitize_subprocess_env(cua_driver_child_env())
to the subprocess.run() call in _call_tool_via_cli(), mirroring the
sanctioned spawn site exactly (telemetry policy applied first, then
Hermes-managed secrets filtered).
This commit is contained in:
srojk34 2026-07-05 17:01:33 +03:00 committed by Teknium
parent 04d732dc56
commit f10851e3fd
2 changed files with 76 additions and 1 deletions

View file

@ -0,0 +1,73 @@
"""Regression test: the cua-driver CLI-fallback transport must sanitize the
subprocess environment like every other cua-driver spawn site.
``_CuaDriverSession._call_tool_via_cli()`` (the EAGAIN/silent-empty MCP
fallback) invoked ``subprocess.run`` with no ``env=`` at all, so the
third-party ``cua-driver`` binary inherited the full, unsanitized parent
environment including provider API keys and other Hermes-managed
secrets that ``_lifecycle_coro``'s primary MCP spawn already strips via
``_sanitize_subprocess_env(cua_driver_child_env())``.
"""
import json
from unittest.mock import MagicMock
from tools.computer_use.cua_backend import _CuaDriverSession
def _make_session() -> _CuaDriverSession:
# _call_tool_via_cli() doesn't touch any instance state (bridge/session/
# capabilities); bypass __init__ so the test doesn't need a real
# _AsyncBridge.
return object.__new__(_CuaDriverSession)
def _fake_completed_process(stdout: str) -> MagicMock:
proc = MagicMock()
proc.stdout = stdout
proc.stderr = ""
proc.returncode = 0
return proc
def test_cli_fallback_strips_provider_secret_from_subprocess_env(monkeypatch):
monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-super-secret-should-not-leak")
monkeypatch.setenv("PATH", "/usr/bin:/bin")
captured = {}
def fake_run(cmd, **kwargs):
captured["env"] = kwargs.get("env")
return _fake_completed_process(json.dumps({"tree_markdown": "root"}))
monkeypatch.setattr("subprocess.run", fake_run)
session = _make_session()
result = session._call_tool_via_cli("list_windows", {}, timeout=5.0)
assert result["isError"] is False
assert captured["env"] is not None, "subprocess.run must receive an explicit env="
assert "ANTHROPIC_API_KEY" not in captured["env"]
# Sanitization filters secrets, not everything — an ordinary var survives.
assert captured["env"].get("PATH") == "/usr/bin:/bin"
def test_cli_fallback_applies_telemetry_policy(monkeypatch):
"""The env should also go through cua_driver_child_env(), like every
other cua-driver spawn site, not just _sanitize_subprocess_env alone."""
monkeypatch.delenv("HERMES_CUA_TELEMETRY", raising=False)
captured = {}
def fake_run(cmd, **kwargs):
captured["env"] = kwargs.get("env")
return _fake_completed_process(json.dumps({"tree_markdown": "root"}))
monkeypatch.setattr("subprocess.run", fake_run)
session = _make_session()
session._call_tool_via_cli("list_windows", {}, timeout=5.0)
# cua_driver_child_env() injects this when telemetry is disabled
# (the default) — confirms the fallback goes through the same helper
# the sanctioned spawn site uses, not an ad hoc env dict.
assert captured["env"].get("CUA_DRIVER_RS_TELEMETRY_ENABLED") == "0"

View file

@ -894,6 +894,7 @@ class _CuaDriverSession:
import subprocess as _subprocess
import tempfile as _tempfile
import time as _time
from tools.environments.local import _sanitize_subprocess_env
call_args = dict(args)
shot_file: Optional[str] = None
@ -911,7 +912,8 @@ class _CuaDriverSession:
for attempt in range(attempts):
try:
proc = _subprocess.run(
cmd, capture_output=True, text=True, timeout=max(15.0, timeout)
cmd, capture_output=True, text=True, timeout=max(15.0, timeout),
env=_sanitize_subprocess_env(cua_driver_child_env()),
)
except Exception as e: # pragma: no cover - subprocess spawn failure
raise RuntimeError(f"cua-driver CLI fallback for {name} failed to spawn: {e}") from e