fix(cli-apps): preserve XDG_RUNTIME_DIR for desktop CLIs

This commit is contained in:
KevinAndrewDong authored and chengyongru committed 2026-10-04 15:03:35 +08:00
1 parent 13cec0146d
commit acdae3d0ae
3 files changed
+68 -3

No files matched your search

+5
View File
@@ -350,6 +350,11 @@ default **Ready** view shows only capabilities that can be used immediately:
localhost WebUIs return automatically; a remote plain-HTTP WebUI shows one
field for pasting the complete localhost callback URL.
CLI Apps run with a limited environment that excludes provider API keys. On
Unix, they inherit `XDG_RUNTIME_DIR` when it is set in the gateway's environment,
so desktop CLIs such as Obsidian can locate the running application. Start the
gateway in the same desktop session as the app to inherit that runtime directory.
Apps intentionally does not list nanobot runtime support packages such as
`api` or `bedrock`. Those packages enable providers, servers, or channels; they
are not tools that can be attached to a turn with `@`. Manage them from
+6 -2
View File
@@ -993,7 +993,7 @@ class CliAppManager:
def _subprocess_env(self) -> dict[str, str]:
"""Minimal env for CLI app subprocesses — no API keys or secrets.
Mirrors the shell tool's allowlist so installed apps cannot read
Uses an explicit allowlist so installed apps cannot read
provider credentials from the parent process environment.
"""
if sys.platform == "win32":
@@ -1011,13 +1011,17 @@ class CliAppManager:
"PYTHONUNBUFFERED": "1",
}
return env
return {
env = {
"HOME": os.environ.get("HOME", "/tmp"),
"LANG": os.environ.get("LANG", "C.UTF-8"),
"TERM": os.environ.get("TERM", "dumb"),
"PATH": os.environ.get("PATH", "/usr/bin:/bin"),
"PYTHONUNBUFFERED": "1",
}
# Desktop CLIs use this directory to locate the running application.
if "XDG_RUNTIME_DIR" in os.environ:
env["XDG_RUNTIME_DIR"] = os.environ["XDG_RUNTIME_DIR"]
return env
def _run_argv(self, argv: list[str], *, timeout: int) -> subprocess.CompletedProcess[str]:
command = subprocess.list2cmdline(argv)
+57 -1
View File
@@ -2,15 +2,26 @@
from __future__ import annotations
import json
import subprocess
import sys
import pytest
from nanobot.apps.cli.service import CliAppManager
def test_subprocess_env_excludes_api_keys(monkeypatch, tmp_path) -> None:
@pytest.mark.parametrize("platform", ["linux", "darwin"])
@pytest.mark.parametrize("runtime_dir", ["/run/user/1000", None])
def test_subprocess_env_excludes_api_keys(monkeypatch, tmp_path, platform, runtime_dir) -> None:
monkeypatch.setattr("nanobot.apps.cli.service.sys.platform", platform)
monkeypatch.setenv("OPENAI_API_KEY", "sk-should-not-leak")
monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-leak")
monkeypatch.setenv("OPENROUTER_API_KEY", "sk-or-leak")
if runtime_dir is None:
monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False)
else:
monkeypatch.setenv("XDG_RUNTIME_DIR", runtime_dir)
manager = CliAppManager(workspace=tmp_path, data_dir=tmp_path / "cli-apps")
env = manager._subprocess_env()
@@ -20,12 +31,16 @@ def test_subprocess_env_excludes_api_keys(monkeypatch, tmp_path) -> None:
assert "OPENROUTER_API_KEY" not in env
assert env.get("PYTHONUNBUFFERED") == "1"
assert "PATH" in env
assert env.get("XDG_RUNTIME_DIR") == runtime_dir
if runtime_dir is None:
assert "XDG_RUNTIME_DIR" not in env
def test_subprocess_env_excludes_api_keys_on_windows(monkeypatch, tmp_path) -> None:
monkeypatch.setattr("nanobot.apps.cli.service.sys.platform", "win32")
monkeypatch.setenv("OPENAI_API_KEY", "sk-should-not-leak")
monkeypatch.setenv("ANTHROPIC_API_KEY", "sk-ant-leak")
monkeypatch.setenv("XDG_RUNTIME_DIR", "/run/user/1000")
manager = CliAppManager(workspace=tmp_path, data_dir=tmp_path / "cli-apps")
env = manager._subprocess_env()
@@ -34,6 +49,7 @@ def test_subprocess_env_excludes_api_keys_on_windows(monkeypatch, tmp_path) -> N
assert "ANTHROPIC_API_KEY" not in env
assert env["PYTHONUNBUFFERED"] == "1"
assert env["SYSTEMROOT"]
assert "XDG_RUNTIME_DIR" not in env
assert all(isinstance(value, str) for value in env.values())
@@ -88,3 +104,43 @@ def test_management_subprocesses_use_filtered_env(monkeypatch, tmp_path) -> None
assert isinstance(env, dict)
assert "OPENAI_API_KEY" not in env
assert env["PYTHONUNBUFFERED"] == "1"
@pytest.mark.parametrize("operation", ["run", "management"])
@pytest.mark.parametrize("runtime_dir", ["/run/user/1000", None])
def test_child_process_receives_runtime_dir_without_api_keys(
monkeypatch, tmp_path, operation, runtime_dir,
) -> None:
secrets = {
"OPENAI_API_KEY": "cli-env-openai-sentinel",
"ANTHROPIC_API_KEY": "cli-env-anthropic-sentinel",
"OPENROUTER_API_KEY": "cli-env-openrouter-sentinel",
}
for key, value in secrets.items():
monkeypatch.setenv(key, value)
if runtime_dir is None:
monkeypatch.delenv("XDG_RUNTIME_DIR", raising=False)
else:
monkeypatch.setenv("XDG_RUNTIME_DIR", runtime_dir)
manager = CliAppManager(workspace=tmp_path, data_dir=tmp_path / "cli-apps")
manager._save_installed({"env-check": {"entry_point": sys.executable}})
monkeypatch.setattr(manager, "get_app", lambda name: {"name": name})
keys = ("XDG_RUNTIME_DIR", *secrets)
args = [
"-c",
"import json, os; print(json.dumps({key: os.environ.get(key) "
f"for key in {keys!r}}}))",
]
if operation == "run":
output = manager.run("env-check", args)
assert "exited 0" in output
else:
result = manager._run_argv([sys.executable, *args], timeout=5)
assert result.returncode == 0
output = result.stdout
expected = {key: None for key in keys}
expected["XDG_RUNTIME_DIR"] = runtime_dir if sys.platform != "win32" else None
assert json.dumps(expected) in output
assert all(value not in output for value in secrets.values())