Files
HealthLog/prisma/migrations/0199_v123_mfa_columns/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

25 lines
1.2 KiB
SQL

-- v1.23 — account-security columns (second-factor substrate).
--
-- Additive, nullable / defaulted columns only — existing rows need no
-- backfill. The TOTP shared secret (`totp_secret_encrypted`) is AES-256-GCM
-- ciphertext like every other `*_encrypted` column (registered in the
-- encrypted-columns registry + the rotation script). `totp_confirmed_at`
-- marks the pending->active promotion; `mfa_enforced` /
-- `passkey_upgrade_nudge_dismissed` are per-account flags later waves read.
--
-- `sessions.mfa_verified_at` is the step-up freshness stamp; `last_active_at`
-- backs the user-facing active-session list. `passkeys.last_used_at` is set on
-- each successful assertion so the management UI can surface "last used".
ALTER TABLE "users"
ADD COLUMN IF NOT EXISTS "totp_secret_encrypted" TEXT,
ADD COLUMN IF NOT EXISTS "totp_confirmed_at" TIMESTAMP(3),
ADD COLUMN IF NOT EXISTS "mfa_enforced" BOOLEAN NOT NULL DEFAULT false,
ADD COLUMN IF NOT EXISTS "passkey_upgrade_nudge_dismissed" BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE "sessions"
ADD COLUMN IF NOT EXISTS "mfa_verified_at" TIMESTAMP(3),
ADD COLUMN IF NOT EXISTS "last_active_at" TIMESTAMP(3);
ALTER TABLE "passkeys"
ADD COLUMN IF NOT EXISTS "last_used_at" TIMESTAMP(3);