Skip to content

Commit e7b0650

Browse files
antsokclaude
andcommitted
Python: Do not let filesystem grep read through a swapped symlink
From Copilot review 5111742587. _search_files_sync read each candidate with entry.read_bytes(), which reopens by path after _enumerate_search_files has already screened it for links. A candidate replaced with a symlink in between was followed, so grep could return content from outside the store root -- and it bypassed the O_NOFOLLOW reader the store's own read() goes through. The read now uses that reader, and re-screens the entry immediately before it: O_NOFOLLOW closes the window atomically where the platform has it, and the re-screen narrows it on Windows, where os.O_NOFOLLOW does not exist and degrades to 0. The race is narrowed rather than eliminated there, which is worth knowing rather than glossing. The handler had to widen with it. _read_file_sync reports non-UTF-8 content and a symlinked path as ValueError and a vanished file as OSError, none of which the existing except UnicodeDecodeError caught, so a single bad entry would have aborted the whole search instead of being skipped. Two log assertions move with the reworded message. Two dead helpers also go: _SkewedStore and _MemorySkewedStore were left behind when the alignment tests were removed, referenced by nothing. Ruff could not see them -- an unused module-level class is not a lint error -- but removing _MemorySkewedStore made ruff flag the FileSearchMatch and FileSearchResult imports it had been keeping alive, which is how the review described it. The symlink test stages both halves of the race rather than creating a link, because a real symlink needs privileges this suite cannot rely on: it skipped here, which is exactly how it would have passed while proving nothing. Staged, it runs everywhere and returns the outside content without the fix. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 6dfea3c commit e7b0650

3 files changed

Lines changed: 47 additions & 59 deletions

File tree

