feat(security): encrypt medication free-text notes at rest + password-change MFA step-up

Encrypt MedicationSideEffect.notes, MedicationDoseChange.note and
MedicationInventoryItem.notes with AES-256-GCM, closing the last plaintext
PHI columns left after the v1.23 note-encryption rollout. Add the three
*Encrypted Bytes columns (migration 0208), register them in the encrypted-
column rotation set, route every read/write through the note cipher, and add
a transactional fail-closed boot-time backfill for legacy rows. Plaintext
columns drop in a later release once the backfill reports zero remaining rows.

Require a fresh second factor on password change for MFA-enrolled accounts,
matching the existing step-up on MFA-disable and recovery-code regen. Redact
free-text note values from the Zod validation excerpt that can reach a wide
event.
This commit is contained in:
Marc-André Bombeck committed 2026-06-28 03:05:10 +02:00
1 parent bc617adc2c
commit 5ea17398bc
27 files changed
+1111 -60

No files matched your search

@@ -28,7 +28,10 @@ const SKIP_DIRS = new Set(["__tests__", "node_modules", ".next", "generated"]);
* Files allowed to write `medicationInventoryItem` rows:
* - the consumption module (intake-driven consume / restore);
* - the inventory persistence helpers (expire cron `updateMany`);
* - the two inventory CRUD routes (register / mutate / delete).
* - the two inventory CRUD routes (register / mutate / delete);
* - the v1.25 note-encryption backfill, whose update only rewrites the
* `notes` / `notesEncrypted` columns (never units / state) and so cannot
* touch the consumption stamp this guard protects.
*/
const ALLOWED_WRITERS = new Set(
[
@@ -36,6 +39,7 @@ const ALLOWED_WRITERS = new Set(
"src/lib/medications/inventory/service.ts",
"src/app/api/medications/[id]/inventory/route.ts",
"src/app/api/medications/[id]/inventory/[itemId]/route.ts",
"src/lib/jobs/med-notes-encryption-backfill.ts",
].map((p) => p.split("/").join(sep)),
);