Cotton code
34 lines · 2.1 KB · TypeScriptRaw
/** * RESTORE ACT (coordinator's tenth review, M1): `restore` is the owner's PHONE bringing back the rows of a database that * is gone. An assistant's token (owner-scoped included) must never run it, on any route: through run_act it could lay * rows straight into the tables that hold assistants' tokens, devices and window grants, or overwrite any row with its * own data. Invented rows only. */import { describe, expect, it } from "vitest";import { http, personalObject, tool, type ToolResult } from "./personal.ts";const said = (r: ToolResult): string => r.content.map((c) => c.text ?? "").join(" ");describe("the restore act", () => {  const slug = "restore-act";  it("refuses every assistant token, through run_act and through /v1/act, and still serves the owner's phone", async () => {    const { owner, credential } = await personalObject(slug);    const rows = [      { table: "mcp_token", id: "planted-token", data: { name: "planted", token_hash: "0".repeat(64), scopes: ["owner"] } },      { table: "device", id: "planted-device", data: { name: "planted" } },      { table: "note", id: "planted-note", data: { title: "planted" } },    ];    for (const row of rows) {      const r = await tool(slug, owner, "run_act", { act: "restore", args: { rows: [row] } });      expect(r.isError, `run_act restore ${row.table} answered ${said(r)}`).toBe(true);      expect(said(r)).toMatch(/owner's own devices|owner_only/);      const direct = await http(slug, "POST", "/v1/act", { act: "restore", args: { rows: [row] } }, { authorization: `Bearer ${owner}` });      expect(direct.status, `/v1/act restore ${row.table} with a token: ${JSON.stringify(direct.body)}`).toBe(403);    }    // The owner's own phone is past the owner check (a sealed table's row it sends in the clear is refused as before).    const phone = await http<{ code?: string }>(slug, "POST", "/v1/act", { act: "restore", args: { rows: [{ table: "note", id: "phone-note", data: { title: "Brought back" } }] } }, { authorization: `Bearer ${credential}` });    expect(phone.status, JSON.stringify(phone.body)).not.toBe(403);    expect(phone.body.code).toBe("sealed_in_clear");  });});