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
165 lines
5.9 KiB
Python
165 lines
5.9 KiB
Python
"""Tests for kuri-tool-guard — execute_code AST guard."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import unittest
|
|
|
|
from tests._support import load_mod, patch_alice_identity
|
|
|
|
|
|
class TestExecuteCodeGuard(unittest.TestCase):
|
|
"""Test _guard_execute_code — AST-based home-root + bulk enumeration blocking."""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
self._restore = patch_alice_identity(self.mod)
|
|
|
|
def tearDown(self):
|
|
self._restore()
|
|
|
|
def _call(self, code: str):
|
|
return self.mod._guards._guard_execute_code({"code": code})
|
|
|
|
# ── Block: home-root reference + bulk enumeration ────────────
|
|
|
|
def test_block_os_walk_pwd(self):
|
|
"""KNOWN LIMITATION: dynamic path args (pwd.pw_dir) can't be resolved statically."""
|
|
code = "import os\nimport pwd\nos.walk(pwd.getpwuid(os.geteuid()).pw_dir)"
|
|
result = self._call(code)
|
|
self.assertIsNone(result)
|
|
|
|
def test_block_path_rglob_home(self):
|
|
code = 'from pathlib import Path\nPath("/home/alice").rglob("*")'
|
|
result = self._call(code)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_glob_recursive(self):
|
|
code = 'import glob\nglob.glob("/var/home/alice/**/*", recursive=True)'
|
|
result = self._call(code)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_subprocess_run_rg(self):
|
|
code = 'import subprocess\nsubprocess.run(["rg", "--files", "/home/alice"])'
|
|
result = self._call(code)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_os_system_find(self):
|
|
code = 'import os\nos.system("find /home/alice -type f")'
|
|
result = self._call(code)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_subprocess_popen(self):
|
|
code = 'import subprocess\nsubprocess.Popen(["find", "/home/alice", "-type", "f"])'
|
|
result = self._call(code)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_path_home_rglob(self):
|
|
"""KNOWN LIMITATION: Path.home() dynamic arg can't be resolved statically."""
|
|
code = "from pathlib import Path\nPath.home().rglob('*')"
|
|
result = self._call(code)
|
|
self.assertIsNone(result)
|
|
|
|
# ── Allow: not both A + B ────────────────────────────────────
|
|
|
|
def test_allow_single_file_read(self):
|
|
code = 'from pathlib import Path\nPath("/home/alice/project/file.txt").read_text()'
|
|
result = self._call(code)
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_iterdir_plugins(self):
|
|
code = 'from pathlib import Path\nPath("/var/home/alice/.hermes/plugins").iterdir()'
|
|
result = self._call(code)
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_os_walk_subpath(self):
|
|
code = 'import os\nos.walk("/home/alice/project")'
|
|
result = self._call(code)
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_path_tmp_rglob(self):
|
|
code = 'from pathlib import Path\nPath("/tmp").rglob("*")'
|
|
result = self._call(code)
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_plain_computation(self):
|
|
code = "x = 1 + 1\nprint(x)"
|
|
result = self._call(code)
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_path_home_subpath_only(self):
|
|
"""Path.home() used but no bulk enumeration."""
|
|
code = "from pathlib import Path\np = Path.home() / '.hermes' / 'config.yaml'\nprint(p)"
|
|
result = self._call(code)
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_empty_code(self):
|
|
result = self._call("")
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_syntax_error(self):
|
|
result = self._call("def foo(")
|
|
self.assertIsNone(result)
|
|
|
|
# ── Literal ~ with HOME != passwd_home ────────────────────────
|
|
|
|
def _call_with_home(self, code: str, home: str):
|
|
"""Call _guard_execute_code with $HOME set to *home*."""
|
|
old = os.environ.get("HOME", "")
|
|
os.environ["HOME"] = home
|
|
try:
|
|
return self.mod._guards._guard_execute_code({"code": code})
|
|
finally:
|
|
os.environ["HOME"] = old
|
|
|
|
def test_block_tilde_walk(self):
|
|
"""literal ~ must resolve via passwd home, not $HOME."""
|
|
result = self._call_with_home(
|
|
'import os\nos.walk("~")',
|
|
home="/profiles/alice",
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_tilde_rglob(self):
|
|
result = self._call_with_home(
|
|
'from pathlib import Path\nPath("~").rglob("*")',
|
|
home="/profiles/alice",
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_block_tilde_glob_recursive(self):
|
|
result = self._call_with_home(
|
|
'import glob\nglob.glob("~/**/*", recursive=True)',
|
|
home="/profiles/alice",
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_allow_tilde_subpath_walk(self):
|
|
result = self._call_with_home(
|
|
'import os\nos.walk("~/project")',
|
|
home="/profiles/alice",
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_tilde_subpath_rglob(self):
|
|
result = self._call_with_home(
|
|
'from pathlib import Path\nPath("~/project").rglob("*")',
|
|
home="/profiles/alice",
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_allow_profile_root_walk(self):
|
|
"""$HOME pointing to a profile is NOT protected as Unix home."""
|
|
result = self._call_with_home(
|
|
'import os\nos.walk("/profiles/alice")',
|
|
home="/profiles/alice",
|
|
)
|
|
self.assertIsNone(result)
|