Cotton code
74 lines · 4.6 KB · TypeScriptRaw
/** * ROUTES-AUDIT 7 (coordinator, 23 Nov 2026): sign-in's per-network hour was counted only at the open host's door, so * `/b/<slug>/v1/code`, `/v1/sign-in` and an object's connect page sent codes with no limit but 5 a number an hour, and * nothing capped every network together (SMS pumping). Now a sign-in call at an object's own door is counted as one at * the open host's, and every network's calls together have an hour's ceiling. */import { env, runInDurableObject, SELF } from "cloudflare:test";import { beforeEach, describe, expect, it } from "vitest";import { ATTEMPTS_PER_HOUR, blockOf, GLOBAL_ATTEMPTS_PER_HOUR, limitsFor } from "../src/directory.ts";import { ADMIN, http } from "./personal.ts";/** A business object, which admits anyone who signs in: a code goes to any number. */const business = async (slug: string) => expect((await http(slug, "PUT", "/v1/admin/business", { name: "An Invented Shop", kind: "business" }, ADMIN)).status).toBe(200);const SLUG = "audit-limits";async function code(ip: string | null, phone: string): Promise<number> {  const res = await SELF.fetch(`https://example.test/b/${SLUG}/v1/code`, { method: "POST", headers: { "content-type": "application/json", ...(ip ? { "cf-connecting-ip": ip } : {}) }, body: JSON.stringify({ phone, device_id: "d" }) });  return res.status;}describe("sign-in at an object's own door", () => {  it("is counted per network, as at the open host", async () => {    await business(SLUG);    // Each number its own, so the 5 a number an hour never speaks first.    for (let i = 0; i < ATTEMPTS_PER_HOUR; i++) expect(await code("203.0.113.7", `+9190000071${String(i).padStart(2, "0")}`)).not.toBe(429);    expect(await code("203.0.113.7", "+919000007199")).toBe(429);    // Another network is not this one's.    expect(await code("203.0.113.8", "+919000007198")).not.toBe(429);  });  it("has a ceiling for every network together", async () => {    expect(limitsFor({}).everywhere).toBe(GLOBAL_ATTEMPTS_PER_HOUR);    expect(limitsFor({ SIGN_IN_ATTEMPTS_GLOBAL_PER_HOUR: "9000" }).everywhere).toBe(9000);    await business(SLUG);    // The hour already holds the ceiling, from networks all over.    await runInDurableObject(env.DIRECTORY.get(env.DIRECTORY.idFromName("directory")), (_i, s) => {      const now = Date.now();      for (let i = 0; i < GLOBAL_ATTEMPTS_PER_HOUR; i += 500) s.storage.sql.exec(`WITH RECURSIVE c(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM c WHERE x < 500) INSERT INTO hit (ip, kind, at) SELECT '*', 'attempt', ? FROM c`, now);    });    expect(await code("198.51.100.200", "+919000007301")).toBe(429);    expect(await code(null, "+919000007302")).toBe(429);  });});describe("AUDIT A H3: no stranger switches sign-in off", () => {  // The ceiling test above leaves the hour full: each of these starts from an empty one.  beforeEach(() => runInDurableObject(env.DIRECTORY.get(env.DIRECTORY.idFromName("directory")), (_i, st) => void st.storage.sql.exec("DELETE FROM hit")));  const post = (ip: string, path: string, body: string) => SELF.fetch(`https://example.test/b/${SLUG}${path}`, { method: "POST", headers: { "content-type": "application/json", "cf-connecting-ip": ip }, body });  it("does not count a junk body that could never reach Twilio, against the network or every network", async () => {    await business(SLUG);    for (let i = 0; i < ATTEMPTS_PER_HOUR + 5; i++) expect((await post("203.0.113.60", "/v1/sign-in", i % 2 ? "" : "{}")).status).not.toBe(429);    expect((await post("203.0.113.60", "/v1/code", "not json")).status).not.toBe(429);    expect(await code("203.0.113.60", "+919000007401")).not.toBe(429);  });  it("counts an IPv6 address by its /64: a machine walking its addresses is one network", async () => {    expect(blockOf("2001:db8:1:2::5")).toBe("2001:db8:1:2::/64");    expect(blockOf("2001:0db8:0001:0002:aaaa:bbbb:cccc:dddd")).toBe("2001:db8:1:2::/64");    expect(blockOf("2001:db8::1")).toBe("2001:db8:0:0::/64");    expect(blockOf("203.0.113.9")).toBe("203.0.113.9");    await business(SLUG);    for (let i = 0; i < ATTEMPTS_PER_HOUR; i++) expect(await code(`2001:db8:77:1::${(i + 1).toString(16)}`, `+9190000075${String(i).padStart(2, "0")}`)).not.toBe(429);    expect(await code("2001:db8:77:1::ffff", "+919000007599")).toBe(429);    expect(await code("2001:db8:77:2::1", "+919000007598"), "the next /64 is another network").not.toBe(429);  });  it("refuses a sign-in body over 16 KB before reading it whole", async () => {    const res = await post("203.0.113.61", "/v1/code", JSON.stringify({ phone: "+919000007601", device_id: "d", pad: "x".repeat(20_000) }));    expect(res.status).toBe(413);  });});