Files
HealthLog/prisma/migrations/0201_v123_note_encryption/migration.sql
T
Marc-André Bombeck 4d3a02cc1a feat(security): add second-factor schema and encrypt free-text health notes at rest
Lay the account-security and data-sovereignty substrate for v1.23. No
user-facing behaviour yet — schema, crypto registry, redaction, and the
note-encryption data path only.

Schema + migrations:
- 0199 — User totp_secret_encrypted / totp_confirmed_at / mfa_enforced /
  passkey_upgrade_nudge_dismissed; Session mfa_verified_at / last_active_at;
  Passkey last_used_at. All additive, nullable / defaulted.
- 0200 — mfa_challenges (single-use step-up ticket; only the ticket hash is
  stored, so no session or token is ever minted before the second factor),
  mfa_recovery_codes (Argon2id at rest), webauthn_mfa_credentials (second-factor
  keys, kept separate from passkeys, which remain the passwordless-primary set).
- 0201 — mood_entries.note_encrypted + measurements.notes_encrypted (BYTEA),
  matching the existing free-text encrypted-note convention.

Free-text health notes:
- New writes encrypt the mood / measurement note into the *_encrypted column and
  null the legacy plaintext column; reads prefer the ciphertext and fall back to
  plaintext only for not-yet-migrated rows, via a single read/write helper.
- A boot-time job migrates existing plaintext rows in an idempotent,
  transactional, fail-closed pass (encrypt, then null the plaintext, per row in
  one transaction; safe to re-run; a row with content is never left both-null).
- The legacy plaintext columns are kept this release and dropped in a follow-up
  once the backfill reports zero remaining rows — the same boundary the
  key-rotation playbook uses before dropping a legacy key.

Crypto coverage:
- Register the three new encrypted columns and wire them into the key-rotation
  script so a rotation never orphans them.
- Extend the observability and log redaction denylists so TOTP secrets, MFA /
  recovery / backup codes, and export passphrases never reach a wide event.
2026-06-27 19:33:10 +02:00

29 lines
1.6 KiB
SQL

-- v1.23 — encrypt free-text health notes at rest.
--
-- Adds the AES-256-GCM ciphertext columns for the two free-text health-note
-- fields that were still plaintext (`mood_entries.note`, `measurements.notes`).
--
-- STORAGE CONVENTION: Bytes (`bytea`), matching the dominant free-text
-- encrypted-note convention already in the schema — illness_day_logs.note_
-- encrypted, cycle_day_logs.notes_encrypted (string-kind), labs.note_encrypted,
-- coach_*.{...}_encrypted are all the shared-codec shape (the `encrypt()`
-- ciphertext string stored UTF-8 as `bytea`). `bytea` is chosen here because
-- the comparable note columns (IllnessDayLog / LabResult) use it and the
-- rotation script's `rotateBytesColumn` already covers that shape with least
-- friction. Both columns are registered in ENCRYPTED_COLUMNS + the rotation
-- script in the same change.
--
-- This migration is ADDITIVE ONLY. The legacy plaintext columns
-- (`mood_entries.note`, `measurements.notes`) are intentionally NOT dropped
-- here: an idempotent, transactional, fail-closed boot-time job encrypts the
-- existing rows (plaintext -> ciphertext, then nulls the plaintext) so the
-- read path can fall back to plaintext for not-yet-migrated rows. Dropping the
-- plaintext columns is a deliberate FOLLOW-UP release, once the backfill
-- reports zero remaining plaintext-but-no-ciphertext rows — the same boundary
-- the encryption-key-rotation playbook uses before dropping a legacy key.
ALTER TABLE "mood_entries"
ADD COLUMN IF NOT EXISTS "note_encrypted" BYTEA;
ALTER TABLE "measurements"
ADD COLUMN IF NOT EXISTS "notes_encrypted" BYTEA;