70 lines · 5.7 KB · TypeScriptRaw
/** * ROUTES-AUDIT 1 (coordinator, 23 Nov 2026): any phone number could sign in to anyone's personal database. The connect * page's phone step sent a code to any number and put its challenge in the page, and /v1/sign-in made whoever proved * it a member with a device credential. Now a personal database admits its owner's number and a person the owner put * on it, and nobody else: a stranger is sent no code, and a stranger's code proved anyway makes nothing. */import { env, runInDurableObject, SELF } from "cloudflare:test";import { describe, expect, it } from "vitest";import { ADMIN, http, OWNER_PHONE, personalObject } from "./personal.ts";const SLUG = "audit-signin";const STRANGER = "+919000000777";const PLACED = "+919000000778";const object = () => env.BUSINESS.get(env.BUSINESS.idFromName(SLUG));const codesFor = (phone: string) => runInDurableObject(object(), (_i, s) => Number(s.storage.sql.exec<{ n: number }>("SELECT count(*) AS n FROM codes WHERE phone = ?", phone).one().n));const personFor = (phone: string) => runInDurableObject(object(), (instance: unknown) => (instance as { store: { personByPhone(p: string): unknown } }).store.personByPhone(phone));async function form(url: string, body: Record<string, string>): Promise<Response> { return SELF.fetch(url, { method: "POST", redirect: "manual", headers: { "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams(body).toString() });}describe("who signs in to a personal database", () => { it("is never a stranger: no code from any door, and a code proved anyway makes no member", async () => { await personalObject(SLUG); const base = `https://example.test/b/${SLUG}`; // The connect page's phone step, as an attacker would use it. const reg = await SELF.fetch(`${base}/oauth/register`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ client_name: "x", redirect_uris: ["https://claude.ai/api/mcp/auth_callback"], token_endpoint_auth_method: "none" }) }); const clientId = ((await reg.json()) as { client_id: string }).client_id; const start = await SELF.fetch(`${base}/oauth/authorize?${new URLSearchParams({ client_id: clientId, redirect_uri: "https://claude.ai/api/mcp/auth_callback", response_type: "code", code_challenge: "x".repeat(43), code_challenge_method: "S256", state: "s", scope: "owner", resource: `${base}/mcp` })}`); const hidden = Object.fromEntries([...(await start.text()).matchAll(/<input type="hidden" name="([^"]+)" value="([^"]*)">/g)].map((m) => [m[1]!, m[2]!.replace(/&/g, "&")])); const phoneStep = await form(`${base}/oauth/authorize`, { ...hidden, phone: STRANGER }); expect(phoneStep.status).toBe(403); expect(await phoneStep.text()).not.toContain("challenge_id"); // /v1/code refuses the stranger too, word for word as a database that does not exist answers: it never says this one is there. const stranger = await http<Record<string, unknown>>(SLUG, "POST", "/v1/code", { phone: STRANGER, device_id: "attacker" }); const nothing = await http<Record<string, unknown>>("audit-signin-missing", "POST", "/v1/code", { phone: STRANGER, device_id: "attacker" }); expect(stranger.status).toBe(nothing.status); expect(JSON.stringify(stranger.body).replaceAll(SLUG, "<slug>")).toBe(JSON.stringify(nothing.body).replaceAll("audit-signin-missing", "<slug>")); expect(await codesFor(STRANGER)).toBe(0); // A stranger's code that exists anyway (any door a later change opens) is proved, and still makes nothing. await runInDurableObject(object(), async (instance: unknown) => { const store = (instance as { store: { addCode(id: string, phone: string, hash: string, device: string, expires: number): void } }).store; const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode("planted-1:123456"))); store.addCode("planted-1", STRANGER, [...digest].map((b) => b.toString(16).padStart(2, "0")).join(""), "attacker", Math.floor(Date.now() / 1000) + 600); }); const signed = await http<{ credential?: string; code?: string }>(SLUG, "POST", "/v1/sign-in", { challenge_id: "planted-1", code: "123456", device_id: "attacker" }); const nobody = await http<Record<string, unknown>>("audit-signin-missing", "POST", "/v1/sign-in", { challenge_id: "planted-1", code: "123456", device_id: "attacker" }); expect(signed.status, JSON.stringify(signed.body)).toBe(nobody.status); expect(JSON.stringify(signed.body).replaceAll(SLUG, "<slug>")).toBe(JSON.stringify(nobody.body).replaceAll("audit-signin-missing", "<slug>")); expect(signed.body.credential).toBeUndefined(); expect(await personFor(STRANGER)).toBeNull(); // The owner signs in as before. const own = await http<{ challenge_id: string }>(SLUG, "POST", "/v1/code", { phone: OWNER_PHONE, device_id: "owner-2" }); expect(own.status).toBe(200); const ownIn = await http<{ credential: string; person: { role: string } }>(SLUG, "POST", "/v1/sign-in", { challenge_id: own.body.challenge_id, code: "123456", device_id: "owner-2" }); expect(ownIn.status).toBe(200); expect(ownIn.body.person.role).toBe("owner"); // A person the owner put on the database signs in too. expect((await http(SLUG, "POST", "/v1/admin/person", { phone: PLACED, role: "member", name: "Invented Member" }, ADMIN)).status).toBe(200); const placed = await http<{ challenge_id: string }>(SLUG, "POST", "/v1/code", { phone: PLACED, device_id: "member-1" }); expect(placed.status).toBe(200); const placedIn = await http<{ credential: string; person: { role: string } }>(SLUG, "POST", "/v1/sign-in", { challenge_id: placed.body.challenge_id, code: "123456", device_id: "member-1" }); expect(placedIn.status).toBe(200); expect(placedIn.body.person.role).toBe("member"); });});