Steuere das Herunterladen von Originalen je Rolle, benenne Dateien richtig und zeige HTML-Mails gesichert im Sandbox-Rahmen.
Deploy Development / deploy (push) Successful in 40s
Test Suite / pytest-backend (push) Successful in 5m35s
Test Suite / smoke-dev (push) Successful in 0s
Test Suite / frontend-build (push) Successful in 22s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
StommerandClaude Sonnet 5.5 committed 2026-10-22 14:22:35 +02:00
1 parent 0a5daf6713
commit 075525ee17
26 files changed
+494 -71

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
"""Download policy: which room role may save the original of a document or mail.
Reading inside the app stays possible. The policy is a rule of use, not a copy protection:
what the screen shows has to reach the browser.
"""
from __future__ import annotations
from uuid import UUID
from fastapi import APIRouter, Depends
from fastapi.responses import JSONResponse
from psycopg import Connection
from psycopg.types.json import Json
from app.access.policy import audit
from app.access.roles import require_main_user, room_role
from app.api.deps import Principal, get_conn, get_principal
from app.api.schemas import DownloadPolicyIn
from app.core.errors import forbidden, not_found
router = APIRouter()
_NO_STORE = {"Cache-Control": "no-store"}
ROOM_KEYS = ("manager", "member", "reader")
def _policy(raw: dict, keys: tuple[str, ...]) -> dict[str, bool]:
return {key: bool(raw.get(key, True)) for key in keys}
def _room_of_member(conn, account_id: UUID, room_id: UUID) -> dict:
# A stranger and an unknown room answer alike.
room = conn.execute("SELECT * FROM data_rooms WHERE id = %s AND kind = 'organization'", (room_id,)).fetchone()
if room is None or room_role(conn, account_id, room_id) is None:
raise not_found()
return room
@router.get("/api/v1/rooms/{room_id}/download-policy")
def get_room_policy(room_id: UUID, principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
room = _room_of_member(conn, principal.account_id, room_id)
return JSONResponse(
{"roles": _policy(room["download_roles"], ROOM_KEYS), "can_change": room["owner_id"] == principal.account_id},
headers=_NO_STORE,
)
@router.put("/api/v1/rooms/{room_id}/download-policy")
def put_room_policy(
room_id: UUID,
body: DownloadPolicyIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
room = _room_of_member(conn, principal.account_id, room_id)
if room["owner_id"] != principal.account_id:
raise forbidden("Das darf nur der Eigentümer des Datenraums.")
roles = {"manager": body.manager, "member": body.member, "reader": body.reader}
conn.execute("UPDATE data_rooms SET download_roles = %s WHERE id = %s", (Json(roles), room_id))
audit(conn, principal.account_id, room_id, "room_download_policy_changed", "room", room_id, detail=roles)
return JSONResponse({"roles": roles, "can_change": True}, headers=_NO_STORE)
@router.get("/api/v1/auth/download-defaults")
def get_defaults(principal: Principal = Depends(get_principal), conn: Connection = Depends(get_conn)):
require_main_user(conn, principal.account_id)
row = conn.execute("SELECT download_defaults FROM auth_accounts WHERE id = %s", (principal.account_id,)).fetchone()
return JSONResponse({"roles": _policy(row["download_defaults"], (*ROOM_KEYS, "direct"))}, headers=_NO_STORE)
@router.put("/api/v1/auth/download-defaults")
def put_defaults(
body: DownloadPolicyIn,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
require_main_user(conn, principal.account_id)
roles = {"manager": body.manager, "member": body.member, "reader": body.reader, "direct": True if body.direct is None else body.direct}
conn.execute("UPDATE auth_accounts SET download_defaults = %s WHERE id = %s", (Json(roles), principal.account_id))
audit(conn, principal.account_id, None, "download_defaults_changed", "account", principal.account_id, detail=roles)
return JSONResponse({"roles": roles}, headers=_NO_STORE)
+8 -2
View File
@@ -8,10 +8,10 @@ from uuid import UUID
from fastapi import APIRouter, Depends, Request, Response
from psycopg import Connection
from app.access.policy import Ctx, parse_query_context
from app.access.policy import Ctx, get_matter, matter_rights, parse_query_context
from app.api.deps import Principal, get_conn, get_principal
from app.api.schemas import ExportIn
from app.core.errors import unprocessable
from app.core.errors import DOWNLOAD_REFUSED, forbidden, unprocessable
from app.exporting import build_export, export_tree, review_export
router = APIRouter()
@@ -79,6 +79,12 @@ def post_export(
conn: Connection = Depends(get_conn),
):
selected, assignments = _selection(body)
matter = get_matter(conn, matter_id)
if matter is not None:
rights = matter_rights(conn, principal.account_id, matter, _context(body.context))
# Without any right the export itself answers as before. With read but no download right: refuse.
if rights.read and not rights.download:
raise forbidden(DOWNLOAD_REFUSED)
filename, payload = build_export(
conn,
principal.account_id,
+9 -3
View File
@@ -182,10 +182,16 @@ def create_room(body: RoomIn, principal: Principal = Depends(get_principal), con
name = body.name.strip()
conn.execute(
"""
INSERT INTO data_rooms (id, kind, name, created_by, owner_id, default_visibility)
VALUES (%s, 'organization', %s, %s, %s, %s)
INSERT INTO data_rooms (id, kind, name, created_by, owner_id, default_visibility, download_roles)
SELECT %s, 'organization', %s, %s, %s, %s,
jsonb_build_object(
'manager', COALESCE((download_defaults ->> 'manager')::boolean, true),
'member', COALESCE((download_defaults ->> 'member')::boolean, true),
'reader', COALESCE((download_defaults ->> 'reader')::boolean, true)
)
FROM auth_accounts WHERE id = %s
""",
(room_id, name, principal.account_id, principal.account_id, body.default_visibility),
(room_id, name, principal.account_id, principal.account_id, body.default_visibility, principal.account_id),
)
conn.execute(
"INSERT INTO room_memberships (room_id, subject_id, role) VALUES (%s, %s, 'manager')",
+4 -1
View File
@@ -101,7 +101,7 @@ from app.auth.security import (
verify_password,
verify_unknown_account,
)
from app.core.errors import Problem, forbidden, not_found, too_many, unprocessable
from app.core.errors import DOWNLOAD_REFUSED, Problem, forbidden, not_found, too_many, unprocessable
from app.core.timeutil import dump_date, dump_dt, utcnow
from app.media.port import read_clear_bytes
from app.media.storage import content_disposition
@@ -932,6 +932,7 @@ def download_file(
request: Request,
context_kind: str | None = None,
context_id: str | None = None,
save: bool = False,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
@@ -942,6 +943,8 @@ def download_file(
rights = element_rights(conn, principal.account_id, element, context)
if not rights.read:
raise not_found()
if save and not rights.download:
raise forbidden(DOWNLOAD_REFUSED)
asset = conn.execute(
"""
SELECT id, original_filename, content_type, sha256, status,
+9
View File
@@ -519,3 +519,12 @@ class DispatchCorrectIn(APIModel):
clear_sent_on: bool = False
counterpart: str | None = Field(default=None, max_length=200)
clear_counterpart: bool = False
class DownloadPolicyIn(APIModel):
"""Who may save or open the original outside of Tsumugu. `direct` only applies to the account default."""
manager: bool
member: bool
reader: bool
direct: bool | None = None
+16 -5
View File
@@ -12,7 +12,7 @@ from app.access.policy import Ctx, element_rights, get_element, parse_query_cont
from app.api.deps import Principal, get_conn, get_principal
from app.api.routes import _created, _ctx_model
from app.api.schemas import ConnectorIn, ExternalIn, SourceAssignmentIn
from app.core.errors import not_found, unprocessable, unsupported
from app.core.errors import DOWNLOAD_REFUSED, forbidden, not_found, unprocessable, unsupported
from app.mailpack import parse_mail
from app.media.storage import content_disposition
from app.ops import create_external
@@ -94,21 +94,23 @@ def view_external_content(
element_id: UUID,
context_kind: str | None = None,
context_id: str | None = None,
save: bool = False,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
element, context, ref = _opened_ref(conn, principal, element_id, context_kind, context_id)
if save:
_require_download(conn, principal, element, context)
module = get_source(ref["source"]) if ref is not None else None
if module is None or module.open_original is None:
raise unprocessable("Für diese Quelle ist kein Dateiabruf vorgesehen.")
data, media_type, filename = module.open_original(conn, principal.account_id, element, context, ref)
safe_name = (filename or "dokument.pdf").replace('"', "")
return Response(
content=data,
media_type=media_type,
headers={
**_PRIVATE,
"Content-Disposition": f'inline; filename="{safe_name}"',
"Content-Disposition": content_disposition(filename or "dokument.pdf").replace("attachment;", "inline;", 1),
},
)
@@ -150,6 +152,11 @@ def view_mail(
)
def _require_download(conn, principal: Principal, element, context) -> None:
if not element_rights(conn, principal.account_id, element, context).download:
raise forbidden(DOWNLOAD_REFUSED)
def _opened_ref(conn, principal: Principal, element_id: UUID, context_kind: str | None, context_id: str | None):
element = get_element(conn, element_id)
if element is None or element["kind"] != "external_ref":
@@ -192,8 +199,9 @@ def download_mail(
conn: Connection = Depends(get_conn),
):
element, context, ref = _opened_ref(conn, principal, element_id, context_kind, context_id)
_parsed, raw = _complete_mail(conn, principal, element, context, ref)
return _mail_bytes(raw, "message/rfc822", "nachricht.eml")
_require_download(conn, principal, element, context)
parsed, raw = _complete_mail(conn, principal, element, context, ref)
return _mail_bytes(raw, "message/rfc822", f"{parsed.subject or 'nachricht'}.eml")
@router.get("/api/v1/external-refs/{element_id}/mail/attachments/{index}")
@@ -202,10 +210,13 @@ def download_mail_attachment(
index: int,
context_kind: str | None = None,
context_id: str | None = None,
save: bool = False,
principal: Principal = Depends(get_principal),
conn: Connection = Depends(get_conn),
):
element, context, ref = _opened_ref(conn, principal, element_id, context_kind, context_id)
if save:
_require_download(conn, principal, element, context)
parsed, _raw = _complete_mail(conn, principal, element, context, ref)
if index < 0 or index >= len(parsed.attachments):
raise not_found()