‎python/packages/core/agent_framework/_harness/_file_access.py‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,23 +1363,33 @@ def _search_files_sync(
13631363
if not _matches_glob(relative_name, glob_pattern):
13641364
continue
13651365
try:
1366-
# Decode the bytes rather than using read_text: text mode applies universal
1367-
# newlines, which would rewrite "\r\n" and a lone "\r" to "\n" and leave grep
1368-
# describing content that read() -- and therefore replace_lines -- never sees.
1369-
file_content = entry.read_bytes().decode("utf-8")
1370-
except UnicodeDecodeError:
1371-
# Skip binary or otherwise non-UTF-8 files so a single
1372-
# un-decodable entry doesn't abort the whole directory search.
1373-
# Log per file so operators can audit which files were skipped.
1374-
logger.warning("Skipping non-UTF-8 file during search: %s", entry)
1366+
# Re-checked here, not only during enumeration: a candidate can be swapped for a
1367+
# link in between. O_NOFOLLOW makes the read itself atomic where the platform has
1368+
# it, and this narrows the window on Windows, where it does not exist.
1369+
if is_link_or_reparse_point(entry):
1370+
logger.warning("Skipping symlinked file during search: %s", entry)
1371+
skipped.append(relative_name)
1372+
continue
1373+
# Read through the same no-follow reader the store's own read() uses; reopening
1374+
# by path would follow a link and return content from outside the root.
1375+
content_or_none = FileSystemAgentFileStore._read_file_sync(entry)
1376+
if content_or_none is None:
1377+
continue
1378+
file_content = content_or_none
1379+
except (OSError, ValueError):
1380+
# _read_file_sync raises ValueError for non-UTF-8 content and for a path that
1381+
# became a symlink since enumeration, and OSError if it vanished. Skip the file
1382+
# either way, so one bad entry cannot abort the whole directory search, and log
1383+
# per file so operators can audit what was left out.
1384+
logger.warning("Skipping unreadable file during search: %s", entry)
13751385
skipped.append(relative_name)
13761386
continue
13771387
result = AgentFileStore.scan_content(relative_name, file_content, regex)
13781388
if result is not None:
13791389
results.append(result)
13801390
if skipped:
13811391
logger.info(
1382-
"Search under %s skipped %d non-UTF-8 file(s) (matched %d).",
1392+
"Search under %s skipped %d unreadable file(s) (matched %d).",
13831393
full_dir,
13841394
len(skipped),
13851395
len(results),

‎python/packages/core/tests/core/test_harness_file_access.py‎

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,31 @@ async def test_filesystem_store_rejects_symlinks_into_root(tmp_path: Path) -> No
318318
assert await _list_files(store) == []
319319

320320

321+
async def test_filesystem_search_does_not_read_through_a_symlink(
322+
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
323+
) -> None:
324+
"""A candidate that became a link after enumeration must be skipped, not read."""
325+
root = tmp_path / "root"
326+
root.mkdir()
327+
swapped = root / "notes.txt"
328+
swapped.write_text("needle\n", encoding="utf-8")
329+
330+
# Enumeration screens links itself, so both halves of the race are staged: the candidate is
331+
# handed over as if it had passed that screen, and reports as a link by the time it is read.
332+
# Staged rather than created, because a real symlink needs privileges this suite cannot rely on.
333+
monkeypatch.setattr(
334+
FileSystemAgentFileStore,
335+
"_enumerate_search_files",
336+
staticmethod(lambda full_dir, recursive: [("notes.txt", swapped)]),
337+
)
338+
monkeypatch.setattr(_file_access_module, "is_link_or_reparse_point", lambda candidate: candidate == swapped)
339+
340+
store = FileSystemAgentFileStore(root)
341+
results = await store.search("", "needle", recursive=True)
342+
343+
assert results == []
344+
345+
321346
async def test_filesystem_store_rejects_in_root_symlinks(tmp_path: Path) -> None:
322347
"""Symlinks whose target lives under the root must still be rejected.
323348
@@ -955,8 +980,8 @@ async def test_filesystem_store_search_logs_skipped_non_utf8_files(
955980
results = await store.search("", "error")
956981

957982
assert [r.file_name for r in results] == ["notes.md"]
958-
assert any("Skipping non-UTF-8 file during search" in rec.message for rec in caplog.records)
959-
assert any("skipped 1 non-UTF-8 file" in rec.message for rec in caplog.records)
983+
assert any("Skipping unreadable file during search" in rec.message for rec in caplog.records)
984+
assert any("skipped 1 unreadable file" in rec.message for rec in caplog.records)
960985

961986

962987
async def test_file_access_tool_wrappers_surface_value_error_as_message(
@@ -1513,30 +1538,6 @@ async def create_directory(self, path: str) -> None:
15131538
return None
15141539

15151540

1516-
class _SkewedStore(_ContentOnlyStore):
1517-
"""Overrides ``search`` and numbers lines with ``splitlines``, which disagrees."""
1518-
1519-
async def search(
1520-
self,
1521-
directory: str,
1522-
regex_pattern: str,
1523-
glob_pattern: str | None = None,
1524-
*,
1525-
recursive: bool = False,
1526-
) -> list[FileSearchResult]:
1527-
regex = re.compile(regex_pattern, re.IGNORECASE)
1528-
results: list[FileSearchResult] = []
1529-
for name, content in self.files.items():
1530-
matches = [
1531-
FileSearchMatch(line_number=number, line=line)
1532-
for number, line in enumerate(content.splitlines(keepends=True), start=1)
1533-
if regex.search(line.rstrip("\r\n"))
1534-
]
1535-
if matches:
1536-
results.append(FileSearchResult(file_name=name, snippet="", matching_lines=matches))
1537-
return results
1538-
1539-
15401541
class _AlignedOverrideStore(_ContentOnlyStore):
15411542
"""Overrides ``search`` but numbers through the published primitive."""
15421543

‎python/packages/core/tests/core/test_harness_file_memory.py‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
AgentSession,
1313
Content,
1414
FileMemoryProvider,
15-
FileSearchMatch,
16-
FileSearchResult,
1715
FunctionTool,
1816
InMemoryAgentFileStore,
1917
)
@@ -495,27 +493,6 @@ async def _memory_tools(store: AgentFileStore, **kwargs: object) -> list[object]
495493
return list(context.tools)
496494

497495

498-
class _MemorySkewedStore(InMemoryAgentFileStore):
499-
"""Reports a line number one past the truth, the way a mis-splitting store would."""
500-
501-
async def search(
502-
self,
503-
directory: str,
504-
regex_pattern: str,
505-
glob_pattern: str | None = None,
506-
*,
507-
recursive: bool = False,
508-
) -> list[FileSearchResult]:
509-
del directory, glob_pattern, recursive
510-
return [
511-
FileSearchResult(
512-
file_name="notes.md",
513-
snippet="",
514-
matching_lines=[FileSearchMatch(line_number=1, line=regex_pattern)],
515-
)
516-
]
517-
518-
519496
async def test_file_memory_replace_lines_honours_expected_line() -> None:
520497
"""The write guard must work in the memory provider too, where nothing asks for approval."""
521498
store = InMemoryAgentFileStore()

0 commit comments

Comments
 (0)