/** * UNDO AND RESTORE OF A PHONE'S SEALED EDIT (found by the undo-shrink proof on the dev Worker, 23 Nov 2026). Since * 9224954f16 an update's log entry keeps only the columns that changed. A phone that edits a sealed note seals it * again under the same key version: sealed_enc and dek_enc change, seal_v does not, so the entry's `before` holds * two of the three seal fields, and undo wrote them back alone, refused as "this one lacks seal_v". Restore-a-version * wrote the same partial patch. Both now write the three together. Invented rows only. */ import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { answer, http, personalObject, tool } from "./personal.ts"; import { sealRow } from "./seal-device.ts"; const DK = new Uint8Array(32).fill(7); const SEAL = ["sealed_enc", "dek_enc", "seal_v"]; const row = (slug: string, id: string) => runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (_i, s) => { const r = s.storage.sql.exec<{ data: string }>("SELECT data FROM rows WHERE table_id = 'note' AND id = ?", id).one(); const data = JSON.parse(r.data) as Record; return Object.fromEntries(SEAL.map((k) => [k, data[k]])); }); describe("undo and restore of a phone's sealed edit", () => { it("undoes the newest edit and restores an earlier one, the seal fields written together", async () => { const slug = "undo-sealed"; const { credential, owner } = await personalObject(slug); const id = crypto.randomUUID(); const phone = (act: string, data: Record) => http<{ change_id: number }>(slug, "POST", "/v1/act", { act, args: { table: "note", id, data } }, { authorization: `Bearer ${credential}` }); const made = await phone("create", { kind: "note", author_kind: "user", ...(await sealRow(DK, "note", id, 1, { body: "Garden plan, version one" })) }); expect(made.status, JSON.stringify(made.body)).toBe(200); const one = await row(slug, id); // Two edits, each sealed again under the same version: seal_v stays 1. const second = await phone("update", await sealRow(DK, "note", id, 1, { body: "Garden plan, version two" })); expect(second.status, JSON.stringify(second.body)).toBe(200); const two = await row(slug, id); const third = await phone("update", await sealRow(DK, "note", id, 1, { body: "Garden plan, version three" })); expect(third.status, JSON.stringify(third.body)).toBe(200); expect((await row(slug, id)).seal_v).toBe(1); // Undo the newest: the note holds version two's seal again, all three fields. await answer(slug, owner, "undo", { change_id: third.body.change_id }); expect(await row(slug, id), "undone to version two").toEqual(two); // Restore the first: the note holds version one's seal. await answer(slug, owner, "restore_version", { table: "note", id, change_id: made.body.change_id }); expect(await row(slug, id), "restored to version one").toEqual(one); }); // A seal key's row is written by the owner's devices alone, checked as it is written; undo and restore_version would // pass that check by, so an assistant (every caller of either tool) never rolls one back. it("never undoes or restores a seal key's change for an assistant", async () => { const slug = "undo-seal-key"; const { credential, owner } = await personalObject(slug); const phone = (act: string, data: Record) => http<{ change_id: number }>(slug, "POST", "/v1/act", { act, args: { table: "seal_key", id: "v:note", data } }, { authorization: `Bearer ${credential}` }); const made = await phone("create", { v: 2 }); expect(made.status, JSON.stringify(made.body)).toBe(200); const bumped = await phone("update", { v: 3 }); expect(bumped.status, JSON.stringify(bumped.body)).toBe(200); const undone = await tool(slug, owner, "undo", { change_id: bumped.body.change_id }); expect(undone.isError).toBe(true); expect(undone.content[0]?.text).toMatch(/only the owner's own devices write seal keys/); const restored = await tool(slug, owner, "restore_version", { table: "seal_key", id: "v:note", change_id: made.body.change_id }); expect(restored.isError).toBe(true); expect(restored.content[0]?.text).toMatch(/only the owner's own devices write seal keys/); expect(JSON.stringify([undone, restored]), "the refusal's code").toMatch(/owner_only/); const v = await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (_i, s) => JSON.parse(s.storage.sql.exec<{ data: string }>("SELECT data FROM rows WHERE table_id = 'seal_key' AND id = 'v:note'").one().data).v); expect(v, "the seal version stands where the phone put it").toBe(3); }); });