142 lines · 9.9 KB · TypeScriptRaw
/** * ADMIN KEYS 1b (NEXT-REVIEWS-4 A, 27 Nov 2026): the hole ADMIN KEYS 1 closed at /v1/admin was open by another road. * An owner-scope key acted set_role { phone, role: "owner" } at /v1/act or by run_act, and the phone became an owner. * Now no act that changes who may do what is a key's, token scopes on or off, at /v1/act or by run_act: no role, no * key minted, no grant or window answered, no write of a table that holds keys, credentials, devices or grants, and no * table's visibility changed. */import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { ACCESS_ACTS, PROTECTED } from "../src/token_scopes.ts";import { http, mint, personalObject, tool } from "./personal.ts";const STRANGER = "+919000000777";/** Each access act with arguments it would otherwise run on. */const ASKS: [string, Record<string, unknown>][] = [ ["set_role", { phone: STRANGER, role: "owner" }], ["mint_token", { name: "Invented second key", scopes: ["owner"] }], // ADMIN KEYS 2: what a key may do is set from the owner's device alone. ["token_allow", { id: "any", allow: "all" }], ["unlock_allow", { request_id: "r-1", tables: ["note"], minutes: 10, wrapped: "w1:x", signer: "s", sig: "s" }], ["assistant_allow", { tables: [], withheld: [], wrapped: "w1:x", signer: "s", sig: "s" }], ["assistant_add", { grant_id: "g-1", tables: ["note"], wrapped: "w1:x" }], ["assistant_scope", { table: "note", access: "on" }], ["assistant_remove", {}], ["unlock_decline", { request_id: "r-1" }], ["unlock_end", { grant_id: "g-1" }],];async function scopesOn(slug: string): Promise<void> { await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (instance) => (instance as unknown as { store: { setMeta(k: string, v: string): void } }).store.setMeta("token_scopes", JSON.stringify({ on_at: new Date().toISOString(), by: "test" })));}async function owners(slug: string): Promise<string[]> { return runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (instance) => (instance as unknown as { store: { personByPhone(p: string): { role: string } | undefined } }).store.personByPhone(STRANGER)?.role === "owner" ? [STRANGER] : []);}describe("an act that changes who may do what", () => { it("names every one the dispatch has, and every credential table", () => { expect([...ACCESS_ACTS].sort()).toEqual(ASKS.map(([a]) => a).sort()); for (const t of ["device", "mcp_token", "passkey", "person_key", "seal_key", "vault_key", "unlock_grant", "unlock_request", "assistant_ask", "code_credential"]) expect(PROTECTED.has(t), t).toBe(true); }); for (const scopes of ["off", "on"] as const) { it(`is refused to an owner-scope key at /v1/act and by run_act, token scopes ${scopes}`, async () => { const slug = `access-acts-${scopes}`; const { credential } = await personalObject(slug); const key = await mint(slug, credential, "Invented assistant", ["owner"]); if (scopes === "on") await scopesOn(slug); for (const [act, args] of ASKS) { // run_act passes the same gate (tokenAct), token scopes on or off. const viaTool = await tool(slug, key, "run_act", { act, args }); expect(viaTool.isError, `run_act ${act}`).toBe(true); expect(viaTool.content[0]!.text, `run_act ${act}`).toMatch(/changes who may do what/); if (scopes === "on") continue; const viaAct = await http<{ code: string }>(slug, "POST", "/v1/act", { act, args }, { authorization: `Bearer ${key}` }); expect([viaAct.status, viaAct.body.code], `/v1/act ${act}`).toEqual([403, "access_act"]); } expect(await owners(slug), "the stranger's phone is no owner").toEqual([]); // A credential table is written by no act and no tool of a key. for (const table of ["device", "passkey", "unlock_grant", "mcp_token"]) { if (scopes === "off") { const r = await http<{ code: string }>(slug, "POST", "/v1/act", { act: "create", args: { table, data: { name: "Invented" } } }, { authorization: `Bearer ${key}` }); expect([r.status, r.body.code], `create ${table}`).toEqual([403, "access_act"]); } const viaRun = await tool(slug, key, "run_act", { act: "create", args: { table, data: { name: "Invented" } } }); expect(viaRun.isError, `run_act create ${table}`).toBe(true); const viaRecord = await tool(slug, key, "create_record", { table, data: { name: "Invented" } }); expect(viaRecord.isError, `create_record ${table}`).toBe(true); } // A table's visibility is the owner's: not by update_definition, not by the definition act. const shown = await tool(slug, key, "update_definition", { table: "note", visibility: "public" }); expect([shown.isError, shown.content[0]!.text]).toEqual([true, expect.stringMatching(/who may read or write the table/)]); // Ending access stays open to a key for the keys it minted: the coordinator's lane-stop revokes the lane token its // lane-start minted with the same key (ADMIN KEYS 4); a key the phone minted is the phone's to end. const laneMint = await tool(slug, key, "run_act", { act: "mint_token", args: { name: "Invented lane", scopes: ["read", "lane:invented"], expires_in_days: 1 } }); expect(laneMint.isError, laneMint.content[0]?.text).toBe(false); await mint(slug, credential, "Invented phone key", ["read"], 1); const ids = await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (instance) => { const rows = [...(instance as unknown as { store: { scan(t: string): Iterable<{ id: string; data: Record<string, unknown> }> } }).store.scan("mcp_token")]; return { lane: rows.find((r) => r.data.name === "Invented lane")!.id, phone: rows.find((r) => r.data.name === "Invented phone key")!.id }; }); const revoked = await tool(slug, key, "run_act", { act: "revoke_token", args: { id: ids.lane } }); expect(revoked.isError, revoked.content[0]?.text).toBe(false); expect((await tool(slug, key, "run_act", { act: "revoke_token", args: { id: ids.phone } })).isError).toBe(true); // Some forty requests: room for a loaded machine running the whole suite. }, 240_000); } // The coordinator's call (27 Nov): the one mint a key makes is a lane token, by an owner-scope key, at /v1/act (what // the token minter sends for lane-start) and by run_act alike; logged with the key that minted it. it("an owner-scope key mints a lane token and nothing else; a lane token mints nothing", async () => { const slug = "access-acts-lane-mint"; const { credential } = await personalObject(slug); const owner = await mint(slug, credential, "Invented coordinator", ["owner"]); const asOwner = { authorization: `Bearer ${owner}` }; const lane = { name: "Lane invented", scopes: ["read", "lane:invented"], expires_in_days: 7, machine: "invented-host" }; const made = await http<{ token?: string; made?: { id: string } }>(slug, "POST", "/v1/act", { act: "mint_token", args: lane }, asOwner); expect(made.status, JSON.stringify(made.body)).toBe(200); expect(made.body.token).toMatch(/^ctn_/); const viaRun = await tool(slug, owner, "run_act", { act: "mint_token", args: { ...lane, name: "Lane invented two", expires_in_days: 1 } }); expect(viaRun.isError, viaRun.content[0]?.text).toBe(false); // Logged with who minted it: the row names the minting key. const rows = await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (instance) => [...(instance as unknown as { store: { scan(t: string): Iterable<{ id: string; data: Record<string, unknown> }> } }).store.scan("mcp_token")]); const ownerRow = rows.find((r) => r.data.name === "Invented coordinator")!; const laneRow = rows.find((r) => r.data.name === "Lane invented")!; expect([laneRow.data.minted_by, laneRow.data.machine]).toEqual([ownerRow.id, "invented-host"]); // Anything wider, longer, or more is refused. for (const wider of [ { ...lane, scopes: ["read", "write", "lane:invented"] }, { ...lane, scopes: ["owner"] }, { ...lane, scopes: ["read"] }, { ...lane, scopes: ["read", "lane:a", "lane:b"] }, { ...lane, expires_in_days: 8 }, { name: "Lane invented", scopes: ["read", "lane:invented"] }, { ...lane, allow: "all" }, { ...lane, allow: { read: ["note"], write: [] } }, ]) { const r = await http<{ code: string }>(slug, "POST", "/v1/act", { act: "mint_token", args: wider }, asOwner); expect([r.status, r.body.code], JSON.stringify(wider)).toEqual([403, "access_act"]); } // A lane token mints nothing, a lane token least of all (it never renews itself); nor does a narrower key. const reader = await mint(slug, credential, "Invented reader", ["read", "write"], 7); for (const key of [made.body.token!, reader]) { const r = await http<{ code: string }>(slug, "POST", "/v1/act", { act: "mint_token", args: { ...lane, name: "Lane again" } }, { authorization: `Bearer ${key}` }); // Refused at the owner-only gate (no owner scope) before the access gate is reached: refused either way. expect([r.status, ["access_act", "owner_only"].includes(r.body.code)]).toEqual([403, true]); expect((await tool(slug, key, "run_act", { act: "mint_token", args: { ...lane, name: "Lane again" } })).isError).toBe(true); } }, 120_000); it("the owner's own phone still does them", async () => { const slug = "access-acts-phone"; const { credential } = await personalObject(slug); const phone = { authorization: `Bearer ${credential}` }; const minted = await http<{ token?: string }>(slug, "POST", "/v1/act", { act: "mint_token", args: { name: "Invented key", scopes: ["read"], expires_in_days: 1 } }, phone); expect(minted.status, JSON.stringify(minted.body)).toBe(200); const role = await http(slug, "POST", "/v1/act", { act: "set_role", args: { phone: STRANGER, role: "member" } }, phone); expect(role.status, JSON.stringify(role.body)).not.toBe(403); });});