mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-13 15:49:16 +03:00
fix(cli): isolate management subprocess environments
This commit is contained in:
@@ -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()
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|||||||
@@ -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())
|
||||||
|
|||||||
Reference in New Issue
Block a user