test_mist_fork_authorization.py
python
sha256:d6608a13a90783ce5cbcc6eabddf1817f1959ea1fcb8cab459d26e413c144eab
fix(mists): only the owner may fork a secret mist; forks ke…
Opus 5.5
patch
8 days ago
| 1 | """Forking follows the mist read contract: a public mist may be forked by anyone, a secret mist only by its owner. |
| 2 | |
| 3 | The check lives in ``services.musehub_mists`` (``assert_can_fork`` inside ``fork_mist``), so the REST route and the MCP |
| 4 | ``muse_mist_fork`` tool share it. A fork's backing repo takes the source's visibility, so secret content never lands |
| 5 | in a public repo. |
| 6 | """ |
| 7 | from __future__ import annotations |
| 8 | |
| 9 | import secrets |
| 10 | from collections.abc import AsyncGenerator |
| 11 | |
| 12 | import pytest |
| 13 | from httpx import AsyncClient |
| 14 | from sqlalchemy import func, select |
| 15 | from sqlalchemy.ext.asyncio import AsyncSession |
| 16 | |
| 17 | from musehub.auth.request_signing import MSignContext, optional_signed_request, require_signed_request |
| 18 | from musehub.core.genesis import compute_identity_id |
| 19 | from musehub.db.musehub_identity_models import MusehubIdentity |
| 20 | from musehub.db.musehub_repo_models import MusehubMist, MusehubRepo |
| 21 | from musehub.main import app |
| 22 | from musehub.mcp.write_tools.mists import execute_create_mist, execute_fork_mist |
| 23 | |
| 24 | OWNER, OTHER = "alice", "bob" |
| 25 | |
| 26 | |
| 27 | def _as(handle: str | None) -> None: |
| 28 | if handle is None: |
| 29 | app.dependency_overrides.pop(require_signed_request, None) |
| 30 | app.dependency_overrides.pop(optional_signed_request, None) |
| 31 | return |
| 32 | ctx = MSignContext(handle=handle, identity_id=compute_identity_id(handle.encode()), is_agent=False, is_admin=False) |
| 33 | app.dependency_overrides[require_signed_request] = lambda: ctx |
| 34 | app.dependency_overrides[optional_signed_request] = lambda: ctx |
| 35 | |
| 36 | |
| 37 | @pytest.fixture |
| 38 | async def people(db_session: AsyncSession) -> AsyncGenerator[None, None]: |
| 39 | for h in (OWNER, OTHER): |
| 40 | db_session.add(MusehubIdentity(identity_id=compute_identity_id(h.encode()), handle=h, display_name=h, identity_type="human")) |
| 41 | await db_session.commit() |
| 42 | yield |
| 43 | _as(None) |
| 44 | |
| 45 | |
| 46 | async def _new_mist(visibility: str) -> str: |
| 47 | r = await execute_create_mist(filename="note.py", content=f"x = 1 # {secrets.token_hex(16)}", actor=OWNER, visibility=visibility) |
| 48 | assert r.ok, r |
| 49 | return str(r.data["mist_id"]) |
| 50 | |
| 51 | |
| 52 | async def _repo_visibility_of(db: AsyncSession, mist_id: str) -> str: |
| 53 | db.expire_all() |
| 54 | repo_id = (await db.execute(select(MusehubMist.repo_id).where(MusehubMist.mist_id == mist_id))).scalar_one() |
| 55 | return str((await db.execute(select(MusehubRepo.visibility).where(MusehubRepo.repo_id == repo_id))).scalar_one()) |
| 56 | |
| 57 | |
| 58 | async def _repo_count(db: AsyncSession, owner: str) -> int: |
| 59 | db.expire_all() |
| 60 | return int((await db.execute(select(func.count()).select_from(MusehubRepo).where(MusehubRepo.owner == owner))).scalar_one()) |
| 61 | |
| 62 | |
| 63 | def test_assert_can_fork_contract() -> None: |
| 64 | from musehub.services.musehub_mists import assert_can_fork |
| 65 | |
| 66 | assert_can_fork("public", OWNER, OTHER) |
| 67 | assert_can_fork("secret", OWNER, OWNER) |
| 68 | for requester in (OTHER, ""): |
| 69 | with pytest.raises(PermissionError): |
| 70 | assert_can_fork("secret", OWNER, requester) |
| 71 | |
| 72 | |
| 73 | async def test_mcp_fork_of_a_secret_mist_by_a_non_owner_is_refused_and_creates_nothing(db_session: AsyncSession, people: None) -> None: |
| 74 | mid = await _new_mist("secret") |
| 75 | before = await _repo_count(db_session, OTHER) |
| 76 | r = await execute_fork_mist(mist_id=mid, actor=OTHER) |
| 77 | assert (r.ok, r.error_code) == (False, "forbidden"), r |
| 78 | assert await _repo_count(db_session, OTHER) == before |
| 79 | db_session.expire_all() |
| 80 | assert (await db_session.execute(select(func.count()).select_from(MusehubMist).where(MusehubMist.fork_parent_id == mid))).scalar_one() == 0 |
| 81 | |
| 82 | |
| 83 | async def test_mcp_owner_fork_of_a_secret_mist_stays_secret(db_session: AsyncSession, people: None) -> None: |
| 84 | mid = await _new_mist("secret") |
| 85 | r = await execute_fork_mist(mist_id=mid, actor=OWNER) |
| 86 | assert r.ok, r |
| 87 | fid = str(r.data["mist_id"]) |
| 88 | db_session.expire_all() |
| 89 | vis = (await db_session.execute(select(MusehubMist.visibility).where(MusehubMist.mist_id == fid))).scalar_one() |
| 90 | assert vis == "secret" |
| 91 | assert await _repo_visibility_of(db_session, fid) != "public" |
| 92 | |
| 93 | |
| 94 | async def test_mcp_fork_of_a_public_mist_by_anyone_still_works(db_session: AsyncSession, people: None) -> None: |
| 95 | mid = await _new_mist("public") |
| 96 | r = await execute_fork_mist(mist_id=mid, actor=OTHER) |
| 97 | assert r.ok, r |
| 98 | assert r.data["owner"] == OTHER and r.data["fork_parent_id"] == mid |
| 99 | assert await _repo_visibility_of(db_session, str(r.data["mist_id"])) == "public" |
| 100 | |
| 101 | |
| 102 | async def test_rest_fork_follows_the_same_contract(client: AsyncClient, db_session: AsyncSession, people: None) -> None: |
| 103 | secret_id = await _new_mist("secret") |
| 104 | public_id = await _new_mist("public") |
| 105 | _as(OTHER) |
| 106 | assert (await client.post(f"/api/mists/{secret_id}/fork")).status_code == 403 |
| 107 | assert (await client.post(f"/api/mists/{public_id}/fork")).status_code == 201 |
| 108 | _as(OWNER) |
| 109 | own = await client.post(f"/api/mists/{secret_id}/fork") |
| 110 | assert own.status_code == 201, own.text |
| 111 | assert await _repo_visibility_of(db_session, own.json()["mistId"]) != "public" |
File History
1 commit
sha256:d6608a13a90783ce5cbcc6eabddf1817f1959ea1fcb8cab459d26e413c144eab
fix(mists): only the owner may fork a secret mist; forks ke…
Opus 5.5
patch
8 days ago