ckb/tests/unit/test_mcp_kb.py
Michał Kopeć 84018323a2 Template 2.0.4: secret scan coverage, mcp and capture hardening
Fixes F17-F19 from the 2.0.0 script review:
- lint check 15 scans hidden folders, key files, dotfiles and
  extensionless text; finds unquoted secret-looking assignments; reports
  files over 50 MB instead of skipping them silently
- mcp: search filtered by the read policy, query after `--`,
  case/trailing-dot-insensitive .local.yaml refusal, verified OAuth
  email only, DNS-rebinding protection on by default, single-use codes
- capture: link-local/metadata always refused, private networks need
  --allow-private (also on redirects), total download limit, charset
  fallback and <meta charset>, <form> kept, svg title ignored, safe
  collision-free binary names with dedup

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:46:38 +01:00

55 lines
2.2 KiB
Python

"""The mcp server's KB policy (kb.py, no MCP SDK needed): search must not
reveal what read_page would refuse, and per-user config stays unreadable
whatever case or trailing dots the file system ignores."""
import os
import shutil
import sys
import tempfile
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
FIX = ROOT / "tests/fixtures/minimal-kb"
sys.path.insert(0, str(ROOT / ".agents/modules/mcp/server"))
from kb import KB, PolicyError # noqa: E402
class McpKb(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
base = Path(self.tmp.name)
self.root, up = base / "kb", base / "up"
shutil.copytree(str(FIX), str(self.root))
shutil.copytree(str(FIX), str(up))
rr = self.root / ".agents/skills/ckb-retrieve/scripts"
rr.mkdir(parents=True)
shutil.copy(str(ROOT / ".agents/skills/ckb-retrieve/scripts/retrieve_candidates.py"), str(rr))
for private in ("workload/notes.md", "raw/saved-conversations/2026-11-11/chat.md"):
p = up / private
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text("zanzibarquokka is private here\n", encoding="utf-8")
(self.root / "linked").mkdir()
os.symlink(str(up), str(self.root / "linked/up"))
self.kb = KB(self.root, git_mode="off")
def tearDown(self):
self.tmp.cleanup()
def test_search_hides_paths_read_page_refuses(self):
res = self.kb.search("zanzibarquokka")
paths = [c["path"] for c in res["candidates"]]
self.assertFalse([p for p in paths if "workload" in p or "saved-conversations" in p], paths)
def test_query_that_looks_like_an_option_is_text(self):
res = self.kb.search("--opened=wiki/x.md")
self.assertIsInstance(res, dict)
self.assertFalse((self.root / "tmp/access.log").exists())
def test_local_yaml_refused_in_any_case_or_with_trailing_dot(self):
for name in ("source.local.yaml", "source.LOCAL.yaml", "source.local.yaml.", "source.Local.Yaml "):
with self.assertRaises(PolicyError, msg=name):
self.kb.resolve_read(f"wiki/{name}")
if __name__ == "__main__":
unittest.main()