Cotton code
50 lines · 3.3 KB · TypeScriptRaw
/** * AUDIT F, the stranger audit's timing oracle (28 Nov 2026): POST /v1/code answered a new number in about 2.0 s (its * database made whole, the personal kit laid) and a number already on Cotton in about 0.65 s, so a stranger could tell * from the clock whether a number is on Cotton. Now asking for a code does the same work either way: the directory * writes the new database's meta (a few rows) and the object issues the code; the kit is laid only once a code is * proved, by the number's own person. A wrong code does no more work for a new number than for an old one. * Invented numbers only. */import { env, runInDurableObject, SELF } from "cloudflare:test";import { describe, expect, it } from "vitest";import { slugFor } from "../src/directory.ts";const OPEN = "https://cottondb.com";let ips = 40;const network = () => ({ "cf-connecting-ip": `198.51.100.${++ips}` });async function open<T = Record<string, unknown>>(path: string, body: unknown, ip: Record<string, string>): Promise<{ status: number; body: T }> {  const res = await SELF.fetch(`${OPEN}${path}`, { method: "POST", headers: { "content-type": "application/json", ...ip }, body: JSON.stringify(body) });  const text = await res.text();  return { status: res.status, body: (text ? JSON.parse(text) : null) as T };}/** Whether the database's object holds its kit's tables yet. */const kitLaid = (databaseId: string) =>  runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slugFor(databaseId))), (instance) => (instance as unknown as { store: { holdsDefinitions(kind: string): boolean } }).store.holdsDefinitions("table"));describe("asking for a code says nothing of whether a number is on Cotton (AUDIT F)", { timeout: 120_000 }, () => {  it("a new number's code lays no kit, a wrong code lays none either, and the right code lays it before the sign-in answers", async () => {    const ip = network();    const code = await open<{ challenge_id: string; database_id: string }>("/v1/code", { phone: "+919000077001", device_id: "phone-1" }, ip);    expect(code.status, JSON.stringify(code.body)).toBe(200);    expect(await kitLaid(code.body.database_id), "the code's answer laid no kit").toBe(false);    const wrong = await open("/v1/sign-in", { database_id: code.body.database_id, challenge_id: code.body.challenge_id, code: "000000", device_id: "phone-1" }, ip);    expect(wrong.status).toBe(401);    expect(await kitLaid(code.body.database_id), "a wrong code laid no kit").toBe(false);    const again = await open<{ challenge_id: string; database_id: string }>("/v1/code", { phone: "+919000077001", device_id: "phone-1" }, ip);    const signed = await open<{ credential: string; person: { role: string } }>("/v1/sign-in", { database_id: again.body.database_id, challenge_id: again.body.challenge_id, code: "123456", device_id: "phone-1" }, ip);    expect(signed.status, JSON.stringify(signed.body)).toBe(200);    expect(signed.body.person.role).toBe("owner");    expect(await kitLaid(code.body.database_id), "the proved code laid the kit").toBe(true);    // The person's database is whole: its definitions answer.    const defs = await SELF.fetch(`${OPEN}/v1/definitions`, { headers: { authorization: `Bearer ${signed.body.credential}`, ...ip } });    expect(defs.status).toBe(200);    expect(((await defs.json()) as { tables: unknown[] }).tables.length).toBeGreaterThan(10);  });});