ckb/tests/mcp_policy.py
Michał Kopeć bbfda5a65d Sync: local changes as of 2026-10-29T13:50:12Z
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-29 14:50:12 +01:00

278 lines
15 KiB
Python

#!/usr/bin/env python3
"""Policy test for the mcp module's kb.py: what a remote client may read and
write. Runs on the system Python without the MCP SDK (kb.py has no MCP
dependency for exactly this reason). Called by tests/smoke.sh.
python3 tests/mcp_policy.py tests/fixtures/minimal-kb
"""
import contextlib
import io
import os
import shutil
import subprocess
import sys
import tempfile
from datetime import datetime, timezone
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / ".agents/modules/mcp/server"))
from kb import KB, Author, PolicyError # noqa: E402
fails = []
def check(cond, msg):
if not cond:
fails.append(msg)
with tempfile.TemporaryDirectory() as tmp:
kb_root = Path(tmp) / "kb"
shutil.copytree(sys.argv[1], kb_root)
(kb_root / ".env").write_text("SECRET=1\n")
(kb_root / "workload").mkdir()
(kb_root / "workload" / "x.md").write_text("private\n")
rr = kb_root / ".agents/skills/ckb-retrieve/scripts"
rr.mkdir(parents=True)
shutil.copy(ROOT / ".agents/skills/ckb-retrieve/scripts/retrieve_candidates.py", rr)
kb = KB(kb_root, git_mode="commit")
subprocess.run(["git", "init", "-q", str(kb_root)], check=True)
subprocess.run(["git", "-C", str(kb_root), "add", "-A"], check=True)
subprocess.run(["git", "-C", str(kb_root), "-c", "user.name=t", "-c", "user.email=t@t",
"commit", "-qm", "init"], check=True)
for bad in [".env", "wiki/../.env", "/etc/passwd", "../outside", "workload/x.md", "ckb.yaml",
"AGENTS.md", "libs/x/source.local.yaml", "wiki/.hidden.md", "raw/saved-conversations/a.md", ""]:
try:
kb.read_page(bad)
fails.append(f"read allowed: {bad!r}")
except PolicyError:
pass
check(kb.read_page("/wiki/entities/atlas-retry-policy.md").startswith("---"), "leading slash path not readable")
check(any(p["path"] == "wiki/entities/maria-nowak.md" for p in kb.list_pages("wiki/entities")), "list_pages missed a page")
top = kb.search("how many times does Atlas retry a failed batch", top=3)["candidates"][0]["path"]
check(top == "wiki/entities/atlas-retry-policy.md", f"search top result: {top}")
now = datetime(2026, 9, 24, 12, 0, tzinfo=timezone.utc)
a = Author("Fixture User", "fixture@example.com")
r1 = kb.add_to_inbox("Atlas: DLQ owner / changed!", "The platform team owns the DLQ.", a, source="standup", now=now)
r2 = kb.add_to_inbox("Atlas: DLQ owner / changed!", "Second note.", a, now=now)
check(r1["path"] == "raw/inbox/2026-09-24_atlas-dlq-owner-changed.md", f"inbox path: {r1['path']}")
check(r2["path"] == "raw/inbox/2026-09-24_atlas-dlq-owner-changed-2.md", f"inbox collision path: {r2['path']}")
check(r1["committed"] and not r1["pushed"], f"commit mode result: {r1}")
text = (kb_root / r1["path"]).read_text()
check('submitted_by: "Fixture User <fixture@example.com>"' in text and "via: mcp" in text, "provenance missing")
author = subprocess.run(["git", "-C", str(kb_root), "log", "-1", "--format=%an <%ae>|%cn"],
capture_output=True, text=True).stdout.strip()
check(author == "Fixture User <fixture@example.com>|ckb-mcp", f"git author/committer: {author}")
for title, body in [("", "x"), ("t", " "), ("t", "x" * 300_000)]:
try:
kb.add_to_inbox(title, body, a, now=now)
fails.append(f"inbox accepted title={title!r} len={len(body)}")
except PolicyError:
pass
# Undo: withdraw only one's own pending item; ingested items go through a retraction request.
other_author = Author("Someone Else", "else@example.com")
for who, path in [(other_author, r2["path"]), (a, "raw/archive/2026-09-01/kickoff-notes.md"), (a, "wiki/index.md")]:
try:
kb.withdraw(path, who)
fails.append(f"withdraw allowed: {who.email} {path}")
except PolicyError:
pass
mine = {s["path"]: s["status"] for s in kb.submissions(a)}
check(mine == {r1["path"]: "pending", r2["path"]: "pending"}, f"submissions: {mine}")
check(kb.submissions(other_author) == [], "someone else sees my submissions")
w = kb.withdraw(r2["path"], a)
check(w["withdrawn"] == r2["path"] and not (kb_root / r2["path"]).exists() and w["committed"], f"withdraw: {w}")
# Simulate an ingest of r1: archived, cited by a page and an edge.
arch = kb_root / "raw/archive/2026-09-24"
arch.mkdir(parents=True)
(kb_root / r1["path"]).rename(arch / Path(r1["path"]).name)
archived = f"raw/archive/2026-09-24/{Path(r1['path']).name}"
(kb_root / "wiki/entities/dlq-owner.md").write_text(f"---\ntype: concept\n---\n# DLQ owner\n\n## Sources\n- `{archived}`\n")
try:
kb.withdraw(r1["path"], a)
fails.append("withdraw of an ingested item allowed")
except PolicyError as e:
check("request_retraction" in str(e), f"ingested withdraw hint: {e}")
try:
kb.request_retraction(archived, a, " ")
fails.append("retraction without a reason accepted")
except PolicyError:
pass
rq = kb.request_retraction(archived, a, "wrong KB; belongs in project-beta", now=now)
check(rq["pages"] == ["wiki/entities/dlq-owner.md"] and rq["committed"], f"retraction request: {rq}")
req = (kb_root / rq["request"]).read_text()
check(f"target: {archived}" in req and 'requested_by: "Fixture User' in req, "retraction request content")
check(kb.request_retraction(archived, a, "dup", now=now).get("already_requested") is True, "duplicate request")
# Only the submitter may ask for removal - of their own MCP submissions, nothing else.
for who, path in [(other_author, archived), (a, "raw/archive/2026-09-01/kickoff-notes.md")]:
try:
kb.request_retraction(path, who, "not mine")
fails.append(f"retraction allowed: {who.email} {path}")
except PolicyError:
pass
sub = kb.submissions(a)
check(sub and sub[0]["status"] == "ingested" and sub[0]["retraction_requested"], f"after ingest: {sub}")
for bad in [r1["path"], "wiki/index.md", "raw/inbox/nope.md"]:
try:
kb.request_retraction(bad, a, "x")
fails.append(f"retraction of a non-archive path accepted: {bad}")
except PolicyError:
pass
check(kb.read_page(rq["request"], viewer=a).startswith("---"), "own retraction request not readable")
for viewer in (None, other_author):
try:
kb.read_page(rq["request"], viewer=viewer)
fails.append(f"retraction request readable by {viewer and viewer.email}")
except PolicyError:
pass
check(not any(p["path"].startswith("raw/retractions/") for p in kb.list_pages("raw/retractions", viewer=other_author)),
"list_pages shows someone else's retraction request")
# Symlinks: an innocent name must not land on .git/, workload/ or .env. Only
# linked/<name> itself may be a link, and nothing inside its target may be.
(kb_root / "wiki/leak.md").symlink_to("../.git/config")
(kb_root / "wiki/notes").symlink_to("../workload")
other = Path(tmp) / "other-kb"
(other / "wiki").mkdir(parents=True)
(other / "wiki/index.md").write_text("# Other KB\n")
(other / "secret.txt").write_text("private\n")
(other / "wiki/leak.md").symlink_to("../secret.txt")
(kb_root / "linked").mkdir(exist_ok=True)
(kb_root / "linked/upstream").symlink_to(other)
for bad in ["wiki/leak.md", "wiki/notes/x.md", "linked/upstream/wiki/leak.md", "linked/upstream/secret.txt",
"linked/upstream/linked/x/wiki/index.md"]:
try:
kb.read_page(bad)
fails.append(f"read allowed through a symlink: {bad!r}")
except PolicyError:
pass
listed = {p["path"] for p in kb.list_pages("wiki")} | {p["path"] for p in kb.list_pages("linked")}
check(not listed & {"wiki/leak.md", "wiki/notes/x.md", "linked/upstream/wiki/leak.md"}, f"list_pages shows links: {listed}")
check(kb.read_page("linked/upstream/wiki/index.md") == "# Other KB\n", "linked/<name> symlink not followed")
check("linked/upstream/wiki/index.md" in listed, f"list_pages('linked') does not reach linked KBs: {listed}")
# Sync mode with no git identity anywhere (as in a container): a push that meets a
# moved remote must rebase and still go out, not strand the commit locally.
with tempfile.TemporaryDirectory() as tmp:
t = Path(tmp)
empty = t / "gitconfig"
empty.write_text("")
env = dict(os.environ, GIT_CONFIG_GLOBAL=str(empty), GIT_CONFIG_NOSYSTEM="1")
os.environ.update(GIT_CONFIG_GLOBAL=str(empty), GIT_CONFIG_NOSYSTEM="1")
g = lambda *a: subprocess.run(["git", *a], check=True, capture_output=True, env=env) # noqa: E731
ident = ["-c", "user.name=t", "-c", "user.email=t@t"]
g("init", "-q", "--bare", "-b", "main", str(t / "remote.git"))
g("clone", "-q", str(t / "remote.git"), str(t / "server"))
shutil.copytree(sys.argv[1], t / "server", dirs_exist_ok=True)
g("-C", str(t / "server"), "checkout", "-q", "-b", "main")
g("-C", str(t / "server"), "add", "-A")
g("-C", str(t / "server"), *ident, "commit", "-qm", "init")
g("-C", str(t / "server"), "push", "-q", "-u", "origin", "main")
g("-C", str(t / "server"), "config", "user.useConfigOnly", "true") # no hostname guessing
g("clone", "-q", str(t / "remote.git"), str(t / "person"))
(t / "person/wiki/new.md").write_text("---\ntype: concept\n---\n# New\n")
g("-C", str(t / "person"), "add", "-A")
g("-C", str(t / "person"), *ident, "commit", "-qm", "moved")
g("-C", str(t / "person"), "push", "-q")
srv = KB(t / "server", git_mode="sync")
srv._last_pull = float("inf") # pretend a pull just ran, so the write meets a stale clone
srv.maybe_pull = lambda force=False: None
r = srv.add_to_inbox("After the remote moved", "x", Author("Fixture User", "fixture@example.com"))
check(r.get("pushed") is True, f"push after a moved remote failed: {r}")
log = subprocess.run(["git", "-C", str(t / "remote.git"), "log", "--format=%s", "main"],
capture_output=True, text=True, env=env).stdout.split("\n")
check(log[:2] == ["Inbox via MCP: After the remote moved", "moved"], f"remote log: {log[:3]}")
# A withdraw that meets a moved remote must neither conflict nor strand the clone:
# someone edits the item upstream, then the server withdraws it without having pulled.
who = Author("Fixture User", "fixture@example.com")
item = srv.add_to_inbox("To withdraw", "x", who)["path"]
other_item = srv.add_to_inbox("Will be ingested", "y", who)["path"]
g("-C", str(t / "person"), "pull", "-q")
(t / "person" / item).write_text((t / "person" / item).read_text() + "\nedited upstream\n")
g("-C", str(t / "person"), "add", "-A")
g("-C", str(t / "person"), *ident, "commit", "-qm", "edit inbox item")
g("-C", str(t / "person"), "push", "-q")
w = srv.withdraw(item, who)
check(w.get("pushed") is True, f"withdraw after an upstream edit: {w}")
# The other item is ingested upstream: moved to the archive.
g("-C", str(t / "person"), "pull", "-q")
(t / "person/raw/archive/2026-10-29").mkdir(parents=True, exist_ok=True)
g("-C", str(t / "person"), "mv", other_item, "raw/archive/2026-10-29/")
g("-C", str(t / "person"), *ident, "commit", "-qm", "ingest")
g("-C", str(t / "person"), "push", "-q")
try:
srv.withdraw(other_item, who)
fails.append("withdraw of an item ingested upstream succeeded")
except PolicyError as e:
check("already ingested" in str(e), f"ingested-upstream message: {e}")
try:
srv.withdraw("wiki/index.md", who)
fails.append("withdraw of wiki/index.md allowed")
except PolicyError as e:
check("already ingested" not in str(e), f"misleading hint for a non-inbox path: {e}")
r = srv.add_to_inbox("Unrelated later write", "z", who)
check(r.get("pushed") is True, f"later write after the conflict cases: {r}")
st = subprocess.run(["git", "-C", str(t / "server"), "status", "-sb"], capture_output=True, text=True, env=env).stdout
check("ahead" not in st and "behind" not in st and len(st.strip().splitlines()) == 1, f"server clone diverged: {st}")
# sync mode refuses a checkout holding work a reset would destroy.
(t / "server/wiki/local-edit.md").write_text("unsaved\n")
try:
KB(t / "server", git_mode="sync").check_dedicated_clone()
fails.append("sync mode accepted a checkout with local changes")
except SystemExit:
pass
# The guard holds after startup too: work that appears later is never reset away.
# A read skips its pull, a write is refused, and the file survives both.
refusals = io.StringIO() # the server logs each refusal to stderr; keep a green run silent
with contextlib.redirect_stderr(refusals):
KB.maybe_pull(srv, force=True) # the real one: this fixture stubbed it out above
check((t / "server/wiki/local-edit.md").is_file(), "a pull reset away a local edit made after startup")
try:
srv.add_to_inbox("Over a local edit", "x", who)
fails.append("write accepted over a local edit made after startup")
except PolicyError as e:
check("did not make" in str(e), f"dirty-checkout write message: {e}")
check(refusals.getvalue().count("did not make") == 2, f"refusals not logged: {refusals.getvalue()!r}")
check((t / "server/wiki/local-edit.md").is_file(), "a refused write reset away a local edit")
(t / "server/wiki/local-edit.md").unlink()
check(srv.add_to_inbox("After the edit is gone", "x", who).get("pushed") is True, "write after cleanup")
# An unreachable remote at startup is one line, not a traceback.
g("-C", str(t / "server"), "remote", "set-url", "origin", str(t / "missing.git"))
try:
KB(t / "server", git_mode="sync").check_dedicated_clone()
fails.append("sync mode started with an unreachable remote")
except SystemExit as e:
check("could not reach origin" in str(e), f"unreachable-remote message: {e}")
for k in ("GIT_CONFIG_GLOBAL", "GIT_CONFIG_NOSYSTEM"):
os.environ.pop(k)
# Token rules (auth.py imports nothing from the MCP SDK at module level).
import json # noqa: E402
import auth # noqa: E402
rw, ro = "w" * 32, "r" * 32
toks = auth.load_tokens({"CKB_MCP_TOKEN": rw, "CKB_MCP_READ_TOKEN": ro})
check([t["write"] for t in toks] == [True, False], f"rw/ro token write bits: {toks}")
check([t["write"] for t in auth.load_tokens({"CKB_MCP_READ_TOKEN": ro})] == [False], "read-only-only server")
with tempfile.NamedTemporaryFile("w", suffix=".json", delete=False) as f:
json.dump([{"name": "a", "token": "a" * 32, "write": True}, {"name": "b", "token": "b" * 32},
{"name": "c", "token": "c" * 32, "write": "yes"}], f)
check([t["write"] for t in auth.load_tokens({"CKB_MCP_TOKENS_FILE": f.name})] == [True, False, False],
"token file: write must be literally true")
Path(f.name).unlink()
for env in [{}, {"CKB_MCP_TOKEN": "short"}, {"CKB_MCP_TOKEN": rw, "CKB_MCP_READ_TOKEN": rw}]:
try:
auth.load_tokens(env)
fails.append(f"load_tokens accepted {sorted(env)}")
except SystemExit:
pass
for f in fails:
print(f"mcp policy: {f}")
sys.exit(1 if fails else 0)