Split the 1341-line __init__.py into 5 focused modules: _state.py - home root helpers, mutable poll state _parser.py - shell tokenizer (independent) _predicates.py - terminal predicates + bounded output _guards.py - all 9 guard implementations + AST helpers __init__.py - thin entry point + dispatcher + re-exports Split the 2226-line test_guard.py into 14 dedicated files: 13 test_*.py files + tests/_support.py (shared helpers) Key changes: - Package-relative imports throughout (Hermes loader compatible) - No top-level module pollution (hermes_plugins.kuri_tool_guard.* only) - Typechecked via ty with symlink alias for hyphenated dir name - All 10 guard policies, messages, and block/allow behaviour preserved - 338 tests (336 original + 2 new: isolation + loader contract) - ty, ruff, bandit, vulture all pass in just verify
194 lines
6.8 KiB
Python
194 lines
6.8 KiB
Python
"""Tests for kuri-tool-guard — terminal home-root operand guard."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from tests._support import load_mod, patch_alice_identity
|
|
|
|
|
|
class TestTerminalHomeRootGuard(unittest.TestCase):
|
|
"""Test _guard_terminal_home_root — home-root operand blocking."""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
self._restore = patch_alice_identity(self.mod)
|
|
|
|
def tearDown(self):
|
|
self._restore()
|
|
|
|
def _call(self, command: str):
|
|
return self.mod._guards._guard_terminal_home_root({"command": command})
|
|
|
|
# ── Block: scanning tools with home root operand ─────────────
|
|
|
|
def test_block_find_tilde(self):
|
|
result = self._call("find ~ -name x")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_rg_tilde(self):
|
|
result = self._call("rg pattern ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_rg_files_tilde(self):
|
|
result = self._call("rg --files ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_grep_recursive_tilde(self):
|
|
result = self._call("grep -R pattern ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_fd_tilde(self):
|
|
result = self._call("fd pattern ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_tree_tilde(self):
|
|
result = self._call("tree ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_ls_recursive_tilde(self):
|
|
result = self._call("ls -R ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_du_sh_tilde(self):
|
|
result = self._call("du -sh ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_tar_home(self):
|
|
result = self._call("tar -cf /tmp/home.tar ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_cp_a_home(self):
|
|
result = self._call("cp -a ~ /tmp/copy")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_rsync_home(self):
|
|
result = self._call("rsync -a ~ /tmp/copy")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_echo_glob(self):
|
|
result = self._call("echo ~/*")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_printf_glob(self):
|
|
result = self._call("printf '%s\\n' ~/*")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_eza_recursive(self):
|
|
result = self._call("eza -R ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_exa_recursive(self):
|
|
result = self._call("exa -R ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_env_wrapper_home(self):
|
|
result = self._call("env LC_ALL=C rg pattern ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_command_wrapper_find(self):
|
|
result = self._call("command find /var/home/alice -name x")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_time_wrapper_tree(self):
|
|
result = self._call("time tree ~")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_nice_wrapper_find(self):
|
|
result = self._call("nice -n 10 find /home/alice -name x")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
# ── Block: locate family ─────────────────────────────────────
|
|
|
|
def test_block_locate(self):
|
|
result = self._call("locate somefile")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("locate", result["message"])
|
|
|
|
def test_block_plocate(self):
|
|
result = self._call("plocate somefile")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_mlocate(self):
|
|
result = self._call("mlocate somefile")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_locate_with_env(self):
|
|
result = self._call("env -u PATH locate somefile")
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
# ── Allow: subpath or unrelated ──────────────────────────────
|
|
|
|
def test_allow_find_subpath(self):
|
|
result = self._call("find ~/.hermes -name x")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_rg_subpath(self):
|
|
result = self._call("rg pattern ~/.hermes/plugins")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_find_dot_after_cd(self):
|
|
"""find . after cd to subpath should pass (relative path)."""
|
|
result = self._call("cd ~/.hermes && find . -type f")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_git_c_subpath(self):
|
|
result = self._call("git -C ~/.hermes/plugins/repo status")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_find_other_user(self):
|
|
"""Different username should not be blocked."""
|
|
result = self._call("find /home/otheruser -type f")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_find_root(self):
|
|
"""/ itself is not a protected home root."""
|
|
result = self._call("find / -name x")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_find_home_ancestor(self):
|
|
"""/home is not a protected home root (ancestor, not alias)."""
|
|
result = self._call("find /home -type d")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_find_var_home_ancestor(self):
|
|
"""/var/home is not a protected home root."""
|
|
result = self._call("find /var/home -type d")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_echo_quoted_gt(self):
|
|
"""echo with > inside quotes should not be flagged."""
|
|
result = self._call("echo 'price > 100'")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_single_file_read(self):
|
|
"""cat with pipe should pass terminal home-root guard."""
|
|
result = self._call("cat ~/.hermes/config.yaml | head")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_empty_command(self):
|
|
result = self._call("")
|
|
self.assertIsNone(result)
|