fix(exec): add Windows support for shell command execution
ExecTool hardcoded bash, breaking exec on Windows. Now uses cmd.exe via COMSPEC on Windows with a curated minimal env (PATH, SYSTEMROOT, etc.) that excludes secrets. bwrap sandbox gracefully skips on Windows.
This commit is contained in:
@@ -15,6 +15,8 @@ from nanobot.agent.tools.sandbox import wrap_command
|
||||
from nanobot.agent.tools.schema import IntegerSchema, StringSchema, tool_parameters_schema
|
||||
from nanobot.config.paths import get_media_dir
|
||||
|
||||
_IS_WINDOWS = sys.platform == "win32"
|
||||
|
||||
|
||||
@tool_parameters(
|
||||
tool_parameters_schema(
|
||||
@@ -88,27 +90,27 @@ class ExecTool(Tool):
|
||||
return guard_error
|
||||
|
||||
if self.sandbox:
|
||||
workspace = self.working_dir or cwd
|
||||
command = wrap_command(self.sandbox, command, workspace, cwd)
|
||||
cwd = str(Path(workspace).resolve())
|
||||
if _IS_WINDOWS:
|
||||
logger.warning(
|
||||
"Sandbox '{}' is not supported on Windows; running unsandboxed",
|
||||
self.sandbox,
|
||||
)
|
||||
else:
|
||||
workspace = self.working_dir or cwd
|
||||
command = wrap_command(self.sandbox, command, workspace, cwd)
|
||||
cwd = str(Path(workspace).resolve())
|
||||
|
||||
effective_timeout = min(timeout or self.timeout, self._MAX_TIMEOUT)
|
||||
|
||||
env = self._build_env()
|
||||
|
||||
if self.path_append:
|
||||
command = f'export PATH="$PATH:{self.path_append}"; {command}'
|
||||
|
||||
bash = shutil.which("bash") or "/bin/bash"
|
||||
if _IS_WINDOWS:
|
||||
env["PATH"] = env.get("PATH", "") + ";" + self.path_append
|
||||
else:
|
||||
command = f'export PATH="$PATH:{self.path_append}"; {command}'
|
||||
|
||||
try:
|
||||
process = await asyncio.create_subprocess_exec(
|
||||
bash, "-l", "-c", command,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
cwd=cwd,
|
||||
env=env,
|
||||
)
|
||||
process = await self._spawn(command, cwd, env)
|
||||
|
||||
try:
|
||||
stdout, stderr = await asyncio.wait_for(
|
||||
@@ -136,7 +138,6 @@ class ExecTool(Tool):
|
||||
|
||||
result = "\n".join(output_parts) if output_parts else "(no output)"
|
||||
|
||||
# Head + tail truncation to preserve both start and end of output
|
||||
max_len = self._MAX_OUTPUT
|
||||
if len(result) > max_len:
|
||||
half = max_len // 2
|
||||
@@ -151,6 +152,29 @@ class ExecTool(Tool):
|
||||
except Exception as e:
|
||||
return f"Error executing command: {str(e)}"
|
||||
|
||||
@staticmethod
|
||||
async def _spawn(
|
||||
command: str, cwd: str, env: dict[str, str],
|
||||
) -> asyncio.subprocess.Process:
|
||||
"""Launch *command* in a platform-appropriate shell."""
|
||||
if _IS_WINDOWS:
|
||||
comspec = env.get("COMSPEC", os.environ.get("COMSPEC", "cmd.exe"))
|
||||
return await asyncio.create_subprocess_exec(
|
||||
comspec, "/c", command,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
cwd=cwd,
|
||||
env=env,
|
||||
)
|
||||
bash = shutil.which("bash") or "/bin/bash"
|
||||
return await asyncio.create_subprocess_exec(
|
||||
bash, "-l", "-c", command,
|
||||
stdout=asyncio.subprocess.PIPE,
|
||||
stderr=asyncio.subprocess.PIPE,
|
||||
cwd=cwd,
|
||||
env=env,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def _kill_process(process: asyncio.subprocess.Process) -> None:
|
||||
"""Kill a subprocess and reap it to prevent zombies."""
|
||||
@@ -160,7 +184,7 @@ class ExecTool(Tool):
|
||||
except asyncio.TimeoutError:
|
||||
pass
|
||||
finally:
|
||||
if sys.platform != "win32":
|
||||
if not _IS_WINDOWS:
|
||||
try:
|
||||
os.waitpid(process.pid, os.WNOHANG)
|
||||
except (ProcessLookupError, ChildProcessError) as e:
|
||||
@@ -169,11 +193,26 @@ class ExecTool(Tool):
|
||||
def _build_env(self) -> dict[str, str]:
|
||||
"""Build a minimal environment for subprocess execution.
|
||||
|
||||
Uses HOME so that ``bash -l`` sources the user's profile (which sets
|
||||
PATH and other essentials). Only PATH is extended with *path_append*;
|
||||
the parent process's environment is **not** inherited, preventing
|
||||
secrets in env vars from leaking to LLM-generated commands.
|
||||
On Unix, only HOME/LANG/TERM are passed; ``bash -l`` sources the
|
||||
user's profile which sets PATH and other essentials.
|
||||
|
||||
On Windows, ``cmd.exe`` has no login-profile mechanism, so a curated
|
||||
set of system variables (including PATH) is forwarded. API keys and
|
||||
other secrets are still excluded.
|
||||
"""
|
||||
if _IS_WINDOWS:
|
||||
sr = os.environ.get("SYSTEMROOT", r"C:\Windows")
|
||||
return {
|
||||
"SYSTEMROOT": sr,
|
||||
"COMSPEC": os.environ.get("COMSPEC", f"{sr}\\system32\\cmd.exe"),
|
||||
"USERPROFILE": os.environ.get("USERPROFILE", ""),
|
||||
"HOMEDRIVE": os.environ.get("HOMEDRIVE", "C:"),
|
||||
"HOMEPATH": os.environ.get("HOMEPATH", "\\"),
|
||||
"TEMP": os.environ.get("TEMP", f"{sr}\\Temp"),
|
||||
"TMP": os.environ.get("TMP", f"{sr}\\Temp"),
|
||||
"PATHEXT": os.environ.get("PATHEXT", ".COM;.EXE;.BAT;.CMD"),
|
||||
"PATH": os.environ.get("PATH", f"{sr}\\system32;{sr}"),
|
||||
}
|
||||
home = os.environ.get("HOME", "/tmp")
|
||||
return {
|
||||
"HOME": home,
|
||||
|
||||
Reference in New Issue
Block a user