Files
vitareba/app/api/documents
GandClaude Opus 5 5d59edf295 fix(prod): email that reaches patients, links that resolve, documents behind auth (#31)
Found by testing the deployed site rather than the test suite. Each of these
was invisible locally and total in production.

1. No patient could receive any email.

RESEND_FROM was "VitaReBa <onboarding@resend.dev>" — Resend's shared sandbox
sender, which delivers ONLY to the provider account owner. Production logs
show every other recipient rejected outright:

  "You can only send testing emails to your own email address ... verify a
   domain at resend.com/domains, and change the `from` address"

So: no welcome mail, no check-in reminder, no message notification, no weekly
digest — and no password reset, which is the only way back into an account.
Prod now sends from the verified fleetcrown.orangecat.ch (delivery confirmed
end-to-end), and isEmailConfigured() treats a sandbox sender in production as
NOT configured, so forgot-password fails loudly with its existing 503 instead
of answering "check your inbox" to a patient who will never get one.

Emails also carry a reply-to now. A clinical email nobody can answer is a
dead end; replies reach the clinic instead of an unmonitored noreply@.

2. Every link inside those emails pointed at a host that does not exist.

PORTAL_URL fell back to https://vitareba.ch, and the deployed env sets no
NEXT_PUBLIC_APP_URL — the domain has no A record and no NS delegation at all.
The dead host was baked into the running build in 41 places. SITE_URL had
already been corrected for exactly this reason; PORTAL_URL had not, so the
reset link was dead even once mail started arriving. Both fallbacks now name
the host that actually serves, and company.test.ts pins the invariants: same
origin, https, and not a host known not to resolve.

3. Patient documents were served to anyone with the URL.

Caddy served UPLOADS_DIR as a plain file_server under /uploads/*, with no
session involved, and the UI linked documents.fileUrl straight into an href.
Any medical document uploaded would have been readable by anyone holding or
guessing the link — forever, and links leak through history, referrers and
forwarded mail. Nothing was exposed only because no document had been
uploaded yet, which makes this the zero-migration moment to close it.

Documents are now read back through GET /api/documents/[id]/file, which
requires a session and answers 404 — never 403 — for a document that is not
yours, because a 403 would confirm the id is real. lib/storage-discipline.test.ts
fails CI if a component ever links the raw storage URL again.

4. Baseline security headers were absent on a health-data portal.

The portal could be framed by any site and leaked full portal URLs through
the Referer header. Adds frame-ancestors/X-Frame-Options, nosniff,
Referrer-Policy, Permissions-Policy and HSTS. Deliberately not a full CSP:
script-src for Next's inline bootstrap needs nonce plumbing, and a broken CSP
silently breaks the app.

pnpm verify green: lint, typecheck, 975 tests. pnpm build clean.

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-15 10:08:02 +00:00
..