mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
fix(privacy): encrypt Coach conversation titles and custom-metric notes at rest
The title of a Coach conversation is the first 80 characters of the opening message, and the note on a custom-metric reading is free text; both were stored readable while every other note was encrypted. Migration 0357 adds coach_conversations.title_encrypted and custom_metric_entries.note_encrypted (the title loses its NOT NULL) plus partial indexes on the backfill's discovery predicate. Every writer now stores ciphertext only: conversation create, proactive nudge, reminder surfacing, rename, entry create and edit, and both restore formats. Readers prefer the ciphertext and fall back to the old column for rows not yet migrated; a ciphertext that does not open throws instead of falling back. The history title search decrypts in memory, since SQL can no longer match the title. free-text-encryption-backfill is a boot-time, idempotent pg-boss job (staggered past the startup storm, daily discovery tick) that seals each remaining readable value and nulls it in one transaction per row. A pass that sees a row twice stops with an error instead of looping. Both columns are registered for key rotation (registry, CLI script, and the in-app rotation that walks the registry). A disaster-recovery backup carries ciphertext only, sealing a not-yet-migrated row on the way out; a portable export carries readable text. The old columns are marked for removal in a later release.
This commit is contained in:
1 parent
4af6b87a2a
commit
f7b3ce6ee0
28 files changed
+1735
-83
No files matched your search
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* v1.39.3 — the Coach conversation title in both backup formats.
|
||||
*
|
||||
* A disaster-recovery file carries the title as ciphertext only, including for
|
||||
* a row the free-text backfill has not reached yet (it is sealed on the way
|
||||
* out). A portable file carries it readable, for the person who owns it. A
|
||||
* restore of either writes the ciphertext column and never the readable one,
|
||||
* and a file written before this release (readable title only) still restores.
|
||||
*/
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
process.env.ENCRYPTION_KEY ??=
|
||||
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
|
||||
import { decryptFromBytes, encryptToBytes } from "@/lib/ai/coach/bytes-codec";
|
||||
import {
|
||||
buildCoachBackupSection,
|
||||
restoreCoachData,
|
||||
type RestoredCoachConversation,
|
||||
} from "../coach-backup";
|
||||
import { parseBackupPayload } from "@/lib/validations/backup";
|
||||
|
||||
const at = "2026-09-20T10:00:00.000Z";
|
||||
|
||||
function row(id: string, title: string | null, sealed: string | null) {
|
||||
return {
|
||||
id,
|
||||
title,
|
||||
titleEncrypted: sealed === null ? null : encryptToBytes(sealed),
|
||||
documentScoped: false,
|
||||
summaryEncrypted: null,
|
||||
summaryUpdatedAt: null,
|
||||
summaryTurnCount: 0,
|
||||
createdAt: new Date(at),
|
||||
updatedAt: new Date(at),
|
||||
messages: [],
|
||||
attachments: [],
|
||||
};
|
||||
}
|
||||
|
||||
function prismaWith(rows: ReturnType<typeof row>[]) {
|
||||
return {
|
||||
coachConversation: { findMany: vi.fn().mockResolvedValue(rows) },
|
||||
} as never;
|
||||
}
|
||||
|
||||
const rows = [
|
||||
row("c1", null, "Sealed title"),
|
||||
row("c2", "Legacy readable title", null),
|
||||
];
|
||||
|
||||
describe("coach backup title", () => {
|
||||
it("a disaster-recovery file carries only ciphertext, sealing legacy rows on the way out", async () => {
|
||||
const section = await buildCoachBackupSection(prismaWith(rows), "u1", {
|
||||
purpose: "disaster-recovery",
|
||||
});
|
||||
const json = JSON.stringify(section);
|
||||
expect(json).not.toContain("Sealed title");
|
||||
expect(json).not.toContain("Legacy readable title");
|
||||
expect(
|
||||
section.coachConversations.map((c) => {
|
||||
expect(c).not.toHaveProperty("title");
|
||||
return decryptFromBytes(Buffer.from(c.titleEncrypted!, "base64"));
|
||||
}),
|
||||
).toEqual(["Sealed title", "Legacy readable title"]);
|
||||
});
|
||||
|
||||
it("a portable file carries the readable title and no ciphertext", async () => {
|
||||
const section = await buildCoachBackupSection(prismaWith(rows), "u1", {
|
||||
purpose: "portable-export",
|
||||
});
|
||||
expect(section.coachConversations.map((c) => c.title)).toEqual([
|
||||
"Sealed title",
|
||||
"Legacy readable title",
|
||||
]);
|
||||
for (const c of section.coachConversations) {
|
||||
expect(c).not.toHaveProperty("titleEncrypted");
|
||||
}
|
||||
});
|
||||
|
||||
it("a restore of either format, and of an older file, writes only the ciphertext", async () => {
|
||||
const created: Array<Record<string, unknown>> = [];
|
||||
const tx = {
|
||||
coachMessage: { count: vi.fn().mockResolvedValue(0) },
|
||||
coachConversationDocument: {
|
||||
count: vi.fn().mockResolvedValue(0),
|
||||
createMany: vi.fn(),
|
||||
},
|
||||
coachConversation: {
|
||||
deleteMany: vi.fn().mockResolvedValue({ count: 0 }),
|
||||
create: vi.fn(async (args: { data: Record<string, unknown> }) => {
|
||||
created.push(args.data);
|
||||
return args.data;
|
||||
}),
|
||||
},
|
||||
};
|
||||
const base = {
|
||||
documentScoped: false,
|
||||
createdAt: at,
|
||||
updatedAt: at,
|
||||
messages: [],
|
||||
attachments: [],
|
||||
};
|
||||
const conversations: RestoredCoachConversation[] = [
|
||||
{
|
||||
...base,
|
||||
id: "dr",
|
||||
titleEncrypted: Buffer.from(encryptToBytes("From DR")).toString(
|
||||
"base64",
|
||||
),
|
||||
},
|
||||
{ ...base, id: "portable", title: "From a portable file" },
|
||||
];
|
||||
await restoreCoachData(
|
||||
tx as never,
|
||||
"u1",
|
||||
{ coachConversations: conversations },
|
||||
new Set(),
|
||||
{ entries: [] } as never,
|
||||
);
|
||||
expect(created.map((d) => d.title)).toEqual([undefined, undefined]);
|
||||
expect(
|
||||
created.map((d) => decryptFromBytes(d.titleEncrypted as Uint8Array)),
|
||||
).toEqual(["From DR", "From a portable file"]);
|
||||
});
|
||||
|
||||
it("the wire schema accepts a pre-release file with a readable title", () => {
|
||||
const parsed = parseBackupPayload({
|
||||
schemaVersion: "2",
|
||||
exportedAt: at,
|
||||
userId: "u1",
|
||||
coachConversations: [
|
||||
{
|
||||
id: "old",
|
||||
title: "Written before the title was sealed",
|
||||
documentScoped: false,
|
||||
createdAt: at,
|
||||
updatedAt: at,
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(parsed.coachConversations[0].title).toBe(
|
||||
"Written before the title was sealed",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { Buffer } from "node:buffer";
|
||||
import { encryptToBytes } from "@/lib/ai/coach/bytes-codec";
|
||||
import { decryptFromBytes, encryptToBytes } from "@/lib/ai/coach/bytes-codec";
|
||||
import { parseBackupPayload } from "@/lib/validations/backup";
|
||||
|
||||
process.env.ENCRYPTION_KEY =
|
||||
@@ -1286,11 +1286,22 @@ describe("buildFullBackupPayload — profile and custom metrics", () => {
|
||||
value: 44.5,
|
||||
unit: "kg",
|
||||
measuredAt: "2026-07-19T06:30:00.000Z",
|
||||
note: "felt strong",
|
||||
// v1.39.3 — a DR file carries the note sealed, never readable,
|
||||
// including for a row the backfill has not reached yet.
|
||||
note: null,
|
||||
noteEncrypted: expect.any(String),
|
||||
}),
|
||||
],
|
||||
}),
|
||||
]);
|
||||
const [sealedMetric] = payload.customMetrics as Array<{
|
||||
entries: Array<{ noteEncrypted: string }>;
|
||||
}>;
|
||||
expect(
|
||||
decryptFromBytes(
|
||||
Buffer.from(sealedMetric.entries[0].noteEncrypted, "base64"),
|
||||
),
|
||||
).toBe("felt strong");
|
||||
expect(counts.customMetrics).toBe(1);
|
||||
expect(counts.customMetricEntries).toBe(1);
|
||||
expect(payload.correlationPatterns).toEqual([
|
||||
|
||||
@@ -572,8 +572,11 @@ describe("backup round trip — export, wire schema, restore", () => {
|
||||
expect(entries[0]).toMatchObject({
|
||||
value: 44.5,
|
||||
unit: "kg",
|
||||
note: "felt strong",
|
||||
});
|
||||
// v1.39.3 — the note comes back sealed; the readable column is never
|
||||
// written, even for a row that was still readable when it was exported.
|
||||
expect(entries[0]).not.toHaveProperty("note");
|
||||
expect(entries[0].noteEncrypted).toBeInstanceOf(Uint8Array);
|
||||
});
|
||||
|
||||
it("writes persisted pattern identity and dismissal evidence back", async () => {
|
||||
|
||||
@@ -92,7 +92,13 @@ export interface CoachConversationBackupEntry {
|
||||
/** Always carried: messages, attachments and the Coach's own facts, plans
|
||||
* and reminders all address a conversation by it. */
|
||||
id: string;
|
||||
title: string;
|
||||
/**
|
||||
* The readable title. Present on a portable payload only (and on every file
|
||||
* written before v1.39.3, when the title was stored readable).
|
||||
*/
|
||||
title?: string;
|
||||
/** v1.39.3 — the title's ciphertext as base64. Disaster-recovery only. */
|
||||
titleEncrypted?: string;
|
||||
/**
|
||||
* The permanent fence marker. Carried and restored verbatim; see the file
|
||||
* header for why letting this default is a security regression and not a
|
||||
@@ -130,6 +136,7 @@ export interface CoachBackupCounts {
|
||||
const COACH_CONVERSATION_BACKUP_SELECT = {
|
||||
id: true,
|
||||
title: true,
|
||||
titleEncrypted: true,
|
||||
documentScoped: true,
|
||||
summaryEncrypted: true,
|
||||
summaryUpdatedAt: true,
|
||||
@@ -174,6 +181,17 @@ function decryptTurnSoft(bytes: Uint8Array | null): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
/** A title for a reader: ciphertext first, legacy readable column after. */
|
||||
function readTitleSoft(row: {
|
||||
title: string | null;
|
||||
titleEncrypted: Uint8Array | null;
|
||||
}): string {
|
||||
if (row.titleEncrypted && row.titleEncrypted.byteLength > 0) {
|
||||
return decryptTurnSoft(row.titleEncrypted) ?? "";
|
||||
}
|
||||
return row.title ?? "";
|
||||
}
|
||||
|
||||
export async function buildCoachBackupSection(
|
||||
prisma: Pick<PrismaClient, "coachConversation">,
|
||||
userId: string,
|
||||
@@ -190,7 +208,15 @@ export async function buildCoachBackupSection(
|
||||
return {
|
||||
coachConversations: rows.map((row) => ({
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
...(disasterRecovery
|
||||
? {
|
||||
// A row the backfill has not reached yet is sealed here, so a DR
|
||||
// file never carries a title readable.
|
||||
titleEncrypted: Buffer.from(
|
||||
row.titleEncrypted ?? encryptToBytes(row.title ?? ""),
|
||||
).toString("base64"),
|
||||
}
|
||||
: { title: readTitleSoft(row) }),
|
||||
documentScoped: row.documentScoped,
|
||||
...(disasterRecovery
|
||||
? {
|
||||
@@ -293,12 +319,17 @@ export type RestoredCoachMessage = Pick<
|
||||
|
||||
export type RestoredCoachConversation = Pick<
|
||||
CoachConversationBackupEntry,
|
||||
"id" | "title" | "documentScoped" | "createdAt" | "updatedAt"
|
||||
"id" | "documentScoped" | "createdAt" | "updatedAt"
|
||||
> &
|
||||
OptionalNullable<
|
||||
Pick<
|
||||
CoachConversationBackupEntry,
|
||||
"summaryEncrypted" | "summary" | "summaryUpdatedAt" | "summaryTurnCount"
|
||||
| "title"
|
||||
| "titleEncrypted"
|
||||
| "summaryEncrypted"
|
||||
| "summary"
|
||||
| "summaryUpdatedAt"
|
||||
| "summaryTurnCount"
|
||||
>
|
||||
> & {
|
||||
messages: RestoredCoachMessage[];
|
||||
@@ -343,7 +374,9 @@ export async function restoreCoachData(
|
||||
data: {
|
||||
id: conversation.id,
|
||||
userId: ownerId,
|
||||
title: conversation.title,
|
||||
// v1.39.3 — the title is stored encrypted; the readable column is
|
||||
// never written on a restore.
|
||||
titleEncrypted: resolveTitleBytes(conversation),
|
||||
// Verbatim. See the file header: this is the one field whose loss is a
|
||||
// security regression rather than a gap in the history.
|
||||
documentScoped: conversation.documentScoped,
|
||||
@@ -420,6 +453,15 @@ function resolveTurnBytes(
|
||||
return encryptToBytes(message.content ?? "");
|
||||
}
|
||||
|
||||
function resolveTitleBytes(
|
||||
conversation: RestoredCoachConversation,
|
||||
): Uint8Array<ArrayBuffer> {
|
||||
if (conversation.titleEncrypted !== undefined) {
|
||||
return decodeBase64(conversation.titleEncrypted);
|
||||
}
|
||||
return encryptToBytes(conversation.title ?? "");
|
||||
}
|
||||
|
||||
function resolveSummaryBytes(
|
||||
conversation: RestoredCoachConversation,
|
||||
): Uint8Array<ArrayBuffer> | null {
|
||||
|
||||
@@ -25,6 +25,7 @@ import { Buffer } from "node:buffer";
|
||||
|
||||
import type { Prisma, PrismaClient } from "@/generated/prisma/client";
|
||||
import { decryptFromBytes, encryptToBytes } from "@/lib/ai/coach/bytes-codec";
|
||||
import { encryptNote } from "@/lib/crypto/note-cipher";
|
||||
import { getEvent } from "@/lib/logging/context";
|
||||
import {
|
||||
DEFAULT_HEALTH_PROFILE_AI_SECTIONS,
|
||||
@@ -116,7 +117,13 @@ export interface CustomMetricEntryBackupEntry {
|
||||
/** Unit snapshot taken from the metric at write time, not a live join. */
|
||||
unit: string;
|
||||
measuredAt: string;
|
||||
/** Portable exports carry the readable note; DR payloads leave this null. */
|
||||
note: string | null;
|
||||
/**
|
||||
* v1.39.3 — base64 ciphertext of the note, present only in disaster-recovery
|
||||
* payloads (null when the reading has no note).
|
||||
*/
|
||||
noteEncrypted?: string | null;
|
||||
createdAt?: string;
|
||||
/** v1.37.20 (A3-11) — soft-delete tombstone; DR payloads only. */
|
||||
deletedAt?: string | null;
|
||||
@@ -227,6 +234,32 @@ function decryptProfileFieldSoft(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* v1.39.3 — a custom-metric reading's note for a portable export: ciphertext
|
||||
* first, the legacy readable column for a row the backfill has not reached.
|
||||
* An unreadable ciphertext is recorded in the manifest and exported as no
|
||||
* note, never replaced by the readable column.
|
||||
*/
|
||||
function readEntryNoteSoft(
|
||||
entry: { id: string; note: string | null; noteEncrypted: Uint8Array | null },
|
||||
failures: string[],
|
||||
): string | null {
|
||||
if (entry.noteEncrypted && entry.noteEncrypted.byteLength > 0) {
|
||||
try {
|
||||
return decryptFromBytes(entry.noteEncrypted);
|
||||
} catch (err) {
|
||||
getEvent()?.addWarning(
|
||||
`custom metric entry note decrypt failed: ${
|
||||
err instanceof Error ? err.message : String(err)
|
||||
}`,
|
||||
);
|
||||
failures.push(`customMetricEntry.${entry.id}.note`);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return entry.note;
|
||||
}
|
||||
|
||||
function toBase64(buf: Uint8Array | null): string | null {
|
||||
return buf && buf.byteLength > 0 ? Buffer.from(buf).toString("base64") : null;
|
||||
}
|
||||
@@ -447,12 +480,18 @@ export async function buildProfileBackupSection(
|
||||
id: entry.id,
|
||||
createdAt: entry.createdAt.toISOString(),
|
||||
deletedAt: entry.deletedAt?.toISOString() ?? null,
|
||||
// v1.39.3 — the ciphertext rides verbatim. A row the backfill has
|
||||
// not reached yet is sealed here, so a DR file never carries the
|
||||
// note readable either way.
|
||||
noteEncrypted: toBase64(
|
||||
entry.noteEncrypted ?? encryptNote(entry.note),
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
value: entry.value,
|
||||
unit: entry.unit,
|
||||
measuredAt: entry.measuredAt.toISOString(),
|
||||
note: entry.note,
|
||||
note: disasterRecovery ? null : readEntryNoteSoft(entry, decryptFailures),
|
||||
})),
|
||||
}));
|
||||
|
||||
@@ -571,6 +610,16 @@ function resolveProfileColumn(
|
||||
return plaintext == null ? null : encryptToBytes(plaintext);
|
||||
}
|
||||
|
||||
function resolveEntryNote(entry: {
|
||||
note?: string | null;
|
||||
noteEncrypted?: string | null;
|
||||
}): Uint8Array<ArrayBuffer> | null {
|
||||
if (entry.noteEncrypted !== undefined && entry.noteEncrypted !== null) {
|
||||
return decodeEncryptedField(entry.noteEncrypted, "customMetricEntry.note");
|
||||
}
|
||||
return encryptNote(entry.note);
|
||||
}
|
||||
|
||||
function resolveFactValue(
|
||||
entry: HealthProfileFactBackupEntry,
|
||||
): Uint8Array<ArrayBuffer> {
|
||||
@@ -848,7 +897,10 @@ export async function restoreProfileData(
|
||||
value: entry.value,
|
||||
unit: entry.unit,
|
||||
measuredAt: new Date(entry.measuredAt),
|
||||
note: entry.note,
|
||||
// v1.39.3 — ciphertext wins when a DR payload carried it; a
|
||||
// portable file's readable note is sealed under this instance's
|
||||
// key. The readable column is never written.
|
||||
noteEncrypted: resolveEntryNote(entry),
|
||||
deletedAt: entry.deletedAt ? new Date(entry.deletedAt) : null,
|
||||
...(entry.createdAt
|
||||
? { createdAt: new Date(entry.createdAt) }
|
||||
|
||||
Reference in new issue
Block a user