Guard file handles and streamline scoped search

This commit is contained in:
Xubin Ren committed 2026-11-14 22:09:21 +08:00
1 parent 51884a244e
commit 5c7ff5b796
6 files changed
+137 -78

No files matched your search

+6 -7
View File
@@ -1,11 +1,11 @@
from __future__ import annotations
import asyncio
from pathlib import Path
import pytest
from nanobot.agent.tools.apply_patch import ApplyPatchTool
from nanobot.execution.files import WritableFile
def test_apply_patch_edits_replace(tmp_path):
@@ -472,18 +472,17 @@ def test_apply_patch_edits_rolls_back_when_later_write_fails(tmp_path, monkeypat
second_original = b"before\r\nsecond tail"
second.write_bytes(second_original)
tool = ApplyPatchTool(workspace=tmp_path)
write_text = Path.write_text
write = WritableFile.write
failed = False
def _fail_second_write(path, *args, **kwargs):
def _fail_second_write(target, *args, **kwargs):
nonlocal failed
written = write_text(path, *args, **kwargs)
if path == second and not failed:
write(target, *args, **kwargs)
if target.path == second.resolve() and not failed:
failed = True
raise OSError("injected second write failure")
return written
monkeypatch.setattr(Path, "write_text", _fail_second_write)
monkeypatch.setattr(WritableFile, "write", _fail_second_write)
result = asyncio.run(
tool.execute(
+34
View File
@@ -11,6 +11,7 @@ from nanobot.agent.tools.filesystem import (
ReadFileTool,
WriteFileTool,
)
from nanobot.execution.files import WorkspaceFiles
@pytest.mark.skipif(not hasattr(os, "mkfifo"), reason="requires a POSIX named pipe")
@@ -39,6 +40,39 @@ async def test_file_mutation_rejects_named_pipe_without_opening_it(tmp_path, mon
assert "Not a regular file: pipe" in result
@pytest.mark.skipif(not hasattr(os, "O_NOFOLLOW"), reason="requires no-follow file opens")
@pytest.mark.parametrize("operation", ["write", "patch"])
async def test_file_mutation_does_not_follow_a_symlink_swapped_in_after_authorization(
tmp_path, monkeypatch, operation,
):
workspace = (tmp_path / "workspace").resolve()
workspace.mkdir()
inside = workspace / "inside.txt"
outside = tmp_path / "private.txt"
inside.write_text("before", encoding="utf-8")
outside.write_text("private", encoding="utf-8")
authorize = WorkspaceFiles.edit_target
def swap_after_authorize(files, path):
target = authorize(files, path)
inside.unlink()
inside.symlink_to(outside)
return target
monkeypatch.setattr(WorkspaceFiles, "edit_target", swap_after_authorize)
if operation == "write":
result = await WriteFileTool(workspace=workspace, allowed_dir=workspace).execute(
path="inside.txt", content="changed",
)
else:
result = await ApplyPatchTool(workspace=workspace, allowed_dir=workspace).execute(
edits=[{"path": "inside.txt", "action": "add", "new_text": "changed"}],
)
assert result.is_error
assert outside.read_text(encoding="utf-8") == "private"
async def test_exact_edit_grant_does_not_list_sibling_files(tmp_path):
workspace = tmp_path / "workspace"
workspace.mkdir()
+27 -2
View File
@@ -21,7 +21,7 @@ from nanobot.agent.tools.search import FindFilesTool, GrepTool
from nanobot.agent.tools.web import WebSearchTool
from nanobot.bus.queue import MessageBus
from nanobot.config.schema import WebSearchConfig
from nanobot.execution.files import FileSearch
from nanobot.execution.files import FileSearch, WorkspaceFiles
from nanobot.providers.base import GenerationSettings
from nanobot.security.workspace_access import (
bind_workspace_scope,
@@ -363,6 +363,31 @@ async def test_exact_read_grant_does_not_authorize_a_directory_tree(tmp_path: Pa
assert "needle private" not in result
async def test_search_cannot_expand_exact_file_grant_after_file_becomes_directory(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch,
) -> None:
workspace = tmp_path / "workspace"
workspace.mkdir()
grant = tmp_path / "history.jsonl"
grant.write_text("needle history", encoding="utf-8")
resolve_read = WorkspaceFiles._read_path
def replace_after_grant(files, path, **kwargs):
target = resolve_read(files, path, **kwargs)
if target == grant.resolve() and target.is_file():
target.unlink()
target.mkdir()
(target / "private.txt").write_text("needle private", encoding="utf-8")
return target
monkeypatch.setattr(WorkspaceFiles, "_read_path", replace_after_grant)
tool = GrepTool(workspace=workspace, allowed_dir=workspace, extra_read_allowed_files=[grant])
result = await tool.execute(pattern="needle", path=str(grant))
assert is_tool_error_result(result)
assert "outside allowed directory" in result
assert "needle private" not in result
@pytest.mark.asyncio
async def test_find_files_cancellation_stops_worker_scan(
tmp_path: Path,
@@ -962,7 +987,7 @@ async def test_grep_keeps_read_limit_when_an_open_file_grows(tmp_path, monkeypat
if exceeds_limit:
assert result.startswith("No matches found")
assert "skipped 1 binary/unreadable files" in result
assert "skipped 1 large files" in result
else:
assert result == "growing.log"