Cotton code
62 lines · 4.5 KB · TypeScriptRaw
/** * AUDIT C, attacker brief 2 (assets/security/AUDIT-C-ASSISTANT-KEY.md): a malicious or stolen assistant key. Each hole * the dev Worker showed is held here, failing first. Every number and name is invented. */import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { http, mint, personalObject, tool } from "./personal.ts";type Row = { id: string; data: Record<string, unknown> };const object = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug));const tokens = (slug: string) => runInDurableObject(object(slug), (i) => [...(i as unknown as { store: { scan(t: string): Iterable<Row> } }).store.scan("mcp_token")]);const act = (slug: string, bearer: string, name: string, args: Record<string, unknown>) => http<{ code?: string; token?: string }>(slug, "POST", "/v1/act", { act: name, args }, { authorization: `Bearer ${bearer}` });/** A key's call at /mcp: whether it was answered as the key (a result that is not an error). */async function answers(slug: string, token: string): Promise<boolean> {  const res = await http<{ result?: { isError?: boolean } }>(slug, "POST", "/mcp", { jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "recall", arguments: {} } }, { authorization: `Bearer ${token}` });  return res.status === 200 && !!res.body.result && !res.body.result.isError;}describe("AUDIT C 2-1: what a stolen key minted ends with it", () => {  it("revoking an owner-scope key from the phone ends the lane token and the machines producer it minted", async () => {    const slug = "audit-c2-minted";    const { credential, owner } = await personalObject(slug);    // The thief, holding the owner-scope key, mints itself a lane token before the owner notices.    const lane = await act(slug, owner, "mint_token", { name: "Invented lane", scopes: ["read", "lane:invented"], expires_in_days: 7 });    expect(lane.status, JSON.stringify(lane.body)).toBe(200);    const child = lane.body.token!;    expect(await answers(slug, child)).toBe(true);    const stolen = (await tokens(slug)).find((r) => !r.data.minted_by && !r.data.revoked_at)!;    // The owner ends the stolen key on the phone.    expect((await act(slug, credential, "revoke_token", { id: stolen.id })).status).toBe(200);    expect(await answers(slug, owner)).toBe(false);    expect(await answers(slug, child), "a key the stolen key minted is ended with it").toBe(false);    expect((await tokens(slug)).find((r) => r.data.name === "Invented lane")!.data.revoked_at).toBeTruthy();  }, 240_000);});describe("AUDIT C 2-2: a key never writes a sealed table in the clear", () => {  it("create_todo on sealed todos seals at arrival, and with no arrival key writes nothing", async () => {    const bare = "audit-c2-todo-bare";    const a = await personalObject(bare);    const todos = await mint(bare, a.credential, "Invented todos", ["read", "write"], 1, undefined, { read: ["todo"], write: ["todo"] });    const refused = await tool(bare, todos, "create_todo", { title: "Invented plain words" });    expect(refused.isError, refused.content[0]!.text).toBe(true);    const none = await runInDurableObject(object(bare), (i) => [...(i as unknown as { store: { scan(t: string): Iterable<Row> } }).store.scan("todo")]);    expect(none.map((r) => r.data.title)).not.toContain("Invented plain words");    const handed = "audit-c2-todo-arrival";    const b = await personalObject(handed, { arrival: true });    const key = await mint(handed, b.credential, "Invented todos", ["read", "write"], 1, undefined, { read: ["todo"], write: ["todo"] });    const made = await tool(handed, key, "create_todo", { title: "Invented sealed words", notes: "Invented notes" });    expect(made.isError, made.content[0]!.text).toBe(false);    const rows = await runInDurableObject(object(handed), (i) => [...(i as unknown as { store: { scan(t: string): Iterable<Row> } }).store.scan("todo")]);    expect(rows).toHaveLength(1);    expect(JSON.stringify(rows[0]!.data)).not.toContain("Invented sealed words");    expect(JSON.stringify(rows[0]!.data)).not.toContain("Invented notes");    // A repeating todo on sealed todos is not stored in the clear either.    const repeating = await tool(handed, key, "create_todo", { title: "Invented weekly", dueDate: "2026-12-01", rrule: "FREQ=WEEKLY" });    const all = await runInDurableObject(object(handed), (i) => [...(i as unknown as { store: { scan(t: string): Iterable<Row> } }).store.scan("todo")]);    expect(JSON.stringify(all.map((r) => r.data)), repeating.content[0]!.text).not.toContain("Invented weekly");  }, 240_000);});