gabriel / musehub public
test_object_cdn_integrity.py python
98 lines 4.1 KB
Raw
sha256:89c7a8268be75aeb33144acf863ad3ebefe7fd3bdfd679faa768199fd789b376 merge: REST wire wave — #296 #297 #298 #299 (Tier A review … Opus 5.5 minor ⚠ breaking 1 day ago
1 """GET /o/{object_id} integrity check (musehub#299).
2
3 Object IDs are typed blob hashes, ``sha256("blob <size>\\0" + data)`` (``muse.core.types.blob_id``). The CDN
4 route must verify served bytes against that formula: a valid object is served without an ``OBJECT_CORRUPT``
5 log, and bytes that do not hash to their ID are refused and never cached publicly.
6 """
7 from __future__ import annotations
8
9 import hashlib
10 import logging
11 from collections.abc import AsyncGenerator
12
13 import pytest
14 import pytest_asyncio
15 from httpx import ASGITransport, AsyncClient
16 from sqlalchemy.ext.asyncio import AsyncSession
17
18 from muse.core.types import blob_id
19 from musehub.auth.request_signing import optional_signed_request
20 from musehub.core.genesis import compute_identity_id
21 from musehub.db.database import get_db
22 from musehub.db.musehub_identity_models import MusehubIdentity
23 from musehub.db.musehub_repo_models import MusehubObject, MusehubObjectRef, MusehubRepo
24 from musehub.main import app
25 from musehub.services.musehub_repository import create_repo
26
27 OWNER = "cdn299-owner"
28
29
30 @pytest_asyncio.fixture
31 async def public_repo(db_session: AsyncSession) -> MusehubRepo:
32 db_session.add(MusehubIdentity(identity_id=compute_identity_id(OWNER.encode()), handle=OWNER,
33 display_name=OWNER, identity_type="human"))
34 await db_session.commit()
35 repo = await create_repo(db_session, name="cdn299", owner=OWNER, owner_user_id=compute_identity_id(OWNER.encode()),
36 visibility="public", initialize=False)
37 await db_session.commit()
38 return repo
39
40
41 async def _store(db_session: AsyncSession, repo: MusehubRepo, object_id: str, data: bytes) -> None:
42 db_session.add(MusehubObject(object_id=object_id, path="f.bin", size_bytes=len(data), content_cache=data))
43 await db_session.flush()
44 db_session.add(MusehubObjectRef(repo_id=repo.repo_id, object_id=object_id))
45 await db_session.commit()
46
47
48 async def _get(db_session: AsyncSession, object_id: str): # noqa: ANN202
49 async def _db() -> AsyncGenerator[AsyncSession, None]:
50 yield db_session
51 app.dependency_overrides[get_db] = _db
52 app.dependency_overrides[optional_signed_request] = lambda: None
53 try:
54 async with AsyncClient(transport=ASGITransport(app=app), base_url="https://localhost:1337") as c:
55 return await c.get(f"/o/{object_id}")
56 finally:
57 app.dependency_overrides.pop(get_db, None)
58 app.dependency_overrides.pop(optional_signed_request, None)
59
60
61 @pytest.mark.asyncio
62 async def test_a_valid_object_is_served_without_a_corruption_log(
63 db_session: AsyncSession, public_repo: MusehubRepo, caplog: pytest.LogCaptureFixture
64 ) -> None:
65 data = b"musehub#299 valid object bytes"
66 oid = blob_id(data)
67 await _store(db_session, public_repo, oid, data)
68 with caplog.at_level(logging.ERROR):
69 r = await _get(db_session, oid)
70 assert r.status_code == 200 and r.content == data
71 assert r.headers["cache-control"].startswith("public")
72 assert "OBJECT_CORRUPT" not in caplog.text
73
74
75 @pytest.mark.asyncio
76 async def test_bytes_that_do_not_hash_to_their_id_are_refused_and_not_cached(
77 db_session: AsyncSession, public_repo: MusehubRepo, caplog: pytest.LogCaptureFixture
78 ) -> None:
79 data = b"musehub#299 corrupt object bytes"
80 oid = blob_id(b"the bytes this id really names")
81 await _store(db_session, public_repo, oid, data)
82 with caplog.at_level(logging.ERROR):
83 r = await _get(db_session, oid)
84 assert r.status_code == 500
85 assert data not in r.content
86 cc = r.headers.get("cache-control", "")
87 assert "public" not in cc and "immutable" not in cc and "no-store" in cc
88 assert "OBJECT_CORRUPT" in caplog.text
89
90
91 @pytest.mark.asyncio
92 async def test_an_untyped_sha256_id_is_not_an_object_id(db_session: AsyncSession, public_repo: MusehubRepo) -> None:
93 """The old check compared against ``sha256(raw)``; that is not how object IDs are formed."""
94 data = b"musehub#299 untyped id"
95 oid = "sha256:" + hashlib.sha256(data).hexdigest()
96 await _store(db_session, public_repo, oid, data)
97 r = await _get(db_session, oid)
98 assert r.status_code == 500 and "public" not in r.headers.get("cache-control", "")
File History 2 commits
sha256:89c7a8268be75aeb33144acf863ad3ebefe7fd3bdfd679faa768199fd789b376 merge: REST wire wave — #296 #297 #298 #299 (Tier A review … Opus 5.5 minor ⚠ 1 day ago
sha256:22ee032c7cb1c6d27f318812c88dd8c2ba76fc8ee54551be7742fe75a1558202 fix(wire): GET /o/{id} verifies the typed blob ID and refus… Opus 5.5 patch 2 days ago