From 8121935642591936d6c0aea22ad91898a43e0344 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sun, 4 Oct 2026 08:17:34 +0200 Subject: [PATCH] Carry cycle free text readable in a portable backup A portable file carried the day-log note, the sensitive-category envelope and custom symptom labels as the stored ciphertext, so a restore onto a host with another key either refused the file or wrote values nobody could open. The portable file now carries them readable and the restore seals them under the receiving host's key; a disaster-recovery file keeps the ciphertext. A file written before this still restores: each ciphertext value is opened with this host's keys first, and one that does not open is kept out of the row and reported in the skip summary as cycleCiphertext. The key preflight leaves the cycle sections to that per-value check. --- docs/api/openapi.yaml | 52 +++ messages/de.json | 3 +- messages/en.json | 3 +- messages/es.json | 3 +- messages/fr.json | 3 +- messages/it.json | 3 +- messages/ko.json | 3 +- messages/pl.json | 3 +- src/components/admin/backups-section.tsx | 3 + src/lib/cycle/__tests__/backup.test.ts | 89 ++++- src/lib/cycle/backup.ts | 179 ++++++++-- .../export/__tests__/backup-key-ids.test.ts | 24 ++ src/lib/export/backup-key-ids.ts | 35 +- src/lib/export/restore-skips.ts | 11 +- src/lib/validations/backup.ts | 24 ++ .../admin-backups-canonical-roundtrip.test.ts | 6 +- .../backup-cycle-cross-key-restore.test.ts | 313 ++++++++++++++++++ tests/integration/cycle-import-export.test.ts | 17 +- 18 files changed, 731 insertions(+), 43 deletions(-) create mode 100644 tests/integration/backup-cycle-cross-key-restore.test.ts diff --git a/docs/api/openapi.yaml b/docs/api/openapi.yaml index 25431a0bd..ffdc1ead6 100644 --- a/docs/api/openapi.yaml +++ b/docs/api/openapi.yaml @@ -36605,6 +36605,54 @@ components: anyOf: - type: string - type: "null" + note: + anyOf: + - type: string + - type: "null" + sensitive: + anyOf: + - anyOf: + - type: object + properties: + sexualActivity: + type: boolean + protectedSex: + anyOf: + - type: boolean + - type: "null" + pregnancyTest: + anyOf: + - type: string + enum: + - NEGATIVE + - POSITIVE + - INDETERMINATE + - type: "null" + progesteroneTest: + anyOf: + - type: string + enum: + - NEGATIVE + - POSITIVE + - INDETERMINATE + - type: "null" + contraceptive: + anyOf: + - type: string + enum: + - NONE + - UNSPECIFIED + - IMPLANT + - INJECTION + - IUD + - INTRAVAGINAL_RING + - ORAL + - PATCH + - EMERGENCY + - type: "null" + additionalProperties: false + - type: string + - type: "null" source: type: string enum: @@ -36708,6 +36756,10 @@ components: anyOf: - type: string - type: "null" + label: + anyOf: + - type: string + - type: "null" required: - key - labelKey diff --git a/messages/de.json b/messages/de.json index 010e91b92..5390c5fb4 100644 --- a/messages/de.json +++ b/messages/de.json @@ -7554,7 +7554,8 @@ "restoreFailureKeyMissing": "Die Sicherung braucht den Verschlüsselungsschlüssel {keys}, mit dem dieser Server sie nicht öffnen kann. Nimm den Schlüssel wieder in ENCRYPTION_KEYS auf, starte neu und starte die Wiederherstellung erneut.", "restoreFailureForeignReference": "Die Datei verweist auf Einträge, die zu einem anderen Konto gehören. Es wurde nichts geschrieben.", "previewKeyMissing": "Diese Sicherung lässt sich auf diesem Server nicht wiederherstellen: Sie braucht den Verschlüsselungsschlüssel {keys}. Nimm den Schlüssel zuerst wieder in ENCRYPTION_KEYS auf und starte neu.", - "restoreSkippedSymptomEpisodeReference": "Symptom-Eintrag mit Verknüpfung zu einer Erkrankung, die die Datei nicht enthält (ohne Verknüpfung behalten)" + "restoreSkippedSymptomEpisodeReference": "Symptom-Eintrag mit Verknüpfung zu einer Erkrankung, die die Datei nicht enthält (ohne Verknüpfung behalten)", + "restoreSkippedCycleCiphertext": "Zyklus-Notiz oder -Bezeichnung mit einem Schlüssel verschlüsselt, den dieser Server nicht hat (Tag oder Symptom ohne sie behalten)" }, "danger-zone": { "title": "Gefahrenzone", diff --git a/messages/en.json b/messages/en.json index b65774c65..dd65185b3 100644 --- a/messages/en.json +++ b/messages/en.json @@ -7554,7 +7554,8 @@ "restoreFailureKeyMissing": "The backup needs encryption key {keys}, which this server cannot open it with. Add the key back to ENCRYPTION_KEYS, restart, and start the restore again.", "restoreFailureForeignReference": "The file points at records that belong to another account. Nothing was written.", "previewKeyMissing": "This backup cannot be restored on this server: it needs encryption key {keys}. Add the key back to ENCRYPTION_KEYS and restart first.", - "restoreSkippedSymptomEpisodeReference": "Symptom entry linked to an illness the file does not contain (kept without the link)" + "restoreSkippedSymptomEpisodeReference": "Symptom entry linked to an illness the file does not contain (kept without the link)", + "restoreSkippedCycleCiphertext": "Cycle note or label sealed with a key this server does not hold (day or symptom kept without it)" }, "danger-zone": { "title": "Danger Zone", diff --git a/messages/es.json b/messages/es.json index 1f9b678f2..8ec71bc7a 100644 --- a/messages/es.json +++ b/messages/es.json @@ -7554,7 +7554,8 @@ "restoreFailureKeyMissing": "La copia necesita la clave de cifrado {keys}, con la que este servidor no puede abrirla. Vuelve a añadir la clave a ENCRYPTION_KEYS, reinicia e inicia de nuevo la restauración.", "restoreFailureForeignReference": "El archivo apunta a registros que pertenecen a otra cuenta. No se escribió nada.", "previewKeyMissing": "Esta copia no se puede restaurar en este servidor: necesita la clave de cifrado {keys}. Vuelve a añadir la clave a ENCRYPTION_KEYS y reinicia primero.", - "restoreSkippedSymptomEpisodeReference": "Entrada de síntoma vinculada a una enfermedad que el archivo no contiene (se conserva sin el vínculo)" + "restoreSkippedSymptomEpisodeReference": "Entrada de síntoma vinculada a una enfermedad que el archivo no contiene (se conserva sin el vínculo)", + "restoreSkippedCycleCiphertext": "Nota o etiqueta del ciclo cifrada con una clave que este servidor no tiene (el día o el síntoma se conserva sin ella)" }, "danger-zone": { "title": "Zona de peligro", diff --git a/messages/fr.json b/messages/fr.json index 1c76f7992..e9d1163a5 100644 --- a/messages/fr.json +++ b/messages/fr.json @@ -7554,7 +7554,8 @@ "restoreFailureKeyMissing": "La sauvegarde a besoin de la clé de chiffrement {keys}, avec laquelle ce serveur ne peut pas l'ouvrir. Remettez la clé dans ENCRYPTION_KEYS, redémarrez, puis relancez la restauration.", "restoreFailureForeignReference": "Le fichier renvoie à des éléments qui appartiennent à un autre compte. Rien n'a été écrit.", "previewKeyMissing": "Cette sauvegarde ne peut pas être restaurée sur ce serveur : elle a besoin de la clé de chiffrement {keys}. Remettez d'abord la clé dans ENCRYPTION_KEYS et redémarrez.", - "restoreSkippedSymptomEpisodeReference": "Entrée de symptôme liée à une maladie absente du fichier (conservée sans le lien)" + "restoreSkippedSymptomEpisodeReference": "Entrée de symptôme liée à une maladie absente du fichier (conservée sans le lien)", + "restoreSkippedCycleCiphertext": "Note ou libellé du cycle chiffré avec une clé que ce serveur ne possède pas (jour ou symptôme conservé sans)" }, "danger-zone": { "title": "Zone de danger", diff --git a/messages/it.json b/messages/it.json index 61397873a..d1538b30b 100644 --- a/messages/it.json +++ b/messages/it.json @@ -7554,7 +7554,8 @@ "restoreFailureKeyMissing": "Il backup richiede la chiave di cifratura {keys}, con cui questo server non riesce ad aprirlo. Aggiungi di nuovo la chiave a ENCRYPTION_KEYS, riavvia e avvia di nuovo il ripristino.", "restoreFailureForeignReference": "Il file rimanda a voci che appartengono a un altro account. Non è stato scritto nulla.", "previewKeyMissing": "Questo backup non può essere ripristinato su questo server: richiede la chiave di cifratura {keys}. Aggiungi prima di nuovo la chiave a ENCRYPTION_KEYS e riavvia.", - "restoreSkippedSymptomEpisodeReference": "Voce di sintomo collegata a una malattia assente dal file (mantenuta senza collegamento)" + "restoreSkippedSymptomEpisodeReference": "Voce di sintomo collegata a una malattia assente dal file (mantenuta senza collegamento)", + "restoreSkippedCycleCiphertext": "Nota o etichetta del ciclo cifrata con una chiave che questo server non possiede (giorno o sintomo mantenuto senza)" }, "danger-zone": { "title": "Zona pericolosa", diff --git a/messages/ko.json b/messages/ko.json index cdabb7b22..a61e278d9 100644 --- a/messages/ko.json +++ b/messages/ko.json @@ -7533,7 +7533,8 @@ "restoreFailureKeyMissing": "이 백업에는 암호화 키 {keys}가 필요하지만 이 서버는 그 키로 백업을 열 수 없습니다. 키를 ENCRYPTION_KEYS에 다시 추가하고 재시작한 뒤 복원을 다시 시작하세요.", "restoreFailureForeignReference": "파일이 다른 계정에 속한 기록을 가리키고 있습니다. 아무것도 기록되지 않았습니다.", "previewKeyMissing": "이 서버에서는 이 백업을 복원할 수 없습니다. 암호화 키 {keys}가 필요합니다. 먼저 키를 ENCRYPTION_KEYS에 다시 추가하고 재시작하세요.", - "restoreSkippedSymptomEpisodeReference": "파일에 없는 질병과 연결된 증상 기록(연결 없이 유지됨)" + "restoreSkippedSymptomEpisodeReference": "파일에 없는 질병과 연결된 증상 기록(연결 없이 유지됨)", + "restoreSkippedCycleCiphertext": "이 서버에 없는 키로 암호화된 주기 메모 또는 이름(해당 날짜나 증상은 그 내용 없이 유지됨)" }, "danger-zone": { "title": "위험 구역", diff --git a/messages/pl.json b/messages/pl.json index 1f931f743..6a51e0eeb 100644 --- a/messages/pl.json +++ b/messages/pl.json @@ -7554,7 +7554,8 @@ "restoreFailureKeyMissing": "Kopia wymaga klucza szyfrowania {keys}, którym ten serwer nie może jej otworzyć. Dodaj klucz z powrotem do ENCRYPTION_KEYS, uruchom ponownie i rozpocznij przywracanie jeszcze raz.", "restoreFailureForeignReference": "Plik wskazuje na wpisy należące do innego konta. Nic nie zostało zapisane.", "previewKeyMissing": "Tej kopii nie da się przywrócić na tym serwerze: wymaga klucza szyfrowania {keys}. Najpierw dodaj klucz z powrotem do ENCRYPTION_KEYS i uruchom ponownie.", - "restoreSkippedSymptomEpisodeReference": "Wpis objawu powiązany z chorobą, której nie ma w pliku (zachowany bez powiązania)" + "restoreSkippedSymptomEpisodeReference": "Wpis objawu powiązany z chorobą, której nie ma w pliku (zachowany bez powiązania)", + "restoreSkippedCycleCiphertext": "Notatka lub etykieta cyklu zaszyfrowana kluczem, którego ten serwer nie ma (dzień lub objaw zachowany bez niej)" }, "danger-zone": { "title": "Strefa zagrożenia", diff --git a/src/components/admin/backups-section.tsx b/src/components/admin/backups-section.tsx index 3a0550cc5..9a295f8a3 100644 --- a/src/components/admin/backups-section.tsx +++ b/src/components/admin/backups-section.tsx @@ -464,6 +464,9 @@ function catalogueLabel( if (catalogue === "symptomEpisodeReference") { return t("admin.section.backups.restoreSkippedSymptomEpisodeReference"); } + if (catalogue === "cycleCiphertext") { + return t("admin.section.backups.restoreSkippedCycleCiphertext"); + } if (catalogue === "medicationTarget") { return t("admin.section.backups.restoreSkippedMedicationTarget"); } diff --git a/src/lib/cycle/__tests__/backup.test.ts b/src/lib/cycle/__tests__/backup.test.ts index 2bcc923ec..cd7318a86 100644 --- a/src/lib/cycle/__tests__/backup.test.ts +++ b/src/lib/cycle/__tests__/backup.test.ts @@ -1,4 +1,7 @@ -import { describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { _resetCryptoCacheForTests, encrypt } from "@/lib/crypto"; +import { UNREADABLE_EXPORT_MARKER } from "@/lib/export/unreadable-marker"; import { buildCycleBackupSection } from "../backup"; @@ -236,3 +239,87 @@ describe("a symptom the account created survives the round trip", () => { expect(section.customSymptoms).toEqual([]); }); }); + +describe("a portable file carries the cycle free text readable", () => { + // Ciphertext in a portable file is noise on any host with another key, so + // the note, the sensitive envelope and a custom label travel opened, and + // the restore seals them under the receiving host's key. + const savedKey = process.env.ENCRYPTION_KEY; + beforeEach(() => { + process.env.ENCRYPTION_KEY = "ab".repeat(32); + _resetCryptoCacheForTests(); + }); + afterEach(() => { + process.env.ENCRYPTION_KEY = savedKey; + _resetCryptoCacheForTests(); + }); + + function client(dayLog: Record, label: string | null) { + return { + cycleProfile: { findUnique: vi.fn().mockResolvedValue(null) }, + menstrualCycle: { findMany: vi.fn().mockResolvedValue([]) }, + cycleDayLog: { + findMany: vi + .fn() + .mockResolvedValue([ + { date: "2026-07-02", symptomLinks: [], ...dayLog }, + ]), + }, + cycleSymptom: { + findMany: vi.fn().mockResolvedValue([ + { + key: "custom_ache", + labelKey: "cycle.symptom.custom_ache", + categoryId: "cat-1", + icon: null, + sortOrder: 0, + isActive: true, + labelEncrypted: label, + }, + ]), + }, + } as never; + } + + it("opens each sealed value and carries no ciphertext", async () => { + const section = await buildCycleBackupSection( + client( + { + notesEncrypted: encrypt("Cramps at four"), + sensitiveEncrypted: encrypt( + JSON.stringify({ sexualActivity: true, contraceptive: "ORAL" }), + ), + }, + encrypt("Back ache"), + ), + "user-1", + {}, + ); + const day = section.cycleDayLogs[0]; + expect(day.note).toBe("Cramps at four"); + expect(day.sensitive).toEqual({ + sexualActivity: true, + contraceptive: "ORAL", + }); + expect(day).not.toHaveProperty("notesEncrypted"); + expect(day).not.toHaveProperty("sensitiveEncrypted"); + expect(section.customSymptoms[0].label).toBe("Back ache"); + expect(section.customSymptoms[0]).not.toHaveProperty("labelEncrypted"); + }); + + it("writes the unreadable marker where this host cannot open a value", async () => { + const section = await buildCycleBackupSection( + client( + { + notesEncrypted: "v9.bm90LWEtcmVhbC1jaXBoZXJ0ZXh0LWF0LWFsbC1yZWFsbHk=", + }, + null, + ), + "user-1", + {}, + ); + expect(section.cycleDayLogs[0].note).toBe(UNREADABLE_EXPORT_MARKER); + expect(section.cycleDayLogs[0].sensitive).toBeNull(); + expect(section.customSymptoms[0].label).toBeNull(); + }); +}); diff --git a/src/lib/cycle/backup.ts b/src/lib/cycle/backup.ts index 1a216b812..4862a41a3 100644 --- a/src/lib/cycle/backup.ts +++ b/src/lib/cycle/backup.ts @@ -5,10 +5,25 @@ * never drift. Mirrors the `backupPayloadSchema` cycle shapes. * * Reads are scoped to `userId` + `deletedAt: null` and exclude predicted - * (forecast) cycle rows — only observed history round-trips. `notesEncrypted` - * is carried as the ciphertext envelope VERBATIM: the backup never decrypts - * the day-log note, so a wrong-surface plaintext leak is impossible and the - * owner's note round-trips encrypted at rest. + * (forecast) cycle rows — only observed history round-trips. + * + * ## Free text follows the note contract + * + * Three columns are sealed: the day-log note, the sensitive-category envelope + * and a custom symptom's label. A disaster-recovery file carries them as the + * stored ciphertext, which the same instance's key reads back. A portable file + * carries them readable (`note`, `sensitive`, `label`), and the restore seals + * them under the receiving host's key, because a portable file exists to be + * moved, and ciphertext from one host is noise on another. + * + * Until v1.40 the portable file carried the ciphertext too, so a note restored + * onto a host with a different key was unreadable for good. Such a file is + * still accepted: every ciphertext value in it is opened with this host's keys + * before it is written, and one that does not open is kept out of the row and + * named in the skip report (`cycleCiphertext`), never written back as + * ciphertext nobody can read. The same check covers a disaster-recovery file, + * which is why `RESTORE_SELF_VERIFIED_SECTIONS` lets these sections past the + * restore's key preflight. */ import type { CervicalMucus, @@ -25,6 +40,9 @@ import type { PrismaClient, SecondarySymptom, } from "@/generated/prisma/client"; +import { decrypt, encrypt } from "@/lib/crypto"; +import { UNREADABLE_EXPORT_MARKER } from "@/lib/export/unreadable-marker"; +import { getEvent } from "@/lib/logging/context"; import type { BackupPayload } from "@/lib/validations/backup"; import { recordUnknownKeys, @@ -91,8 +109,17 @@ export interface CycleBackupSection { pregnancyTest: string | null; progesteroneTest: string | null; contraceptive: string | null; - sensitiveEncrypted: string | null; - notesEncrypted: string | null; + /** Ciphertext; present on a disaster-recovery payload. */ + sensitiveEncrypted?: string | null; + /** Ciphertext; present on a disaster-recovery payload. */ + notesEncrypted?: string | null; + /** Plaintext; present on a portable payload. */ + note?: string | null; + /** + * The sensitive-category envelope opened; present on a portable payload. + * The unreadable marker when this instance could not open it. + */ + sensitive?: CycleSensitiveFields | string | null; source: string; externalId: string | null; tz: string | null; @@ -142,18 +169,54 @@ export interface CycleBackupSection { sortOrder: number; isActive: boolean; /** - * The user's own words for the symptom, carried as ciphertext verbatim. + * The user's own words for the symptom: ciphertext on a disaster-recovery + * payload, plaintext (`label`) on a portable one. * - * The builder has always emitted it and this interface did not declare it, - * which held while the payload was assembled key by key and stopped holding - * the moment the section is spread onto the wire: an undeclared field is a - * field the wire type says is not there. Declared, so the type describes - * the file. + * Declared rather than left to the spread, because an undeclared field is + * a field the wire type says is not there. */ - labelEncrypted: string | null; + labelEncrypted?: string | null; + label?: string | null; }>; } +/** The five intent fields the sensitive-category envelope holds. */ +export interface CycleSensitiveFields { + sexualActivity?: boolean; + protectedSex?: boolean | null; + pregnancyTest?: string | null; + progesteroneTest?: string | null; + contraceptive?: string | null; +} + +/** Open a sealed value for a portable file, or the unreadable marker. */ +function openForExport(sealed: string, field: string): string { + try { + return decrypt(sealed); + } catch (err) { + getEvent()?.addWarning( + `cycle ${field} decrypt failed: ${ + err instanceof Error ? err.message : String(err) + }`, + ); + return UNREADABLE_EXPORT_MARKER; + } +} + +/** The sensitive envelope opened for a portable file, or the marker. */ +function openSensitiveForExport( + sealed: string | null, +): CycleSensitiveFields | string | null { + if (!sealed) return null; + const opened = openForExport(sealed, "sensitive envelope"); + if (opened === UNREADABLE_EXPORT_MARKER) return opened; + try { + return JSON.parse(opened) as CycleSensitiveFields; + } catch { + return UNREADABLE_EXPORT_MARKER; + } +} + /** * Build the cycle section of a user's full backup. Accepts any client with * the cycle delegates (the route's global client OR the worker's local @@ -259,8 +322,17 @@ export async function buildCycleBackupSection( pregnancyTest: d.pregnancyTest, progesteroneTest: d.progesteroneTest, contraceptive: d.contraceptive, - sensitiveEncrypted: d.sensitiveEncrypted, - notesEncrypted: d.notesEncrypted, + ...(disasterRecovery + ? { + sensitiveEncrypted: d.sensitiveEncrypted, + notesEncrypted: d.notesEncrypted, + } + : { + sensitive: openSensitiveForExport(d.sensitiveEncrypted), + note: d.notesEncrypted + ? openForExport(d.notesEncrypted, "day-log note") + : null, + }), source: d.source, externalId: d.externalId, tz: d.tz, @@ -277,7 +349,13 @@ export async function buildCycleBackupSection( icon: sym.icon, sortOrder: sym.sortOrder, isActive: sym.isActive, - labelEncrypted: sym.labelEncrypted, + ...(disasterRecovery + ? { labelEncrypted: sym.labelEncrypted } + : { + label: sym.labelEncrypted + ? openForExport(sym.labelEncrypted, "symptom label") + : null, + }), })), }; } @@ -369,6 +447,39 @@ function severityOrNull(value: number | null | undefined): number | null { : null; } +/** A readable value the file could not supply: the writer could not open it. */ +const UNOPENED = Symbol("unopened"); + +/** + * The sealed column value for one free-text field from the file. + * + * Ciphertext is written back only when this host opens it — a value sealed + * under a key this host does not hold (or the same key id over other key + * material) would be a row nobody can read. A readable value is sealed under + * this host's active key. Anything kept out is recorded under `path`. + */ +function sealForRestore( + sealed: string | null | undefined, + readable: string | null | undefined | typeof UNOPENED, + path: string, + unopened: string[], +): string | null { + if (sealed) { + try { + decrypt(sealed); + return sealed; + } catch { + unopened.push(path); + return null; + } + } + if (readable === UNOPENED) { + unopened.push(path); + return null; + } + return readable ? encrypt(readable) : null; +} + function enumOrNull( value: string | null | undefined, allow: ReadonlySet, @@ -383,8 +494,9 @@ function enumOrNull( * inside an existing transaction. Delete-then-recreate, matching the * measurement/mood restore contract. Symptom links are re-resolved against * the seeded catalogue by key, and each one gets back the 1-4 intensity the - * file recorded for it, or none if the file recorded none. `notesEncrypted` is - * written back as the ciphertext envelope verbatim — never re-encrypted. + * file recorded for it, or none if the file recorded none. Sealed free text + * comes back as the file-header comment describes: readable values sealed + * under this host's key, ciphertext written back only when this host opens it. * * A key that resolves against nothing is dropped from the day's links and * recorded on `skips`. It is a required argument rather than an optional one: @@ -400,6 +512,9 @@ export async function restoreCycleData( payload: BackupPayload, skips: RestoreSkipLog, ): Promise { + // Sealed values from the file this host's keys do not open, by file path. + const unopened: string[] = []; + // Wipe (child links cascade off the day-log delete). const dayLogs = await tx.cycleDayLog.deleteMany({ where: { userId: ownerId }, @@ -488,7 +603,12 @@ export async function restoreCycleData( icon: sym.icon ?? null, sortOrder: sym.sortOrder, isActive: sym.isActive, - labelEncrypted: sym.labelEncrypted ?? null, + labelEncrypted: sealForRestore( + sym.labelEncrypted, + sym.label, + `customSymptoms.${sym.key}.labelEncrypted`, + unopened, + ), }, // `key` is globally unique, so a seeded key would collide. Leave the // catalogue row alone: the account's links resolve against it either way. @@ -591,9 +711,22 @@ export async function restoreCycleData( pregnancyTest: enumOrNull(d.pregnancyTest, HOME_TESTS), progesteroneTest: enumOrNull(d.progesteroneTest, HOME_TESTS), contraceptive: enumOrNull(d.contraceptive, CONTRACEPTIVES), - // Both ciphertext envelopes written back verbatim — never re-encrypted. - sensitiveEncrypted: d.sensitiveEncrypted ?? null, - notesEncrypted: d.notesEncrypted ?? null, + sensitiveEncrypted: sealForRestore( + d.sensitiveEncrypted, + d.sensitive === undefined || d.sensitive === null + ? d.sensitive + : typeof d.sensitive === "string" + ? UNOPENED + : JSON.stringify(d.sensitive), + `cycleDayLogs.${d.date}.sensitiveEncrypted`, + unopened, + ), + notesEncrypted: sealForRestore( + d.notesEncrypted, + d.note, + `cycleDayLogs.${d.date}.notesEncrypted`, + unopened, + ), source: enumOrNull(d.source, CYCLE_SOURCES) ?? "MANUAL", externalId: d.externalId ?? null, tz: d.tz ?? null, @@ -608,6 +741,8 @@ export async function restoreCycleData( }); } + recordUnknownKeys(skips, "cycleCiphertext", unopened, unopened); + return { cycles: cycles.count, cycleDayLogs: dayLogs.count, diff --git a/src/lib/export/__tests__/backup-key-ids.test.ts b/src/lib/export/__tests__/backup-key-ids.test.ts index 03377c809..386f12878 100644 --- a/src/lib/export/__tests__/backup-key-ids.test.ts +++ b/src/lib/export/__tests__/backup-key-ids.test.ts @@ -138,6 +138,30 @@ describe("assessBackupKeys", () => { ).toEqual([]); }); + it("leaves the cycle sections to the restore, which opens each value itself", () => { + // A same-id impostor in a cycle note must neither refuse the file nor be + // picked as the sample that proves the key for the sections that need it. + const fine = encrypt("a measurement note under the real key"); + useKeys({ old: K3, cur: K2 }, "old"); + const impostor = encrypt("x"); + useKeys({ old: K1, cur: K2 }, "cur"); + const file = { + measurements: [{ notesEncrypted: fine }], + cycleDayLogs: [ + { notesEncrypted: impostor }, + { notesEncrypted: `gone.${Buffer.alloc(40).toString("base64")}` }, + ], + customSymptoms: [ + { labelEncrypted: `gone.${Buffer.alloc(40).toString("base64")}` }, + ], + }; + const collector = new BackupKeyIdCollector(); + collector.visit(file); + const verdict = assessBackupKeys(collector); + expect(verdict.missing).toEqual([]); + expect(verdict.unreadable).toEqual([]); + }); + it("catches an id that is configured with different key material", () => { const file = fileWrittenUnderOld(); useKeys({ old: K3, cur: K2 }, "cur"); diff --git a/src/lib/export/backup-key-ids.ts b/src/lib/export/backup-key-ids.ts index acbe75e07..aead1cb94 100644 --- a/src/lib/export/backup-key-ids.ts +++ b/src/lib/export/backup-key-ids.ts @@ -97,6 +97,25 @@ export function isEncryptedMember(name: string): boolean { return name.endsWith("Encrypted") && name.length > "Encrypted".length; } +/** + * Sections whose restore opens every inner value with this host's keys itself, + * and keeps out (and names in the skip report) any value it cannot open. + * + * The cycle day-logs and custom cycle symptoms are here because a portable + * file written before v1.40 carried their free text as the stored ciphertext, + * not as readable text. Such a file is still restorable onto another host: + * what that host can open comes back, what it cannot stays out of the row and + * is reported. Refusing the whole file over a note would cost every other + * part of the account. The trade, stated: a disaster-recovery copy whose ONLY + * missing key sits in these sections restores with those values reported as + * skipped instead of being refused; any other section needing the key still + * refuses it, which in practice is every real copy. + */ +export const RESTORE_SELF_VERIFIED_SECTIONS: ReadonlySet = new Set([ + "cycleDayLogs", + "customSymptoms", +]); + interface KeyUse { /** How many values need this key. */ count: number; @@ -127,7 +146,10 @@ export class BackupKeyIdCollector { } use.count += 1; use.sections.add(section); + // A sample from a self-verified section proves nothing the restore needs: + // a value there that does not open is skipped, not fatal. if ( + !RESTORE_SELF_VERIFIED_SECTIONS.has(section) && value.length <= MAX_SAMPLE_CHARS && (!use.sample || value.length < use.sample.value.length) ) { @@ -257,7 +279,9 @@ function openSample(sample: { value: string; form: InnerCiphertextForm }) { * key to prove the key under that id is the right one. * * `ignoreSections` leaves out sections the caller will not write: the - * instance settings, when the operator has not asked for them back. + * instance settings, when the operator has not asked for them back. The + * sections in {@link RESTORE_SELF_VERIFIED_SECTIONS} are always left out, + * because their restore checks each value itself. */ export function assessBackupKeys( collector: BackupKeyIdCollector, @@ -274,7 +298,14 @@ export function assessBackupKeys( for (const [keyId, use] of [...collector.entries()].sort(([a], [b]) => a.localeCompare(b), )) { - if ([...use.sections].every((section) => ignore.has(section))) continue; + if ( + [...use.sections].every( + (section) => + ignore.has(section) || RESTORE_SELF_VERIFIED_SECTIONS.has(section), + ) + ) { + continue; + } keyIds.push(keyId); configured ??= new Set(getConfiguredKeyIds()); if (!configured.has(keyId)) { diff --git a/src/lib/export/restore-skips.ts b/src/lib/export/restore-skips.ts index d79d09bbd..2b2ca30fc 100644 --- a/src/lib/export/restore-skips.ts +++ b/src/lib/export/restore-skips.ts @@ -152,6 +152,14 @@ * real foreign key, so the POINTER is dropped and the occurrence restores * unlinked. A portable export omits soft-deleted episodes, which is the * ordinary way an occurrence ends up naming one the file does not carry. + * + * The sixteenth, `cycleCiphertext`, is not a reference at all. It names a + * sealed cycle value (a day-log note, the sensitive-category envelope, a + * custom symptom's label) the file carried as ciphertext this host's keys do + * not open — a portable file written before v1.40 on a host with another key. + * Writing it back would store a value no reader can open, so the FIELD is kept + * out and the day or the symptom restores without it. Its key is the path in + * the file (`cycleDayLogs..notesEncrypted`), never the value. */ export type SkippedCatalogue = | "cycleSymptom" @@ -172,7 +180,8 @@ export type SkippedCatalogue = | "scheduleRevisionLink" | "checkupClosure" | "accountSetting" - | "symptomEpisodeReference"; + | "symptomEpisodeReference" + | "cycleCiphertext"; /** One key this instance does not know, and the links it cost. */ export interface SkippedCatalogueKey { diff --git a/src/lib/validations/backup.ts b/src/lib/validations/backup.ts index 8a9af3f62..ad0334bcc 100644 --- a/src/lib/validations/backup.ts +++ b/src/lib/validations/backup.ts @@ -625,6 +625,15 @@ const cycleSymptomSeveritySchema = z }) .passthrough(); +/** The sensitive-category envelope's five fields, as a portable file carries them. */ +const cycleSensitiveFieldsSchema = z.object({ + sexualActivity: z.boolean().optional(), + protectedSex: z.boolean().nullable().optional(), + pregnancyTest: z.enum(HomeTestResult).nullable().optional(), + progesteroneTest: z.enum(HomeTestResult).nullable().optional(), + contraceptive: z.enum(ContraceptiveKind).nullable().optional(), +}); + const cycleDayLogSchema = z .object({ id: z.string().min(1).optional(), @@ -644,8 +653,20 @@ const cycleDayLogSchema = z pregnancyTest: z.enum(HomeTestResult).nullable().optional(), progesteroneTest: z.enum(HomeTestResult).nullable().optional(), contraceptive: z.enum(ContraceptiveKind).nullable().optional(), + // Free text travels like every other note: a disaster-recovery file + // carries the ciphertext (`*Encrypted`), a portable file the readable + // value (`note`, `sensitive`), which the restore seals under the + // receiving host's key. A portable file written before v1.40 carries the + // ciphertext too; the restore keeps only what this host can open. sensitiveEncrypted: z.string().nullable().optional(), notesEncrypted: z.string().nullable().optional(), + note: z.string().nullable().optional(), + // The five intent fields the sensitive-category envelope holds, or the + // unreadable marker when the writing host could not open the envelope. + sensitive: z + .union([cycleSensitiveFieldsSchema, z.string()]) + .nullable() + .optional(), source: z.enum(MeasurementSource).optional(), externalId: z.string().nullable().optional(), tz: z.string().nullable().optional(), @@ -752,7 +773,10 @@ const customCycleSymptomSchema = z sortOrder: z.number().int().default(0), isActive: z.boolean().default(true), // Same reasoning as the mood tag's: the user's own words live here. + // Ciphertext on a disaster-recovery file, readable `label` on a portable + // one (the day-log note's split). labelEncrypted: z.string().nullable().optional(), + label: z.string().nullable().optional(), }) .passthrough(); diff --git a/tests/integration/admin-backups-canonical-roundtrip.test.ts b/tests/integration/admin-backups-canonical-roundtrip.test.ts index ee3015901..bb20699a6 100644 --- a/tests/integration/admin-backups-canonical-roundtrip.test.ts +++ b/tests/integration/admin-backups-canonical-roundtrip.test.ts @@ -391,8 +391,10 @@ describe("canonical disaster-recovery backup round-trip", () => { cervixFirmness: "FIRM", cervixOpening: "CLOSED", sexualActivity: false, - sensitiveEncrypted: "sensitive-cycle-dr", - notesEncrypted: "notes-cycle-dr", + // Real ciphertext: the restore writes a sealed value back only when + // this host opens it. + sensitiveEncrypted: encrypt(JSON.stringify({ sexualActivity: false })), + notesEncrypted: encrypt("notes-cycle-dr"), source: "APPLE_HEALTH", externalId: "cycle-day-external-dr", tz: "Europe/London", diff --git a/tests/integration/backup-cycle-cross-key-restore.test.ts b/tests/integration/backup-cycle-cross-key-restore.test.ts new file mode 100644 index 000000000..4d3f50aee --- /dev/null +++ b/tests/integration/backup-cycle-cross-key-restore.test.ts @@ -0,0 +1,313 @@ +/** + * A portable file's cycle free text survives a move to a host with other keys. + * + * The day-log note, the sensitive-category envelope and a custom symptom's + * label are sealed under the writing host's key. A portable file used to carry + * those three as the stored ciphertext, so a restore onto a host with a + * different key either refused the whole file at the key check or, with a + * matching key id over different key material, wrote rows nobody could open. + * + * Both ends and the pipe: the file is built by the real `buildFullBackupPayload` + * under key A, stored and restored through the real route under key B, and + * the restored rows are opened under key B. + * + * The second case is the file written before the fix, which still carries + * ciphertext. A value this host's keys open comes back; a value they cannot + * open is kept out of the row and named in the skip report, rather than + * written back as ciphertext no reader can open. + * + * Mutation: restore the verbatim copy of `notesEncrypted` in + * `restoreCycleData` and the legacy case writes the foreign ciphertext back. + */ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { _resetCryptoCacheForTests, decrypt, encrypt } from "@/lib/crypto"; +import { buildFullBackupPayload } from "@/lib/export/full-backup-payload"; +import { parseBackupPayload } from "@/lib/validations/backup"; +import { POST } from "./restore-job-driver"; + +import { cookieJar, headerJar } from "./mock-next-headers"; +import { getPrismaClient, truncateAllTables } from "./setup"; + +vi.mock("next/headers", async () => { + const { cookieJar, headerJar } = await import("./mock-next-headers"); + return { + headers: vi.fn(async () => ({ + get: (name: string) => headerJar.get(name.toLowerCase()) ?? null, + })), + cookies: vi.fn(async () => ({ + get: (name: string) => { + const value = cookieJar.get(name); + return value ? { name, value } : undefined; + }, + set: (name: string, value: string) => cookieJar.set(name, value), + delete: (name: string) => cookieJar.delete(name), + })), + }; +}); + +vi.mock("@/lib/db-compat", () => ({ + ensureDbCompatibility: vi.fn().mockResolvedValue(undefined), +})); + +vi.mock("@/lib/cache/invalidate", () => ({ + invalidateUserData: vi.fn(), +})); + +const saved = { + key: process.env.ENCRYPTION_KEY, + keys: process.env.ENCRYPTION_KEYS, + active: process.env.ENCRYPTION_ACTIVE_KEY_ID, +}; + +/** Make this process the host holding exactly `keys`, writing under `active`. */ +function useKeys(keys: Record, active: string) { + process.env.ENCRYPTION_KEY = ""; + process.env.ENCRYPTION_KEYS = JSON.stringify(keys); + process.env.ENCRYPTION_ACTIVE_KEY_ID = active; + _resetCryptoCacheForTests(); +} + +const HOST_A = { a: "11".repeat(32) }; +const HOST_B = { b: "22".repeat(32) }; +/** Same key id as host B, different key material. */ +const HOST_B_IMPOSTOR = { b: "33".repeat(32) }; + +const NOTE = "Cramps woke me at four"; +const SENSITIVE = { + sexualActivity: true, + protectedSex: false, + pregnancyTest: "NEGATIVE", + progesteroneTest: null, + contraceptive: "ORAL", +}; +const LABEL = "Lower back ache"; + +beforeEach(async () => { + await truncateAllTables(getPrismaClient()); + cookieJar.clear(); + headerJar.clear(); +}); + +afterEach(() => { + process.env.ENCRYPTION_KEY = saved.key ?? ""; + process.env.ENCRYPTION_KEYS = saved.keys ?? ""; + process.env.ENCRYPTION_ACTIVE_KEY_ID = saved.active ?? ""; + _resetCryptoCacheForTests(); +}); + +function makeRequest(id: string) { + return new Request(`http://localhost/api/admin/backups/${id}/restore`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ confirm: "RESTORE" }), + }); +} + +async function seedOwner() { + const prisma = getPrismaClient(); + const owner = await prisma.user.create({ + data: { + username: "cycle-cross-key-owner", + email: "cycle-cross-key-owner@example.test", + role: "ADMIN", + }, + }); + const session = await prisma.session.create({ + data: { userId: owner.id, expiresAt: new Date(Date.now() + 60_000) }, + }); + cookieJar.set("healthlog_session", session.id); + return owner; +} + +/** Store `payload` as a backup on this host and run the real restore. */ +async function restore(ownerId: string, payload: unknown) { + const prisma = getPrismaClient(); + const backup = await prisma.dataBackup.create({ + data: { + userId: ownerId, + type: "CYCLE_CROSS_KEY_RESTORE", + data: encrypt(JSON.stringify(payload)), + }, + }); + return POST(makeRequest(backup.id) as unknown as Parameters[0], { + params: Promise.resolve({ id: backup.id }), + }); +} + +describe("cycle free text in a portable file restored under another key", () => { + it("comes back readable under the receiving host's key", async () => { + const prisma = getPrismaClient(); + const owner = await seedOwner(); + + /* ── host A writes the account and exports it ─────────────────────── */ + useKeys(HOST_A, "a"); + const category = await prisma.cycleSymptomCategory.findFirstOrThrow(); + await prisma.cycleSymptom.create({ + data: { + userId: owner.id, + categoryId: category.id, + key: "custom:cross-key-ache", + labelKey: "cycle.symptom.custom", + labelEncrypted: encrypt(LABEL), + }, + }); + await prisma.cycleDayLog.create({ + data: { + userId: owner.id, + date: "2026-06-02", + flow: "MEDIUM", + notesEncrypted: encrypt(NOTE), + sensitiveEncrypted: encrypt(JSON.stringify(SENSITIVE)), + }, + }); + const { payload: built } = await buildFullBackupPayload(prisma, owner.id); + const file = JSON.parse(JSON.stringify(built)) as unknown; + + /* ── host B, which has never held key A, restores it ──────────────── */ + useKeys(HOST_B, "b"); + // Host B is another database: the account's own symptom row from host A + // is not there (the restore upserts by key and keeps an existing row). + await prisma.cycleDayLog.deleteMany({ where: { userId: owner.id } }); + await prisma.cycleSymptom.deleteMany({ where: { userId: owner.id } }); + const response = await restore(owner.id, file); + expect(response.status).toBe(200); + + const day = await prisma.cycleDayLog.findFirstOrThrow({ + where: { userId: owner.id }, + }); + expect(day.flow).toBe("MEDIUM"); + expect(day.notesEncrypted).not.toBeNull(); + expect(decrypt(day.notesEncrypted!)).toBe(NOTE); + expect(day.sensitiveEncrypted).not.toBeNull(); + expect(JSON.parse(decrypt(day.sensitiveEncrypted!))).toEqual(SENSITIVE); + + const symptom = await prisma.cycleSymptom.findUniqueOrThrow({ + where: { key: "custom:cross-key-ache" }, + }); + expect(decrypt(symptom.labelEncrypted!)).toBe(LABEL); + + const body = (await response.json()) as { + data: { skipped: { links: number } }; + }; + expect(body.data.skipped.links).toBe(0); + }); + + it("restores a legacy file's ciphertext only where this host can open it", async () => { + const prisma = getPrismaClient(); + const owner = await seedOwner(); + + // Written under host B's own key: opens here. + useKeys(HOST_B, "b"); + const ownNote = encrypt(NOTE); + // Written elsewhere: an id host B does not hold, and host B's id over + // different key material. + useKeys(HOST_A, "a"); + const foreignNote = encrypt("written under key a"); + const foreignLabel = encrypt("label under key a"); + useKeys(HOST_B_IMPOSTOR, "b"); + const impostorSensitive = encrypt(JSON.stringify(SENSITIVE)); + + // The shape a portable file had before the fix: every free-text field as + // the stored ciphertext, no plaintext beside it. + useKeys(HOST_B, "b"); + const { payload: built } = await buildFullBackupPayload(prisma, owner.id); + const file = JSON.parse(JSON.stringify(built)) as Record; + const category = await prisma.cycleSymptomCategory.findFirstOrThrow(); + file.customSymptoms = [ + { + key: "custom:legacy-foreign", + labelKey: "cycle.symptom.custom", + categoryId: category.id, + icon: null, + sortOrder: 0, + isActive: true, + labelEncrypted: foreignLabel, + }, + ]; + const day = ( + date: string, + notes: string | null, + sensitive: string | null, + ) => ({ + date, + flow: "LIGHT", + intermenstrualBleeding: false, + basalBodyTempC: null, + temperatureExcluded: false, + ovulationTest: null, + cervicalMucus: null, + cervixPosition: null, + cervixFirmness: null, + cervixOpening: null, + sexualActivity: false, + protectedSex: null, + pregnancyTest: null, + progesteroneTest: null, + contraceptive: null, + sensitiveEncrypted: sensitive, + notesEncrypted: notes, + source: "MANUAL", + externalId: null, + tz: null, + symptomKeys: [], + symptomSeverities: [], + }); + file.cycleDayLogs = [ + day("2026-06-01", ownNote, null), + day("2026-06-02", foreignNote, impostorSensitive), + ]; + // Still a file this release accepts. + parseBackupPayload(JSON.stringify(file)); + + const response = await restore(owner.id, file); + expect(response.status).toBe(200); + + const days = await prisma.cycleDayLog.findMany({ + where: { userId: owner.id }, + orderBy: { date: "asc" }, + }); + expect(days.map((d) => d.date)).toEqual(["2026-06-01", "2026-06-02"]); + expect(decrypt(days[0].notesEncrypted!)).toBe(NOTE); + // The day itself comes back; only what cannot be opened stays out. + expect(days[1].flow).toBe("LIGHT"); + expect(days[1].notesEncrypted).toBeNull(); + expect(days[1].sensitiveEncrypted).toBeNull(); + + const symptom = await prisma.cycleSymptom.findUniqueOrThrow({ + where: { key: "custom:legacy-foreign" }, + }); + expect(symptom.labelEncrypted).toBeNull(); + + const body = (await response.json()) as { + data: { + skipped: { + links: number; + catalogueKeys: Array<{ + catalogue: string; + key: string; + links: number; + }>; + }; + }; + }; + expect(body.data.skipped.catalogueKeys).toEqual([ + { + catalogue: "cycleCiphertext", + key: "customSymptoms.custom:legacy-foreign.labelEncrypted", + links: 1, + }, + { + catalogue: "cycleCiphertext", + key: "cycleDayLogs.2026-06-02.notesEncrypted", + links: 1, + }, + { + catalogue: "cycleCiphertext", + key: "cycleDayLogs.2026-06-02.sensitiveEncrypted", + links: 1, + }, + ]); + expect(body.data.skipped.links).toBe(3); + }); +}); diff --git a/tests/integration/cycle-import-export.test.ts b/tests/integration/cycle-import-export.test.ts index 73eed32e2..a6f3014b3 100644 --- a/tests/integration/cycle-import-export.test.ts +++ b/tests/integration/cycle-import-export.test.ts @@ -6,8 +6,8 @@ * into CYCLE day-logs (NOT Measurement), gated on cycle-tracking enabled, * merging same-day samples into one row with first-write-wins re-import * - the full-backup cycle section round-trips through - * `buildCycleBackupSection` → `restoreCycleData` with `notesEncrypted` - * preserved verbatim and symptom links re-resolved by key + * `buildCycleBackupSection` → `restoreCycleData` with the note carried + * readable, re-sealed on restore, and symptom links re-resolved by key */ import { writeFileSync, mkdtempSync } from "node:fs"; import { tmpdir } from "node:os"; @@ -17,6 +17,7 @@ import { beforeEach, describe, expect, it } from "vitest"; import { getPrismaClient, truncateAllTables } from "./setup"; import { streamParseExportXml } from "@/lib/measurements/import-apple-health-export"; import { buildCycleBackupSection, restoreCycleData } from "@/lib/cycle/backup"; +import { decrypt, encrypt } from "@/lib/crypto"; import type { RestoreSkipLog } from "@/lib/export/restore-skips"; import { parseBackupPayload } from "@/lib/validations/backup"; @@ -163,7 +164,7 @@ describe("cycle import — reproductive HK samples route to CycleDayLog", () => }); describe("cycle backup — round-trips through build + restore", () => { - it("preserves notesEncrypted ciphertext and symptom links by key", async () => { + it("carries the note readable and re-seals it, with symptom links by key", async () => { const prisma = getPrismaClient(); const cycle = await prisma.menstrualCycle.create({ @@ -184,7 +185,7 @@ describe("cycle backup — round-trips through build + restore", () => { cervixPosition: "HIGH", cervixFirmness: "SOFT", cervixOpening: "OPEN", - notesEncrypted: "v1:abc123:cipher-envelope", + notesEncrypted: encrypt("Cramps in the evening"), source: "MANUAL", }, }); @@ -207,9 +208,9 @@ describe("cycle backup — round-trips through build + restore", () => { const section = await buildCycleBackupSection(prisma, USER_ID); expect(section.cycles).toHaveLength(1); expect(section.cycleDayLogs).toHaveLength(1); - expect(section.cycleDayLogs[0].notesEncrypted).toBe( - "v1:abc123:cipher-envelope", - ); + // A portable section carries the note readable, never the ciphertext. + expect(section.cycleDayLogs[0].note).toBe("Cramps in the evening"); + expect(section.cycleDayLogs[0]).not.toHaveProperty("notesEncrypted"); expect(section.cycleDayLogs[0].symptomKeys).toEqual(["cramps"]); const payload = parseBackupPayload({ @@ -234,7 +235,7 @@ describe("cycle backup — round-trips through build + restore", () => { include: { symptomLinks: { include: { symptom: true } } }, }); expect(restored).toHaveLength(1); - expect(restored[0].notesEncrypted).toBe("v1:abc123:cipher-envelope"); + expect(decrypt(restored[0].notesEncrypted!)).toBe("Cramps in the evening"); expect(restored[0].cervixPosition).toBe("HIGH"); expect(restored[0].cervixFirmness).toBe("SOFT"); expect(restored[0].cervixOpening).toBe("OPEN");