Public: anyone can read this; the crew can change it.
Access 5c: a linked game player mints the visitor principal #18209
Merged 3h 33m ago, live in production
scout/forge:scout/18189-access-5c-a-linked-game-player-mints-the → main 0 commits fork
8 files
changed +136
−12
docs/features/games.md +1 −1
Diff not shown — a line 2,200 characters wide (limit 2,000). +1 −1
View filesrc/core/visitors.ts +8 −8
| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * A visitor principal (#18112): someone with no account the forge knows, minted only at the moment |
| 3 | 3 | * they make something (a note on an app's page), never on a |
| 4 | * read. Only notes mint today (`apps/feedback.ts`); the shared app (#18188) and the game | |
| 5 | * player (#18189) follow. The id rides its own cookie, `forge_maker`, signed with a key derived for | |
| 6 | * this purpose alone (HKDF, "forge visitor principal v1"), so the analytics cookie | |
| 7 | * (`perf/visitor_id.ts`, `forge_visitor`) can never verify here and is never joined to it: the two | |
| 8 | * share no id, no key and no table. `Sec-GPC: 1` or `DNT: 1` mints nothing, as the analytics side | |
| 9 | * does. | |
| 4 | * read. Notes (`apps/feedback.ts`) and a game client link (#18189, | |
| 5 | * `web/pages/games_client_pack.ts`) mint today; the shared app (#18188) follows. The id rides its | |
| 6 | * own cookie, `forge_maker`, signed with a key derived for this purpose alone (HKDF, "forge visitor | |
| 7 | * principal v1"), so the analytics cookie (`perf/visitor_id.ts`, `forge_visitor`) can never verify | |
| 8 | * here and is never joined to it: the two share no id, no key and no table. `Sec-GPC: 1` or | |
| 9 | * `DNT: 1` mints nothing, as the analytics side does. | |
| 10 | 10 | * |
| 11 | 11 | * A visitor is not a user: `userOf` answers none for it, and `may` (`core/may.ts`) lets it do |
| 12 | 12 | * exactly what anonymous may plus `visitor.make`, within `VISITOR_CAPS` (`http/limits.ts`). |
| @@ -97,11 +97,11 @@ const FORGE_FULL = "This forge has taken all it takes from visitors today; sign | ||
| 97 | 97 | export function admitVisitor( |
| 98 | 98 | db: Database, |
| 99 | 99 | secret: Uint8Array | (() => Uint8Array), |
| 100 | request: { readonly headers: Headers; readonly secure: boolean; readonly client: string }, | |
| 100 | request: { readonly headers: Headers; readonly secure: boolean; readonly client: string; readonly refused?: boolean }, | |
| 101 | 101 | at: number = Date.now(), |
| 102 | 102 | ): Admission { |
| 103 | 103 | const held = heldVisitor(request.headers, secret); |
| 104 | const p: Principal = held === undefined ? { kind: "anonymous", refused: false } : { kind: "visitor", visitorId: held }; | |
| 104 | const p: Principal = held === undefined ? { kind: "anonymous", refused: request.refused === true } : { kind: "visitor", visitorId: held }; | |
| 105 | 105 | if (!holds(db, p, "visitor.make")) return { ok: false, status: 403, says: REFUSED_WORDS }; |
| 106 | 106 | table(db); |
| 107 | 107 | const since = at - DAY_MS; |
src/http/games_routes.ts +2 −1
| @@ -24,7 +24,7 @@ import { BODY_CAPS } from "../kernel/body_caps.ts"; | ||
| 24 | 24 | import { |
| 25 | 25 | gameBoxPage, gameCommandSubmit, gameCrashShare, gameEnvSubmit, gameGrantSubmit, gameLendSubmit, gameServersSubmit, gameJarDownload, gameJarSubmit, gameModInstall, gameModPack, gameModRemove, gameModUpdate, gamePeopleSubmit, gameHead, gameWorldSubmit, gamePowerSubmit, gameSleepSubmit, gameServicePage, gameSettingsSubmit, gameServiceStream, gameShellStream, gameShellSubmit, gameTakeBackSubmit, gamesPage, |
| 26 | 26 | } from "../web/pages/games.ts"; |
| 27 | import { gamePackDownload, gamePackLink, gamePackRevoke } from "../web/pages/games_client_pack.ts"; | |
| 27 | import { gamePackDownload, gamePackLinked, gamePackLink, gamePackRevoke } from "../web/pages/games_client_pack.ts"; | |
| 28 | 28 | import { gameMap, gameMapSlash } from "./games_map.ts"; |
| 29 | 29 | import { gameSparkLink, gameSparkOpen, gameSparkPost, gameSparkRead } from "../web/pages/games_spark.ts"; |
| 30 | 30 | import { gameConfigCommit, gameConfigPage, gameConfigPreview } from "../web/pages/games_config.ts"; |
| @@ -165,6 +165,7 @@ export function gamesRoutes(router: Router<Handler>): void { | ||
| 165 | 165 | router.add("GET", "/games/jar/:id/:name", gameJarDownload); |
| 166 | 166 | // "@pack" is no box name, so a box named "pack" keeps its /games/pack/<service>/stream. |
| 167 | 167 | router.add("GET", "/games/@pack/:token/:file", gamePackDownload); |
| 168 | router.add("POST", "/games/@pack/:token/:file", gamePackLinked); | |
| 168 | 169 | // A box's spark profiles (#18137): "@spark" is no box name, as "@pack". |
| 169 | 170 | router.add("POST", "/games/@spark/:secret/post", gameSparkPost); |
| 170 | 171 | router.add("GET", "/games/@spark/:secret/:key", gameSparkRead); |
src/http/lazy_lists.ts +1
| @@ -648,6 +648,7 @@ export const OPERATIONS_ROUTES = [ | ||
| 648 | 648 | ["GET", "/games"], |
| 649 | 649 | ["GET", "/games/jar/:id/:name"], |
| 650 | 650 | ["GET", "/games/@pack/:token/:file"], |
| 651 | ["POST", "/games/@pack/:token/:file"], | |
| 651 | 652 | ["POST", "/games/@spark/:secret/post"], |
| 652 | 653 | ["GET", "/games/@spark/:secret/:key"], |
| 653 | 654 | ["GET", "/games/head/:uuid"], |
src/web/pages/games_client_pack.ts +55 −2
| @@ -2,11 +2,18 @@ | ||
| 2 | 2 | * The client pack's three routes (`games/client_pack.ts`): an operator makes a link or ends every |
| 3 | 3 | * link, and anyone holding a link downloads the pack. The link is shown once, in the flash. |
| 4 | 4 | */ |
| 5 | import { admitVisitor, countMake } from "../../core/visitors.ts"; | |
| 5 | 6 | import { LINK_DAYS, clientPack, linkOf, makePackLink, revokeLinks } from "../../games/client_pack.ts"; |
| 6 | 7 | import { boxById } from "../../games/store.ts"; |
| 7 | 8 | import type { Ctx } from "../../http/context.ts"; |
| 9 | import { clientBucket } from "../../http/peer.ts"; | |
| 10 | import { secureFor } from "../../http/cookie_secure.ts"; | |
| 11 | import { loadKey } from "../../kernel/secret.ts"; | |
| 12 | import { html } from "../html.ts"; | |
| 8 | 13 | import { readForm } from "../form.ts"; |
| 9 | import { flashTo } from "../layout.ts"; | |
| 14 | import { pageHead } from "../chrome.ts"; | |
| 15 | import { flashTo, page } from "../layout.ts"; | |
| 16 | import { btn } from "../ui.ts"; | |
| 10 | 17 | import { boxOf } from "./games.ts"; |
| 11 | 18 | |
| 12 | 19 | // POST /games/:box/:service/pack/link: a link for the service's pack, 30 days, shown once. |
| @@ -35,11 +42,57 @@ export async function gamePackRevoke(ctx: Ctx): Promise<Response> { | ||
| 35 | 42 | return flashTo(ctx, `/games/${got.box.name}/${service}`, { message: n === 0 ? "No link was open." : `${n} link${n === 1 ? "" : "s"} ended.` }); |
| 36 | 43 | } |
| 37 | 44 | |
| 38 | // GET /games/@pack/:token/:file: the pack, for whoever holds the link. | |
| 45 | /** The pack's own address, past the page: what a launcher fetches and what "Link me" ends at. */ | |
| 46 | const rawOf = (ctx: Ctx): string => `${ctx.url.pathname}?raw=1`; | |
| 47 | ||
| 48 | function linkPage(ctx: Ctx, server: string, says?: string, status = 200): Response { | |
| 49 | return page( | |
| 50 | { | |
| 51 | ctx, | |
| 52 | title: `Join ${server}`, | |
| 53 | share: false, | |
| 54 | body: html`${pageHead(`Join ${server}`, { sub: "The mods your game needs, as one pack for your launcher." })} | |
| 55 | ${says === undefined ? null : html`<p class="notice danger" role="alert">${says}</p>`} | |
| 56 | <form method="post" action="${ctx.url.pathname}">${btn("Link me and get the pack", { variant: "primary" })}</form> | |
| 57 | <p class="small">Linking makes you a visitor here, with a cookie to come back by. Or <a href="${rawOf(ctx)}">download the pack</a> without linking.</p>`, | |
| 58 | }, | |
| 59 | status, | |
| 60 | ); | |
| 61 | } | |
| 62 | ||
| 63 | // POST /games/@pack/:token/:file: a player links themselves. A visitor is admitted (may, then the | |
| 64 | // caps) and counted once the link is made; the cookie rides only then. A signed-in person is | |
| 65 | // already someone: they get the pack and mint nothing. | |
| 66 | export async function gamePackLinked(ctx: Ctx): Promise<Response> { | |
| 67 | const form = await readForm(ctx); | |
| 68 | if (form instanceof Response) return form; | |
| 69 | const link = linkOf(ctx.app.db, ctx.params.token ?? ""); | |
| 70 | const box = link === undefined ? undefined : boxById(ctx.app.db, link.boxId); | |
| 71 | if (link === undefined || box === undefined) return new Response("not found", { status: 404 }); | |
| 72 | const headers = new Headers({ location: rawOf(ctx), "cache-control": "private, no-store" }); | |
| 73 | if (ctx.auth.user === undefined) { | |
| 74 | const client = clientBucket(ctx.request); | |
| 75 | const admitted = admitVisitor(ctx.app.db, () => loadKey(ctx.app.config.dataDir), { | |
| 76 | headers: ctx.request.headers, | |
| 77 | secure: secureFor(ctx.url, ctx.app.config.url), | |
| 78 | client, | |
| 79 | refused: ctx.auth.refused === true, | |
| 80 | }); | |
| 81 | if (!admitted.ok) return linkPage(ctx, link.service, admitted.says, admitted.status); | |
| 82 | countMake(ctx.app.db, admitted.visitorId, client); | |
| 83 | if (admitted.setCookie !== undefined) headers.append("set-cookie", admitted.setCookie); | |
| 84 | } | |
| 85 | return new Response(null, { status: 303, headers }); | |
| 86 | } | |
| 87 | ||
| 88 | // GET /games/@pack/:token/:file: the pack, for whoever holds the link. A browser asking for a page | |
| 89 | // is shown one with the choice to link; a launcher, or `?raw=1`, gets the file. A read mints | |
| 90 | // nothing. | |
| 39 | 91 | export async function gamePackDownload(ctx: Ctx): Promise<Response> { |
| 40 | 92 | const link = linkOf(ctx.app.db, ctx.params.token ?? ""); |
| 41 | 93 | const box = link === undefined ? undefined : boxById(ctx.app.db, link.boxId); |
| 42 | 94 | if (link === undefined || box === undefined) return new Response("not found", { status: 404 }); |
| 95 | if (ctx.url.searchParams.get("raw") !== "1" && (ctx.request.headers.get("accept") ?? "").includes("text/html")) return linkPage(ctx, link.service); | |
| 43 | 96 | const made = await clientPack(ctx.app, box, link.service); |
| 44 | 97 | if (!made.ok) return new Response(made.error, { status: 409, headers: { "cache-control": "private, no-store" } }); |
| 45 | 98 | if (made.value.kind === "link") return new Response(null, { status: 302, headers: { location: made.value.url, "cache-control": "private, no-store" } }); |
test/cookie-flags.test.ts +1
| @@ -113,6 +113,7 @@ const WALKED: Record<string, number> = { | ||
| 113 | 113 | "src/http/rp.ts": 6, // parent sign-in and link: state cookie set and cleared |
| 114 | 114 | "src/apps/proxy.ts": 3, // app grant, app sign-out, toolbar dismissal |
| 115 | 115 | "src/apps/feedback.ts": 1, // a stranger's note on an app host: only the answer to a note made mints a visitor id |
| 116 | "src/web/pages/games_client_pack.ts": 1, // a linked game player mints the visitor id only on the link (#18189) | |
| 116 | 117 | "src/web/pages/passkeys.ts": 5, // import, passkey sign-in, passkey add/recovery-link sign-in, access-link sign-in (+1 mention) |
| 117 | 118 | "src/web/pages/site.ts": 5, // sessionCookie() itself and its uses: password sign-in/sign-up, logout; the theme cookie; the closing flash on a sign-in (#15619) |
| 118 | 119 | "src/web/pages/first-admin.ts": 1, // the first admin made by passkey |
test/cookie-floor.test.ts +1
| @@ -27,6 +27,7 @@ const SCRIPT_READABLE: Record<string, string> = {}; | ||
| 27 | 27 | const PASSED_IN: Record<string, string> = { |
| 28 | 28 | "src/web/layout.ts o.cookie": "a page's `cookie` option; each caller passes a line from setCookie (the theme in src/web/pages/site.ts)", |
| 29 | 29 | "src/apps/feedback.ts admitted.setCookie": "the visitor id `admitVisitor` mints, its line from setCookie in src/core/visitors.ts", |
| 30 | "src/web/pages/games_client_pack.ts admitted.setCookie": "the visitor id `admitVisitor` mints, its line from setCookie in src/core/visitors.ts", | |
| 30 | 31 | "src/web/pages/start-forge.ts resumeLine": "withCookie's line; each caller passes keepResumeKey or dropResumeKey from src/web/resume_cookie.ts, which call setCookie and expireCookie", |
| 31 | 32 | }; |
| 32 | 33 |
test/game-client-pack.test.ts +67
| @@ -14,6 +14,7 @@ import { getRepo } from "../src/core/repos.ts"; | ||
| 14 | 14 | import { createToken } from "../src/core/tokens.ts"; |
| 15 | 15 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; |
| 16 | 16 | import { keptPath } from "../src/http/access_log.ts"; |
| 17 | import { VISITOR_CAPS } from "../src/http/limits.ts"; | |
| 17 | 18 | import { LINK_DAYS, clientPack, linkOf, loaderOf, makeLink, openLinks, packRow, revokeLinks, zipOne } from "../src/games/client_pack.ts"; |
| 18 | 19 | import { createSession } from "../src/core/sessions.ts"; |
| 19 | 20 | import type { Locked } from "../src/games/modrinth.ts"; |
| @@ -287,6 +288,72 @@ describe("the client pack", () => { | ||
| 287 | 288 | expect((await http.handle(new Request(`${ORIGIN}/games/@pack/${token}/survival.mrpack`))).status).toBe(404); |
| 288 | 289 | }); |
| 289 | 290 | |
| 291 | test("a player links themselves (#18189): a read mints nothing; linking mints, counts, and sets the cookie; DNT and the caps are kept", async () => { | |
| 292 | const token = makeLink(app.db, box, "survival", "t"); | |
| 293 | const file = `${ORIGIN}/games/@pack/${token}/survival.mrpack`; | |
| 294 | // The table is made by the first admission: before it, nothing was counted. | |
| 295 | const made = () => (app.db.query("SELECT 1 FROM sqlite_master WHERE name = 'visitor_made'").get() === null ? 0 : app.db.query<{ n: number }, []>("SELECT count(*) AS n FROM visitor_made").get()!.n); | |
| 296 | const link = (extra: Record<string, string> = {}, ip = "198.51.100.7") => | |
| 297 | http.handle(new Request(file, { method: "POST", headers: { "x-forwarded-for": ip, ...extra } })); | |
| 298 | const none = made(); | |
| 299 | // A read, as a launcher or a browser: no cookie, nothing counted. A browser sees the choice. | |
| 300 | const raw = await http.handle(new Request(file)); | |
| 301 | expect(raw.headers.get("content-type")).toBe("application/x-modrinth-modpack+zip"); | |
| 302 | const seen = await http.handle(new Request(file, { headers: { accept: "text/html" } })); | |
| 303 | expect(await seen.text()).toContain("Link me and get the pack"); | |
| 304 | for (const r of [raw, seen]) expect(r.headers.get("set-cookie")).toBeNull(); | |
| 305 | expect(made()).toBe(none); | |
| 306 | // Linking mints the cookie and counts once; the redirect ends at the file. | |
| 307 | const r = await link(); | |
| 308 | expect(r.status).toBe(303); | |
| 309 | expect(r.headers.get("location")).toBe(`/games/@pack/${token}/survival.mrpack?raw=1`); | |
| 310 | expect(r.headers.get("set-cookie")).toMatch(/^forge_maker=v[0-9a-f]{32}\./); | |
| 311 | expect(r.headers.get("set-cookie")).toMatch(/HttpOnly/i); | |
| 312 | expect(made()).toBe(none + 1); | |
| 313 | expect((await http.handle(new Request(`${file}?raw=1`, { headers: { accept: "text/html" } }))).status).toBe(200); | |
| 314 | // Do Not Track: linked and counted, no id kept. | |
| 315 | const dnt = await link({ dnt: "1" }); | |
| 316 | expect(dnt.status).toBe(303); | |
| 317 | expect(dnt.headers.get("set-cookie")).toBeNull(); | |
| 318 | expect(made()).toBe(none + 2); | |
| 319 | // A dead link mints nothing. | |
| 320 | const gone = await http.handle(new Request(`${ORIGIN}/games/@pack/${token}x/survival.mrpack`, { method: "POST" })); | |
| 321 | expect(gone.status).toBe(404); | |
| 322 | expect(made()).toBe(none + 2); | |
| 323 | // Past the client's cap the next link is refused with 429, no cookie, nothing counted. | |
| 324 | const rows = app.db.prepare("INSERT INTO visitor_made (visitor_id, client, at) VALUES ('', ?, ?)"); | |
| 325 | const client = app.db.query<{ client: string }, []>("SELECT client FROM visitor_made ORDER BY rowid DESC LIMIT 1").get()!.client; | |
| 326 | for (let i = 0; i < VISITOR_CAPS.perClient.perDay; i++) rows.run(client, Date.now()); | |
| 327 | const full = await link(); | |
| 328 | expect(full.status).toBe(429); | |
| 329 | expect(full.headers.get("set-cookie")).toBeNull(); | |
| 330 | expect(await full.text()).toContain("sign in to make more"); | |
| 331 | }); | |
| 332 | ||
| 333 | test("a refused credential is refused by may (403), and a full forge by its cap (429): no cookie, nothing counted (#18189)", async () => { | |
| 334 | const token = makeLink(app.db, box, "survival", "t2"); | |
| 335 | const file = `${ORIGIN}/games/@pack/${token}/survival.mrpack`; | |
| 336 | const made = () => app.db.query<{ n: number }, [number]>("SELECT count(*) AS n FROM visitor_made WHERE at > ?").get(Date.now() - 86_400_000)!.n; | |
| 337 | const before = made(); | |
| 338 | const link = (headers: Record<string, string>) => http.handle(new Request(file, { method: "POST", headers })); | |
| 339 | // A credential sent and refused is a refused principal: not a visitor. | |
| 340 | const bad = await link({ authorization: "Bearer not-a-token", "x-forwarded-for": "198.51.100.8" }); | |
| 341 | expect(bad.status).toBe(403); | |
| 342 | expect(bad.headers.get("set-cookie")).toBeNull(); | |
| 343 | expect(await bad.text()).toContain("Sign in to make this."); | |
| 344 | expect(made()).toBe(before); | |
| 345 | // The forge's own cap, spent by other clients: the next link is refused whoever asks. | |
| 346 | const rows = app.db.prepare("INSERT INTO visitor_made (visitor_id, client, at) VALUES ('', ?, ?)"); | |
| 347 | app.db.transaction(() => { | |
| 348 | for (let i = before; i < VISITOR_CAPS.perForge.perDay; i++) rows.run(`other-${i}`, Date.now()); | |
| 349 | })(); | |
| 350 | const full = await link({ "x-forwarded-for": "198.51.100.9" }); | |
| 351 | expect(full.status).toBe(429); | |
| 352 | expect(full.headers.get("set-cookie")).toBeNull(); | |
| 353 | expect(await full.text()).toContain("all it takes from visitors today"); | |
| 354 | expect(made()).toBe(VISITOR_CAPS.perForge.perDay); | |
| 355 | }); | |
| 356 | ||
| 290 | 357 | test("a link of 30 days ends at its time and when revoked; a viewer, and a server not theirs, cannot make one", async () => { |
| 291 | 358 | const token = makeLink(app.db, box, "survival", "t", Date.now()); |
| 292 | 359 | expect(linkOf(app.db, token)).toEqual({ boxId: box.id, service: "survival" }); |
new above
offline