Skip to content

The agent that grows with you

fix(tui-gateway): scrub shell RPC secrets

fangliquana5c044d2e76b
diff --git a/tests/tui_gateway/test_shell_exec_security.py b/tests/tui_gateway/test_shell_exec_security.py
new file mode 100644
index 00000000000..3dfcb7f6de5
--- /dev/null
+++ b/tests/tui_gateway/test_shell_exec_security.py
@@ -0,0 +1,36 @@
+"""Security-boundary regressions for the TUI ``shell.exec`` RPC."""
+
+from __future__ import annotations
+
+import subprocess
+
+from agent import redact
+from tui_gateway import server
+
+
+def test_shell_exec_scrubs_child_env_and_force_redacts_rpc_output(monkeypatch):
+ secret = "sk-shell-exec-secret-1234567890"
+ captured = {}
+
+ def fake_run(command, **kwargs):
+ captured.update(kwargs)
+ return subprocess.CompletedProcess(
+ command,
+ 0,
+ stdout=f"visible\n{secret}\n",
+ stderr=f"diagnostic {secret}",
+ )
+
+ monkeypatch.setenv("OPENROUTER_API_KEY", secret)
+ monkeypatch.setattr(redact, "_REDACT_ENABLED", False)
+ monkeypatch.setattr(server.subprocess, "run", fake_run)
+ monkeypatch.setattr(server, "detect_hardline_command", lambda _command: (False, ""), raising=False)
+
+ response = server.handle_request(
+ {"id": "security", "method": "shell.exec", "params": {"command": "echo safe"}}
+ )
+
+ assert "OPENROUTER_API_KEY" not in captured["env"]
+ assert secret not in response["result"]["stdout"]
+ assert secret not in response["result"]["stderr"]
+ assert "visible" in response["result"]["stdout"]
\ No newline at end of file
diff --git a/tui_gateway/methods_tools.py b/tui_gateway/methods_tools.py
index b2c7405e559..942573a92d2 100644
--- a/tui_gateway/methods_tools.py
+++ b/tui_gateway/methods_tools.py
@@ -1532,9 +1532,22 @@ def _(rid, params: dict) -> dict:
return _err(rid, 4005, f"blocked: {desc}. Use the agent for dangerous commands.")
except ImportError:
return _err(rid, 5001, "shell.exec unavailable: approval safety module not importable")
+
+ def done(result):
+ redact = _tools_mod("agent.redact").redact_sensitive_text
+ # Unlike the interactive CLI, this output crosses the RPC boundary and can be persisted
+ # in the transcript. Redact before tailing so a credential crossing the slice boundary
+ # cannot survive as two unmatched fragments.
+ stdout = redact(result.stdout or "", force=True, redact_url_credentials=True)[-4000:]
+ stderr = redact(result.stderr or "", force=True, redact_url_credentials=True)[-2000:]
+ return _ok(rid, {"stdout": stdout, "stderr": stderr, "code": result.returncode})
+
+ # shell=True preserves the user-facing !cmd grammar (pipes, redirects and interpolation).
+ # The child must not inherit credentials held by the long-lived gateway process.
+ env = _tools_mod("tools.environments.local").build_subprocess_env()
return _captured_exec(
- rid, cmd, 30, shell=True, fail_code=5003, timeout_err=(5002, "command timed out (30s)"),
- on_result=lambda r: _ok(rid, {"stdout": r.stdout[-4000:], "stderr": r.stderr[-2000:], "code": r.returncode}))
+ rid, cmd, 30, shell=True, env=env, fail_code=5003,
+ timeout_err=(5002, "command timed out (30s)"), on_result=done)
def register(server) -> None: