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

+120
View File
@@ -1163,3 +1163,123 @@ describe("POST /api/import — write failure classification", () => {
expect(auditLog).toHaveBeenCalledTimes(1);
});
});
describe("POST /api/import — external-id stability floor", () => {
/**
* The importer upserts on `(userId, type, source=IMPORT, externalId)` so
* a re-import of the same export converges instead of duplicating. An id
* that cannot be stable between two exports defeats that. The importer
* has no per-entry status envelope — its vocabulary is a skipped COUNT —
* so the offending row lands there and every other row still imports.
*/
beforeEach(() => {
vi.mocked(checkRateLimit).mockResolvedValue({
allowed: true,
remaining: 4,
resetAt: new Date(Date.now() + 1000),
} as never);
vi.mocked(prisma.measurement.createManyAndReturn).mockResolvedValue([
{ id: "m-1", type: "WEIGHT", measuredAt: new Date() },
] as never);
vi.mocked(prisma.measurement.create).mockResolvedValue({
id: "m-2",
type: "WEIGHT",
measuredAt: new Date(),
} as never);
vi.mocked(prisma.moodEntry.upsert).mockResolvedValue({} as never);
vi.mocked(prisma.moodEntry.create).mockResolvedValue({} as never);
});
function importReq(body: unknown) {
return new NextRequest("http://localhost/api/import", {
method: "POST",
body: JSON.stringify(body),
headers: { "content-type": "application/json" },
});
}
function measurement(externalId?: string) {
return {
type: "WEIGHT",
value: 80,
unit: "kg",
measuredAt: "2026-05-01T08:00:00.000Z",
...(externalId ? { externalId } : {}),
};
}
it("skips the poisoned row, imports the two good ones, and never writes the bad id", async () => {
const response = await POST(
importReq({
measurements: [
measurement("scale-row-42"),
measurement("<HKQuantitySample: 0x12568db80>"),
measurement("scale-row-43"),
],
}),
);
expect(response.status).toBeLessThan(400);
const body = (await response.json()) as {
data: { measurements: number; skipped: number };
};
expect(body.data.measurements).toBe(2);
expect(body.data.skipped).toBe(1);
const written = vi
.mocked(prisma.measurement.createManyAndReturn)
.mock.calls.flatMap(
(call) => (call[0] as { data: Array<{ externalId?: string }> }).data,
);
expect(written).toHaveLength(2);
expect(written.map((r) => r.externalId)).toEqual([
"scale-row-42",
"scale-row-43",
]);
});
it("skips a poisoned mood row and still imports the good one", async () => {
const response = await POST(
importReq({
moodEntries: [
{
date: "2026-05-01",
mood: "GUT",
score: 4,
loggedAt: "2026-05-01T08:00:00.000Z",
externalId: "daylio-1041",
},
{
date: "2026-05-02",
mood: "OKAY",
score: 3,
loggedAt: "2026-05-02T08:00:00.000Z",
externalId: "0xdeadbeef",
},
],
}),
);
expect(response.status).toBeLessThan(400);
const body = (await response.json()) as {
data: { moodEntries: number; skipped: number };
};
expect(body.data.moodEntries).toBe(1);
expect(body.data.skipped).toBe(1);
expect(vi.mocked(prisma.moodEntry.upsert)).toHaveBeenCalledTimes(1);
});
it("accepts the exporter row ids a real re-import carries", async () => {
const response = await POST(
importReq({
measurements: [
measurement("scale-row-42"),
measurement("8AD2A9CB-3F0C-4E4D-9C1E-4B7E2A1D6F30"),
],
}),
);
expect(response.status).toBeLessThan(400);
const body = (await response.json()) as {
data: { measurements: number; skipped: number };
};
expect(body.data.measurements).toBe(2);
expect(body.data.skipped).toBe(0);
});
});