gabriel / musehub public
test_security_r1_scopes.py python
283 lines 14.7 KB
Raw
sha256:4defb0407ee7fe26b1addb8df5cf3d31dae4ebf1ab92eeca1c05348b9d4c8458 merge: integrate R1 into the publishable #114 line (private… Opus 5.5 minor ⚠ breaking 12 days ago
1 """Repository scopes, MCP gates, cross-repo object/commit isolation, push integrity (security R1).
2
3 - Scopes only narrow access: a scoped identity needs ``repo:read``/``repo:write``
4 (on top of owner/collaborator status) on REST, wire and MCP; ``scope=None``
5 identities are unaffected; public repos stay readable.
6 - Every repo-scoped MCP tool passes one read checkpoint; tools without their
7 own write check pass a write checkpoint.
8 - Objects and commits are global: fetch by ID only returns ones the addressed
9 repo references.
10 - Pushed content must hash to its declared ID (wire mpack and MCP push).
11 """
12 from __future__ import annotations
13
14 import base64
15 import json
16 from collections.abc import AsyncGenerator
17 from datetime import datetime, timezone
18 from unittest.mock import AsyncMock, MagicMock, patch
19
20 import pytest
21 import pytest_asyncio
22 from httpx import ASGITransport, AsyncClient
23 from sqlalchemy import select
24 from sqlalchemy.ext.asyncio import AsyncSession
25
26 from muse.core.mpack import build_wire_mpack
27 from muse.core.types import blob_id, fake_id
28 from musehub.auth.repo_access import mcp_caller_scope
29 from musehub.auth.request_signing import MSignContext, optional_signed_request, require_signed_request
30 from musehub.core.genesis import compute_collaborator_id, compute_identity_id
31 from musehub.db.database import get_db
32 from musehub.db.musehub_collaborator_models import MusehubCollaborator
33 from musehub.db.musehub_identity_models import MusehubIdentity
34 from musehub.db.musehub_repo_models import (
35 MusehubCommit, MusehubCommitRef, MusehubObject, MusehubObjectRef, MusehubRepo,
36 )
37 from musehub.main import app
38 from musehub.mcp.dispatcher import handle_request
39 from musehub.mcp.scope_policy import READ_TOOLS, REQUIRED_SCOPE, SCOPED_CALLER_DENIED, tool_scope_error
40 from musehub.mcp.tools import MUSEHUB_WRITE_TOOL_NAMES
41 from musehub.models.musehub import ObjectInput
42 from musehub.services.musehub_repository import create_repo
43 from musehub.services.musehub_sync import _write_object
44 from musehub.services.musehub_wire_fetch import FetchCommitNotFound, wire_fetch
45 from musehub.services.musehub_wire_push import wire_fetch_objects, wire_push_unpack_mpack
46 from musehub.services.musehub_wire_shared import MPackValidationError
47
48 OWNER, READER, STRANGER = "s1-owner", "s1-reader", "s1-stranger"
49
50
51 def _ctx(handle: str, scope: list[str] | None = None) -> MSignContext:
52 return MSignContext(handle=handle, identity_id=compute_identity_id(handle.encode()),
53 is_agent=scope is not None, is_admin=False, scope=scope)
54
55
56 @pytest_asyncio.fixture
57 async def world(db_session: AsyncSession) -> AsyncGenerator[dict[str, MusehubRepo], None]:
58 for h in (OWNER, READER, STRANGER):
59 db_session.add(MusehubIdentity(identity_id=compute_identity_id(h.encode()), handle=h,
60 display_name=h, identity_type="human"))
61 await db_session.commit()
62 repos = {}
63 for key, owner, vis in (("private", OWNER, "private"), ("public", OWNER, "public"), ("stranger_public", STRANGER, "public")):
64 repos[key] = await create_repo(db_session, name=f"s1-{key.replace('_', '-')}", owner=owner,
65 owner_user_id=compute_identity_id(owner.encode()),
66 visibility=vis, initialize=False)
67 await db_session.commit()
68 row = MusehubCollaborator(
69 id=compute_collaborator_id(repos["private"].repo_id, compute_identity_id(READER.encode()),
70 datetime.now(timezone.utc).isoformat()),
71 repo_id=repos["private"].repo_id, identity_handle=READER, permission="read",
72 )
73 db_session.add(row)
74 await db_session.flush()
75 row.accepted_at = row.invited_at
76 await db_session.commit()
77 yield repos
78
79
80 async def _call(db_session: AsyncSession, method: str, path: str, ctx: MSignContext | None,
81 json_body: object | None = None) -> int:
82 async def _db() -> AsyncGenerator[AsyncSession, None]:
83 yield db_session
84 app.dependency_overrides[get_db] = _db
85 app.dependency_overrides[optional_signed_request] = lambda: ctx
86 if ctx is not None:
87 app.dependency_overrides[require_signed_request] = lambda: ctx
88 try:
89 async with AsyncClient(transport=ASGITransport(app=app), base_url="https://localhost:1337") as c:
90 return (await c.request(method, path, json=json_body)).status_code
91 finally:
92 for dep in (get_db, optional_signed_request, require_signed_request):
93 app.dependency_overrides.pop(dep, None)
94
95
96 # ── scope policy table ───────────────────────────────────────────────────────
97
98 def test_every_authenticated_mcp_tool_is_classified_once() -> None:
99 groups = [set(READ_TOOLS), set(REQUIRED_SCOPE), set(SCOPED_CALLER_DENIED)]
100 classified = set().union(*groups)
101 assert classified == set(MUSEHUB_WRITE_TOOL_NAMES), (
102 f"unclassified: {sorted(set(MUSEHUB_WRITE_TOOL_NAMES) - classified)} "
103 f"stale: {sorted(classified - set(MUSEHUB_WRITE_TOOL_NAMES))}"
104 )
105 assert sum(len(g) for g in groups) == len(classified), "a tool is classified twice"
106
107
108 def test_tool_scope_error_rules() -> None:
109 assert tool_scope_error("musehub_create_label", None) is None # humans unrestricted
110 assert tool_scope_error("musehub_create_issue", ["issue:write"]) is None
111 assert tool_scope_error("musehub_create_label", ["issue:write"]) is not None
112 assert tool_scope_error("muse_push", ["issue:write"]) is not None
113 assert tool_scope_error("muse_push", ["repo:write"]) is None
114 assert tool_scope_error("musehub_claim_coord_task", ["repo:write"]) is not None
115 assert tool_scope_error("musehub_list_collaborators", []) is None # reads gated by repo access
116
117
118 # ── REST ─────────────────────────────────────────────────────────────────────
119
120 @pytest.mark.asyncio
121 async def test_rest_private_read_requires_repo_scope(db_session: AsyncSession, world) -> None:
122 path = f"/api/repos/{world['private'].repo_id}"
123 assert await _call(db_session, "GET", path, _ctx(READER)) == 200
124 assert await _call(db_session, "GET", path, _ctx(READER, ["issue:write"])) == 404
125 assert await _call(db_session, "GET", path, _ctx(READER, ["repo:read"])) == 200
126 assert await _call(db_session, "GET", path, _ctx(READER, ["repo:write"])) == 200
127 # a scope never grants access the membership rule denies
128 assert await _call(db_session, "GET", path, _ctx(STRANGER, ["repo:read", "repo:write"])) == 404
129
130
131 @pytest.mark.asyncio
132 async def test_rest_public_read_unaffected_by_scope(db_session: AsyncSession, world) -> None:
133 path = f"/api/repos/{world['public'].repo_id}"
134 assert await _call(db_session, "GET", path, _ctx(STRANGER, [])) == 200
135
136
137 @pytest.mark.asyncio
138 async def test_rest_repo_write_requires_repo_write_scope(db_session: AsyncSession, world) -> None:
139 path = f"/api/repos/{world['private'].repo_id}/collaborators"
140 body = {"handle": STRANGER, "permission": "read"}
141 assert await _call(db_session, "POST", path, _ctx(OWNER, ["repo:read"]), body) == 403
142
143
144 # ── wire ─────────────────────────────────────────────────────────────────────
145
146 @pytest.mark.asyncio
147 async def test_wire_private_read_requires_repo_scope(db_session: AsyncSession, world) -> None:
148 path = f"/{OWNER}/s1-private/version-tags"
149 assert await _call(db_session, "GET", path, _ctx(READER, ["issue:write"])) == 404
150 assert await _call(db_session, "GET", path, _ctx(READER, ["repo:read"])) == 200
151
152
153 @pytest.mark.asyncio
154 async def test_wire_write_requires_repo_write_scope(db_session: AsyncSession, world) -> None:
155 path = f"/{OWNER}/s1-private/branches/no-such-branch"
156 assert await _call(db_session, "DELETE", path, _ctx(OWNER, ["repo:read"])) == 403
157 assert await _call(db_session, "DELETE", path, _ctx(OWNER, ["repo:write"])) != 403
158 assert await _call(db_session, "DELETE", path, _ctx(OWNER)) != 403
159
160
161 # ── MCP ──────────────────────────────────────────────────────────────────────
162
163 async def _tool(name: str, args: dict[str, object], user_id: str | None, scope: list[str] | None = None) -> dict:
164 token = mcp_caller_scope.set(scope)
165 try:
166 resp = await handle_request(
167 {"jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": {"name": name, "arguments": args}},
168 user_id=user_id,
169 )
170 finally:
171 mcp_caller_scope.reset(token)
172 assert resp is not None
173 return resp["result"]
174
175
176 @pytest.mark.asyncio
177 async def test_mcp_private_repo_reads_gated(db_session: AsyncSession, world) -> None:
178 rid = world["private"].repo_id
179 for tool in ("musehub_list_commits", "musehub_list_branches", "musehub_list_issues"):
180 for who in (None, STRANGER):
181 assert (await _tool(tool, {"repo_id": rid}, who)).get("isError"), f"{who} {tool}"
182 assert not (await _tool(tool, {"repo_id": rid}, READER)).get("isError"), f"reader {tool}"
183 assert (await _tool("musehub_list_commits", {"repo_id": rid}, READER, ["issue:write"])).get("isError")
184 assert not (await _tool("musehub_list_commits", {"repo_id": world["public"].repo_id}, None)).get("isError")
185
186
187 @pytest.mark.asyncio
188 async def test_mcp_write_gate_for_tools_without_own_check(db_session: AsyncSession, world) -> None:
189 for repo in (world["private"], world["public"]):
190 for tool, args in (
191 ("muse_push", {"repo_id": repo.repo_id, "branch": "main", "head_commit_id": fake_id("x"),
192 "commits": [], "snapshots": [], "blobs": []}),
193 ("musehub_list_webhook_deliveries", {"repo_id": repo.repo_id, "webhook_id": fake_id("w")}),
194 ("musehub_redeliver_webhook", {"repo_id": repo.repo_id, "webhook_id": fake_id("w"),
195 "delivery_id": fake_id("d")}),
196 ):
197 result = await _tool(tool, args, READER if repo is world["private"] else STRANGER)
198 assert result.get("isError"), f"{tool} on {repo.visibility}"
199 assert "owner or a write/admin collaborator" in result["content"][0]["text"], tool
200
201
202 @pytest.mark.asyncio
203 async def test_mcp_scope_policy_enforced(db_session: AsyncSession, world) -> None:
204 result = await _tool("musehub_create_label", {"repo_id": world["private"].repo_id, "name": "x", "color": "#000000"},
205 OWNER, ["issue:write"])
206 assert result.get("isError") and "label:write" in result["content"][0]["text"]
207
208
209 # ── cross-repo object / commit isolation ─────────────────────────────────────
210
211 @pytest.mark.asyncio
212 async def test_fetch_objects_only_returns_repo_objects(db_session: AsyncSession, world) -> None:
213 data = b"s1 private-only object"
214 oid = blob_id(data)
215 db_session.add(MusehubObject(object_id=oid, path="p.bin", size_bytes=len(data), content_cache=data))
216 await db_session.flush()
217 db_session.add(MusehubObjectRef(repo_id=world["private"].repo_id, object_id=oid))
218 await db_session.commit()
219 assert await wire_fetch_objects(db_session, world["stranger_public"].repo_id, [oid]) == []
220 got = await wire_fetch_objects(db_session, world["private"].repo_id, [oid])
221 assert [o["object_id"] for o in got] == [oid]
222
223
224 @pytest.mark.asyncio
225 async def test_fetch_rejects_foreign_commit_wants(db_session: AsyncSession, world) -> None:
226 cid = fake_id("s1-private-commit")
227 db_session.add(MusehubCommit(commit_id=cid, parent_ids=[], snapshot_id=fake_id("s1-snap"),
228 message="m", author=OWNER, timestamp=datetime.now(timezone.utc), branch="main"))
229 db_session.add(MusehubCommitRef(repo_id=world["private"].repo_id, commit_id=cid))
230 await db_session.commit()
231 with pytest.raises(FetchCommitNotFound):
232 await wire_fetch(db_session, world["stranger_public"].repo_id, want=[cid], have=[])
233
234
235 # ── push integrity ───────────────────────────────────────────────────────────
236
237 def _backend(wire: bytes) -> MagicMock:
238 b = MagicMock()
239 b.put = AsyncMock(return_value="s3://muse-objects/fake")
240 b.get = AsyncMock(return_value=None)
241 b.get_mpack = AsyncMock(return_value=wire)
242 b.put_mpack = AsyncMock(return_value=None)
243 b.quarantine_mpack = AsyncMock(return_value=None)
244 return b
245
246
247 @pytest.mark.asyncio
248 async def test_wire_push_rejects_mislabelled_blob(db_session: AsyncSession, world) -> None:
249 victim_oid = blob_id(b"s1 victim content")
250 wire = build_wire_mpack({"blobs": [{"object_id": victim_oid, "content": b"attacker bytes"}],
251 "commits": [], "snapshots": []})
252 backend = _backend(wire)
253 with patch("musehub.storage.backends.get_backend", return_value=backend):
254 with pytest.raises(MPackValidationError):
255 await wire_push_unpack_mpack(db_session, world["stranger_public"].repo_id, blob_id(wire), STRANGER,
256 branch="main", head_commit_id="", commits_count=0, blobs_count=1)
257 await db_session.rollback()
258 refs = (await db_session.execute(select(MusehubObjectRef).where(MusehubObjectRef.object_id == victim_oid))).all()
259 assert refs == []
260 backend.quarantine_mpack.assert_awaited()
261
262
263 @pytest.mark.asyncio
264 async def test_wire_push_accepts_correct_blob(db_session: AsyncSession, world) -> None:
265 content = b"s1 honest content"
266 oid = blob_id(content)
267 wire = build_wire_mpack({"blobs": [{"object_id": oid, "content": content}], "commits": [], "snapshots": []})
268 with patch("musehub.storage.backends.get_backend", return_value=_backend(wire)):
269 await wire_push_unpack_mpack(db_session, world["stranger_public"].repo_id, blob_id(wire), STRANGER,
270 branch="main", head_commit_id="", commits_count=0, blobs_count=1)
271 assert await db_session.get(MusehubObject, oid) is not None
272
273
274 @pytest.mark.asyncio
275 async def test_mcp_push_rejects_mislabelled_object(db_session: AsyncSession, world) -> None:
276 victim_oid = blob_id(b"s1 victim content 2")
277 obj = ObjectInput(object_id=victim_oid, path="x.bin",
278 content_b64=base64.b64encode(b"attacker bytes").decode())
279 backend = _backend(b"")
280 with patch("musehub.storage.backends.get_backend", return_value=backend):
281 with pytest.raises(ValueError):
282 await _write_object(db_session, repo_id=world["stranger_public"].repo_id, obj=obj)
283 backend.put.assert_not_awaited()
File History 2 commits
sha256:4defb0407ee7fe26b1addb8df5cf3d31dae4ebf1ab92eeca1c05348b9d4c8458 merge: integrate R1 into the publishable #114 line (private… Opus 5.5 minor ⚠ 12 days ago
sha256:83227647a82e1733a20b0b6d225df3d153769630d3c710d255e0c1b8aab8ae61 Co-Authored-By: Claude Opus 5.5 <[email protected]> Opus 5.5 minor ⚠ 13 days ago