Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Skip unreadable files in the grep tool, closes #1419 (#1443)

Paco Cartones16c9fbd18ea2
diff --git a/src/python/txtai/agent/tool/grep.py b/src/python/txtai/agent/tool/grep.py
index 860f6739..d65bcd0d 100644
--- a/src/python/txtai/agent/tool/grep.py
+++ b/src/python/txtai/agent/tool/grep.py
@@ -69,7 +69,7 @@ class GrepTool(Tool):
if matches:
results[path] = matches
- except (UnicodeDecodeError, FileNotFoundError):
+ except (UnicodeDecodeError, FileNotFoundError, PermissionError):
pass
return results
diff --git a/test/python/testagent.py b/test/python/testagent.py
index 3afee7f2..1ee1a701 100644
--- a/test/python/testagent.py
+++ b/test/python/testagent.py
@@ -247,6 +247,29 @@ class TestAgent(unittest.TestCase):
with open(path, "rb") as f:
self.assertEqual(f.read(), b"bye\r\nworld\r\n")
+ def testToolsGrepUnreadable(self):
+ """
+ Test the grep tool skips a file it is not allowed to read
+ """
+
+ with tempfile.TemporaryDirectory() as work:
+ for name in ("a.txt", "b.txt"):
+ with open(os.path.join(work, name), "w", encoding="utf-8") as f:
+ f.write("hello world\n")
+
+ # Matches found in readable files must survive an unreadable file
+ unreadable = os.path.join(work, "b.txt")
+ os.chmod(unreadable, 0o000)
+
+ try:
+ if os.access(unreadable, os.R_OK):
+ self.skipTest("File permissions are not enforced for this user")
+
+ result = ToolFactory.default("grep")("world", work)
+ self.assertEqual(result, {os.path.join(work, "a.txt"): ["hello world\n"]})
+ finally:
+ os.chmod(unreadable, 0o644)
+
def testToolsBashAllowedEmpty(self):
"""
Test an empty allowed commands list permits no commands