Merge branch 'fix/backup-rotation-and-deletion' into release/v1.39.3

# Conflicts:
#	docs/api/openapi.yaml
#	src/lib/jobs/apple-health-import-worker.ts
This commit is contained in:
Marc-André Bombeck committed 2026-09-26 21:48:20 +02:00
commit 4409529c89
74 files changed
+4022 -307

No files matched your search

@@ -549,7 +549,11 @@ describe("canonical disaster-recovery backup round-trip", () => {
const offhostObject = s3.objects.get(`2026-07-02/user-${ownerId}.json.enc`);
expect(offhostObject).toBeDefined();
const payload = parseBackupPayload(
decryptBackup(offhostObject!, Buffer.from(OFFHOST_ENCRYPTION_KEY, "hex")),
decryptBackup(
offhostObject!,
Buffer.from(OFFHOST_ENCRYPTION_KEY, "hex"),
`2026-07-02/user-${ownerId}.json.enc`,
),
);
expect(payload).toMatchObject({
schemaVersion: "2",
@@ -219,3 +219,28 @@ describe("apple health import worker — failure paths (issue #486)", () => {
expect(existsSync(uploadPath)).toBe(false);
});
});
describe("an account deleted before the worker reaches its upload", () => {
it("removes the staged export and does not fail on the missing account", async () => {
const user = await createUser("deleted-before-import");
const uploadPath = join(
scratchDir,
"healthlog-apple-health-import-gone.bin",
);
writeFileSync(uploadPath, "the export, in plain text");
// The account goes, and its ImportJob row with it by cascade.
await getPrismaClient().user.delete({ where: { id: user.id } });
const outcome = await handleAppleHealthImport(
bossJob("boss-gone-1", {
userId: user.id,
uploadPath,
uploadBytes: 25,
enqueuedAt: new Date().toISOString(),
}),
);
expect(outcome).toMatchObject({ ok: true });
expect(existsSync(uploadPath)).toBe(false);
expect(await getPrismaClient().importJob.count()).toBe(0);
});
});
+126
View File
@@ -0,0 +1,126 @@
/**
* A stored backup records the keys its content needs, and the encryption view
* says a retired key is still needed while any backup lists it.
*
* Both ends and the pipe: the copy is written by the real `storeBackupBlob`,
* the key ids are read from the JSON on its way into the pieces, and the
* admin status route reads them back. The case the view used to get wrong is
* the one asserted: every row rotated, the badge said the old key was safe to
* drop, and a backup still needed it.
*
* Mutations that must turn this red: drop the scanner from `storeBackupBlob`
* (nothing is recorded, the view has no key to name), or leave
* `retiredKeysStillNeeded` out of `safeToDropRetiredKeys` (the badge says
* safe again).
*/
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { _resetCryptoCacheForTests, encrypt } from "@/lib/crypto";
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),
}));
const saved = {
key: process.env.ENCRYPTION_KEY,
keys: process.env.ENCRYPTION_KEYS,
active: process.env.ENCRYPTION_ACTIVE_KEY_ID,
};
function useKeys(active: string) {
process.env.ENCRYPTION_KEY = "";
process.env.ENCRYPTION_KEYS = JSON.stringify({
old: "11".repeat(32),
cur: "22".repeat(32),
});
process.env.ENCRYPTION_ACTIVE_KEY_ID = active;
_resetCryptoCacheForTests();
}
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();
});
describe("the keys stored backups still need", () => {
it("names a retired key a backup's content was written under", async () => {
const prisma = getPrismaClient();
const admin = await prisma.user.create({
data: { username: "keys-admin", email: "k@example.test", role: "ADMIN" },
});
const session = await prisma.session.create({
data: { userId: admin.id, expiresAt: new Date(Date.now() + 60_000) },
});
cookieJar.set("healthlog_session", session.id);
const { storeBackupBlob } = await import("@/lib/export/store-backup-blob");
// A copy taken before the rotation: its note was written under "old".
useKeys("old");
const note = Buffer.from(encrypt("before"), "utf8").toString("base64");
// The copy itself is stored after the rotation, so its envelope is
// sealed under "cur" and only its content needs "old".
useKeys("cur");
await storeBackupBlob(
prisma,
{ userId: admin.id, type: "MANUAL_UPLOAD_KEYS" },
async (write) => {
await write('{"userId":"x","measurements":[{"notesEncrypted":');
await write(`"${note}"}]}`);
},
);
const row = await prisma.dataBackup.findFirstOrThrow({
where: { type: "MANUAL_UPLOAD_KEYS" },
});
expect(row.innerKeyIds).toEqual(["old"]);
expect(row.innerKeyIdsRecorded).toBe(true);
const { GET } = await import("@/app/api/admin/encryption/status/route");
const res = await GET();
expect(res.status).toBe(200);
const body = (await res.json()).data as {
rotationComplete: boolean;
safeToDropRetiredKeys: boolean;
backups: {
stored: Array<{ keyId: string; copies: number }>;
retiredKeysStillNeeded: string[];
};
};
expect(body.backups.stored).toEqual([
expect.objectContaining({ keyId: "old", copies: 1 }),
]);
expect(body.backups.retiredKeysStillNeeded).toEqual(["old"]);
expect(body.safeToDropRetiredKeys).toBe(false);
});
});
@@ -0,0 +1,262 @@
/**
* Two refusals a restore has to make before it changes anything.
*
* 1. The file's inner ciphertext was written under a key this host no
* longer has. The envelope opens (it is sealed under the current key),
* the schema passes, and without the check the restore would write back
* rows no reader can open. Refused with the key named; the account keeps
* what it had.
*
* 2. The file attaches the account's rows to rows another account owns: a
* custom mood tag filed under another account's category. The foreign
* key is satisfied, so only the tenant check stands in the way. Refused;
* the transaction rolls back and the account keeps what it had.
*
* Mutations that must turn these red: drop the `keyProblem` refusal in
* `restoreBackup` (case 1 restores and the note comes back unreadable), or the
* `assertNoNewForeignReferences` call (case 2 commits the foreign link).
*/
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { _resetCryptoCacheForTests, encrypt } from "@/lib/crypto";
import { STORED_BACKUP_SELECT } from "@/lib/export/stored-backup";
import { restoreBackup } from "@/lib/export/restore-backup";
import { getPrismaClient, truncateAllTables } from "./setup";
const saved = {
key: process.env.ENCRYPTION_KEY,
keys: process.env.ENCRYPTION_KEYS,
active: process.env.ENCRYPTION_ACTIVE_KEY_ID,
};
function useKeys(keys: Record<string, string>, active: string) {
process.env.ENCRYPTION_KEY = "";
process.env.ENCRYPTION_KEYS = JSON.stringify(keys);
process.env.ENCRYPTION_ACTIVE_KEY_ID = active;
_resetCryptoCacheForTests();
}
beforeEach(async () => {
await truncateAllTables(getPrismaClient());
});
afterEach(() => {
process.env.ENCRYPTION_KEY = saved.key ?? "";
process.env.ENCRYPTION_KEYS = saved.keys ?? "";
process.env.ENCRYPTION_ACTIVE_KEY_ID = saved.active ?? "";
_resetCryptoCacheForTests();
});
async function seedOwner(username: string) {
return getPrismaClient().user.create({
data: { username, email: `${username}@example.test` },
});
}
async function storeBackup(userId: string, payload: unknown) {
const prisma = getPrismaClient();
const row = await prisma.dataBackup.create({
data: {
userId,
type: "MANUAL_UPLOAD_BOUNDARY_TEST",
data: encrypt(JSON.stringify(payload)),
},
});
return prisma.dataBackup.findUniqueOrThrow({
where: { id: row.id },
select: STORED_BACKUP_SELECT,
});
}
function basePayload(userId: string) {
return {
schemaVersion: "1",
exportedAt: "2026-05-09T10:00:00.000Z",
userId,
measurements: [] as unknown[],
medications: [],
intakeEvents: [],
moodEntries: [],
};
}
describe("restore refuses before it changes anything", () => {
it("refuses a file whose inner ciphertext needs a key the host dropped", async () => {
const prisma = getPrismaClient();
useKeys({ old: "11".repeat(32), cur: "22".repeat(32) }, "old");
const owner = await seedOwner("key-owner");
const note = Buffer.from(encrypt("written before the rotation"), "utf8");
const payload = basePayload(owner.id);
payload.measurements = [
{
type: "WEIGHT",
value: 80,
unit: "kg",
measuredAt: "2026-05-08T07:00:00.000Z",
source: "MANUAL",
notesEncrypted: note.toString("base64"),
},
];
// The rotation ran and the operator dropped the old key; the stored copy's
// envelope is sealed under the key that remains.
useKeys({ cur: "22".repeat(32) }, "cur");
const backup = await storeBackup(owner.id, payload);
const kept = await prisma.measurement.create({
data: {
userId: owner.id,
type: "WEIGHT",
value: 99,
unit: "kg",
measuredAt: new Date("2026-05-01T07:00:00.000Z"),
source: "MANUAL",
},
});
const outcome = await restoreBackup({
backup,
actorUserId: owner.id,
ipAddress: null,
restoreInstanceSettings: false,
});
expect(outcome.ok).toBe(false);
if (outcome.ok) return;
expect(outcome.code).toBe("backup.key.missing");
expect(outcome.message).toContain("'old'");
expect(outcome.meta?.keyIds).toEqual(["old"]);
const rows = await prisma.measurement.findMany({
where: { userId: owner.id },
});
expect(rows.map((r) => r.id)).toEqual([kept.id]);
// With the key back, the same copy restores and the note opens.
useKeys({ old: "11".repeat(32), cur: "22".repeat(32) }, "cur");
const again = await restoreBackup({
backup,
actorUserId: owner.id,
ipAddress: null,
restoreInstanceSettings: false,
});
expect(again.ok).toBe(true);
const restored = await prisma.measurement.findFirstOrThrow({
where: { userId: owner.id },
});
expect(restored.notesEncrypted).not.toBeNull();
expect(
Buffer.from(restored.notesEncrypted!).toString("utf8").startsWith("old."),
).toBe(true);
});
it("refuses a file that files a tag under another account's category", async () => {
const prisma = getPrismaClient();
const owner = await seedOwner("tenant-owner");
const other = await seedOwner("tenant-other");
const foreign = await prisma.moodTagCategory.create({
data: {
userId: other.id,
key: "custom:other-category",
labelKey: "mood.tagCategory.custom",
},
});
const kept = await prisma.moodEntry.create({
data: {
userId: owner.id,
date: "2026-05-01",
mood: "OKAY",
score: 3,
moodLoggedAt: new Date("2026-05-01T20:00:00.000Z"),
},
});
const payload = {
...basePayload(owner.id),
moodEntries: [
{
date: "2026-05-08",
mood: "GUT",
score: 4,
tags: null,
source: "MOODLOG",
loggedAt: "2026-05-08T20:00:00.000Z",
structuredTags: ["custom:owner-tag"],
},
],
customMoodTags: [
{
key: "custom:owner-tag",
labelKey: "mood.tag.custom",
categoryId: foreign.id,
kind: "BINARY",
},
],
};
const backup = await storeBackup(owner.id, payload);
const outcome = await restoreBackup({
backup,
actorUserId: owner.id,
ipAddress: null,
restoreInstanceSettings: false,
});
expect(outcome.ok).toBe(false);
if (outcome.ok) return;
expect(outcome.code).toBe("backup.foreign_reference");
expect(
await prisma.moodTag.count({ where: { key: "custom:owner-tag" } }),
).toBe(0);
const moods = await prisma.moodEntry.findMany({
where: { userId: owner.id },
});
expect(moods.map((m) => m.id)).toEqual([kept.id]);
expect(outcome.message).toContain("mood_tags.category_id");
});
it("restores a file whose tag uses the account's own category", async () => {
const prisma = getPrismaClient();
const owner = await seedOwner("tenant-own");
const payload = {
...basePayload(owner.id),
moodEntries: [
{
date: "2026-05-08",
mood: "GUT",
score: 4,
tags: null,
source: "MOODLOG",
loggedAt: "2026-05-08T20:00:00.000Z",
structuredTags: ["custom:own-tag"],
},
],
customMoodTagCategories: [
{
id: "own-category-id",
key: "custom:own-category",
labelKey: "mood.tagCategory.custom",
},
],
customMoodTags: [
{
key: "custom:own-tag",
labelKey: "mood.tag.custom",
categoryId: "own-category-id",
kind: "BINARY",
},
],
};
const backup = await storeBackup(owner.id, payload);
const outcome = await restoreBackup({
backup,
actorUserId: owner.id,
ipAddress: null,
restoreInstanceSettings: false,
});
expect(outcome.ok).toBe(true);
const tag = await prisma.moodTag.findUniqueOrThrow({
where: { key: "custom:own-tag" },
});
expect(tag.userId).toBe(owner.id);
});
});
@@ -273,7 +273,9 @@ describe("off-host backup under a memory budget", () => {
).toBeLessThan(STREAM_BUDGET_BYTES);
// An object that writes but does not read is worse than none.
const restored = JSON.parse(decryptBackup(s3.kept!, BACKUP_KEY)) as {
const restored = JSON.parse(
decryptBackup(s3.kept!, BACKUP_KEY, objectKey),
) as {
measurements: Array<{ deletedAt: string | null }>;
moodEntries: unknown[];
intakeEvents: unknown[];
@@ -0,0 +1,157 @@
/**
* Deleting an account, or wiping its data, takes its off-host copies out of
* the bucket.
*
* The bucket is replaced at the client boundary (`getS3Client`) by an
* in-memory map; everything above it is real: the route, its transaction, the
* request row it commits and the purge job that works it. Mutations that must
* turn this red: drop `requestOffhostPurge` from either route (no request is
* written and the objects stay), or write the request outside the deletion's
* transaction and let it fail (the deletion commits without one).
*/
import { NextRequest } from "next/server";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { cookieJar, headerJar } from "./mock-next-headers";
import { getPrismaClient, truncateAllTables } from "./setup";
const bucket = vi.hoisted(() => new Set<string>());
vi.mock("@/lib/jobs/offhost-backup", async (importOriginal) => {
const actual =
await importOriginal<typeof import("@/lib/jobs/offhost-backup")>();
return {
...actual,
getS3Client: vi.fn(async () => ({
putObject: vi.fn(),
putStream: vi.fn(),
getObject: vi.fn(),
headObject: vi.fn(),
listObjects: vi.fn(async (prefix: string) =>
[...bucket].filter((k) => k.startsWith(prefix)).map((key) => ({ key })),
),
deleteObject: vi.fn(async (key: string) => {
bucket.delete(key);
}),
})),
};
});
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),
}));
beforeEach(async () => {
await truncateAllTables(getPrismaClient());
cookieJar.clear();
headerJar.clear();
bucket.clear();
process.env.BACKUP_S3_ENDPOINT = "https://r2.example";
process.env.BACKUP_S3_BUCKET = "hl-backups";
process.env.BACKUP_S3_ACCESS_KEY = "AKIA";
process.env.BACKUP_S3_SECRET_KEY = "secret";
process.env.BACKUP_ENCRYPTION_KEY = "ab".repeat(32);
});
async function seedUserWithSession(username: string) {
const prisma = getPrismaClient();
const user = await prisma.user.create({
data: { username, email: `${username}@example.test` },
});
const session = await prisma.session.create({
data: { userId: user.id, expiresAt: new Date(Date.now() + 3_600_000) },
});
cookieJar.set("healthlog_session", session.id);
return user.id;
}
function seedBucket(userId: string, otherId: string) {
for (const day of ["2026-09-01", "2026-09-02", "2026-09-03"]) {
bucket.add(`${day}/user-${userId}.json.enc`);
bucket.add(`${day}/user-${otherId}.json.enc`);
}
bucket.add("_healthcheck/1.bin");
}
describe("off-host copies leave with the account", () => {
it("account deletion removes every copy of that account", async () => {
const prisma = getPrismaClient();
const other = await prisma.user.create({
data: { username: "neighbour", email: "neighbour@example.test" },
});
const userId = await seedUserWithSession("leaving");
seedBucket(userId, other.id);
const { DELETE } = await import("@/app/api/settings/account/route");
const res = await DELETE(
new NextRequest("http://localhost/api/settings/account", {
method: "DELETE",
headers: { "content-type": "application/json" },
body: JSON.stringify({ confirm: "DELETE_ACCOUNT" }),
}),
);
expect(res.status).toBe(200);
const pending = await prisma.offhostPurgeRequest.findMany();
expect(pending.map((r) => [r.subjectId, r.reason])).toEqual([
[userId, "account_deleted"],
]);
const { processOffhostPurges } = await import("@/lib/jobs/offhost-purge");
const report = await processOffhostPurges(prisma);
expect(report).toMatchObject({ completed: 1, objectsDeleted: 3 });
expect([...bucket].filter((k) => k.includes(userId))).toEqual([]);
expect([...bucket].filter((k) => k.includes(other.id))).toHaveLength(3);
expect(bucket.has("_healthcheck/1.bin")).toBe(true);
expect(await prisma.offhostPurgeRequest.count()).toBe(0);
});
it("delete-all-data removes every copy and leaves the receipt", async () => {
const prisma = getPrismaClient();
const other = await prisma.user.create({
data: { username: "neighbour2", email: "neighbour2@example.test" },
});
const userId = await seedUserWithSession("wiping");
seedBucket(userId, other.id);
const { DELETE } = await import("@/app/api/settings/data/route");
const res = await DELETE(
new NextRequest("http://localhost/api/settings/data", {
method: "DELETE",
headers: { "content-type": "application/json" },
body: JSON.stringify({ confirm: "DELETE" }),
}),
);
expect(res.status).toBe(200);
const { processOffhostPurges } = await import("@/lib/jobs/offhost-purge");
await processOffhostPurges(prisma);
expect([...bucket].filter((k) => k.includes(userId))).toEqual([]);
expect([...bucket].filter((k) => k.includes(other.id))).toHaveLength(3);
const receipt = await prisma.auditLog.findFirst({
where: { action: "offhost.backup.purged", userId },
});
expect(receipt).not.toBeNull();
});
});