mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 23:33:29 +00:00
Merge branch 'fix/encrypt-free-text-and-local-ai' into release/v1.39.3
# Conflicts: # src/app/api/import/csv/route.ts
This commit is contained in:
commit
66b1aef40c
69 files changed
+3625
-367
No files matched your search
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* v1.39.3 — the Coach conversation title in both backup formats.
|
||||
*
|
||||
* A disaster-recovery file carries the title as stored: the ciphertext, or
|
||||
* for a row the free-text backfill has not reached yet the old readable value
|
||||
* (sealing it on the way out would make the export non-deterministic). A
|
||||
* portable file carries it readable, for the person who owns it. A restore of
|
||||
* any of these 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 the title as stored", async () => {
|
||||
const section = await buildCoachBackupSection(prismaWith(rows), "u1", {
|
||||
purpose: "disaster-recovery",
|
||||
});
|
||||
const [sealed, legacy] = section.coachConversations;
|
||||
expect(JSON.stringify(sealed)).not.toContain("Sealed title");
|
||||
expect(sealed).not.toHaveProperty("title");
|
||||
expect(
|
||||
decryptFromBytes(Buffer.from(sealed.titleEncrypted!, "base64")),
|
||||
).toBe("Sealed title");
|
||||
expect(legacy).toMatchObject({ title: "Legacy readable title" });
|
||||
expect(legacy).not.toHaveProperty("titleEncrypted");
|
||||
// The same input exports byte-identically twice: nothing is sealed with
|
||||
// a fresh IV on the way out.
|
||||
const again = await buildCoachBackupSection(prismaWith(rows), "u1", {
|
||||
purpose: "disaster-recovery",
|
||||
});
|
||||
expect(JSON.stringify(again)).toBe(JSON.stringify(section));
|
||||
});
|
||||
|
||||
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",
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -1286,7 +1286,10 @@ describe("buildFullBackupPayload — profile and custom metrics", () => {
|
||||
value: 44.5,
|
||||
unit: "kg",
|
||||
measuredAt: "2026-07-19T06:30:00.000Z",
|
||||
// v1.39.3 — the fixture row predates the encryption backfill:
|
||||
// a DR file carries it as stored, and the restore seals it.
|
||||
note: "felt strong",
|
||||
noteEncrypted: null,
|
||||
}),
|
||||
],
|
||||
}),
|
||||
|
||||
@@ -578,8 +578,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,14 @@ 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, on every file written
|
||||
* before v1.39.3, and on a disaster-recovery payload for a row the free-text
|
||||
* backfill had not reached yet.
|
||||
*/
|
||||
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 +137,7 @@ export interface CoachBackupCounts {
|
||||
const COACH_CONVERSATION_BACKUP_SELECT = {
|
||||
id: true,
|
||||
title: true,
|
||||
titleEncrypted: true,
|
||||
documentScoped: true,
|
||||
summaryEncrypted: true,
|
||||
summaryUpdatedAt: true,
|
||||
@@ -174,6 +182,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 +209,18 @@ export async function buildCoachBackupSection(
|
||||
return {
|
||||
coachConversations: rows.map((row) => ({
|
||||
id: row.id,
|
||||
title: row.title,
|
||||
// A disaster-recovery file carries the stored bytes: the ciphertext,
|
||||
// or, for a row the free-text backfill has not reached yet, the old
|
||||
// readable title exactly as stored (the restore seals it). Sealing it
|
||||
// here instead would make the export non-deterministic, and the
|
||||
// streaming writer and this builder must produce the same file.
|
||||
...(disasterRecovery && row.titleEncrypted
|
||||
? {
|
||||
titleEncrypted: Buffer.from(row.titleEncrypted).toString("base64"),
|
||||
}
|
||||
: disasterRecovery
|
||||
? { title: row.title ?? "" }
|
||||
: { title: readTitleSoft(row) }),
|
||||
documentScoped: row.documentScoped,
|
||||
...(disasterRecovery
|
||||
? {
|
||||
@@ -293,12 +323,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 +378,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 +457,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,16 @@ export interface CustomMetricEntryBackupEntry {
|
||||
/** Unit snapshot taken from the metric at write time, not a live join. */
|
||||
unit: string;
|
||||
measuredAt: string;
|
||||
/**
|
||||
* The readable note: portable exports, and DR payloads for a row the
|
||||
* free-text backfill had not reached yet. Null otherwise.
|
||||
*/
|
||||
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 +237,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 +483,21 @@ 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 free-text
|
||||
// backfill has not reached yet carries its old readable note in
|
||||
// `note` instead, as stored; the restore seals it. Sealing it
|
||||
// here would make the export non-deterministic.
|
||||
noteEncrypted: toBase64(entry.noteEncrypted),
|
||||
}
|
||||
: {}),
|
||||
value: entry.value,
|
||||
unit: entry.unit,
|
||||
measuredAt: entry.measuredAt.toISOString(),
|
||||
note: entry.note,
|
||||
note: disasterRecovery
|
||||
? entry.noteEncrypted
|
||||
? null
|
||||
: entry.note
|
||||
: readEntryNoteSoft(entry, decryptFailures),
|
||||
})),
|
||||
}));
|
||||
|
||||
@@ -571,6 +616,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 +903,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