mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-14 06:40:50 +00:00
fix(crypto): page the key-rotation walks by id, not by cursor
The corpus walk and the rotation script paged with Prisma's cursor and skip. Prisma resolves that cursor by reading the page's last row again, so when that row was hard-deleted before the next page (a backup replaced, a cached response expired), the next page came back empty and the walk ended early. The scan then counted fewer rows than the database holds, which could read as nothing left on the old key while rows it never reached still were. Both walks now ask for the rows whose id is greater than the last one read, which needs no row to still exist.
This commit is contained in:
1 parent
4897752c3c
commit
0a5fa4b2ae
6 files changed
+133
-25
No files matched your search
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* The key-rotation walks page by `id > last`, never by Prisma's cursor.
|
||||
*
|
||||
* `cursor: { id }, skip: 1` looks the last row of a page up again. When that
|
||||
* row was hard-deleted between pages, Prisma answers the next page with
|
||||
* nothing, the walk stops early, and the scan reports a corpus smaller than
|
||||
* the one on disk, which can read as "nothing left on the old key". The
|
||||
* library walk is proven against Postgres in
|
||||
* `tests/integration/encryption-corpus-cursor-row-deleted.test.ts`; the
|
||||
* rotation script calls `process.exit` on import and cannot be driven from a
|
||||
* test, so its walks are held to the same shape here.
|
||||
*
|
||||
* Mutation check: put `cursor: { id: cursor }, skip: 1` back in either file
|
||||
* and this goes red.
|
||||
*/
|
||||
import { readFileSync } from "node:fs";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
const WALKS = [
|
||||
"scripts/rotate-encryption-key.ts",
|
||||
"src/lib/crypto/encryption-corpus.ts",
|
||||
] as const;
|
||||
|
||||
describe("rotation walks page by key", () => {
|
||||
it.each(WALKS)("%s pages with `id > last`, not cursor + skip", (path) => {
|
||||
const code = readFileSync(path, "utf8")
|
||||
.split("\n")
|
||||
.filter((line) => !line.trim().startsWith("//"))
|
||||
.join("\n");
|
||||
expect(code).not.toMatch(/cursor\s*:\s*\{[^}]*\}\s*,\s*skip\s*:/);
|
||||
// Non-zero proof: the walk is still there and still pages by key.
|
||||
expect(code).toMatch(/\{\s*gt\s*:\s*cursor\s*\}/);
|
||||
});
|
||||
});
|
||||
@@ -45,13 +45,12 @@ interface FindManyArgs {
|
||||
select: Record<string, true>;
|
||||
orderBy?: Record<string, "asc" | "desc">;
|
||||
take?: number;
|
||||
cursor?: Record<string, unknown>;
|
||||
skip?: number;
|
||||
where?: Record<string, { gt: string }>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pagination-honouring fake delegate: applies orderBy(id) + cursor + skip +
|
||||
* take exactly like Prisma keyset pagination, and records every findMany
|
||||
* Pagination-honouring fake delegate: applies orderBy(id) + `id > last` +
|
||||
* take exactly like the keyset pagination the walk sends, and records every findMany
|
||||
* call so the test can assert the batch boundedness.
|
||||
*/
|
||||
function makeDocClient(rows: DocRow[]): {
|
||||
@@ -68,10 +67,8 @@ function makeDocClient(rows: DocRow[]): {
|
||||
findMany: async (args: FindManyArgs) => {
|
||||
findManyCalls.push(args);
|
||||
let list = [...store].sort((a, b) => a.id.localeCompare(b.id));
|
||||
if (args.cursor) {
|
||||
const idx = list.findIndex((r) => r.id === args.cursor!.id);
|
||||
list = list.slice(idx + (args.skip ?? 0));
|
||||
}
|
||||
const after = args.where?.id?.gt;
|
||||
if (after !== undefined) list = list.filter((r) => r.id > after);
|
||||
if (args.take !== undefined) list = list.slice(0, args.take);
|
||||
return list.map((row) => {
|
||||
const out: Record<string, unknown> = {};
|
||||
@@ -181,8 +178,8 @@ describe("codec-dispatched blob rotation (bounded batches)", () => {
|
||||
expect(call.orderBy).toEqual({ id: "asc" });
|
||||
}
|
||||
// Cursor advanced between pages.
|
||||
expect(findManyCalls[0].cursor).toBeUndefined();
|
||||
expect(findManyCalls[1].cursor).toEqual({ id: pad(24) });
|
||||
expect(findManyCalls[0].where).toBeUndefined();
|
||||
expect(findManyCalls[1].where).toEqual({ id: { gt: pad(24) } });
|
||||
|
||||
// Every row now reads under the active key, each in its OWN codec.
|
||||
for (const row of store) {
|
||||
|
||||
@@ -45,15 +45,16 @@ function makeClient(seed: Record<string, Row[]>): {
|
||||
if (client[key]) continue;
|
||||
const model = col.model;
|
||||
client[key] = {
|
||||
// `take` / `cursor` are honoured so the batched-column walk terminates
|
||||
// here the way it does against Postgres.
|
||||
findMany: async ({ select, take, cursor, skip }) => {
|
||||
// `take` / `id > last` are honoured so the batched-column walk
|
||||
// terminates here the way it does against Postgres.
|
||||
findMany: async ({ select, take, where }) => {
|
||||
const fields = Object.keys(select);
|
||||
let rows = store[model];
|
||||
if (cursor) {
|
||||
const at = rows.findIndex((r) => r.id === cursor.id);
|
||||
rows = at < 0 ? [] : rows.slice(at + (skip ?? 0));
|
||||
}
|
||||
let rows = [...store[model]].sort((a, b) =>
|
||||
String(a.id).localeCompare(String(b.id)),
|
||||
);
|
||||
const after = where?.id?.gt;
|
||||
if (after !== undefined)
|
||||
rows = rows.filter((r) => String(r.id) > after);
|
||||
if (take !== undefined) rows = rows.slice(0, take);
|
||||
return rows.map((row) => {
|
||||
const out: Record<string, unknown> = {};
|
||||
|
||||
@@ -69,8 +69,7 @@ interface ColumnDelegate {
|
||||
select: Record<string, true>;
|
||||
orderBy?: Record<string, "asc" | "desc">;
|
||||
take?: number;
|
||||
cursor?: Record<string, unknown>;
|
||||
skip?: number;
|
||||
where?: Record<string, { gt: string }>;
|
||||
}) => Promise<Array<Record<string, unknown>>>;
|
||||
update: (args: {
|
||||
where: Record<string, string>;
|
||||
@@ -190,7 +189,9 @@ async function walkColumn(
|
||||
},
|
||||
orderBy: { [pk]: "asc" },
|
||||
take: batchSize,
|
||||
...(cursor ? { cursor: { [pk]: cursor }, skip: 1 } : {}),
|
||||
// `id > last`, not Prisma's `cursor` + `skip`: the cursor form looks
|
||||
// the last row up again, and a row deleted between pages ends the walk.
|
||||
...(cursor ? { where: { [pk]: { gt: cursor } } } : {}),
|
||||
});
|
||||
if (rows.length === 0) break;
|
||||
for (const row of rows) {
|
||||
|
||||
Reference in new issue
Block a user