fix(cli): isolate management subprocess environments

This commit is contained in:
Xubin Ren
2026-08-12 21:09:29 +09:00
parent 5fc8303f9e
commit edaef4e4f5
3 changed files with 25 additions and 0 deletions
+1
View File
@@ -1029,6 +1029,7 @@ class CliAppManager:
encoding="utf-8", encoding="utf-8",
errors="replace", errors="replace",
timeout=timeout, timeout=timeout,
env=self._subprocess_env(),
) )
logger.info("CLI Apps: command exited with code {}: {}", result.returncode, command) logger.info("CLI Apps: command exited with code {}: {}", result.returncode, command)
output = (result.stderr or result.stdout or "").strip() output = (result.stderr or result.stdout or "").strip()
+21
View File
@@ -2,6 +2,8 @@
from __future__ import annotations from __future__ import annotations
import subprocess
from nanobot.apps.cli.service import CliAppManager 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") env = captured.get("env")
assert isinstance(env, dict) assert isinstance(env, dict)
assert "OPENAI_API_KEY" not in env 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"
+3
View File
@@ -442,12 +442,15 @@ def test_run_argv_logs_command_exit_and_output(
encoding: str, encoding: str,
errors: str, errors: str,
timeout: int, timeout: int,
env: dict[str, str],
) -> subprocess.CompletedProcess[str]: ) -> subprocess.CompletedProcess[str]:
assert capture_output is True assert capture_output is True
assert text is True assert text is True
assert encoding == "utf-8" assert encoding == "utf-8"
assert errors == "replace" assert errors == "replace"
assert timeout == 5 assert timeout == 5
assert "OPENAI_API_KEY" not in env
assert env["PYTHONUNBUFFERED"] == "1"
return subprocess.CompletedProcess(argv, 0, stdout="installed ok", stderr="") return subprocess.CompletedProcess(argv, 0, stdout="installed ok", stderr="")
monkeypatch.setattr(cli_service, "logger", _Logger()) monkeypatch.setattr(cli_service, "logger", _Logger())