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
221 lines
8.9 KiB
Python
221 lines
8.9 KiB
Python
"""Tests for kuri-tool-guard — home root policy."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from tests._support import load_mod, patch_alice_identity
|
|
|
|
|
|
class TestHomeRootGuard(unittest.TestCase):
|
|
"""Test _guard_home_root and _path_is_protected_home_root."""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
self._restore = patch_alice_identity(self.mod)
|
|
|
|
def tearDown(self):
|
|
self._restore()
|
|
|
|
# ── _path_is_protected_home_root ─────────────────────────────
|
|
|
|
def test_block_tilde(self):
|
|
self.assertTrue(self.mod._state._path_is_protected_home_root("~"))
|
|
|
|
def test_block_tilde_slash(self):
|
|
self.assertTrue(self.mod._state._path_is_protected_home_root("~/"))
|
|
|
|
def test_block_tilde_dot(self):
|
|
"""~/. with normpath becomes home root."""
|
|
self.assertTrue(self.mod._state._path_is_protected_home_root("~/."))
|
|
|
|
# ── Guard 1 behavioral change: no ancestor blocking ──────────
|
|
# New design only protects exact home root aliases (normpath + realpath).
|
|
# /home, /var/home, /var, / are NOT blocked.
|
|
|
|
def test_pass_parent_home(self):
|
|
"""/home is not a protected home root (ancestor, not alias)."""
|
|
self.assertFalse(self.mod._state._path_is_protected_home_root("/home"))
|
|
|
|
def test_pass_parent_var_home(self):
|
|
"""/var/home is not a protected home root."""
|
|
self.assertFalse(self.mod._state._path_is_protected_home_root("/var/home"))
|
|
|
|
def test_pass_root(self):
|
|
"""/ is not a protected home root."""
|
|
self.assertFalse(self.mod._state._path_is_protected_home_root("/"))
|
|
|
|
def test_pass_var(self):
|
|
"""/var is not a protected home root."""
|
|
self.assertFalse(self.mod._state._path_is_protected_home_root("/var"))
|
|
|
|
def test_pass_unrelated_var_something(self):
|
|
"""/var/log is not an ancestor of any home root."""
|
|
self.assertFalse(self.mod._state._path_is_protected_home_root("/var/log"))
|
|
|
|
# ── Subpath passes (behavior preserved) ──────────────────────
|
|
|
|
def test_pass_subpath(self):
|
|
"""A subpath of the home root passes (not an alias)."""
|
|
self.assertFalse(self.mod._state._path_is_protected_home_root("/home/alice/projects"))
|
|
|
|
def test_pass_var_subpath(self):
|
|
self.assertFalse(self.mod._state._path_is_protected_home_root("/var/home/alice/git"))
|
|
|
|
def test_pass_other_path(self):
|
|
self.assertFalse(
|
|
self.mod._state._path_is_protected_home_root(str(Path(tempfile.gettempdir()) / "test"))
|
|
)
|
|
|
|
def test_pass_empty(self):
|
|
self.assertFalse(self.mod._state._path_is_protected_home_root(""))
|
|
|
|
# ── _guard_home_root ─────────────────────────────────────────
|
|
|
|
def test_guard_blocks_tilde(self):
|
|
result = self.mod._guards._guard_home_root({"path": "~"})
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_guard_blocks_home_alice(self):
|
|
result = self.mod._guards._guard_home_root({"path": "/home/alice"})
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_guard_passes_subdir(self):
|
|
result = self.mod._guards._guard_home_root({"path": "/home/alice/projects"})
|
|
self.assertIsNone(result)
|
|
|
|
def test_guard_passes_empty(self):
|
|
result = self.mod._guards._guard_home_root({"path": ""})
|
|
self.assertIsNone(result)
|
|
|
|
def test_guard_passes_non_string(self):
|
|
result = self.mod._guards._guard_home_root({"path": None})
|
|
self.assertIsNone(result)
|
|
|
|
def test_guard_passes_no_path_key(self):
|
|
result = self.mod._guards._guard_home_root({"command": "ls"})
|
|
self.assertIsNone(result)
|
|
|
|
|
|
class TestProtectedRootBuilder(unittest.TestCase):
|
|
"""Test _build_protected_home_roots with injected accounts.
|
|
|
|
All tests run with deterministic usernames and passwd homes,
|
|
independent of the actual Unix user running the test suite.
|
|
"""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
|
|
def _build(self, username: str, passwd_home: str, realpath_fn=None) -> frozenset[str]:
|
|
return self.mod._state._build_protected_home_roots(
|
|
username=username,
|
|
passwd_home=passwd_home,
|
|
realpath_fn=realpath_fn or os.path.realpath,
|
|
)
|
|
|
|
# ── Standard accounts ─────────────────────────────────────────
|
|
|
|
def test_alice_home(self):
|
|
"""alice → /home/alice"""
|
|
roots = self._build("alice", "/home/alice")
|
|
self.assertIn("/home/alice", roots)
|
|
|
|
def test_alice_var_home(self):
|
|
"""alice also has /var/home/alice"""
|
|
roots = self._build("alice", "/home/alice")
|
|
self.assertIn("/var/home/alice", roots)
|
|
|
|
def test_bob_var_home(self):
|
|
"""bob → /var/home/bob"""
|
|
roots = self._build("bob", "/var/home/bob")
|
|
self.assertIn("/var/home/bob", roots)
|
|
|
|
def test_root_root(self):
|
|
"""root → /root"""
|
|
roots = self._build("root", "/root")
|
|
self.assertIn("/root", roots)
|
|
|
|
def test_svc_custom(self):
|
|
"""svc → /srv/accounts/svc"""
|
|
roots = self._build("svc", "/srv/accounts/svc")
|
|
self.assertIn("/srv/accounts/svc", roots)
|
|
|
|
# ── Realpath aliases ──────────────────────────────────────────
|
|
|
|
def test_home_alice_realpath_to_var(self):
|
|
"""/home/alice realpath → /var/home/alice"""
|
|
|
|
def _realpath(p: str) -> str:
|
|
return {
|
|
"HOME": "/home/alice",
|
|
"/home/alice": "/var/home/alice",
|
|
"/var/home/alice": "/var/home/alice",
|
|
"/home": "/home",
|
|
"/": "/",
|
|
}.get(p, p)
|
|
|
|
roots = self._build("alice", "/home/alice", realpath_fn=_realpath)
|
|
self.assertIn("/home/alice", roots)
|
|
self.assertIn("/var/home/alice", roots)
|
|
|
|
# ── Passwd home ≠ conventional home ──────────────────────────
|
|
|
|
def test_passwd_home_differs(self):
|
|
"""passwd_home = /srv/accounts/bob, /home/bob also added"""
|
|
roots = self._build("bob", "/srv/accounts/bob")
|
|
self.assertIn("/srv/accounts/bob", roots)
|
|
self.assertIn("/home/bob", roots)
|
|
self.assertIn("/var/home/bob", roots)
|
|
|
|
# ── Similar names ─────────────────────────────────────────────
|
|
|
|
def test_alice_not_alice2(self):
|
|
"""alice should NOT detect /home/alice2"""
|
|
roots = self._build("alice", "/home/alice")
|
|
self.assertIn("/home/alice", roots)
|
|
self.assertNotIn("/home/alice2", roots)
|
|
|
|
# ── Account lookup failure ────────────────────────────────────
|
|
|
|
def test_no_root_excluded(self):
|
|
"""The root path / must NOT be in protected roots."""
|
|
roots = self._build("alice", "/home/alice")
|
|
self.assertNotIn("/", roots)
|
|
self.assertNotIn("//", roots)
|
|
|
|
def test_get_protected_home_roots_key_error(self):
|
|
"""pwd.getpwuid raises KeyError → empty frozenset, no crash."""
|
|
with patch.object(self.mod._state.pwd, "getpwuid", side_effect=KeyError("no such user")):
|
|
roots = self.mod._state._get_protected_home_roots()
|
|
self.assertEqual(roots, frozenset())
|
|
|
|
def test_get_protected_home_roots_os_error(self):
|
|
"""pwd.getpwuid raises OSError → empty frozenset, no crash."""
|
|
with patch.object(self.mod._state.pwd, "getpwuid", side_effect=OSError("passwd db error")):
|
|
roots = self.mod._state._get_protected_home_roots()
|
|
self.assertEqual(roots, frozenset())
|
|
|
|
def test_get_passwd_home_key_error(self):
|
|
"""_get_passwd_home returns None on KeyError."""
|
|
with patch.object(self.mod._state.pwd, "getpwuid", side_effect=KeyError):
|
|
result = self.mod._state._get_passwd_home()
|
|
self.assertIsNone(result)
|
|
|
|
def test_get_passwd_home_os_error(self):
|
|
"""_get_passwd_home returns None on OSError."""
|
|
with patch.object(self.mod._state.pwd, "getpwuid", side_effect=OSError):
|
|
result = self.mod._state._get_passwd_home()
|
|
self.assertIsNone(result)
|
|
|
|
def test_guard_survives_lookup_failure(self):
|
|
"""_guard_terminal_home_root does not crash when pwd lookup fails."""
|
|
with patch.object(self.mod._state.pwd, "getpwuid", side_effect=KeyError):
|
|
result = self.mod._guards._guard_terminal_home_root({"command": "find ~ -type f"})
|
|
self.assertIsNone(result)
|