feat(share-links): attach documents and serve them through the share token

P3-1-T2/T4/T5. Extend createShareLinkSchema with an optional, bounded
documentIds set; validate each id as the caller's own live document and create
the membership rows in the same transaction (frozen write-once). Summary DTOs
gain documentCount (never ids, never bytes).

Add the public share-scoped serve route /c/<token>/d/<id>: token-resolved
(never requireAuth), passphrase-gated via the token-scoped unlock cookie,
revocation/expiry re-checked before any decrypt, confined to the link's frozen
document set (foreign/deleted id → flat 404), fail-closed decrypt, identical
serving-class posture as the owner route (inline Class A / attachment Class B,
nosniff, no-store), EXIF-stripped for raster images on egress, per-link rate
limit.

Carve the serve path out of the proxy's blanket DENY posture with the same
document CSP as the owner route (default-src 'none'; frame-ancestors 'self'),
anchored to exactly two dynamic segments so no lookalike over-matches.
This commit is contained in:
Marc-André Bombeck committed 2026-07-07 18:43:13 +02:00
1 parent 95eab66945
commit 844dc8457d
7 files changed
+232 -40

No files matched your search

+18 -6
View File
@@ -313,12 +313,23 @@ export function proxy(request: NextRequest) {
const isDocumentServeRoute =
/^\/api\/documents\/inbound\/[^/]+\/original$/.test(pathname);
// v1.28 — the public share-scoped document serve route at
// `/c/<token>/d/<id>` is the share analogue of the owner `/original` route:
// it decrypts and serves a user-uploaded document that the clinician view
// (`/c/<token>`) frames same-origin (an <iframe> for inline-class PDFs). It
// needs the SAME narrow posture — SAMEORIGIN framing + a document CSP — and
// NOT the `/c/` page CSP, which pins `frame-ancestors 'none'` and would
// refuse the preview embed. Anchored to EXACTLY two dynamic segments so a
// `/c/<token>` page, a `/c/<token>/d/<id>/extra`, or a `/c/<token>/dx/<id>`
// lookalike never inherits the carve-out.
const isShareDocumentServeRoute = /^\/c\/[^/]+\/d\/[^/]+$/.test(pathname);
// Both serve routes take the narrow, document-only header posture.
const isServeRoute = isDocumentServeRoute || isShareDocumentServeRoute;
// Security headers
response.headers.set("X-Content-Type-Options", "nosniff");
response.headers.set(
"X-Frame-Options",
isDocumentServeRoute ? "SAMEORIGIN" : "DENY",
);
response.headers.set("X-Frame-Options", isServeRoute ? "SAMEORIGIN" : "DENY");
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
response.headers.set(
"Permissions-Policy",
@@ -405,8 +416,9 @@ export function proxy(request: NextRequest) {
// page-load. Avatars now live on the User row and serve from
// same-origin `/api/user/avatar/{id}`, so `img-src 'self'` covers
// them.
const csp = isDocumentServeRoute
? // Vault serve route (see the X-Frame-Options note above): the
const csp = isServeRoute
? // Vault serve route (owner `/original` OR share `/c/<token>/d/<id>`; see
// the X-Frame-Options note above): the
// response is a user-uploaded document, not an app page.
// `default-src 'none'` — a served document may load nothing as if
// it were a page — and `frame-ancestors 'self'` permits exactly the