Merge branch 'wave/v125-sec' into release/v1.25.0

This commit is contained in:
Marc-André Bombeck committed 2026-06-28 14:40:39 +02:00
commit 52e8dab3d7
27 files changed
+1111 -60

No files matched your search

+10 -3
View File
@@ -39,6 +39,11 @@ import {
resolveReorderLeadDays,
} from "@/lib/validations/notification-prefs";
import { invalidateUserMedications } from "@/lib/cache/invalidate";
import {
encryptNote,
readNote,
shapeDoseChangeNote,
} from "@/lib/crypto/note-cipher";
import { NextRequest } from "next/server";
type RouteParams = { params: Promise<{ id: string }> };
@@ -193,7 +198,7 @@ export const GET = apiHandler(
effectiveFrom: dc.effectiveFrom.toISOString(),
doseValue: dc.doseValue,
doseUnit: dc.doseUnit,
note: dc.note,
note: readNote(dc.noteEncrypted, dc.note),
})),
recentIntakes: medication.intakeEvents.map((iv) => ({
takenAt: iv.takenAt ? iv.takenAt.toISOString() : null,
@@ -261,7 +266,9 @@ export const POST = apiHandler(
effectiveFrom,
doseValue,
doseUnit,
note: note ?? null,
// Encrypt the titration note at rest; the plaintext column stays null.
noteEncrypted: encryptNote(note),
note: null,
},
});
@@ -291,7 +298,7 @@ export const POST = apiHandler(
// buckets so the card reflects the new runway on the next read.
invalidateUserMedications(user.id, { evict: true });
return apiSuccess({ doseChange: created }, 201);
return apiSuccess({ doseChange: shapeDoseChangeNote(created) }, 201);
}
// The schema's XOR refinement guarantees `inventory` is present
@@ -37,6 +37,7 @@ import {
} from "@/lib/medications/inventory/state-machine";
import { serializeInventoryItem } from "@/lib/medications/inventory/service";
import { invalidateUserMedications } from "@/lib/cache/invalidate";
import { encryptNote, shapeInventoryItemNotes } from "@/lib/crypto/note-cipher";
type RouteParams = { params: Promise<{ id: string; itemId: string }> };
@@ -142,6 +143,14 @@ export const PATCH = apiHandler(
Date.now(),
);
// Only touch the note columns when the caller supplied a value; an absent
// `notes` leaves the existing ciphertext untouched. When present, encrypt
// at rest and null the plaintext column.
const notesData =
notes === undefined
? {}
: { notesEncrypted: encryptNote(notes), notes: null };
const updated = await prisma.medicationInventoryItem.update({
where: { id: itemId },
data: {
@@ -150,7 +159,7 @@ export const PATCH = apiHandler(
unitsRemaining: nextUnitsRemaining,
printedExpiry: nextPrintedExpiry,
expiresAt: nextExpiresAt,
notes: notes === undefined ? existing.notes : notes,
...notesData,
},
});
@@ -180,7 +189,7 @@ export const PATCH = apiHandler(
// read instead of after the `cachedSwr` stale window.
invalidateUserMedications(user.id, { evict: true });
return apiSuccess(serializeInventoryItem(updated));
return apiSuccess(serializeInventoryItem(shapeInventoryItemNotes(updated)));
},
);
@@ -35,6 +35,7 @@ import {
} from "@/lib/medications/inventory/service";
import { assertMedicationOwnership } from "@/lib/medications/route-guards";
import { invalidateUserMedications } from "@/lib/cache/invalidate";
import { shapeInventoryItemNotes } from "@/lib/crypto/note-cipher";
type RouteParams = { params: Promise<{ id: string }> };
@@ -60,7 +61,11 @@ export const GET = apiHandler(
}),
]);
const serialized = items.map(serializeInventoryItem);
// Decrypt each note and strip the raw `notesEncrypted` ciphertext before
// the decimal-unit serialisation runs.
const serialized = items.map((item) =>
serializeInventoryItem(shapeInventoryItemNotes(item)),
);
// v1.19.0 (iOS#25) — the canonical supply summary is computed HERE,
// server-side, through the one source of truth (`summariseSupply`).
// The detail-page client renders these ready figures instead of
@@ -162,6 +167,9 @@ export const POST = apiHandler(
// serve the pre-write stock for the rest of the stale window.
invalidateUserMedications(user.id, { evict: true });
return apiSuccess(serializeInventoryItem(created), 201);
return apiSuccess(
serializeInventoryItem(shapeInventoryItemNotes(created)),
201,
);
},
);
@@ -57,6 +57,13 @@ import { DELETE } from "../[logId]/route";
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth/session";
import { checkRateLimit } from "@/lib/rate-limit";
import { readNote } from "@/lib/crypto/note-cipher";
// The POST path encrypts the free-text note at rest; drive the legacy single
// key so encryptNote has material to work with.
vi.stubEnv("ENCRYPTION_KEYS", "");
vi.stubEnv("ENCRYPTION_ACTIVE_KEY_ID", "");
vi.stubEnv("ENCRYPTION_KEY", "a".repeat(64));
const SESSION_OK = {
session: { id: "sess-1", expiresAt: new Date(Date.now() + 3_600_000) },
@@ -233,17 +240,33 @@ describe("POST /api/medications/[id]/side-effects", () => {
{ params: Promise.resolve({ id: "med-1" }) },
);
expect(res.status).toBe(201);
expect(prisma.medicationSideEffect.create).toHaveBeenCalledWith({
data: expect.objectContaining({
userId: "user-1",
medicationId: "med-1",
category: "GI",
entry: "NAUSEA",
severity: 2,
occurredAt: new Date(occurredAt),
notes: "after breakfast",
}),
const createArgs = vi.mocked(prisma.medicationSideEffect.create).mock
.calls[0][0] as {
data: {
userId: string;
medicationId: string;
category: string;
entry: string;
severity: number;
occurredAt: Date;
notes: string | null;
notesEncrypted: Uint8Array | null;
};
};
expect(createArgs.data).toMatchObject({
userId: "user-1",
medicationId: "med-1",
category: "GI",
entry: "NAUSEA",
severity: 2,
occurredAt: new Date(occurredAt),
// v1.25 — the plaintext column is nulled; the note lands encrypted.
notes: null,
});
expect(createArgs.data.notesEncrypted).toBeInstanceOf(Uint8Array);
expect(readNote(createArgs.data.notesEncrypted, null)).toBe(
"after breakfast",
);
});
it("defaults occurredAt to now when omitted", async () => {
@@ -36,6 +36,7 @@ import {
} from "@/lib/medications/side-effects/validators";
import { categoryForEntry } from "@/lib/medications/side-effects/taxonomy";
import { assertMedicationOwnership } from "@/lib/medications/route-guards";
import { encryptNote, shapeSideEffectNotes } from "@/lib/crypto/note-cipher";
type RouteParams = { params: Promise<{ id: string }> };
@@ -85,7 +86,12 @@ export const GET = apiHandler(
meta: { medication_id: id, total: items.length },
});
return apiSuccess({ items, meta: { total: items.length } });
// Decrypt each note and strip the raw `notesEncrypted` ciphertext so it
// never leaves the server.
return apiSuccess({
items: items.map(shapeSideEffectNotes),
meta: { total: items.length },
});
},
);
@@ -139,7 +145,9 @@ export const POST = apiHandler(
entry,
severity,
occurredAt: occurredAt ?? new Date(),
notes: notes ?? null,
// Encrypt the free-text note at rest; the plaintext column stays null.
notesEncrypted: encryptNote(notes),
notes: null,
},
});
@@ -163,6 +171,6 @@ export const POST = apiHandler(
meta: { medication_id: id, entry, severity },
});
return apiSuccess(created, 201);
return apiSuccess(shapeSideEffectNotes(created), 201);
},
);
+2 -4
View File
@@ -34,7 +34,7 @@ import { Prisma } from "@/generated/prisma/client";
import { z } from "zod/v4";
import { invalidateUserMedicationListLayout } from "@/lib/cache/invalidate";
import { cached, caches, type ServerCache } from "@/lib/cache/server-cache";
import { redactSensitiveFields } from "@/lib/observability/redact-payload";
import { redactForExcerpt } from "@/lib/observability/redact-payload";
import { shouldEmitAuditRow } from "@/lib/audit-dedup";
import type { NextRequest } from "next/server";
@@ -92,9 +92,7 @@ export const PUT = apiHandler(async (request: NextRequest) => {
// layout routes; the wide-event line carries the redacted payload
// shape for operator debugging without leaking the raw body.
const issues = sanitiseZodIssues(parsed.error.issues);
const payloadDiagnostic = buildPayloadDiagnostic(
redactSensitiveFields(body),
);
const payloadDiagnostic = buildPayloadDiagnostic(redactForExcerpt(body));
annotate({
action: { name: "medication.layout.validation-failed" },
meta: {