fix(exec): reject shell paths with unresolved home directories
This commit is contained in:
1 parent
af6abde2f4
commit
1d43b46e54
2 files changed
+69
-1
No files matched your search
@@ -897,7 +897,15 @@ class ExecTool(Tool):
|
|||||||
+ _WORKSPACE_BOUNDARY_NOTE
|
+ _WORKSPACE_BOUNDARY_NOTE
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
p = Path(expanded).expanduser().resolve()
|
try:
|
||||||
|
home_path = Path(expanded).expanduser()
|
||||||
|
except RuntimeError:
|
||||||
|
return ToolResult.error(
|
||||||
|
"Error: Command blocked by safety guard "
|
||||||
|
"(path outside working dir)"
|
||||||
|
+ _WORKSPACE_BOUNDARY_NOTE
|
||||||
|
)
|
||||||
|
p = home_path.resolve()
|
||||||
# Match against the un-resolved path first. On Linux,
|
# Match against the un-resolved path first. On Linux,
|
||||||
# /dev/stderr is a symlink to /proc/self/fd/2 and
|
# /dev/stderr is a symlink to /proc/self/fd/2 and
|
||||||
# ``Path.resolve()`` would mask the device-file intent.
|
# ``Path.resolve()`` would mask the device-file intent.
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import shlex
|
import shlex
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
@@ -343,6 +345,64 @@ def test_exec_guard_blocks_home_path_outside_workspace(tmp_path) -> None:
|
|||||||
assert "hard policy boundary" in error
|
assert "hard policy boundary" in error
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.skipif(sys.platform == "win32", reason="Unix resolves named homes through passwd")
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"command",
|
||||||
|
["cat ~unknown/file.txt", "cat --config=~unknown/file.txt", "cat <~unknown/file.txt"],
|
||||||
|
)
|
||||||
|
async def test_exec_rejects_unknown_user_home(tmp_path, monkeypatch, command) -> None:
|
||||||
|
home_path = f"~nanobot-missing-{tmp_path.name}/file.txt"
|
||||||
|
command = command.replace("~unknown/file.txt", home_path)
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
Path(home_path).expanduser()
|
||||||
|
|
||||||
|
spawn = AsyncMock(side_effect=AssertionError("Blocked commands must not start a process"))
|
||||||
|
monkeypatch.setattr(ExecTool, "_spawn", spawn)
|
||||||
|
result = await ExecTool(
|
||||||
|
working_dir=str(tmp_path), restrict_to_workspace=True,
|
||||||
|
).execute(command=command)
|
||||||
|
|
||||||
|
assert result.startswith("Error: Command blocked by safety guard (path outside working dir)")
|
||||||
|
assert "hard policy boundary" in result
|
||||||
|
spawn.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_exec_rejects_home_path_without_home_directory(tmp_path, monkeypatch) -> None:
|
||||||
|
for key in ("HOME", "USERPROFILE", "HOMEDRIVE", "HOMEPATH"):
|
||||||
|
monkeypatch.delenv(key, raising=False)
|
||||||
|
if sys.platform != "win32":
|
||||||
|
import pwd
|
||||||
|
|
||||||
|
def missing_passwd_entry(uid):
|
||||||
|
# A container UID may have no passwd entry when HOME is unset.
|
||||||
|
raise KeyError(uid)
|
||||||
|
|
||||||
|
monkeypatch.setattr(pwd, "getpwuid", missing_passwd_entry)
|
||||||
|
with pytest.raises(RuntimeError):
|
||||||
|
Path("~/file.txt").expanduser()
|
||||||
|
|
||||||
|
spawn = AsyncMock(side_effect=AssertionError("Blocked commands must not start a process"))
|
||||||
|
monkeypatch.setattr(ExecTool, "_spawn", spawn)
|
||||||
|
result = await ExecTool(
|
||||||
|
working_dir=str(tmp_path), restrict_to_workspace=True,
|
||||||
|
).execute(command="cat ~/file.txt")
|
||||||
|
|
||||||
|
assert result.startswith("Error: Command blocked by safety guard (path outside working dir)")
|
||||||
|
assert "hard policy boundary" in result
|
||||||
|
spawn.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
async def test_exec_allows_resolved_home_inside_workspace(tmp_path, monkeypatch) -> None:
|
||||||
|
monkeypatch.setenv("USERPROFILE" if sys.platform == "win32" else "HOME", str(tmp_path))
|
||||||
|
(tmp_path / "file.txt").write_text("allowed home content", encoding="utf-8")
|
||||||
|
result = await ExecTool(
|
||||||
|
working_dir=str(tmp_path), restrict_to_workspace=True,
|
||||||
|
).execute(command="cat ~/file.txt", login=False)
|
||||||
|
|
||||||
|
assert "allowed home content" in result
|
||||||
|
assert "Exit code: 0" in result
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"command",
|
"command",
|
||||||
[
|
[
|
||||||
|
|||||||
Reference in new issue
Block a user