412 lines
18 KiB
Python
412 lines
18 KiB
Python
"""Tests for kuri-tool-guard — skill directory lockdown guard."""
|
|
|
|
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
|
|
|
|
_ALICE_SKILL = "/home/alice/.hermes/skills/foo/SKILL.md"
|
|
|
|
|
|
class TestSkillDirGuard(unittest.TestCase):
|
|
"""Test _guard_skill_dir — blocks patch/write_file on Hermes skill roots."""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
self._restore = patch_alice_identity(self.mod)
|
|
# Pin canonical resolver unavailable so base tests exercise the fallback
|
|
self._canonical = patch.object(self.mod._state, "_canonical_hermes_home", None)
|
|
self._canonical.start()
|
|
# Pin Path.home to the mocked identity (fallback default home)
|
|
self._home = patch.object(Path, "home", return_value=Path("/home/alice"))
|
|
self._home.start()
|
|
# Clear HERMES_HOME so base tests use the mocked home
|
|
self._env = patch.dict(os.environ, {"HERMES_HOME": ""})
|
|
self._env.start()
|
|
|
|
def tearDown(self):
|
|
self._env.stop()
|
|
self._home.stop()
|
|
self._canonical.stop()
|
|
self._restore()
|
|
|
|
# ── Default home (~/.hermes/skills, mocked as /home/alice) ─────
|
|
|
|
def assert_block(self, tool, path):
|
|
result = self.mod._guards._guard_skill_dir(tool, {"path": path})
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
msg = result["message"]
|
|
self.assertIn("Hermes skill 目錄", msg)
|
|
self.assertIn("不允許用", msg)
|
|
self.assertIn("skill_manage", msg)
|
|
self.assertIn("action", msg)
|
|
self.assertIn("file_path", msg)
|
|
return result
|
|
|
|
def test_blocks_patch_skill_sk(self):
|
|
self.assert_block("patch", _ALICE_SKILL)
|
|
|
|
def test_blocks_patch_skill_reference(self):
|
|
self.assert_block("patch", "/home/alice/.hermes/skills/foo/references/bar.md")
|
|
|
|
def test_blocks_write_file_new_skill_file(self):
|
|
self.assert_block("write_file", "/home/alice/.hermes/skills/foo/new.md")
|
|
|
|
def test_blocks_skill_root_exact(self):
|
|
self.assert_block("patch", "/home/alice/.hermes/skills")
|
|
|
|
# ── Passes (non-skill paths) ──────────────────────────────────
|
|
|
|
def test_passes_non_skill_path(self):
|
|
result = self.mod._guards._guard_skill_dir("patch", {"path": "/tmp/project/main.go"})
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_git_repo(self):
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"patch", {"path": "/home/alice/git/searxng-mcp-go/main.go"}
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_memory_path(self):
|
|
"""memories/ is not a skill root — the memory guard handles it."""
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"patch", {"path": "/home/alice/.hermes/memories/MEMORY.md"}
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_hermes_other_dir(self):
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"write_file", {"path": "/home/alice/.hermes/config.yaml"}
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_empty_path(self):
|
|
result = self.mod._guards._guard_skill_dir("patch", {"path": ""})
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_non_string_path(self):
|
|
result = self.mod._guards._guard_skill_dir("patch", {"path": 123})
|
|
self.assertIsNone(result)
|
|
|
|
# ── Unresolvable tokens (conservative block) ──────────────────
|
|
|
|
def test_unresolved_var_token_is_blocked(self):
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"patch", {"path": "$UNKNOWN_VAR/skills/foo/SKILL.md"}
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("無法解析", result["message"])
|
|
self.assertIn("skill_manage", result["message"])
|
|
self.assertIn("file_path", result["message"])
|
|
|
|
def test_unresolved_windows_var_token_is_blocked(self):
|
|
"""Unknown %VAR% (Windows semantics) is conservatively blocked."""
|
|
with patch.dict(os.environ, {}, clear=True):
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"patch", {"path": "%UNKNOWN_VAR%/skills/foo/SKILL.md"}
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("無法解析", result["message"])
|
|
|
|
def test_literal_token_characters_are_conservatively_blocked(self):
|
|
"""Literal $, ~ or % in a path is blocked (documented tradeoff)."""
|
|
for path in (
|
|
"/home/alice/.hermes/skills/foo/$file.md",
|
|
"/home/alice/.hermes/skills/foo/~file.md",
|
|
"/home/alice/.hermes/skills/foo/%file.md",
|
|
):
|
|
result = self.mod._guards._guard_skill_dir("patch", {"path": path})
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_unresolved_user_tilde_is_blocked(self):
|
|
with patch.object(os.path, "expanduser", side_effect=lambda p: p):
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"patch", {"path": "~nosuchuser/skills/foo/SKILL.md"}
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("無法解析", result["message"])
|
|
self.assertIn("skill_manage", result["message"])
|
|
|
|
def test_home_unresolvable_fails_closed(self):
|
|
with patch.object(self.mod._state, "_get_hermes_home", return_value=None):
|
|
result = self.mod._guards._guard_skill_dir("patch", {"path": "/tmp/whatever/file.go"})
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("無法解析 Hermes home", result["message"])
|
|
self.assertIn("skill_manage", result["message"])
|
|
self.assertIn("file_path", result["message"])
|
|
|
|
def test_canonical_resolver_exception_fails_closed(self):
|
|
"""A RuntimeError from the canonical resolver still fails closed."""
|
|
|
|
def _boom():
|
|
raise RuntimeError("home unresolvable")
|
|
|
|
with patch.object(self.mod._state, "_canonical_hermes_home", _boom):
|
|
result = self.mod._guards._guard_skill_dir("patch", {"path": "/tmp/whatever/file.go"})
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("無法解析 Hermes home", result["message"])
|
|
|
|
# ── HERMES_HOME env (standalone fallback) ─────────────────────
|
|
|
|
def test_hermes_home_env_root(self):
|
|
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {"HERMES_HOME": tmp}):
|
|
Path(tmp, "skills").mkdir()
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"patch", {"path": f"{tmp}/skills/foo/SKILL.md"}
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_hermes_home_env_profile_skills(self):
|
|
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {"HERMES_HOME": tmp}):
|
|
profile_skills = Path(tmp, "profiles", "work", "skills")
|
|
profile_skills.mkdir(parents=True)
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"write_file", {"path": f"{profile_skills}/foo/SKILL.md"}
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_hermes_home_env_other_path_passes(self):
|
|
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {"HERMES_HOME": tmp}):
|
|
result = self.mod._guards._guard_skill_dir("patch", {"path": "/tmp/outside/file.go"})
|
|
self.assertIsNone(result)
|
|
|
|
def test_hermes_home_env_var_expansion(self):
|
|
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {"HERMES_HOME": tmp}):
|
|
Path(tmp, "skills").mkdir()
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"patch", {"path": "$HERMES_HOME/skills/foo/SKILL.md"}
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
# ── Dispatcher integration ────────────────────────────────────
|
|
|
|
def test_dispatcher_patch_skill_blocked(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="patch",
|
|
args={"mode": "replace", "path": _ALICE_SKILL},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_dispatcher_write_file_new_skill_blocked(self):
|
|
"""New file under skills root is blocked by the skill guard."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="write_file", args={"path": "/home/alice/.hermes/skills/foo/new.md"}
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_dispatcher_write_file_existing_skill_gets_skill_manage_message(self):
|
|
"""Existing file under skills root: Guard 11 message wins over Guard 3."""
|
|
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {"HERMES_HOME": tmp}):
|
|
skill = Path(tmp, "skills", "foo", "SKILL.md")
|
|
skill.parent.mkdir(parents=True)
|
|
skill.write_text("content")
|
|
result = self.mod._on_pre_tool_call(tool_name="write_file", args={"path": str(skill)})
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("skill_manage", result["message"])
|
|
|
|
def test_dispatcher_patch_non_skill_passes(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="patch",
|
|
args={"mode": "replace", "path": "/home/alice/git/searxng-mcp-go/main.go"},
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_dispatcher_skill_manage_allows_protected_path(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="skill_manage",
|
|
args={"action": "patch", "name": "foo", "path": _ALICE_SKILL},
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_dispatcher_read_file_allows_protected_path(self):
|
|
result = self.mod._on_pre_tool_call(tool_name="read_file", args={"path": _ALICE_SKILL})
|
|
self.assertIsNone(result)
|
|
|
|
def test_dispatcher_search_files_allows_protected_path(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="search_files", args={"path": "/home/alice/.hermes/skills/foo"}
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_skill_block_message_contract(self):
|
|
for tool in ("patch", "write_file"):
|
|
result = self.mod._guards._guard_skill_dir(tool, {"path": _ALICE_SKILL})
|
|
self.assertIsNotNone(result)
|
|
msg = result["message"]
|
|
self.assertIn("skill", msg)
|
|
self.assertIn("skill_manage", msg)
|
|
|
|
|
|
class TestSkillDirMatrix(unittest.TestCase):
|
|
"""Path coverage matrix: Unix layouts, boundaries, symlinks, resolution."""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
self._restore = patch_alice_identity(self.mod)
|
|
self._canonical = patch.object(self.mod._state, "_canonical_hermes_home", None)
|
|
self._canonical.start()
|
|
self._home = patch.object(Path, "home", return_value=Path("/home/alice"))
|
|
self._home.start()
|
|
self._env = patch.dict(os.environ, {"HERMES_HOME": ""})
|
|
self._env.start()
|
|
|
|
def tearDown(self):
|
|
self._env.stop()
|
|
self._home.stop()
|
|
self._canonical.stop()
|
|
self._restore()
|
|
|
|
def _guard(self, tool, path):
|
|
return self.mod._guards._guard_skill_dir(tool, {"path": path})
|
|
|
|
def _patch_home(self, home):
|
|
return patch.object(Path, "home", return_value=Path(home))
|
|
|
|
# ── Canonical resolution: override precedes env ───────────────
|
|
|
|
def test_context_override_precedes_env(self):
|
|
with (
|
|
patch.dict(os.environ, {"HERMES_HOME": "/tmp/guard11-env"}),
|
|
patch.object(
|
|
self.mod._state,
|
|
"_canonical_hermes_home",
|
|
return_value=Path("/tmp/guard11-override"),
|
|
),
|
|
):
|
|
self.assertIsNotNone(self._guard("patch", "/tmp/guard11-override/skills/foo/SKILL.md"))
|
|
self.assertIsNone(self._guard("patch", "/tmp/guard11-env/skills/foo/SKILL.md"))
|
|
|
|
def test_canonical_resolver_used_when_available(self):
|
|
"""When the canonical resolver is importable, roots come from it."""
|
|
with patch.object(
|
|
self.mod._state,
|
|
"_canonical_hermes_home",
|
|
return_value=Path("/opt/hermes"),
|
|
):
|
|
self.assertIsNotNone(self._guard("patch", "/opt/hermes/skills/foo/SKILL.md"))
|
|
self.assertIsNone(self._guard("patch", "/home/alice/.hermes/skills/foo/SKILL.md"))
|
|
|
|
def test_custom_home_does_not_protect_other_home(self):
|
|
with tempfile.TemporaryDirectory() as tmp_a, tempfile.TemporaryDirectory() as tmp_b:
|
|
Path(tmp_a, "skills").mkdir()
|
|
with patch.dict(os.environ, {"HERMES_HOME": tmp_a}):
|
|
self.assertIsNotNone(self._guard("patch", f"{tmp_a}/skills/foo/SKILL.md"))
|
|
self.assertIsNone(self._guard("patch", f"{tmp_b}/skills/foo/SKILL.md"))
|
|
|
|
# ── Different Unix home layouts ───────────────────────────────
|
|
|
|
def test_root_default_home_skill_is_blocked(self):
|
|
with self._patch_home("/root"):
|
|
self.assertIsNotNone(self._guard("patch", "/root/.hermes/skills/foo/SKILL.md"))
|
|
|
|
def test_var_home_default_skill_is_blocked(self):
|
|
with self._patch_home("/var/home/alice"):
|
|
self.assertIsNotNone(
|
|
self._guard("patch", "/var/home/alice/.hermes/skills/foo/SKILL.md")
|
|
)
|
|
|
|
def test_var_roothome_default_skill_is_blocked(self):
|
|
with self._patch_home("/var/roothome"):
|
|
self.assertIsNotNone(self._guard("patch", "/var/roothome/.hermes/skills/foo/SKILL.md"))
|
|
|
|
# ── Allowed roots and parents ─────────────────────────────────
|
|
|
|
def test_home_roots_and_parent_are_allowed(self):
|
|
for path in ("/home", "/var/home", "/home/alice/.hermes", "/home/alice/other"):
|
|
self.assertIsNone(self._guard("patch", path), f"expected allow: {path}")
|
|
|
|
def test_skill_sibling_prefixes_are_allowed(self):
|
|
for path in (
|
|
"/home/alice/.hermes/skills-other/foo/SKILL.md",
|
|
"/home/alice/.hermes/my-skills/foo/SKILL.md",
|
|
):
|
|
self.assertIsNone(self._guard("patch", path), f"expected allow: {path}")
|
|
|
|
# ── Boundary: dotdot, relative, trailing slash, case ──────────
|
|
|
|
def test_dotdot_path_uses_normalized_boundary(self):
|
|
self.assertIsNone(self._guard("patch", "/home/alice/.hermes/skills/../secrets/file"))
|
|
self.assertIsNotNone(
|
|
self._guard("patch", "/home/alice/.hermes/skills/../skills/foo/SKILL.md")
|
|
)
|
|
|
|
def test_relative_skill_path_is_blocked(self):
|
|
with (
|
|
tempfile.TemporaryDirectory() as tmp,
|
|
patch.dict(os.environ, {"HERMES_HOME": tmp}),
|
|
patch.object(os, "getcwd", return_value=tmp),
|
|
):
|
|
Path(tmp, "skills").mkdir()
|
|
self.assertIsNotNone(self._guard("patch", "skills/foo/SKILL.md"))
|
|
|
|
def test_tilde_skill_path_uses_test_identity(self):
|
|
with patch.dict(os.environ, {"HOME": "/home/alice"}):
|
|
self.assertIsNotNone(self._guard("patch", "~/.hermes/skills/foo/SKILL.md"))
|
|
|
|
def test_skill_root_trailing_slash_and_case_boundary(self):
|
|
self.assertIsNotNone(self._guard("patch", "/home/alice/.hermes/skills/"))
|
|
if os.path.normcase("Skills") == "skills":
|
|
# Case-insensitive platform (Windows): same root, must block
|
|
self.assertIsNotNone(self._guard("patch", "/home/alice/.hermes/Skills/foo/SKILL.md"))
|
|
else:
|
|
self.assertIsNone(self._guard("patch", "/home/alice/.hermes/Skills/foo/SKILL.md"))
|
|
|
|
def test_profile_skills_lexical_pattern_blocks_nonexistent_profile(self):
|
|
"""profiles/<name>/skills is protected even before the profile exists."""
|
|
with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {"HERMES_HOME": tmp}):
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"write_file", {"path": f"{tmp}/profiles/newproj/skills/foo/SKILL.md"}
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_profile_pattern_follows_hermes_home_realpath(self):
|
|
"""A symlinked HERMES_HOME does not bypass the profile pattern."""
|
|
with (
|
|
tempfile.TemporaryDirectory() as tmp,
|
|
tempfile.TemporaryDirectory() as real,
|
|
):
|
|
link = Path(tmp, "hermes-link")
|
|
link.symlink_to(real, target_is_directory=True)
|
|
with patch.dict(os.environ, {"HERMES_HOME": str(link)}):
|
|
result = self.mod._guards._guard_skill_dir(
|
|
"write_file", {"path": f"{real}/profiles/work/skills/foo/SKILL.md"}
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
# ── Symlink policy (conservative: lexical or realpath hit blocks) ──
|
|
|
|
def test_symlink_alias_into_skill_root_is_blocked(self):
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
alias = Path(tmp, "skills-alias")
|
|
alias.symlink_to("/home/alice/.hermes/skills", target_is_directory=True)
|
|
self.assertIsNotNone(self._guard("patch", f"{alias}/foo/SKILL.md"))
|
|
|
|
def test_symlink_entry_policy_is_explicit(self):
|
|
"""A symlink entry inside the root pointing outside is still blocked
|
|
(lexical path is inside the protected root — conservative policy)."""
|
|
with tempfile.TemporaryDirectory() as tmp, tempfile.TemporaryDirectory() as outside:
|
|
Path(tmp, "skills").mkdir()
|
|
Path(tmp, "skills", "link-outside").symlink_to(outside, target_is_directory=True)
|
|
with patch.dict(os.environ, {"HERMES_HOME": tmp}):
|
|
self.assertIsNotNone(self._guard("patch", f"{tmp}/skills/link-outside/file.go"))
|