fix(tools): reject special files before file mutations

This commit is contained in:
Xubin Ren committed 2026-11-14 22:09:21 +08:00
1 parent b9c1fc7196
commit 5a234e8328
2 files changed
+40 -2

No files matched your search

+10 -2
View File
@@ -474,13 +474,21 @@ class WorkspaceFiles:
def edit_target(self, path: str) -> WritableFile:
workspace, root = self._roots()
return WritableFile(resolve_allowed_path(
target = resolve_allowed_path(
path,
workspace=workspace,
allowed_root=root,
extra_allowed_roots=self._write_dirs if root is not None else (),
extra_allowed_files=self._write_files,
))
)
try:
mode = target.stat().st_mode
except FileNotFoundError:
pass # Write operations can create regular files.
else:
if not stat.S_ISREG(mode):
raise NotRegularFileError(f"Not a regular file: {path}")
return WritableFile(target)
def read_file(self, path: str) -> ReadableFile:
_reject_device(path)
+30
View File
@@ -1,5 +1,9 @@
import os
from pathlib import Path
import pytest
from nanobot.agent.tools.apply_patch import ApplyPatchTool
from nanobot.agent.tools.file_state import file_read_context
from nanobot.agent.tools.filesystem import (
EditFileTool,
@@ -9,6 +13,32 @@ from nanobot.agent.tools.filesystem import (
)
@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="requires a POSIX named pipe")
@pytest.mark.parametrize("operation", ["write", "patch"])
async def test_file_mutation_rejects_named_pipe_without_opening_it(tmp_path, monkeypatch, operation):
pipe = (tmp_path / "pipe").resolve()
os.mkfifo(pipe)
open_file = Path.open
def reject_blocking_open(path, *args, **kwargs):
if path == pipe:
raise AssertionError("Opening a named pipe can block the agent")
return open_file(path, *args, **kwargs)
monkeypatch.setattr(Path, "open", reject_blocking_open)
if operation == "write":
result = await WriteFileTool(workspace=tmp_path, allowed_dir=tmp_path).execute(
path="pipe", content="hello",
)
else:
result = await ApplyPatchTool(workspace=tmp_path, allowed_dir=tmp_path).execute(
edits=[{"path": "pipe", "action": "add", "new_text": "hello"}],
)
assert result.is_error
assert "Not a regular file: pipe" in result
async def test_exact_edit_grant_does_not_list_sibling_files(tmp_path):
workspace = tmp_path / "workspace"
workspace.mkdir()