Skip to content

Commit 0b0bfe8

Browse files
committed
fix: keep grepping when an entry cannot be statted
_classify_walk_entry called Path.is_file() unguarded. That swallows ENOENT, ENOTDIR, EBADF and ELOOP, but raises on EACCES -- so a stat failure escaped the classifier and propagated out of grep_content, discarding every match already found elsewhere in the tree. The reachable case is a directory that is readable but not searchable (mode 0o444): os.walk lists its children fine, then stat on each child is denied. The suite's existing permission tests use mode 0o000, where os.walk itself fails and the error is already handled by the walk_errors path, so they could not catch this. Classify any OSError from those stats as UNREADABLE instead, matching the Go runtime, whose classifyWalkEntry already maps every EvalSymlinks/Stat failure to walkEntryUnreadable. The caller counts UNREADABLE and keeps walking. Also corrects the docstring, which claimed this divergence was "not reachable through any case we could construct". EACCES reaches it trivially. The remaining divergence is narrower and is now described accurately. Reported by EItanya in review of #2267. Signed-off-by: brandonkeung <[email protected]>
1 parent b4f76af commit 0b0bfe8

2 files changed

Lines changed: 91 additions & 18 deletions

File tree

‎python/packages/kagent-skills/src/kagent/skills/shell.py‎

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,44 @@ def _classify_walk_entry(root: Path, entry: Path) -> tuple[_WalkEntryAction, Pat
5656
"exists, resolves, and is a regular file" into one call. The outcomes
5757
agree; the number of branches reaching them does not.
5858
59-
One known divergence, not reachable through any case we could construct:
60-
Go counts a failed Stat on a non-symlink as UNREADABLE, while
61-
Path.is_file() swallows the OSError and lands here on SKIP. Reaching it
62-
needs a stat that fails on an entry os.walk just listed -- EIO, a stale
63-
NFS handle, a misbehaving FUSE mount. Symlink loops and broken links are
64-
unaffected; both are UNREADABLE in either runtime.
59+
One divergence remains, and it is narrower than a stat failure in general:
60+
Path.is_file() swallows ENOENT, ENOTDIR, EBADF and ELOOP, so on a
61+
non-symlink those return False and land on SKIP where Go's failed Stat
62+
gives UNREADABLE. Reaching it needs the entry to vanish between os.walk
63+
listing it and the stat below. EACCES is *not* in that set -- it raises,
64+
and is caught below as UNREADABLE, so the two runtimes agree there.
65+
Symlink loops and broken links are unaffected; both are UNREADABLE in
66+
either runtime.
6567
6668
Returns the resolved path alongside GREP so the caller reads what was
6769
just checked rather than re-resolving the symlink separately.
6870
"""
69-
# is_file() follows symlinks and checks S_ISREG, so a False covers two
70-
# cases that deserve different treatment.
71-
if not entry.is_file():
72-
# A broken symlink (or a symlink loop) is a genuine read failure.
73-
# Counting it is what stops a tree of dangling links from reporting a
74-
# confidently empty "no matches found".
75-
if entry.is_symlink() and not entry.exists():
76-
return _WalkEntryAction.UNREADABLE, None
77-
# A FIFO/socket/device is excluded by policy, not failure: opening one
78-
# can block indefinitely, and grep has no business reading it.
79-
return _WalkEntryAction.SKIP, None
71+
try:
72+
# is_file() follows symlinks and checks S_ISREG, so a False covers two
73+
# cases that deserve different treatment.
74+
if not entry.is_file():
75+
# A broken symlink (or a symlink loop) is a genuine read failure.
76+
# Counting it is what stops a tree of dangling links from reporting
77+
# a confidently empty "no matches found".
78+
if entry.is_symlink() and not entry.exists():
79+
return _WalkEntryAction.UNREADABLE, None
80+
# A FIFO/socket/device is excluded by policy, not failure: opening
81+
# one can block indefinitely, and grep has no business reading it.
82+
return _WalkEntryAction.SKIP, None
83+
84+
resolved = entry.resolve()
85+
except OSError:
86+
# Every stat above is on an entry os.walk has already listed, so a
87+
# raise here means the entry became unreadable in between -- most often
88+
# because its directory is readable but not searchable (mode 0o444),
89+
# which lists names while denying stat on the children.
90+
#
91+
# This must be an outcome rather than an exception: the caller counts
92+
# UNREADABLE and keeps walking, so letting it propagate would discard
93+
# every match already found elsewhere in the tree. Go reaches the same
94+
# answer by mapping each EvalSymlinks/Stat failure to unreadable.
95+
return _WalkEntryAction.UNREADABLE, None
8096

81-
resolved = entry.resolve()
8297
# Bound each entry by the directory actually being searched. The caller's
8398
# allowed_root is the whole session plus the skills dir, so it alone would
8499
# let a symlink here pull in a file from a sibling directory nobody asked

‎python/packages/kagent-skills/src/kagent/tests/unittests/test_skill_execution.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,34 @@ def test_classify_walk_entry_covers_each_outcome(tmp_path):
503503
assert _classify_walk_entry(root, loop) == (_WalkEntryAction.UNREADABLE, None)
504504

505505

506+
def test_classify_walk_entry_counts_a_permission_error_as_unreadable(tmp_path):
507+
"""A stat that fails with EACCES is a read failure, not a policy skip.
508+
509+
Path.is_file() swallows ENOENT/ENOTDIR/EBADF/ELOOP but *raises* on EACCES,
510+
so without an explicit guard this escapes the classifier entirely rather
511+
than returning an outcome. Go's classifyWalkEntry maps every EvalSymlinks
512+
or Stat failure to walkEntryUnreadable; this pins Python to the same answer.
513+
514+
Mode 0o444 is the case that matters: dropping only the execute bit leaves
515+
the directory listable, so os.walk succeeds and hands us an entry we then
516+
cannot stat. The suite's other permission tests use 0o000, where os.walk
517+
itself fails and the error is caught by the walk_errors path instead.
518+
"""
519+
if os.geteuid() == 0:
520+
pytest.skip("root bypasses directory permissions; cannot exercise this case")
521+
522+
locked_dir = tmp_path / "locked"
523+
locked_dir.mkdir()
524+
entry = locked_dir / "unreachable.txt"
525+
entry.write_text("foo\n")
526+
locked_dir.chmod(0o444)
527+
528+
try:
529+
assert _classify_walk_entry(tmp_path, entry) == (_WalkEntryAction.UNREADABLE, None)
530+
finally:
531+
locked_dir.chmod(0o755)
532+
533+
506534
def test_classify_walk_entry_treats_a_fifo_as_a_silent_skip(tmp_path):
507535
"""A FIFO is excluded by policy, not failure, so it must not be counted."""
508536
fifo_path = tmp_path / "pipe"
@@ -618,6 +646,36 @@ def test_grep_content_recursive_does_not_abort_or_discard_matches_on_an_unreadab
618646
assert "match.txt:1:foo readable" in result
619647

620648

649+
def test_grep_content_keeps_sibling_matches_when_an_entry_cannot_be_statted(tmp_path):
650+
"""An unstattable entry must be counted, not allowed to abort the search.
651+
652+
This is the 0o444 sibling of the 0o000 test above, and it reaches a
653+
different code path: os.walk *succeeds* here and lists the entry, so the
654+
failure surfaces from the classifier's stat rather than from walk_errors.
655+
Letting it propagate discards matches already found elsewhere in the tree.
656+
"""
657+
if os.geteuid() == 0:
658+
pytest.skip("root bypasses directory permissions; cannot exercise this case")
659+
660+
ok_sub = tmp_path / "aaa_ok"
661+
ok_sub.mkdir()
662+
(ok_sub / "match.txt").write_text("foo readable\n")
663+
664+
# Readable but not searchable: listable, but its children cannot be stat'ed.
665+
locked_sub = tmp_path / "mmm_locked"
666+
locked_sub.mkdir()
667+
(locked_sub / "hidden.txt").write_text("foo hidden\n")
668+
locked_sub.chmod(0o444)
669+
670+
try:
671+
result = grep_content(tmp_path, "foo", recursive=True, allowed_root=tmp_path)
672+
finally:
673+
locked_sub.chmod(0o755)
674+
675+
assert "match.txt:1:foo readable" in result
676+
assert "could not be read" in result
677+
678+
621679
def test_grep_content_annotates_skip_count_alongside_real_matches(tmp_path):
622680
"""A skip count found alongside real matches must not be silently dropped."""
623681
if os.geteuid() == 0:

0 commit comments

Comments
 (0)