test_identity_email_privacy.py
python
sha256:59e0741acce2f81e1c75113c4fb49ba3bf61fd8855f733dbc8582e56065b1be1
fix(identities): email is returned only to the identity it …
Sonnet 5.5
minor
⚠ breaking
4 days ago
| 1 | """The identity endpoints return ``email`` only to the identity it belongs to. |
| 2 | |
| 3 | ``GET /api/identities`` and ``GET /api/identities/{handle}`` are readable by |
| 4 | anonymous callers. A profile's ``email`` is voluntarily supplied, private |
| 5 | contact data: it is returned to the identity's own signed caller and to no one |
| 6 | else. The response shape is unchanged: the ``email`` key stays, with ``null`` |
| 7 | for every other viewer. |
| 8 | """ |
| 9 | from __future__ import annotations |
| 10 | |
| 11 | import pytest |
| 12 | from httpx import AsyncClient |
| 13 | from sqlalchemy.ext.asyncio import AsyncSession |
| 14 | |
| 15 | from musehub.core.genesis import compute_identity_id |
| 16 | from musehub.db.musehub_identity_models import MusehubIdentity |
| 17 | from musehub.main import app |
| 18 | from musehub.auth.request_signing import ( |
| 19 | MSignContext, |
| 20 | optional_signed_request, |
| 21 | require_signed_request, |
| 22 | ) |
| 23 | |
| 24 | _OWNER_EMAIL = "[email protected]" |
| 25 | _OTHER_EMAIL = "[email protected]" |
| 26 | |
| 27 | |
| 28 | async def _identity(session: AsyncSession, handle: str, email: str | None) -> MusehubIdentity: |
| 29 | row = MusehubIdentity( |
| 30 | identity_id=compute_identity_id(handle.encode()), |
| 31 | handle=handle, |
| 32 | identity_type="human", |
| 33 | email=email, |
| 34 | ) |
| 35 | session.add(row) |
| 36 | await session.commit() |
| 37 | await session.refresh(row) |
| 38 | await session.commit() |
| 39 | return row |
| 40 | |
| 41 | |
| 42 | def _as(handle: str) -> MSignContext: |
| 43 | return MSignContext( |
| 44 | handle=handle, |
| 45 | identity_id=compute_identity_id(handle.encode()), |
| 46 | is_agent=False, |
| 47 | is_admin=False, |
| 48 | ) |
| 49 | |
| 50 | |
| 51 | @pytest.fixture |
| 52 | def signed_as(): |
| 53 | """Make requests in a test arrive signed by ``handle``; restore afterwards.""" |
| 54 | |
| 55 | def _set(handle: str) -> None: |
| 56 | ctx = _as(handle) |
| 57 | app.dependency_overrides[require_signed_request] = lambda: ctx |
| 58 | app.dependency_overrides[optional_signed_request] = lambda: ctx |
| 59 | |
| 60 | yield _set |
| 61 | app.dependency_overrides.pop(require_signed_request, None) |
| 62 | app.dependency_overrides.pop(optional_signed_request, None) |
| 63 | |
| 64 | |
| 65 | @pytest.mark.asyncio |
| 66 | class TestIdentityEmailPrivacy: |
| 67 | async def test_anonymous_get_by_handle_hides_email( |
| 68 | self, client: AsyncClient, db_session: AsyncSession |
| 69 | ) -> None: |
| 70 | await _identity(db_session, "emailowner", _OWNER_EMAIL) |
| 71 | resp = await client.get("/api/identities/emailowner") |
| 72 | assert resp.status_code == 200 |
| 73 | body = resp.json() |
| 74 | assert body["handle"] == "emailowner" |
| 75 | assert body["email"] is None |
| 76 | assert _OWNER_EMAIL not in resp.text |
| 77 | |
| 78 | async def test_anonymous_list_hides_every_email( |
| 79 | self, client: AsyncClient, db_session: AsyncSession |
| 80 | ) -> None: |
| 81 | await _identity(db_session, "emailowner", _OWNER_EMAIL) |
| 82 | await _identity(db_session, "emailother", _OTHER_EMAIL) |
| 83 | resp = await client.get("/api/identities") |
| 84 | assert resp.status_code == 200 |
| 85 | rows = {r["handle"]: r for r in resp.json()["identities"]} |
| 86 | assert {"emailowner", "emailother"} <= set(rows) |
| 87 | assert all(r["email"] is None for r in rows.values()) |
| 88 | assert _OWNER_EMAIL not in resp.text and _OTHER_EMAIL not in resp.text |
| 89 | |
| 90 | async def test_another_signed_identity_does_not_see_the_email( |
| 91 | self, client: AsyncClient, db_session: AsyncSession, signed_as |
| 92 | ) -> None: |
| 93 | await _identity(db_session, "emailowner", _OWNER_EMAIL) |
| 94 | await _identity(db_session, "emailother", _OTHER_EMAIL) |
| 95 | signed_as("emailother") |
| 96 | by_handle = await client.get("/api/identities/emailowner") |
| 97 | assert by_handle.status_code == 200 |
| 98 | assert by_handle.json()["email"] is None |
| 99 | listing = await client.get("/api/identities") |
| 100 | rows = {r["handle"]: r for r in listing.json()["identities"]} |
| 101 | assert rows["emailowner"]["email"] is None |
| 102 | assert _OWNER_EMAIL not in listing.text |
| 103 | |
| 104 | async def test_the_owner_sees_their_own_email( |
| 105 | self, client: AsyncClient, db_session: AsyncSession, signed_as |
| 106 | ) -> None: |
| 107 | await _identity(db_session, "emailowner", _OWNER_EMAIL) |
| 108 | await _identity(db_session, "emailother", _OTHER_EMAIL) |
| 109 | signed_as("emailowner") |
| 110 | own = await client.get("/api/identities/emailowner") |
| 111 | assert own.json()["email"] == _OWNER_EMAIL |
| 112 | listing = await client.get("/api/identities") |
| 113 | rows = {r["handle"]: r for r in listing.json()["identities"]} |
| 114 | assert rows["emailowner"]["email"] == _OWNER_EMAIL |
| 115 | assert rows["emailother"]["email"] is None |
| 116 | |
| 117 | async def test_patch_response_returns_the_owners_email( |
| 118 | self, client: AsyncClient, db_session: AsyncSession, signed_as |
| 119 | ) -> None: |
| 120 | await _identity(db_session, "emailowner", None) |
| 121 | signed_as("emailowner") |
| 122 | resp = await client.patch( |
| 123 | "/api/identities/emailowner", json={"email": _OWNER_EMAIL} |
| 124 | ) |
| 125 | assert resp.status_code == 200 |
| 126 | assert resp.json()["email"] == _OWNER_EMAIL |
| 127 | |
| 128 | async def test_response_shape_is_unchanged( |
| 129 | self, client: AsyncClient, db_session: AsyncSession |
| 130 | ) -> None: |
| 131 | await _identity(db_session, "emailowner", _OWNER_EMAIL) |
| 132 | body = (await client.get("/api/identities/emailowner")).json() |
| 133 | assert { |
| 134 | "identity_id", "handle", "identity_type", "display_name", "pubkey", |
| 135 | "quorum", "bio", "avatar_url", "website_url", "email", "agent_model", |
| 136 | "agent_capabilities", "spawned_by", "scope", "expires_at", |
| 137 | "created_at", "updated_at", |
| 138 | } <= set(body) |
| 139 | |
| 140 | async def test_public_profile_page_does_not_expose_the_email( |
| 141 | self, client: AsyncClient, db_session: AsyncSession |
| 142 | ) -> None: |
| 143 | await _identity(db_session, "emailowner", _OWNER_EMAIL) |
| 144 | as_json = await client.get("/emailowner?format=json") |
| 145 | assert as_json.status_code == 200 |
| 146 | assert _OWNER_EMAIL not in as_json.text |
| 147 | as_html = await client.get("/emailowner") |
| 148 | assert as_html.status_code == 200 |
| 149 | assert _OWNER_EMAIL not in as_html.text |
File History
1 commit
sha256:59e0741acce2f81e1c75113c4fb49ba3bf61fd8855f733dbc8582e56065b1be1
fix(identities): email is returned only to the identity it …
Sonnet 5.5
minor
⚠
4 days ago