/** * ADMIN KEYS 2 (NEXT-ADMIN-KEYS part 2, 27 Nov 2026; src/key_allow.ts): an assistant key carries what it may do (the * tables it reads, the tables it writes, outward). Real credential kinds: the owner's phone credential mints and widens; * a minted owner-scope key acts at /mcp and /v1/act under its list; a key made "all" is as every key was. */ import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { checkAllow, DEFAULT_ALLOW, mayRead, mayWrite, within } from "../src/key_allow.ts"; import { answer, http, mint, NOTES_OPEN, personalObject, TODOS_OPEN, tool } from "./personal.ts"; const NOTES_ONLY = { read: ["note"], write: ["note"], outward: false, sealed: false, acts: [] as string[] }; const none = { outward: false, sealed: false, acts: [] as string[] }; async function listed(slug: string, token: string): Promise { const r = await http<{ result: { tools: { name: string }[] } }>(slug, "POST", "/mcp", { jsonrpc: "2.0", id: 1, method: "tools/list" }, { authorization: `Bearer ${token}` }); return r.body.result.tools.map((t) => t.name); } describe("the allow itself", () => { it("reads lists, refuses the credential tables and unknown fields, and orders narrower within wider", () => { expect(checkAllow({ read: ["note", "note"], write: [] })).toEqual({ read: ["note"], write: [], ...none }); expect(checkAllow({ read: ["mcp_token"] })).toMatch(/keys and credentials/); expect(checkAllow({ read: [], admin: true })).toMatch(/not admin/); expect(checkAllow({ read: "*", write: ["memory"], outward: true, sealed: true })).toEqual({ read: "*", write: ["memory"], outward: true, sealed: true, acts: [] }); // An act opened by its name: update_definition alone, and nothing else by name. expect(checkAllow({ acts: ["update_definition"] })).toEqual({ read: [], write: [], outward: false, sealed: false, acts: ["update_definition"] }); expect(checkAllow({ acts: ["set_role"] })).toMatch(/allow.acts names/); expect(within({ read: [], write: [], ...none, acts: ["update_definition"] }, NOTES_ONLY)).toBe(false); expect(mayRead(DEFAULT_ALLOW, "memory") && mayWrite(DEFAULT_ALLOW, "memory")).toBe(true); expect(mayRead(DEFAULT_ALLOW, "note") || mayRead(DEFAULT_ALLOW, "message") || DEFAULT_ALLOW.outward).toBe(false); // A sealed table: read as far as the phone grants it with `sealed`, never written by it. expect(mayRead(DEFAULT_ALLOW, "diary", true) && !mayWrite(DEFAULT_ALLOW, "diary")).toBe(true); expect(mayRead(NOTES_ONLY, "diary", true)).toBe(false); expect(mayRead({ read: "*", write: "*", outward: true, sealed: true, acts: [] }, "mcp_token", true)).toBe(false); expect(within({ read: ["note"], write: [], ...none }, NOTES_ONLY)).toBe(true); expect(within({ read: ["todo"], write: [], ...none }, NOTES_ONLY)).toBe(false); expect(within({ read: [], write: [], ...none, outward: true }, NOTES_ONLY)).toBe(false); expect(within({ read: [], write: [], ...none, sealed: true }, NOTES_ONLY)).toBe(false); expect(within(NOTES_ONLY, null)).toBe(true); }); }); describe("a new key, minted by the owner's phone with no allow named", () => { it("carries the narrowest useful set: its memory and sessions, nothing of the apps, no whole-database tool, no device route", async () => { const slug = "allow-default"; const { credential } = await personalObject(slug, { open: [...NOTES_OPEN.open, ...TODOS_OPEN] }); const key = await mint(slug, credential, "Invented assistant", ["owner"], undefined, undefined, null); const tools = await listed(slug, key); expect(tools).toEqual(expect.arrayContaining(["brief", "recall", "remember", "log_session", "list_projects"])); for (const whole of ["list_changes", "list_bin", "storage_summary", "export_open_bundle", "undo", "update_definition", "dev_reveal_secret"]) expect(tools, whole).not.toContain(whole); // The apps' tables are not there for it: not listed, not read by a fixed-table tool, not written. expect((await tool(slug, key, "list_records", { table: "note" })).isError).toBe(true); expect((await tool(slug, key, "list_todos", {})).isError).toBe(true); expect((await tool(slug, key, "create_record", { table: "note", data: { title: "Invented" } })).isError).toBe(true); // A device route is shut: sync, the media list, and mail sent from the owner's domain (outward is off). for (const [method, path, body] of [["GET", "/v1/sync", undefined], ["GET", "/v1/bin", undefined], ["POST", "/v1/mail/send", { from: "me@invented.test", to: ["you@invented.test"], subject: "x", text: "x" }]] as const) { const r = await http<{ code: string }>(slug, method, path, body, { authorization: `Bearer ${key}` }); expect([r.status, r.body.code], path).toEqual([403, "key_allow"]); } // At /v1/act, an act on a table it does not write is refused; the admin door too (part 1). expect((await http<{ code: string }>(slug, "POST", "/v1/act", { act: "create", args: { table: "note", data: { title: "Invented" } } }, { authorization: `Bearer ${key}` })).body.code).toBe("key_allow"); expect((await http<{ code: string }>(slug, "POST", "/v1/act", { act: "start_book", args: { book: "b-1" } }, { authorization: `Bearer ${key}` })).body.code).toBe("key_allow"); }); }); describe("a key named to its tables", () => { it("reads and writes those alone, at /mcp and /v1/act, and mints only within itself", async () => { const slug = "allow-notes"; const { credential } = await personalObject(slug, { open: [...NOTES_OPEN.open, ...TODOS_OPEN] }); const all = await mint(slug, credential, "Invented old key", ["owner"]); const key = await mint(slug, credential, "Invented notes key", ["owner"], undefined, undefined, NOTES_ONLY); const made = await answer<{ record: { id: string } }>(slug, key, "create_record", { table: "note", data: { title: "Invented note" } }); expect((await answer<{ records: unknown[] }>(slug, key, "list_records", { table: "note" })).records.length).toBeGreaterThan(0); expect((await tool(slug, key, "list_records", { table: "todo" })).isError).toBe(true); expect((await tool(slug, key, "create_todo", { title: "Invented todo" })).isError).toBe(true); // Its own memory is not on its list either. expect((await tool(slug, key, "remember", { title: "Invented", content: "x", kind: "fact" })).isError).toBe(true); const viaAct = await http(slug, "POST", "/v1/act", { act: "update", args: { table: "note", id: made.record.id, data: { title: "Invented note, again" } } }, { authorization: `Bearer ${key}` }); expect(viaAct.status, JSON.stringify(viaAct.body)).toBe(200); expect((await http<{ code: string }>(slug, "POST", "/v1/act", { act: "create", args: { table: "todo", data: { title: "Invented" } } }, { authorization: `Bearer ${key}` })).body.code).toBe("key_allow"); // It mints nothing, narrower or wider (ADMIN KEYS 1b: a key never makes a key). const asKey = { authorization: `Bearer ${key}` }; for (const allow of [{ read: ["note"], write: [] }, { read: ["todo"] }, "all"]) { const r = await http<{ code: string }>(slug, "POST", "/v1/act", { act: "mint_token", args: { name: "Invented other", scopes: ["read"], expires_in_days: 1, allow } }, asKey); expect(r.body.code, JSON.stringify(allow)).toBe("access_act"); } // A key made before allows ("all") is as it was: every tool its scope holds, a device route. expect(await listed(slug, all)).toContain("list_changes"); expect((await tool(slug, all, "list_records", { table: "todo" })).isError).toBe(false); expect((await http<{ code?: string }>(slug, "GET", "/v1/sync", undefined, { authorization: `Bearer ${all}` })).body.code).not.toBe("key_allow"); expect((await http(slug, "POST", "/v1/act", { act: "create", args: { table: "todo", data: { title: "Invented todo" } } }, { authorization: `Bearer ${all}` })).status).toBe(200); }); it("the owner's phone narrows a key made all, and widens it again; a key never does", async () => { const slug = "allow-narrow"; const { credential } = await personalObject(slug, { open: [...NOTES_OPEN.open, ...TODOS_OPEN] }); const key = await mint(slug, credential, "Invented old key", ["owner"]); const rows = await answer<{ records: { id: string; data: { name: string } }[] }>(slug, key, "list_records", { table: "mcp_token" }).catch(() => null); expect(rows, "a key never lists the token table").toBeNull(); const id = await idOf(slug, credential, "Invented old key"); const phone = { authorization: `Bearer ${credential}` }; expect((await http(slug, "POST", "/v1/act", { act: "token_allow", args: { id, allow: NOTES_ONLY } }, phone)).status).toBe(200); expect((await tool(slug, key, "list_records", { table: "todo" })).isError).toBe(true); expect((await http<{ code: string }>(slug, "POST", "/v1/act", { act: "token_allow", args: { id, allow: "all" } }, { authorization: `Bearer ${key}` })).body.code).toBe("access_act"); expect((await tool(slug, key, "run_act", { act: "token_allow", args: { id, allow: "all" } })).isError).toBe(true); expect((await http(slug, "POST", "/v1/act", { act: "token_allow", args: { id, allow: "all" } }, phone)).status).toBe(200); expect((await tool(slug, key, "list_records", { table: "todo" })).isError).toBe(false); expect((await http<{ code: string }>(slug, "POST", "/v1/act", { act: "token_allow", args: { id, allow: { read: ["vault_key"] } } }, phone)).body.code).toBe("bad_allow"); }); }); describe("update_definition, named in the key's acts", () => { it("is off for every key that does not name it, however wide its tables, and on for one that does", async () => { const slug = "allow-definition"; const { credential } = await personalObject(slug, { open: [...NOTES_OPEN.open, ...TODOS_OPEN] }); const wide = await mint(slug, credential, "Invented wide key", ["owner"], undefined, undefined, { read: "*", write: "*", outward: false, sealed: false }); const named = await mint(slug, credential, "Invented look key", ["owner"], undefined, undefined, { read: ["note"], write: [], acts: ["update_definition"] }); expect(await listed(slug, wide)).not.toContain("update_definition"); const refused = await tool(slug, wide, "update_definition", { kind: "table", table: "note", sort: { column: "title" } }); expect([refused.isError, refused.content[0]!.text]).toEqual([true, expect.stringMatching(/not open to this key/)]); expect((await tool(slug, wide, "run_act", { act: "update_definition", args: { kind: "table", table: "note" } })).isError).toBe(true); expect(await listed(slug, named)).toContain("update_definition"); const reshaped = await tool(slug, named, "update_definition", { kind: "table", table: "note", sort: { column: "title" } }); expect(reshaped.content[0]!.text).not.toMatch(/not open to this key/); // Who sees the table stays the owner's device's, named or not (ADMIN KEYS 1b). const seen = await tool(slug, named, "update_definition", { kind: "table", table: "note", visibility: "public" }); expect([seen.isError, seen.content[0]!.text]).toEqual([true, expect.stringMatching(/says who may read or write the table/)]); }); }); /** A token row's id by its name, as the owner's phone holds it (read here from the object's own rows). */ async function idOf(slug: string, _credential: string, name: string): Promise { const row = await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (instance) => { const store = (instance as unknown as { store: { scan(t: string): Iterable<{ id: string; data: Record }> } }).store; return [...store.scan("mcp_token")].find((r) => r.data.name === name) ?? null; }); expect(row).not.toBeNull(); return row!.id; }