Files
LarsandCursor 7e39fbb2bd
Deploy Development / deploy (push) Successful in 41s
Test Suite / pytest-backend (push) Failing after 2m55s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 23s
Mache große IMAP-Fächer, die Paperless-Suche und die interne Markdown-Notiz über die gemeinsamen Schnittstellen benutzbar.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-20 07:34:14 +02:00

139 lines
4.8 KiB
Python

"""Rights stay in one place. A source module only fetches its own bytes."""
from __future__ import annotations
from dataclasses import dataclass
from uuid import UUID
import psycopg
from app.access.policy import Ctx, audit, mediated_source_owner
from app.core.errors import Problem, not_found, unprocessable
from app.sourcebind import resolve_for_read
ORIGINAL_UNAVAILABLE = "Das Original ist über die Freigabe nicht abrufbar."
@dataclass
class BorrowedOriginal:
row: dict
external_id: str
mediated: bool
actor_id: UUID
owner_id: UUID
element: dict
context: Ctx | None
kind: str
def release_for_external(conn) -> None:
"""External calls must not sit inside an open domain transaction."""
if conn.info.transaction_status != psycopg.pq.TransactionStatus.IDLE:
conn.commit()
def connector_owner(conn, connector_id: UUID | None) -> UUID | None:
if connector_id is None:
return None
row = conn.execute("SELECT owner_id FROM connectors WHERE id = %s", (connector_id,)).fetchone()
return row["owner_id"] if row else None
def assert_still_owned(conn, connector_id, owner_id: UUID) -> None:
row = conn.execute(
"SELECT id FROM connectors WHERE id = %s AND owner_id = %s",
(connector_id, owner_id),
).fetchone()
if row is None:
raise not_found()
def audit_mediated(conn, actor_id: UUID, element: dict, context: Ctx | None, source: str, connector_id, outcome: str) -> None:
audit(
conn,
actor_id,
element["home_scope_id"],
"source_read_mediated",
"element",
element["id"],
context,
result="ok" if outcome == "ok" else "unavailable",
detail={
"source": source,
"connector_id": str(connector_id) if connector_id else None,
"outcome": outcome,
},
)
if outcome != "ok":
conn.commit()
def _uuid(value) -> UUID | None:
if isinstance(value, UUID):
return value
try:
return UUID(str(value))
except (TypeError, ValueError):
return None
def borrow_original(conn, actor_id: UUID, element: dict, context: Ctx | None, ref, kind: str) -> BorrowedOriginal:
"""Resolve the owner's connector. A grantee never supplies another account's connector."""
detail = (ref or {}).get("detail") or {}
external_id = str((ref or {}).get("external_id") or "")
stored = _uuid(detail.get("connector_id")) or _uuid((ref or {}).get("connector_id"))
stored_owner = connector_owner(conn, stored)
mediated = not (element["owner_subject_id"] == actor_id or stored_owner == actor_id)
if mediated:
owner_id = mediated_source_owner(conn, actor_id, element, context)
if owner_id is None:
raise unprocessable(ORIGINAL_UNAVAILABLE)
else:
owner_id = actor_id
result = resolve_for_read(conn, owner_id, kind, ref, None)
if result.row is None:
if mediated:
audit_mediated(conn, actor_id, element, context, kind, stored, "unavailable")
raise unprocessable(ORIGINAL_UNAVAILABLE)
raise unprocessable(result.message)
return BorrowedOriginal(
row=result.row,
external_id=external_id,
mediated=mediated,
actor_id=actor_id,
owner_id=owner_id,
element=element,
context=context,
kind=kind,
)
def _recheck(conn, borrowed: BorrowedOriginal) -> None:
if borrowed.mediated:
owner_id = mediated_source_owner(conn, borrowed.actor_id, borrowed.element, borrowed.context)
if owner_id != borrowed.owner_id:
audit_mediated(conn, borrowed.actor_id, borrowed.element, borrowed.context, borrowed.kind, borrowed.row["id"], "unavailable")
raise unprocessable(ORIGINAL_UNAVAILABLE)
row = conn.execute("SELECT owner_id FROM connectors WHERE id = %s", (borrowed.row["id"],)).fetchone()
if row is None or row["owner_id"] != borrowed.owner_id:
if borrowed.mediated:
audit_mediated(conn, borrowed.actor_id, borrowed.element, borrowed.context, borrowed.kind, borrowed.row["id"], "unavailable")
raise unprocessable(ORIGINAL_UNAVAILABLE)
raise not_found()
def complete_borrow(conn, borrowed: BorrowedOriginal, fetch):
"""Run the adapter fetch outside the open transaction, then check the grant again."""
release_for_external(conn)
try:
value = fetch(borrowed.row)
except Problem:
if borrowed.mediated:
audit_mediated(conn, borrowed.actor_id, borrowed.element, borrowed.context, borrowed.kind, borrowed.row["id"], "unavailable")
raise unprocessable(ORIGINAL_UNAVAILABLE) from None
raise
_recheck(conn, borrowed)
if borrowed.mediated:
audit_mediated(conn, borrowed.actor_id, borrowed.element, borrowed.context, borrowed.kind, borrowed.row["id"], "ok")
return value