Public: anyone can read this; the crew can change it.
A tampered, stale or replayed signed call and a wrong vouch are each refused by a test that fails without the check #15933
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15928-a-tampered-stale-or-replayed-signed-call → main 0 commits fork 1 thread · 1 unresolved
2 files
changed +296
−0
test/pay-again.test.ts added +143
| @@ -0,0 +1,143 @@ | ||
| 1 | /** | |
| 2 | * Paying again for a lapsed forge (`billing/pay_again.ts`, #15928): offered | |
| 3 | * only with a provider connected and a paid plan known; the checkout is for | |
| 4 | * that forge's own plan and its own payer, never for whoever pressed. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, beforeEach, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { useCheckout, type CheckoutProvider, type CheckoutRequest } from "../src/billing/checkout.ts"; | |
| 13 | import { canPayAgain, payAgainPlan, startPayAgain } from "../src/billing/pay_again.ts"; | |
| 14 | import { putEntitlement } from "../src/billing/store.ts"; | |
| 15 | import { err, ok } from "../src/kernel/result.ts"; | |
| 16 | import { childUrlOf } from "../src/spawn/door.ts"; | |
| 17 | import { payAgainGate } from "../src/spawn/host.ts"; | |
| 18 | import { mkdir, writeFile } from "node:fs/promises"; | |
| 19 | ||
| 20 | let dir = ""; | |
| 21 | let app: App; | |
| 22 | let asked: CheckoutRequest[] = []; | |
| 23 | let fails = false; | |
| 24 | ||
| 25 | const provider: CheckoutProvider = { | |
| 26 | name: "fake", | |
| 27 | async create(req) { | |
| 28 | asked.push(req); | |
| 29 | return fails ? err("down") : ok({ id: "cs_1", url: "https://pay.example/cs_1" }); | |
| 30 | }, | |
| 31 | async state() { | |
| 32 | return err("unused"); | |
| 33 | }, | |
| 34 | }; | |
| 35 | ||
| 36 | beforeAll(async () => { | |
| 37 | dir = await mkdtemp(join(tmpdir(), "pay-again-")); | |
| 38 | app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 39 | await mkdir(join(dir, "spawn"), { recursive: true }); | |
| 40 | for (const name of ["alice", "dave"]) | |
| 41 | await writeFile(join(dir, "spawn", `${name}.json`), JSON.stringify({ name, bodyId: `body-${name}`, done: true })); | |
| 42 | // alice's forge paid for Medium, then was cancelled down to Free. | |
| 43 | putEntitlement(app.db, { forge: "alice", plan: "medium", status: "active", payer: "[email protected]", subscriptionId: "sub_a", source: "stripe" }, "free", "system"); | |
| 44 | putEntitlement(app.db, { forge: "alice", plan: "free", status: "none", source: "stripe" }, "free", "system"); | |
| 45 | // bob's forge is still on Large, lapsed. | |
| 46 | putEntitlement(app.db, { forge: "bob", plan: "large", status: "none", payer: "[email protected]", source: "stripe" }, "free", "system"); | |
| 47 | // dave's forge pays: active on Small. | |
| 48 | putEntitlement(app.db, { forge: "dave", plan: "small", status: "active", payer: "[email protected]", subscriptionId: "sub_d", source: "stripe" }, "free", "system"); | |
| 49 | // carol's forge was only ever free. | |
| 50 | putEntitlement(app.db, { forge: "carol", plan: "free", status: "none", source: "granted" }, "free", "system"); | |
| 51 | }); | |
| 52 | ||
| 53 | afterAll(async () => { | |
| 54 | app.db.close(); | |
| 55 | await rm(dir, { recursive: true, force: true }); | |
| 56 | }); | |
| 57 | ||
| 58 | beforeEach(() => { | |
| 59 | asked = []; | |
| 60 | fails = false; | |
| 61 | useCheckout(app, provider); | |
| 62 | }); | |
| 63 | ||
| 64 | describe("payAgainPlan", () => { | |
| 65 | test("is the forge's own paid plan, else the last paid one it had, else none", () => { | |
| 66 | expect(payAgainPlan(app, "bob")?.key).toBe("large"); | |
| 67 | expect(payAgainPlan(app, "alice")?.key).toBe("medium"); | |
| 68 | expect(payAgainPlan(app, "carol")).toBeUndefined(); | |
| 69 | expect(payAgainPlan(app, "nobody")).toBeUndefined(); | |
| 70 | }); | |
| 71 | }); | |
| 72 | ||
| 73 | describe("canPayAgain", () => { | |
| 74 | test("is offered only with a provider and a paid plan", () => { | |
| 75 | expect(canPayAgain(app, "alice")).toBe(true); | |
| 76 | expect(canPayAgain(app, "carol")).toBe(false); | |
| 77 | expect(canPayAgain(app, "nobody")).toBe(false); | |
| 78 | useCheckout(app, undefined); | |
| 79 | expect(canPayAgain(app, "alice")).toBe(false); | |
| 80 | }); | |
| 81 | }); | |
| 82 | ||
| 83 | describe("startPayAgain", () => { | |
| 84 | test("checks out the forge's own plan for the forge's own payer", async () => { | |
| 85 | const made = await startPayAgain(app, "alice", "https://alice.forge.test"); | |
| 86 | expect(made).toEqual(ok("https://pay.example/cs_1")); | |
| 87 | expect(asked).toHaveLength(1); | |
| 88 | const req = asked[0]!; | |
| 89 | expect(req.forge).toBe("alice"); | |
| 90 | expect(req.plan).toBe("medium"); | |
| 91 | expect(req.priceUsd).toBe(49); | |
| 92 | expect(req.email).toBe("[email protected]"); | |
| 93 | expect(req.orderId).toMatch(/^again-/); | |
| 94 | expect(req.successUrl).toBe("https://alice.forge.test/"); | |
| 95 | }); | |
| 96 | ||
| 97 | test("each forge pays for itself: bob's checkout never names alice", async () => { | |
| 98 | await startPayAgain(app, "bob", "https://bob.forge.test"); | |
| 99 | expect(asked[0]?.forge).toBe("bob"); | |
| 100 | expect(asked[0]?.email).toBe("[email protected]"); | |
| 101 | expect(asked[0]?.plan).toBe("large"); | |
| 102 | }); | |
| 103 | ||
| 104 | test("refuses without asking the provider when there is nothing to pay for or no provider", async () => { | |
| 105 | expect((await startPayAgain(app, "carol", "https://c.test")).ok).toBe(false); | |
| 106 | useCheckout(app, undefined); | |
| 107 | expect((await startPayAgain(app, "alice", "https://a.test")).ok).toBe(false); | |
| 108 | expect(asked).toHaveLength(0); | |
| 109 | }); | |
| 110 | ||
| 111 | test("a provider that fails says nothing was charged", async () => { | |
| 112 | fails = true; | |
| 113 | const made = await startPayAgain(app, "alice", "https://a.test"); | |
| 114 | expect(made.ok).toBe(false); | |
| 115 | if (!made.ok) expect(made.error).toContain("nothing was charged"); | |
| 116 | }); | |
| 117 | }); | |
| 118 | ||
| 119 | describe("payAgainGate", () => { | |
| 120 | const press = (name: string, origin?: string): Request => { | |
| 121 | const url = childUrlOf(app, name).replace(/\/+$/, ""); | |
| 122 | const host = new URL(url).host; | |
| 123 | return new Request(`${url}/pay-again`, { method: "POST", headers: { host, origin: origin ?? url } }); | |
| 124 | }; | |
| 125 | ||
| 126 | test("a lapsed forge's own address sends the payer to the checkout", async () => { | |
| 127 | const res = await payAgainGate(app, press("alice")); | |
| 128 | expect(res?.status).toBe(303); | |
| 129 | expect(res?.headers.get("location")).toBe("https://pay.example/cs_1"); | |
| 130 | expect(asked[0]?.forge).toBe("alice"); | |
| 131 | }); | |
| 132 | ||
| 133 | test("a forge that is paying is not offered it", async () => { | |
| 134 | expect(await payAgainGate(app, press("dave"))).toBeUndefined(); | |
| 135 | expect(asked).toHaveLength(0); | |
| 136 | }); | |
| 137 | ||
| 138 | test("a press from another origin is refused before the provider is asked", async () => { | |
| 139 | const res = await payAgainGate(app, press("alice", "https://evil.example")); | |
| 140 | expect(res?.status).toBe(403); | |
| 141 | expect(asked).toHaveLength(0); | |
| 142 | }); | |
| 143 | }); |
test/signed-call.test.ts added +153
| @@ -0,0 +1,153 @@ | ||
| 1 | /** | |
| 2 | * Who may call a child forge (#15928): a signed call (`spawn/signed_call.ts`) | |
| 3 | * is good only with the child's own key over the exact bytes and a time near | |
| 4 | * this forge's clock; a vouch (`spawn/vouch_db.ts`) only for a live session or | |
| 5 | * token of an active account in that very database. | |
| 6 | */ | |
| 7 | ||
| 8 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 9 | import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; | |
| 10 | import { tmpdir } from "node:os"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { createApp, type App } from "../src/app.ts"; | |
| 13 | import { createSession } from "../src/core/sessions.ts"; | |
| 14 | import { createToken } from "../src/core/tokens.ts"; | |
| 15 | import { createUser } from "../src/core/users.ts"; | |
| 16 | import { sha256 } from "../src/kernel/hash.ts"; | |
| 17 | import { signWith } from "../src/spawn/origin_broker_wire.ts"; | |
| 18 | import { CALL_SKEW_MS, signedCall } from "../src/spawn/signed_call.ts"; | |
| 19 | import { vouchedWith } from "../src/spawn/vouch_db.ts"; | |
| 20 | ||
| 21 | const KEY = "a".repeat(64); | |
| 22 | const OTHER_KEY = "b".repeat(64); | |
| 23 | let dir = ""; | |
| 24 | let app: App; | |
| 25 | let other: App; | |
| 26 | ||
| 27 | beforeAll(async () => { | |
| 28 | dir = await mkdtemp(join(tmpdir(), "signed-call-")); | |
| 29 | app = createApp({ dataDir: join(dir, "parent"), port: 0 }); | |
| 30 | other = createApp({ dataDir: join(dir, "other"), port: 0 }); | |
| 31 | await mkdir(join(app.config.dataDir, "spawn"), { recursive: true }); | |
| 32 | for (const [name, beatKey] of [["kid", KEY], ["twin", OTHER_KEY]] as const) | |
| 33 | await writeFile(join(app.config.dataDir, "spawn", `${name}.json`), JSON.stringify({ name, bodyId: `body-${name}`, done: true, beatKey })); | |
| 34 | }); | |
| 35 | ||
| 36 | afterAll(async () => { | |
| 37 | app.db.close(); | |
| 38 | other.db.close(); | |
| 39 | await rm(dir, { recursive: true, force: true }); | |
| 40 | }); | |
| 41 | ||
| 42 | describe("signedCall", () => { | |
| 43 | const now = 1_800_000_000_000; | |
| 44 | const body = (fields: Record<string, unknown> = {}): string => JSON.stringify({ name: "kid", at: now, host: "example.com", ...fields }); | |
| 45 | ||
| 46 | test("a call signed with the child's key is accepted, naming the child", async () => { | |
| 47 | const raw = body(); | |
| 48 | const call = await signedCall(app, raw, signWith(KEY, raw), now); | |
| 49 | expect(call?.name).toBe("kid"); | |
| 50 | expect(call?.fields["host"]).toBe("example.com"); | |
| 51 | }); | |
| 52 | ||
| 53 | test("a changed body is refused", async () => { | |
| 54 | const raw = body(); | |
| 55 | const sig = signWith(KEY, raw); | |
| 56 | expect(await signedCall(app, body({ host: "evil.example" }), sig, now)).toBeUndefined(); | |
| 57 | expect(await signedCall(app, `${raw} `, sig, now)).toBeUndefined(); | |
| 58 | }); | |
| 59 | ||
| 60 | test("a call signed with another child's key, or naming another child, is refused", async () => { | |
| 61 | const raw = body(); | |
| 62 | expect(await signedCall(app, raw, signWith(OTHER_KEY, raw), now)).toBeUndefined(); | |
| 63 | const asTwin = body({ name: "twin" }); | |
| 64 | expect(await signedCall(app, asTwin, signWith(KEY, asTwin), now)).toBeUndefined(); | |
| 65 | }); | |
| 66 | ||
| 67 | test("a child this forge did not make is refused", async () => { | |
| 68 | const raw = body({ name: "stranger" }); | |
| 69 | expect(await signedCall(app, raw, signWith(KEY, raw), now)).toBeUndefined(); | |
| 70 | }); | |
| 71 | ||
| 72 | test("a timestamp beyond CALL_SKEW_MS either side is refused; at the edge it is good", async () => { | |
| 73 | for (const at of [now - CALL_SKEW_MS - 1, now + CALL_SKEW_MS + 1]) { | |
| 74 | const raw = body({ at }); | |
| 75 | expect(await signedCall(app, raw, signWith(KEY, raw), now)).toBeUndefined(); | |
| 76 | } | |
| 77 | for (const at of [now - CALL_SKEW_MS, now + CALL_SKEW_MS]) { | |
| 78 | const raw = body({ at }); | |
| 79 | expect((await signedCall(app, raw, signWith(KEY, raw), now))?.name).toBe("kid"); | |
| 80 | } | |
| 81 | }); | |
| 82 | ||
| 83 | test("a replayed call is refused once its time is stale", async () => { | |
| 84 | const raw = body(); | |
| 85 | const sig = signWith(KEY, raw); | |
| 86 | expect((await signedCall(app, raw, sig, now))?.name).toBe("kid"); | |
| 87 | expect(await signedCall(app, raw, sig, now + CALL_SKEW_MS + 1)).toBeUndefined(); | |
| 88 | }); | |
| 89 | ||
| 90 | test("a missing, malformed or upper-cased signature is refused", async () => { | |
| 91 | const raw = body(); | |
| 92 | const sig = signWith(KEY, raw); | |
| 93 | for (const shown of [null, "", sig.slice(0, 63), `${sig}0`, sig.toUpperCase(), "z".repeat(64)]) | |
| 94 | expect(await signedCall(app, raw, shown, now)).toBeUndefined(); | |
| 95 | }); | |
| 96 | ||
| 97 | test("a body that is not one object with a child name and an integer time is refused", async () => { | |
| 98 | for (const raw of ["not json", "[]", "null", JSON.stringify({ name: "kid" }), JSON.stringify({ name: "kid", at: now + 0.5 }), JSON.stringify({ name: "../kid", at: now }), JSON.stringify({ name: "kid", at: String(now) })]) | |
| 99 | expect(await signedCall(app, raw, signWith(KEY, raw), now)).toBeUndefined(); | |
| 100 | }); | |
| 101 | }); | |
| 102 | ||
| 103 | describe("vouchedWith", () => { | |
| 104 | let session = ""; | |
| 105 | let token = ""; | |
| 106 | let otherSession = ""; | |
| 107 | let otherToken = ""; | |
| 108 | let ownerId = ""; | |
| 109 | ||
| 110 | beforeAll(async () => { | |
| 111 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase" }); | |
| 112 | if (!made.ok) throw new Error("user"); | |
| 113 | ownerId = made.value.id; | |
| 114 | session = createSession(app.db, ownerId).token; | |
| 115 | token = createToken(app.db, ownerId, "t", "ro").token; | |
| 116 | const elsewhere = await createUser(other.db, { origin: "seed", trusted: true, username: "mallory", email: "[email protected]", password: "a-good-passphrase" }); | |
| 117 | if (!elsewhere.ok) throw new Error("user"); | |
| 118 | otherSession = createSession(other.db, elsewhere.value.id).token; | |
| 119 | otherToken = createToken(other.db, elsewhere.value.id, "t", "ro").token; | |
| 120 | }); | |
| 121 | ||
| 122 | test("a live session or token of an active account is vouched for", () => { | |
| 123 | expect(vouchedWith(app.db, { session })).toBe(true); | |
| 124 | expect(vouchedWith(app.db, { token })).toBe(true); | |
| 125 | }); | |
| 126 | ||
| 127 | test("nothing presented, or a secret it never issued, is not", () => { | |
| 128 | expect(vouchedWith(app.db, {})).toBe(false); | |
| 129 | expect(vouchedWith(app.db, { session: "nope", token: "forge_nope" })).toBe(false); | |
| 130 | }); | |
| 131 | ||
| 132 | test("another forge's session or token is not", () => { | |
| 133 | expect(vouchedWith(app.db, { session: otherSession })).toBe(false); | |
| 134 | expect(vouchedWith(app.db, { token: otherToken })).toBe(false); | |
| 135 | }); | |
| 136 | ||
| 137 | test("an expired session or token is not", () => { | |
| 138 | const later = Date.now() + 400 * 86_400_000; | |
| 139 | expect(vouchedWith(app.db, { session }, later)).toBe(false); | |
| 140 | const short = createToken(app.db, ownerId, "short", "ro", { expiresAt: new Date(Date.now() - 1000).toISOString() }).token; | |
| 141 | expect(vouchedWith(app.db, { token: short })).toBe(false); | |
| 142 | app.db.query("UPDATE session SET expires_at = ?1 WHERE token_hash = ?2").run(new Date(Date.now() - 1000).toISOString(), sha256(session)); | |
| 143 | expect(vouchedWith(app.db, { session })).toBe(false); | |
| 144 | expect(vouchedWith(app.db, { token })).toBe(true); | |
| 145 | }); | |
| 146 | ||
| 147 | test("a suspended account's token is not", () => { | |
| 148 | app.db.query("UPDATE user SET suspended_at = ?1 WHERE id = ?2").run(new Date().toISOString(), ownerId); | |
| 149 | expect(vouchedWith(app.db, { token })).toBe(false); | |
| 150 | app.db.query("UPDATE user SET suspended_at = NULL WHERE id = ?1").run(ownerId); | |
| 151 | expect(vouchedWith(app.db, { token })).toBe(true); | |
| 152 | }); | |
| 153 | }); |
new above
offline
Second import from node:fs/promises; fold mkdir and writeFile into the mkdtemp/rm import on line 8.