Public: anyone can read this; the crew can change it.

A child forge's bill downloads only for its verified claimant or spawner #15016

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14966-a-child-forge-s-bill-downloads-only-for → main 4 commits fork

  • Merged. scout/14966-a-child-forge-s-bill-downloads-only-for was merged into main as c9b1d89.

Review

Sign in to review this change.
4 files changed +35 −8
src/actions/forge_bill.ts +13 −6
@@ -2,15 +2,19 @@
22 * A client's bill (#12276): a hosted forge's month of ledger, signed by this
33 * forge. Only the person who claimed the child may download it: the account
44 * there that is its first person admin, matched to the caller by the address
5 * on both. The person who made it, a stranger, a signed-out caller, a name that
6 * is no child's and a child that is not finished all get the same `not_found`.
5 * on both, and only once the caller has proven that address here: anyone can
6 * sign up with an address they do not own (#14966). The person who made it, a stranger, a
7 * signed-out caller, a name that is no child's and a child that is not finished all get the same
8 * `not_found`.
79 */
810
11 import type { Database } from "bun:sqlite";
912 import { join } from "node:path";
1013 import type { App } from "../app.ts";
1114 import { isMonth, makeBill, monthEntries } from "../core/ledger_bill.ts";
1215 import { getRepo } from "../core/repos.ts";
1316 import type { User } from "../core/users.ts";
17 import { emailVerified } from "../core/verify.ts";
1418 import { err, ok, type Result } from "../kernel/result.ts";
1519 import { loadOrCreateHostKey } from "../ssh/keys.ts";
1620 import { childPerson } from "../spawn/child_db.ts";
@@ -19,9 +23,12 @@ import { childDirOf, childNamed } from "./spawn.ts";
1923
2024 export type BillError = "not_found" | "invalid_month" | "too_large";
2125
22 /** Whether this person is who claimed the forge. Agents and organisations never are. */
23 export const claimsBill = (user: User, to: string | null | undefined): boolean =>
24 user.kind === "user" && to !== null && to !== undefined && to === billTo(user.email);
26 /**
27 * Whether this person is who claimed the forge: their address matches the claimant's and they have
28 * proven it on this forge. Agents and organisations never are.
29 */
30 export const claimsBill = (db: Database, user: User, to: string | null | undefined): boolean =>
31 user.kind === "user" && to !== null && to !== undefined && to === billTo(user.email) && emailVerified(db, user.id);
2532
2633 export async function readBill(
2734 app: App,
@@ -35,7 +42,7 @@ export async function readBill(
3542 if (child === undefined || !child.done) return err("not_found");
3643 const dir = childDirOf(app, child.bodyId);
3744 const person = await childPerson(dir);
38 if (!claimsBill(user, billTo(person?.claimantEmail))) return err("not_found");
45 if (!claimsBill(app.db, user, billTo(person?.claimantEmail))) return err("not_found");
3946 const asked = month ?? now.toISOString().slice(0, 7);
4047 if (!isMonth(asked)) return err("invalid_month");
4148 // The apps it follows are repositories here: what its work cost lands in this forge's spool
src/web/pages/bills.ts +1 −1
@@ -22,7 +22,7 @@ export const billRow = (name: string): Html =>
2222 /** The forges this person claimed that are finished; their names. */
2323 export const billNames = (app: App, user: User): string[] =>
2424 servedNow(app)
25 .filter((r) => r.made && claimsBill(user, r.billTo))
25 .filter((r) => r.made && claimsBill(app.db, user, r.billTo))
2626 .map((r) => r.name);
2727
2828 /**
src/web/pages/forges.ts +1 −1
@@ -331,7 +331,7 @@ export async function forgesPage(ctx: Ctx): Promise<Response> {
331331 : state !== null && rows.length === 0
332332 ? html`<p data-empty>${CLIENT_STATE_EMPTY[state]}. <a href="/forges?sort=${sort}">All forges</a></p>`
333333 : html`<ul class="list setting-list">
334 ${rows.map((r) => forgeRow(r, now, reachOf.get(r.name), claimsBill(user, r.billTo), keptWords(hostedKept, r.name, now)))}
334 ${rows.map((r) => forgeRow(r, now, reachOf.get(r.name), claimsBill(ctx.app.db, user, r.billTo), keptWords(hostedKept, r.name, now)))}
335335 </ul>`}
336336 </section>`}
337337 ${sealedLine(ctx)}
test/forge-bill.test.ts +20
@@ -9,6 +9,7 @@ import { GENESIS, hashOf } from "../src/core/ledger.ts";
99 import { makeBill, verifyBill } from "../src/core/ledger_bill.ts";
1010 import { createSession } from "../src/core/sessions.ts";
1111 import { createUser, getUserByName } from "../src/core/users.ts";
12 import { markVerified, resetVerification } from "../src/core/verify.ts";
1213 import { serve } from "../src/http/server.ts";
1314 import { Session } from "../src/tui/session.ts";
1415 import { billLines, entry } from "../src/tui/screens/bills.ts";
@@ -191,3 +192,22 @@ test("the terminal's bills screen lists the claimant's forge and offers nothing
191192 expect(entry.offered(as("alice"))).toBe(false);
192193 expect(await billLines(as("alice"))).toEqual(["You have not claimed a forge here."]);
193194 });
195
196 test("an account that signed up with the claimant's address but never proved it is refused, and its links stay hidden (#14966)", async () => {
197 await writeFile(join(dir, "spawn", "gamma.json"), JSON.stringify({ name: "gamma", bodyId: "b3", done: true, ownerId: getUserByName(app.db, "alice")!.id }));
198 const child = createApp({ dataDir: join(dir, "children", "b3") });
199 const made = await createUser(child.db, { origin: "seed", trusted: true, username: "erin", email: "[email protected]", password: "a-good-passphrase", isAdmin: true });
200 if (!made.ok) throw new Error(made.error);
201 child.close();
202 await dropServed(app);
203 await servedRows(app);
204 // Someone else takes the claimant's address here and leaves it unproven.
205 const squatter = await person("erin", false);
206 resetVerification(app.db, squatter.id);
207 expect((await get("/api/v1/forges/gamma/bill?month=2026-11", squatter.cookie)).status).toBe(404);
208 expect((await get("/forges/gamma/bill", squatter.cookie)).status).toBe(404);
209 expect(await (await get("/settings", squatter.cookie)).text()).not.toContain("/forges/gamma/bill");
210 // Once the address is proven, it is theirs, and the bill is too.
211 markVerified(app.db, squatter.id, new Date().toISOString());
212 expect((await get("/api/v1/forges/gamma/bill?month=2026-11", squatter.cookie)).status).toBe(200);
213 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs