fix(ingest): hold the stability floor for identities no request schema sees

The dedup key on the medication intake surfaces is not only `externalId`.
`MedicationIntakeEvent.idempotency_key` is a `@unique` column persisted
with the dose row forever, with no TTL and no sweep, matched by plain
equality on the per-medication POST, the bulk drain and the external
ingest bridge. That makes it an identity, not a replay token, and it
carries the same failure mode: a key that rotates per client launch never
matches its own earlier post, so every retry logs the dose again. The
blank case is sharper still — the column is unique table-wide, so an
empty key resolves to whichever unrelated row stored it first. Refuse
both shapes at the schema on the single-entry paths and per entry on the
bulk drain.

The MCP write path hashes a host-supplied key into the `externalId` half
of its dedup key, so the shape is laundered before anything downstream
could see it and the check has to run on the input. That closes a live
one: every tool handler falls back to `""` when the host omits the
argument, and `""` digests to a constant, so each keyless write after the
first reported `already_logged` and was silently dropped.

The HTTP `Idempotency-Key` header stays out, and now says why in the
guard: `IdempotencyKey.expiresAt` bounds it and the cleanup job sweeps
it, so it is a windowed replay token and rotation is its design.

Extend the structural guard with a provenance rule for identities that
never pass a Zod request schema — a spreadsheet column, an MCP tool
argument. Inside the import and MCP trees, a module handling an identity
must apply the floor itself or import a module that does. That is a real
chain rather than a list of known offenders: a new importer that mints an
`externalId` from file input and imports nothing checked fails it, with
no allowlist to escape into.
This commit is contained in:
Marc-André Bombeck committed 2026-07-25 18:07:13 +02:00
1 parent 6e4bcffadf
commit 15b5f6f92f
19 files changed
+1574 -43

No files matched your search

