/** * AUDIT FIX B1 (assets/design/shared/SECURITY-AUDIT-B.md): the findings SECURITY AUDIT B reproduced, now refused. * Every person, number and list is invented. */ import { env, listDurableObjectIds, SELF } from "cloudflare:test"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { bearer, fakes, open } from "./calling.ts"; import { chatPhone, type ChatPhone } from "./chat.ts"; vi.setConfig({ testTimeout: 600_000 }); let n = 0; const number = (): string => `+91900007${String(1000 + ++n).padStart(4, "0")}`; beforeEach(() => { fakes(); }); afterEach(() => { vi.restoreAllMocks(); }); type Body = Record; interface Person { phone: ChatPhone; slug: string; number: string; key: string; ask(method: string, path: string, body?: unknown): Promise<{ status: number; body: T }>; } const b64url = (bytes: Uint8Array): string => btoa(String.fromCharCode(...bytes)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, ""); const opaque = (k: number): string => b64url(crypto.getRandomValues(new Uint8Array(k))); async function person(): Promise { const phone = await chatPhone(number(), `dev-audit-${n}`); const slug = (await env.DIRECTORY.get(env.DIRECTORY.idFromName("directory")).lookup(phone.number))!; const key = opaque(32); // The person's sharing key, as their phone posts it: what a list holds for them, and what reading it is checked against (AUDIT FIX B1). const posted = await open("POST", "/v1/send/key", { key }, bearer(phone.credential)); if (posted.status !== 200) throw new Error(`send key: ${posted.status}`); return { phone, slug, number: phone.number, key, ask: (method: string, path: string, body?: unknown) => open(method, path, body, bearer(phone.credential)) }; } async function accept(x: Person, y: Person): Promise { await x.phone.say("accept", y.number); await y.phone.say("accept", x.number); } function state(id: string, epoch: number, members: [string, string][], items: Record) { return { membership: { set_id: id, epoch, members: Object.fromEntries(members), wraps: { [String(epoch)]: Object.fromEntries(members.map(([num]) => [num, `w1:${opaque(80)}`])) } }, items: Object.fromEntries(Object.entries(items).map(([k, e]) => [k, `g1:${e}:${opaque(40)}`])), }; } describe("SECURITY AUDIT B, reproduced (today's behaviour)", () => { it("B-2: only the founder adds, and only their own people; no post changes another member's key; only the founder removes others; a slot whose key is not the reader's reads nothing", async () => { const a = await person(); // founder const b = await person(); // the attacker, an ordinary member const c = await person(); // another member const g = await person(); // the attacker's second account: a stranger to the founder await accept(a, b); await accept(a, c); await accept(b, g); const id = "list-audit"; const members = (...ps: [string, string][]) => ps; expect((await a.ask("POST", "/v1/shared/lists", { id, state: state(id, 1, members([a.number, a.key], [b.number, b.key], [c.number, c.key]), { i1: 1 }) })).status).toBe(200); const post = (p: Person, s: unknown) => p.ask("POST", "/v1/shared/list/state", { owner: a.number, id, state: s }); // b adds g: refused (a member never adds; and g is not the founder's person). expect((await post(b, state(id, 1, members([a.number, a.key], [b.number, b.key], [c.number, c.key], [g.number, g.key]), { i1: 1 }))).status).toBe(403); // b swaps c's key for its own: refused. expect((await post(b, state(id, 1, members([a.number, a.key], [b.number, b.key], [c.number, b.key]), { i1: 1 }))).status).toBe(403); // b removes c: refused (only the founder removes others). expect((await post(b, state(id, 2, members([a.number, a.key], [b.number, b.key]), { i1: 2 }))).status).toBe(403); // Even the founder adds only their own people: g is not a's. expect((await post(a, state(id, 1, members([a.number, a.key], [b.number, b.key], [c.number, c.key], [g.number, g.key]), { i1: 1 }))).status).toBe(403); // g reads nothing; c and b read as before. expect((await g.ask("GET", `/v1/shared/list?owner=${encodeURIComponent(a.number)}&id=${id}`)).status).toBe(404); expect((await c.ask("GET", `/v1/shared/list?owner=${encodeURIComponent(a.number)}&id=${id}`)).status).toBe(200); // b leaves (removes only themselves): taken. expect((await post(b, state(id, 2, members([a.number, a.key], [c.number, c.key]), { i1: 2 }))).status).toBe(200); // A slot holding a key that is not its reader's own reads nothing: c's phone moves to a new sharing key. expect((await open("POST", "/v1/send/key", { key: opaque(32) }, bearer(c.phone.credential))).status).toBe(200); expect((await c.ask("GET", `/v1/shared/list?owner=${encodeURIComponent(a.number)}&id=${id}`)).status).toBe(404); // The founder still adds their own person and removes anyone. const d = await person(); await accept(a, d); expect((await post(a, state(id, 2, members([a.number, a.key], [c.number, c.key], [d.number, d.key]), { i1: 2 }))).status).toBe(200); expect((await post(a, state(id, 3, members([a.number, a.key], [d.number, d.key]), { i1: 3 }))).status).toBe(200); expect((await d.ask("GET", `/v1/shared/list?owner=${encodeURIComponent(a.number)}&id=${id}`)).status).toBe(200); }); it("B-6: a request to a made-up slug makes no object of its own and writes no schema; it is answered as an absent database", async () => { const slug = `zz-audit-${crypto.randomUUID().slice(0, 8)}`; const id = env.BUSINESS.idFromName(slug); const res = await SELF.fetch(`https://audit.workers.dev/b/${slug}/v1/sync?since=0`); const said = (await res.json()) as { code?: string }; expect(res.status).toBe(401); expect(said.code).toBe("not_signed_in"); expect((await listDurableObjectIds(env.BUSINESS)).some((x) => x.equals(id))).toBe(false); // A database the directory holds is still reached by its slug. const p = await person(); expect((await SELF.fetch(`https://audit.workers.dev/b/${p.slug}/v1/me`, { headers: bearer(p.phone.credential) })).status).toBe(200); }); }); describe("AUDIT FIX B1, shared spaces: a roster statement is the sending member's own, signed, on its own route", () => { it("B-1 and B-10: unsigned, someone else's, on the wrong route or an owner's kind from a member is refused; the owner takes a bad head off", async () => { const sp = await import("./shared_space_people.ts"); const [a, b] = [await sp.person(), await sp.person()]; await sp.accept(a, b); const { space, id } = await sp.found(a, [b]); const aId = id[a.number]!; const bId = id[b.number]!; const head = async () => (await a.ask<{ roster: { seq: number; hash: string } | null }>("GET", `/v1/spaces/${space}`)).body.roster; // A garbage signature from a member, on a member route that succeeds (B-1's attack): refused, nothing kept. const garbage = sp.statement(space, 0, "", { kind: "leave", by: bId }); const refusedLeave = await b.ask<{ code: string }>("POST", `/v1/spaces/${space}/leave`, { statement: garbage }); expect(refusedLeave.body.code).toBe("bad_signature"); expect(await head()).toBeNull(); // b signs a statement that says a made it: refused. expect((await b.ask<{ code: string }>("POST", `/v1/spaces/${space}/leave`, { statement: await sp.signed(b, aId, space, 0, "", { kind: "leave" }) })).body.code).toBe("not_yours"); // A kind on another act's route: refused. expect((await b.ask<{ code: string }>("POST", `/v1/spaces/${space}/keep`, { statement: await sp.signed(b, bId, space, 0, "", { kind: "leave" }) })).body.code).toBe("bad_statement"); // B-10: a member's claim while the owner is here: refused. expect((await b.ask<{ code: string }>("POST", `/v1/spaces/${space}/roster`, { statement: await sp.signed(b, bId, space, 0, "", { kind: "claim" }) })).body.code).toBe("owner_only"); // The owner's own signed statement is kept; a member's correctly signed one that the chain will refuse can be taken // off by the owner, newest first, never the founding one. const first = await sp.signed(a, aId, space, 0, "", { kind: "handover" }); expect((await a.ask("POST", `/v1/spaces/${space}/roster`, { statement: first })).status).toBe(200); const second = await sp.signed(a, aId, space, 1, await sp.hashOf(first), { kind: "handover", junk: true }); expect((await a.ask("POST", `/v1/spaces/${space}/roster`, { statement: second })).status).toBe(200); const h = (await head())!; expect(h.seq).toBe(1); expect((await b.ask<{ code: string }>("POST", `/v1/spaces/${space}/roster/drop`, { seq: 1, hash: h.hash })).body.code).toBe("owner_only"); expect((await a.ask("POST", `/v1/spaces/${space}/roster/drop`, { seq: 1, hash: h.hash })).status).toBe(200); expect((await head())!.seq).toBe(0); expect((await a.ask<{ code: string }>("POST", `/v1/spaces/${space}/roster/drop`, { seq: 0, hash: await sp.hashOf(first) })).body.code).toBe("roster_moved"); }); }); describe("AUDIT FIX B1, chat one-time keys (B-3)", () => { it("a number on no account runs out of one-time keys as a phone does: its fallback, the same key each time", async () => { const asker = await person(); const nobody = number(); const keys = await asker.ask<{ devices: { device: string }[] }>("GET", `/v1/chat/keys?number=${encodeURIComponent(nobody)}`); expect(keys.status, JSON.stringify(keys.body)).toBe(200); const handle = keys.body.devices[0]!.device; const fallbacks: string[] = []; let first = -1; for (let i = 0; i < 101; i++) { const got = await asker.ask<{ prekeys: Record }>("POST", "/v1/chat/prekeys/claim", { number: nobody, devices: [handle] }); expect(got.status).toBe(200); const p = got.body.prekeys[handle]!; if (p.fallback) { if (first < 0) first = i; fallbacks.push(p.key); } else expect(first, "a fallback stays a fallback").toBe(-1); } expect(first).toBeGreaterThanOrEqual(60); expect(first).toBeLessThanOrEqual(100); expect(new Set(fallbacks).size).toBe(1); }); });