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
193 lines
7.4 KiB
Python
193 lines
7.4 KiB
Python
"""Tests for kuri-tool-guard — GitHub web guard."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from tests._support import load_mod
|
|
|
|
|
|
class TestGitHubWebGuard(unittest.TestCase):
|
|
"""Test GitHub and raw.githubusercontent.com web calls are blocked."""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
|
|
# ── github.com ──────────────────────────────────────────────
|
|
|
|
def test_blocks_browser_navigate_github(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "https://github.com/NousResearch/hermes-agent"},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("gh CLI", result["message"])
|
|
|
|
def test_blocks_web_extract_github(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="web_extract",
|
|
args={"urls": ["https://github.com/NousResearch/hermes-agent"]},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_blocks_github_bare_domain(self):
|
|
"""Bare github.com without scheme."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "github.com"},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("gh CLI", result["message"])
|
|
|
|
def test_blocks_github_subdomain(self):
|
|
"""Subdomain of github.com (e.g. api.github.com) also blocked."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="web_extract",
|
|
args={"urls": ["https://api.github.com/repos/user/repo"]},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
# ── raw.githubusercontent.com (HTTPS) ───────────────────────
|
|
|
|
def test_blocks_browser_navigate_raw_github(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "https://raw.githubusercontent.com/xlionjuan/test/main/f"},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("curl", result["message"])
|
|
|
|
def test_blocks_web_extract_raw_github(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="web_extract",
|
|
args={"urls": ["https://raw.githubusercontent.com/xlionjuan/test/main/f"]},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("curl", result["message"])
|
|
|
|
def test_blocks_raw_github_bare_domain(self):
|
|
"""Bare domain name without path also blocked."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "raw.githubusercontent.com"},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
# ── raw.githubusercontent.com (edge: HTTP, no trailing slash) ─
|
|
|
|
def test_blocks_http_raw_github_with_path(self):
|
|
"""HTTP (not HTTPS) with path — should block."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "http://raw.githubusercontent.com/owner/repo/file"},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("curl", result["message"])
|
|
|
|
def test_blocks_raw_github_https_no_trailing_slash(self):
|
|
"""HTTPS without trailing slash — no path after domain."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "https://raw.githubusercontent.com"},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_blocks_raw_github_http_no_trailing_slash(self):
|
|
"""HTTP without trailing slash — no path after domain."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "http://raw.githubusercontent.com"},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
# ── raw.githubusercontent.com (edge: userinfo, port) ─────────
|
|
|
|
def test_blocks_raw_github_with_userinfo(self):
|
|
"""URL with userinfo — hostname still matches."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "https://[email protected]/x"},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_blocks_raw_github_with_explicit_port(self):
|
|
"""URL with explicit port — hostname still matches."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "https://raw.githubusercontent.com:443/x"},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
# ── Negative cases (should NOT block) ───────────────────────
|
|
|
|
def test_passes_non_github_url(self):
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "https://hermes-agent.nousresearch.com/docs/"},
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_something_raw_github(self):
|
|
"""something-raw.githubusercontent.com is not the same domain."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "https://something-raw.githubusercontent.com/"},
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_query_embedded_raw_github(self):
|
|
"""URL with raw.githubusercontent.com in query param — not hostname."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "https://example.com/?next=https://raw.githubusercontent.com/x"},
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
def test_passes_github_in_path(self):
|
|
"""github.com as a path segment, not hostname."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="browser_navigate",
|
|
args={"url": "https://example.com/github.com/file"},
|
|
)
|
|
self.assertIsNone(result)
|
|
|
|
# ── Multi-URL list ──────────────────────────────────────────
|
|
|
|
def test_blocks_multi_url_list_first(self):
|
|
"""Target URL not first in list — still blocked."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="web_extract",
|
|
args={
|
|
"urls": [
|
|
"https://example.com/page1",
|
|
"https://raw.githubusercontent.com/owner/repo/file",
|
|
]
|
|
},
|
|
)
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
|
|
def test_blocks_multi_url_list_all_safe(self):
|
|
"""All safe in multi-URL list — passes."""
|
|
result = self.mod._on_pre_tool_call(
|
|
tool_name="web_extract",
|
|
args={
|
|
"urls": [
|
|
"https://example.com/page1",
|
|
"https://example.com/page2",
|
|
]
|
|
},
|
|
)
|
|
self.assertIsNone(result)
|