Steuere das Herunterladen von Originalen je Rolle, benenne Dateien richtig und zeige HTML-Mails gesichert im Sandbox-Rahmen.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0a5daf6713
commit
075525ee17
26 files changed
+494
-71
No files matched your search
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user