Skip to content

Your lightweight open-source personal agent.

fix(cli): isolate management subprocess environments

Xubin Renedaef4e4f50b
diff --git a/nanobot/apps/cli/service.py b/nanobot/apps/cli/service.py
index 4872207de..2ccf3305b 100644
--- a/nanobot/apps/cli/service.py
+++ b/nanobot/apps/cli/service.py
@@ -1029,6 +1029,7 @@ class CliAppManager:
encoding="utf-8",
errors="replace",
timeout=timeout,
+ env=self._subprocess_env(),
)
logger.info("CLI Apps: command exited with code {}: {}", result.returncode, command)
output = (result.stderr or result.stdout or "").strip()
diff --git a/tests/apps/test_cli_subprocess_env.py b/tests/apps/test_cli_subprocess_env.py
index b444ebd3b..7f0ae1f56 100644
--- a/tests/apps/test_cli_subprocess_env.py
+++ b/tests/apps/test_cli_subprocess_env.py
@@ -2,6 +2,8 @@
from __future__ import annotations
+import subprocess
+
from nanobot.apps.cli.service import CliAppManager
@@ -67,3 +69,22 @@ def test_run_passes_filtered_env(monkeypatch, tmp_path) -> None:
env = captured.get("env")
assert isinstance(env, dict)
assert "OPENAI_API_KEY" not in env
+
+
+def test_management_subprocesses_use_filtered_env(monkeypatch, tmp_path) -> None:
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-should-not-leak")
+ captured: dict[str, object] = {}
+
+ def fake_run(*args, **kwargs):
+ captured.update(kwargs)
+ return subprocess.CompletedProcess(args[0], 0, stdout="ok", stderr="")
+
+ monkeypatch.setattr("nanobot.apps.cli.service.subprocess.run", fake_run)
+ manager = CliAppManager(workspace=tmp_path, data_dir=tmp_path / "cli-apps")
+
+ manager._run_argv(["example-cli", "--help"], timeout=5)
+
+ env = captured.get("env")
+ assert isinstance(env, dict)
+ assert "OPENAI_API_KEY" not in env
+ assert env["PYTHONUNBUFFERED"] == "1"
diff --git a/tests/cli_apps/test_service.py b/tests/cli_apps/test_service.py
index 5fd532898..ebd0038aa 100644
--- a/tests/cli_apps/test_service.py
+++ b/tests/cli_apps/test_service.py
@@ -442,12 +442,15 @@ def test_run_argv_logs_command_exit_and_output(
encoding: str,
errors: str,
timeout: int,
+ env: dict[str, str],
) -> subprocess.CompletedProcess[str]:
assert capture_output is True
assert text is True
assert encoding == "utf-8"
assert errors == "replace"
assert timeout == 5
+ assert "OPENAI_API_KEY" not in env
+ assert env["PYTHONUNBUFFERED"] == "1"
return subprocess.CompletedProcess(argv, 0, stdout="installed ok", stderr="")
monkeypatch.setattr(cli_service, "logger", _Logger())