@@ -0,0 +1,213 @@
/**
* The two single-entry cycle writes — `POST /api/cycle/day-logs` and
* `POST /api/cycle/period` — against the external-id stability floor.
*
* Both upsert on `(userId, source, externalId)`, so an id that changes
* between two launches of the same client never matches its own earlier
* row: the mirror mints a fresh day log (or a fresh period marker) on
* every sweep. Unlike their bulk twin these are single-entry, so the
* refusal is the standard 422 multi-issue envelope and nothing is written.
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import { NextRequest } from "next/server";
vi.mock("@/lib/db", () => ({
prisma: {
auditLog: { create: vi.fn() },
cycleDayLog: {
findUniqueOrThrow: vi.fn(),
findFirst: vi.fn(),
findFirstOrThrow: vi.fn(),
},
menstrualCycle: {
findMany: vi.fn().mockResolvedValue([]),
findUniqueOrThrow: vi.fn(),
},
$transaction: vi.fn(),
},
}));
vi.mock("@/lib/auth/session", () => ({ getSession: vi.fn() }));
vi.mock("@/lib/auth/audit", () => ({
auditLog: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("@/lib/rate-limit", () => ({
checkRateLimit: vi.fn(),
rateLimitHeaders: () => ({}),
}));
vi.mock("@/lib/idempotency", () => ({
withIdempotency:
<Args extends unknown[]>(fn: (...args: Args) => Promise<Response>) =>
(...args: Args) =>
fn(...args),
}));
vi.mock("@/lib/cycle/gate", () => ({
requireCycleEnabled: vi.fn(),
}));
vi.mock("@/lib/cycle/day-log-write", () => ({
upsertCycleDayLog: vi.fn(),
}));
vi.mock("@/lib/cycle/cycle-attribution", () => ({
findOwningCycleId: vi.fn().mockResolvedValue(null),
}));
vi.mock("@/lib/cycle/dto", async (importOriginal) => ({
...(await importOriginal<typeof import("@/lib/cycle/dto")>()),
toCycleDayLogDTO: vi.fn(() => ({ id: "day-1" })),
toMenstrualCycleDTO: vi.fn(() => ({ id: "cycle-1" })),
}));
vi.mock("@/lib/logging/transports", () => ({ emitIfSampled: vi.fn() }));
vi.mock("@/lib/db-compat", () => ({
ensureDbCompatibility: vi.fn().mockResolvedValue(undefined),
}));
vi.mock("next/headers", () => ({
headers: vi.fn(async () => ({ get: () => null })),
cookies: vi.fn(async () => ({
get: () => undefined,
set: () => {},
delete: () => {},
})),
}));
import { POST as postDayLog } from "../day-logs/route";
import { POST as postPeriod } from "../period/route";
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth/session";
import { checkRateLimit } from "@/lib/rate-limit";
import { requireCycleEnabled } from "@/lib/cycle/gate";
import { upsertCycleDayLog } from "@/lib/cycle/day-log-write";
const SESSION_OK = {
session: { id: "sess-1", expiresAt: new Date(Date.now() + 3_600_000) },
user: { id: "user-1", username: "tester", role: "USER" as const },
};
function req(path: string, body: unknown): NextRequest {
return new NextRequest(`http://localhost${path}`, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(body),
});
}
const POISONED = "<HKHealthConceptIdentifier: 0x12568db80>";
/** What the HealthKit cycle mirror really writes (`import-accumulator.ts`). */
const REAL_ID = "hkcycle:2026-01-01";
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(checkRateLimit).mockResolvedValue({ allowed: true } as never);
vi.mocked(requireCycleEnabled).mockResolvedValue({
enabled: true,
profile: { sensitiveCategoryEncryption: false },
} as never);
vi.mocked(upsertCycleDayLog).mockResolvedValue({
id: "day-1",
existed: false,
changed: true,
} as never);
vi.mocked(prisma.cycleDayLog.findUniqueOrThrow).mockResolvedValue({
id: "day-1",
symptoms: [],
} as never);
vi.mocked(prisma.cycleDayLog.findFirst).mockResolvedValue(null as never);
vi.mocked(prisma.cycleDayLog.findFirstOrThrow).mockResolvedValue({
id: "day-1",
symptoms: [],
} as never);
vi.mocked(prisma.menstrualCycle.findUniqueOrThrow).mockResolvedValue({
id: "cycle-1",
startDate: "2026-01-01",
} as never);
});
describe("POST /api/cycle/day-logs — external-id stability floor", () => {
it("422s on an object-description externalId and writes nothing", async () => {
const res = await postDayLog(
req("/api/cycle/day-logs", {
date: "2026-01-01",
loggedAt: "2026-01-01T08:00:00.000Z",
source: "APPLE_HEALTH",
externalId: POISONED,
}),
);
expect(res.status).toBe(422);
const body = (await res.json()) as {
details: { issues: Array<{ path: string; message: string }> };
meta?: { errorCode?: string };
};
const issue = body.details.issues.find((i) => i.path === "externalId");
expect(issue).toBeDefined();
expect(issue!.message).toContain("stable across app restarts");
expect(body.meta?.errorCode).toBe("cycle.day-log.invalid");
expect(upsertCycleDayLog).not.toHaveBeenCalled();
});
it("422s on a bare memory-address externalId", async () => {
const res = await postDayLog(
req("/api/cycle/day-logs", {
date: "2026-01-01",
loggedAt: "2026-01-01T08:00:00.000Z",
externalId: "0x126b25160",
}),
);
expect(res.status).toBe(422);
expect(upsertCycleDayLog).not.toHaveBeenCalled();
});
it("accepts the HealthKit cycle-day id the mirror really sends", async () => {
const res = await postDayLog(
req("/api/cycle/day-logs", {
date: "2026-01-01",
loggedAt: "2026-01-01T08:00:00.000Z",
source: "APPLE_HEALTH",
externalId: REAL_ID,
}),
);
expect(res.status).toBe(201);
expect(upsertCycleDayLog).toHaveBeenCalledTimes(1);
});
});
describe("POST /api/cycle/period — external-id stability floor", () => {
it("422s on an object-description externalId and opens no cycle", async () => {
const res = await postPeriod(
req("/api/cycle/period", {
action: "start",
date: "2026-01-01",
loggedAt: "2026-01-01T08:00:00.000Z",
externalId: POISONED,
}),
);
expect(res.status).toBe(422);
const body = (await res.json()) as {
details: { issues: Array<{ path: string; message: string }> };
meta?: { errorCode?: string };
};
expect(
body.details.issues.some(
(i) =>
i.path === "externalId" &&
i.message.includes("stable across app restarts"),
),
).toBe(true);
expect(body.meta?.errorCode).toBe("cycle.period.invalid");
expect(prisma.$transaction).not.toHaveBeenCalled();
});
it("accepts the real HealthKit id and reaches the write transaction", async () => {
vi.mocked(prisma.$transaction).mockResolvedValue({
cycleId: "cycle-1",
} as never);
const res = await postPeriod(
req("/api/cycle/period", {
action: "start",
date: "2026-01-01",
loggedAt: "2026-01-01T08:00:00.000Z",
externalId: REAL_ID,
}),
);
expect(res.status).toBeLessThan(400);
expect(prisma.$transaction).toHaveBeenCalledTimes(1);
});
});
@@ -11,6 +11,7 @@ import { NextRequest } from "next/server";
vi.mock("@/lib/db", () => ({
prisma: {
auditLog: { create: vi.fn() },
menstrualCycle: { findMany: vi.fn().mockResolvedValue([]) },
},
}));
@@ -48,6 +49,7 @@ vi.mock("next/headers", () => ({
}));
import { POST } from "../route";
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth/session";
import { checkRateLimit } from "@/lib/rate-limit";
import { requireCycleEnabled } from "@/lib/cycle/gate";
@@ -72,7 +74,9 @@ beforeEach(() => {
vi.mocked(checkRateLimit).mockResolvedValue({ allowed: true } as never);
vi.mocked(requireCycleEnabled).mockResolvedValue({
enabled: true,
profile: { sensitiveCategoryEncryption: false },
} as never);
vi.mocked(prisma.menstrualCycle.findMany).mockResolvedValue([] as never);
});
describe("POST /api/cycle/day-logs/bulk — body cap", () => {
@@ -94,3 +98,108 @@ describe("POST /api/cycle/day-logs/bulk — body cap", () => {
expect(upsertCycleDayLog).not.toHaveBeenCalled();
});
});
describe("POST /api/cycle/day-logs/bulk — external-id stability floor", () => {
/**
* The bulk drain upserts on `(userId, source, externalId)`. A per-launch
* id never matches its own earlier row, so every re-post mints another
* day log. Refused PER ENTRY — the client's other days must still land.
*/
beforeEach(() => {
let n = 0;
vi.mocked(upsertCycleDayLog).mockImplementation(
async () =>
({ id: `day-${++n}`, existed: false, changed: true }) as never,
);
});
it("skips the poisoned entry and still writes the two good ones", async () => {
const res = await POST(
postReq({
entries: [
{
date: "2026-01-01",
loggedAt: "2026-01-01T08:00:00.000Z",
source: "APPLE_HEALTH",
externalId: "hkcycle:2026-01-01",
},
{
date: "2026-01-02",
loggedAt: "2026-01-02T08:00:00.000Z",
source: "APPLE_HEALTH",
externalId: "<HKHealthConceptIdentifier: 0x12568db80>",
},
{
date: "2026-01-03",
loggedAt: "2026-01-03T08:00:00.000Z",
source: "APPLE_HEALTH",
externalId: "8AD2A9CB-3F0C-4E4D-9C1E-4B7E2A1D6F30",
},
],
}),
);
expect(res.status).toBe(200);
const body = (await res.json()) as {
data: {
processed: number;
inserted: number;
skipped: number;
entries: Array<{
index: number;
status: string;
reason?: string;
externalId?: string;
}>;
};
};
expect(body.data.processed).toBe(3);
expect(body.data.inserted).toBe(2);
expect(body.data.skipped).toBe(1);
expect(body.data.entries[0].status).toBe("inserted");
expect(body.data.entries[1]).toMatchObject({
index: 1,
status: "skipped",
reason: "unstable_external_id",
externalId: "<HKHealthConceptIdentifier: 0x12568db80>",
});
expect(body.data.entries[2].status).toBe("inserted");
// Only the two good entries reached the writer.
expect(upsertCycleDayLog).toHaveBeenCalledTimes(2);
});
it("never turns one poisoned entry into a whole-batch 422", async () => {
const res = await POST(
postReq({
entries: [
{
date: "2026-01-01",
loggedAt: "2026-01-01T08:00:00.000Z",
externalId: "0xdeadbeef",
},
],
}),
);
expect(res.status).toBe(200);
});
it("accepts the HealthKit cycle-day id shape the mirror really sends", async () => {
const res = await POST(
postReq({
entries: [
{
date: "2026-01-01",
loggedAt: "2026-01-01T08:00:00.000Z",
source: "APPLE_HEALTH",
externalId: "hkcycle:2026-01-01",
},
],
}),
);
expect(res.status).toBe(200);
const body = (await res.json()) as {
data: { entries: Array<{ status: string }> };
};
expect(body.data.entries[0].status).toBe("inserted");
expect(upsertCycleDayLog).toHaveBeenCalledTimes(1);
});
});