Public: anyone can read this; the crew can change it.
A child forge's bill downloads only for its verified claimant or spawner #15016
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14966-a-child-forge-s-bill-downloads-only-for → main 4 commits fork
4 files
changed +35
−8
src/actions/forge_bill.ts +13 −6
| @@ -2,15 +2,19 @@ | ||
| 2 | 2 | * A client's bill (#12276): a hosted forge's month of ledger, signed by this |
| 3 | 3 | * forge. Only the person who claimed the child may download it: the account |
| 4 | 4 | * 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`. | |
| 7 | 9 | */ |
| 8 | 10 | |
| 11 | import type { Database } from "bun:sqlite"; | |
| 9 | 12 | import { join } from "node:path"; |
| 10 | 13 | import type { App } from "../app.ts"; |
| 11 | 14 | import { isMonth, makeBill, monthEntries } from "../core/ledger_bill.ts"; |
| 12 | 15 | import { getRepo } from "../core/repos.ts"; |
| 13 | 16 | import type { User } from "../core/users.ts"; |
| 17 | import { emailVerified } from "../core/verify.ts"; | |
| 14 | 18 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 15 | 19 | import { loadOrCreateHostKey } from "../ssh/keys.ts"; |
| 16 | 20 | import { childPerson } from "../spawn/child_db.ts"; |
| @@ -19,9 +23,12 @@ import { childDirOf, childNamed } from "./spawn.ts"; | ||
| 19 | 23 | |
| 20 | 24 | export type BillError = "not_found" | "invalid_month" | "too_large"; |
| 21 | 25 | |
| 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); | |
| 25 | 32 | |
| 26 | 33 | export async function readBill( |
| 27 | 34 | app: App, |
| @@ -35,7 +42,7 @@ export async function readBill( | ||
| 35 | 42 | if (child === undefined || !child.done) return err("not_found"); |
| 36 | 43 | const dir = childDirOf(app, child.bodyId); |
| 37 | 44 | 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"); | |
| 39 | 46 | const asked = month ?? now.toISOString().slice(0, 7); |
| 40 | 47 | if (!isMonth(asked)) return err("invalid_month"); |
| 41 | 48 | // 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 => | ||
| 22 | 22 | /** The forges this person claimed that are finished; their names. */ |
| 23 | 23 | export const billNames = (app: App, user: User): string[] => |
| 24 | 24 | servedNow(app) |
| 25 | .filter((r) => r.made && claimsBill(user, r.billTo)) | |
| 25 | .filter((r) => r.made && claimsBill(app.db, user, r.billTo)) | |
| 26 | 26 | .map((r) => r.name); |
| 27 | 27 | |
| 28 | 28 | /** |
src/web/pages/forges.ts +1 −1
| @@ -331,7 +331,7 @@ export async function forgesPage(ctx: Ctx): Promise<Response> { | ||
| 331 | 331 | : state !== null && rows.length === 0 |
| 332 | 332 | ? html`<p data-empty>${CLIENT_STATE_EMPTY[state]}. <a href="/forges?sort=${sort}">All forges</a></p>` |
| 333 | 333 | : 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)))} | |
| 335 | 335 | </ul>`} |
| 336 | 336 | </section>`} |
| 337 | 337 | ${sealedLine(ctx)} |
test/forge-bill.test.ts +20
| @@ -9,6 +9,7 @@ import { GENESIS, hashOf } from "../src/core/ledger.ts"; | ||
| 9 | 9 | import { makeBill, verifyBill } from "../src/core/ledger_bill.ts"; |
| 10 | 10 | import { createSession } from "../src/core/sessions.ts"; |
| 11 | 11 | import { createUser, getUserByName } from "../src/core/users.ts"; |
| 12 | import { markVerified, resetVerification } from "../src/core/verify.ts"; | |
| 12 | 13 | import { serve } from "../src/http/server.ts"; |
| 13 | 14 | import { Session } from "../src/tui/session.ts"; |
| 14 | 15 | 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 | ||
| 191 | 192 | expect(entry.offered(as("alice"))).toBe(false); |
| 192 | 193 | expect(await billLines(as("alice"))).toEqual(["You have not claimed a forge here."]); |
| 193 | 194 | }); |
| 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 | }); |
new above
offline