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

Access 3, games: mayOnGame answers alone for the box, server and game-door checks #18429

opened this

Merged 8h 21m ago, live in production

mason/forge:mason/18427-access-3-games-mayongame-answers-alone-f → main 0 commits fork 1 thread · 1 unresolved

  • Merged. mason/18427-access-3-games-mayongame-answers-alone-f was merged into main as a73dcca.

Review

Sign in to review this change.
15 files changed +152 −93
docs/features/may.md +15 −7
@@ -79,13 +79,21 @@ Nothing anyone is offered changed: the palette offers just what the old gates di
7979 has no action for (a telemetry role, a subscription, an organization, a game grant) or on nothing names no action and
8080 keeps its own `offered`, with a `// Names no \`asks\`: …` comment beside it saying why.
8181
82 **The game grants ask it.** A game box's grant checks (the box and server pages, the games MCP tool, the game door's
83 API, the map, and the terminal's Games screens and keys) go through `mayOnGame` (`src/games/game_may.ts`): the grant still
84 decides, `may` is asked `game.view`, `game.operate` or `game.own` beside it with the caller's credential, and where the
85 two part the count's door is `game <door>` (`game page`, `game tool console`, `game shell`, …). An `ro` token that
86 operates a server by its grant is the parting you will see: `may` says its scope only reads. Seeing a box at all (its
87 page, the tool's `metrics` without a `service`) goes through `seesGameBox`, which asks `game.view` over the box: a person
88 whose grants are on one server only sees the box but holds no `game.view` over all of it, an expected parting.
82 **The game grants ask it alone.** A game box's grant checks (the box and server pages, the games MCP tool, the game
83 door's API, the map, the infrastructure page's container console, and the terminal's Games screens and keys) go through
84 `mayOnGame` (`src/games/game_may.ts`), which is `may`'s answer and nothing beside it (#18427): `game.view`,
85 `game.operate` or `game.own` over the box or one server, by the `game_grant` row there (forge admins own every box).
86 `may` holds a token's scope too, so who is asked decides how far a token reaches. A read (opening a page, `GET` of the
87 door's API) asks of the account behind the request, so an `ro` token opens what its account's grant opens; a change
88 (a page's `POST`, the door's `POST`, the MCP tool) asks with the credential sent, so an `ro` token operates and owns
89 nothing: it is told not found on a page and 403 on the API (`askingFor`, `src/http/page_may.ts`). The terminal and the
90 map's pass ask as the account. `test/door-inline-checks.test.ts` keeps an inline `!mayDo(…)` refusal out of the game
91 door files.
92
93 Seeing a box at all (its page, the tool's `metrics` without a `service`) goes through `seesGameBox`, still in shadow:
94 `seesBox` decides and `may` is asked `game.view` over the box beside it. A person whose grants are on one server only
95 sees the box but holds no `game.view` over all of it, an expected parting counted under `game <door>` (`game page`,
96 `game tool metrics`).
8997
9098 Not asked yet: the checks that only filter what a list shows (which servers, snapshots and status lines a box's page,
9199 the tool's `list`, the terminal's Games list, the cloud's containers and the in-game chat show to whom), and the checks
src/design/door_checks.ts +20 −11
@@ -1,11 +1,13 @@
11 /**
22 * The inline access check does not come back into a moved door (#18406, Access 3's last step). The
33 * repository routes under `src/http/api/`, the MCP tools (`src/http/mcp.ts`, `src/http/mcp/`), the
4 * infrastructure pages and the cloud API (`src/http/infra_routes.ts`, `src/http/api/cloud.ts`)
5 * and the web pages (`src/web/pages/`, #18424) now ask `may`; this ratchet reads their source and
6 * finds each line that gates on the account inline: a refusal on `isAdmin` (`!user.isAdmin`,
7 * `isAdmin !== true`, or `isAdmin` in an `if`), a `scopeAllows(…, "rw")` write floor, or a
8 * signed-out test (`!ctx.auth.user`, `ctx.auth.user === undefined` in a condition).
4 * infrastructure pages and the cloud API (`src/http/infra_routes.ts`, `src/http/api/cloud.ts`),
5 * the web pages (`src/web/pages/`, #18424) and the game doors (the door's API, the map; #18427)
6 * now ask `may`; this ratchet reads their source and finds each line that gates on the account
7 * inline: a refusal on `isAdmin` (`!user.isAdmin`, `isAdmin !== true`, or `isAdmin` in an `if`), a
8 * `scopeAllows(…, "rw")` write floor, a signed-out test (`!ctx.auth.user`, `ctx.auth.user ===
9 * undefined` in a condition), or a refusal on a game grant read inline (`!mayDo(…)`, where
10 * `mayOnGame` answers).
911 * The terminal's palette entries under `src/tui/screens/` offer by `may` too (#18426): there the
1012 * gate is an entry's own `offered`, and one is found unless the comment right above it says why
1113 * the entry names no action (`// Names no \`asks\`: …`).
@@ -20,7 +22,7 @@
2022 */
2123 import { tsSources, type Source } from "./ratchets.ts";
2224
23 export type DoorCheck = "isAdmin" | "scopeAllows" | "signedOut" | "entryGate";
25 export type DoorCheck = "isAdmin" | "scopeAllows" | "signedOut" | "grant" | "entryGate";
2426
2527 /** One line found: its file, line number, which check, and the line's trimmed text. */
2628 export interface DoorFound {
@@ -35,6 +37,7 @@ const HELPER: Record<DoorCheck, string> = {
3537 isAdmin: `adminPage(ctx) or personAdminPage(ctx) on a page, holds(db, principalOf(ctx.auth), "forge.administer") on a route`,
3638 scopeAllows: `holds(db, principalOf(ctx.auth), "forge.write") (mayToolWrite in the MCP)`,
3739 signedOut: "signedInPage(ctx)",
40 grant: `mayOnGame(db, askingFor(ctx), box, service, need) (src/games/game_may.ts)`,
3841 entryGate: `asks: "forge.administer" or "forge.read" on the entry, with for: "person" for a person's screen`,
3942 };
4043
@@ -50,6 +53,7 @@ export const DOOR_FIX = {
5053 const SHAPES: readonly (readonly [DoorCheck, RegExp])[] = [
5154 ["isAdmin", /!\s*[\w?.]*\bisAdmin\b|\bisAdmin\s*!==?\s*true\b|\bisAdmin\s*===?\s*false\b|\bif\s*\(.*\bisAdmin\b/],
5255 ["scopeAllows", /\bscopeAllows\([^)]*"rw"\s*\)/],
56 ["grant", /!\s*mayDo\(/],
5357 ["signedOut", /!\s*ctx\.auth\.user\b(?!\s*\??\.)|(?:\bif\s*\(|\|\||&&)\s*ctx\.auth\.user\s*===?\s*(?:undefined|null)\b/],
5458 ];
5559
@@ -85,15 +89,20 @@ export function doorChecks(src: Source): DoorFound[] {
8589 });
8690 }
8791
88 /** The moved doors outside those: the infrastructure pages and the cloud API (#18425). */
89 const MOVED = new Set(["src/http/mcp.ts", "src/http/infra_routes.ts"]);
92 /**
93 * The moved doors outside those: the infrastructure pages and the cloud API (#18425), the game
94 * door's API and the map (#18427).
95 */
96 const MOVED = new Set(["src/http/mcp.ts", "src/http/infra_routes.ts", "src/http/games_map.ts"]);
97 const MOVED_API = new Set(["src/http/api/cloud.ts", "src/http/api/game_door.ts"]);
9098
9199 /**
92 * The files the moved doors live in: repository route modules and the cloud API under
93 * src/http/api, the infrastructure pages, the MCP, the web pages, and the terminal's screens.
100 * The files the moved doors live in: repository route modules, the cloud API and the game door's
101 * under src/http/api, the infrastructure pages, the map, the MCP, the web pages, and the
102 * terminal's screens.
94103 */
95104 export function doorSources(root: string): Source[] {
96 const api = tsSources(root, "src/http/api").filter((f) => f.text.includes("/api/v1/repos/") || f.path === "src/http/api/cloud.ts");
105 const api = tsSources(root, "src/http/api").filter((f) => f.text.includes("/api/v1/repos/") || MOVED_API.has(f.path));
97106 const mcp = tsSources(root, "src/http").filter((f) => MOVED.has(f.path));
98107 return [...api, ...mcp, ...tsSources(root, "src/http/mcp"), ...tsSources(root, "src/web/pages"), ...tsSources(root, SCREENS.slice(0, -1))];
99108 }
src/games/game_may.ts +22 −27
@@ -1,21 +1,25 @@
11 /**
2 * A game box's grant checks ask `may` in shadow (#18396), as the repository routes, the MCP tools,
3 * the web pages and the terminal's palette did before them. `game_grant`'s viewer, operator and
4 * owner are `may`'s `game.view`, `game.operate` and `game.own` over the box or one server of it
5 * (`core/may.ts`); the grant is the one there is, and no table is added.
2 * A game box's grant checks ask `may` (#18396, #18427). `game_grant`'s viewer, operator and owner
3 * are `may`'s `game.view`, `game.operate` and `game.own` over the box or one server of it
4 * (`core/may.ts`); the grant is the data behind them, and no table is added.
65 *
7 * The old check (`mayDo(roleOn(…), need)`) still decides; `may` is asked beside it with the
8 * principal the door has (a request's credential, or the terminal's account), and where the two
9 * part one row is counted under `game <door>` (`core/access_disagreement.ts`). Nobody's access
10 * changes here.
6 * `mayOnGame` answers alone for the box, server and game-door checks: the inline grant check
7 * (`mayDo(roleOn(…), need)`) it was asked beside is gone. `may` holds a token's scope too, so the
8 * principal a door passes decides how far a token reaches: a read asks of the account behind it,
9 * a change with the credential sent, which an `ro` token is refused (`http/page_may.ts`,
10 * `askingFor`).
11 *
12 * `seesGameBox` is still asked in shadow: a person whose grants are on one server only sees the box
13 * yet holds no `game.view` over all of it, so `seesBox` decides and a parting is counted under
14 * `game <door>` (`core/access_disagreement.ts`).
1115 */
1216
1317 import type { Database } from "bun:sqlite";
1418 import { askBeside } from "../core/access_disagreement.ts";
15 import type { Action, Resource } from "../core/may.ts";
19 import { holds, type Action, type Resource } from "../core/may.ts";
1620 import { userOf, type Principal } from "../core/principal.ts";
1721 import type { User } from "../core/users.ts";
18 import { grantOn, isWaker, mayDo, roleOn, seesBox, type Role } from "./store.ts";
22 import { grantOn, isWaker, seesBox, type Role } from "./store.ts";
1923
2024 const ACTION_OF: Readonly<Record<Role, Action>> = { viewer: "game.view", operator: "game.operate", owner: "game.own" };
2125
@@ -24,21 +28,9 @@ export const gameResource = (db: Database, user: User | undefined, boxId: string
2428 game: { granted: grantOn(db, user, boxId, service), helper: isWaker(service) },
2529 });
2630
27 /**
28 * Ask `may` `action` over the box (or its `service`) beside `old`, counting a parting; returns
29 * `old`.
30 */
31 function beside(db: Database, asking: Principal, door: string, action: Action, old: boolean, boxId: string, service = ""): boolean {
32 askBeside(db, asking, `game ${door}`, action, old, gameResource(db, userOf(asking), boxId, service));
33 return old;
34 }
35
36 /**
37 * Whether `asking` holds `need` on the box `boxId` (or its `service`), by the grant check, which
38 * still decides; `may` is asked beside it and a parting counted under `game <door>`.
39 */
40 export const mayOnGame = (db: Database, asking: Principal, door: string, boxId: string, service: string, need: Role): boolean =>
41 beside(db, asking, door, ACTION_OF[need], mayDo(roleOn(db, userOf(asking), boxId, service), need), boxId, service);
31 /** Whether `asking` holds `need` on the box `boxId` (or its `service`): `may`'s answer alone. */
32 export const mayOnGame = (db: Database, asking: Principal, boxId: string, service: string, need: Role): boolean =>
33 holds(db, asking, ACTION_OF[need], gameResource(db, userOf(asking), boxId, service));
4234
4335 /**
4436 * Whether `asking` sees the box `boxId` at all (any grant on it, a server's too), by `seesBox`,
@@ -46,5 +38,8 @@ export const mayOnGame = (db: Database, asking: Principal, door: string, boxId:
4638 * are on one server only sees the box yet holds no `game.view` over all of it: that parting is
4739 * expected, and counted under `game <door>`.
4840 */
49 export const seesGameBox = (db: Database, asking: Principal, door: string, boxId: string): boolean =>
50 beside(db, asking, door, "game.view", seesBox(db, userOf(asking), boxId), boxId);
41 export function seesGameBox(db: Database, asking: Principal, door: string, boxId: string): boolean {
42 const old = seesBox(db, userOf(asking), boxId);
43 askBeside(db, asking, `game ${door}`, "game.view", old, gameResource(db, userOf(asking), boxId));
44 return old;
45 }
src/http/api/game_door.ts +2 −2
@@ -14,14 +14,14 @@ import { doorOf, doorWords, lockDoor, openWindow } from "../../games/write_windo
1414 import type { Ctx, Handler } from "../context.ts";
1515 import { apiError, body, json, notFound, requireScope } from "../respond.ts";
1616 import type { Router } from "../router.ts";
17 import { principalOf } from "../../core/principal.ts";
17 import { askingFor } from "../page_may.ts";
1818 import { mayOnGame } from "../../games/game_may.ts";
1919
2020 /** The box when the caller owns it. */
2121 function ownedBox(ctx: Ctx) {
2222 const user = ctx.auth.user;
2323 const box = user === null || user === undefined ? undefined : boxNamed(ctx.app.db, ctx.params.box ?? "");
24 return user && box && mayOnGame(ctx.app.db, principalOf(ctx.auth), "door", box.id, "", "owner") ? box : undefined;
24 return user && box && mayOnGame(ctx.app.db, askingFor(ctx), box.id, "", "owner") ? box : undefined;
2525 }
2626
2727 /** The door as the app reads it: the state and the words the page says of it. */
src/http/games_map.ts +1 −1
@@ -55,7 +55,7 @@ export async function gameMap(ctx: Ctx): Promise<Response> {
5555 const userId = box === undefined ? undefined : passUser(ctx.app.config.dataDir, box.id, service, ctx.params.pass ?? "");
5656 const user = userId === undefined ? undefined : getUserById(ctx.app.db, userId);
5757 // A grant taken back ends the pass at the next file.
58 if (box === undefined || user === undefined || !mayOnGame(ctx.app.db, principalOfUser(user), "map", box.id, service, "viewer")) return refuse(404, "no such map");
58 if (box === undefined || user === undefined || !mayOnGame(ctx.app.db, principalOfUser(user), box.id, service, "viewer")) return refuse(404, "no such map");
5959 if (!servicesOf(ctx.app.db, box.id).some((s) => s.name === service)) return refuse(404, "no such server");
6060 const ask = mapPath(ctx.params.path ?? "");
6161 if (!ask.ok) return refuse(404, ask.error);
src/http/mcp/tools/games.ts +5 −5
@@ -122,7 +122,7 @@ export const tool: Tool = {
122122 // A server added or taken out is the box's: its operators', planned first.
123123 if (action === "create_server" || action === "remove_server") {
124124 if (box === undefined || !seesBox(db, user, box.id)) return bad("not_found", "no such box here");
125 if (!mayOnGame(db, principalOf(ctx.auth), `tool ${action}`, box.id, "", "operator")) return bad("forbidden", `you are not an operator of ${box.name}`);
125 if (!mayOnGame(db, principalOf(ctx.auth), box.id, "", "operator")) return bad("forbidden", `you are not an operator of ${box.name}`);
126126 let planned;
127127 if (action === "create_server") {
128128 const parsed = await wishOn(ctx.app, box, str(args["words"]));
@@ -151,15 +151,15 @@ export const tool: Tool = {
151151 const asking = principalOf(ctx.auth);
152152 // The whole box's numbers ask may over the box; one server's, over that server alone.
153153 if (box === undefined || !(service === "" ? seesGameBox(db, asking, "tool metrics", box.id) : seesBox(db, user, box.id))) return bad("not_found", "no such box here");
154 if (service === "") return good(compactMetrics(db, box, (s) => mayOnGame(db, asking, "tool metrics", box.id, s, "viewer")));
155 if (!SERVICE_NAME.test(service) || !mayOnGame(db, asking, "tool metrics", box.id, service, "viewer")) return bad("not_found", "no such service here");
154 if (service === "") return good(compactMetrics(db, box, (s) => mayOnGame(db, asking, box.id, s, "viewer")));
155 if (!SERVICE_NAME.test(service) || !mayOnGame(db, asking, box.id, service, "viewer")) return bad("not_found", "no such service here");
156156 const window = (str(args["window"]) || "1h") as Window;
157157 if (!(window in WINDOWS)) return bad("invalid", `window is one of ${Object.keys(WINDOWS).join(", ")}`);
158158 return good(compactSeries(db, box, service, window));
159159 }
160160 if (box === undefined || !seesBox(db, user, box.id) || !SERVICE_NAME.test(service)) return bad("not_found", "no such box or service here");
161161 if (action === "config") {
162 if (!mayOnGame(db, principalOf(ctx.auth), "tool config", box.id, service, "viewer")) return bad("forbidden", `you may not see ${service} on ${box.name}`);
162 if (!mayOnGame(db, principalOf(ctx.auth), box.id, service, "viewer")) return bad("forbidden", `you may not see ${service} on ${box.name}`);
163163 const got = await serviceSettings(ctx.app, box, service);
164164 if (!got.ok) return bad("refused", got.error);
165165 return good({
@@ -176,7 +176,7 @@ export const tool: Tool = {
176176 })),
177177 });
178178 }
179 if (!mayOnGame(db, principalOf(ctx.auth), `tool ${action}`, box.id, service, "operator")) return bad("forbidden", `you are not an operator of ${service} on ${box.name}`);
179 if (!mayOnGame(db, principalOf(ctx.auth), box.id, service, "operator")) return bad("forbidden", `you are not an operator of ${service} on ${box.name}`);
180180 if (action === "console") {
181181 const text = str(args["text"]).trim();
182182 if (text === "") return bad("invalid", "say the command as text");
src/http/page_may.ts +10 −1
@@ -17,7 +17,7 @@
1717 */
1818
1919 import { holds, type Action } from "../core/may.ts";
20 import { principalOfUser } from "../core/principal.ts";
20 import { principalOf, principalOfUser, type Principal } from "../core/principal.ts";
2121 import type { User } from "../core/users.ts";
2222 import type { Ctx } from "./context.ts";
2323
@@ -34,3 +34,12 @@ export const adminPage = (ctx: Ctx): ctx is SignedIn => asks(ctx, "forge.adminis
3434
3535 /** The forge's admin, and a person rather than an agent: the admin pages that are a person's. */
3636 export const personAdminPage = (ctx: Ctx): ctx is SignedIn => adminPage(ctx) && ctx.auth.user.kind !== "agent";
37
38 /**
39 * Who a door that holds a role on something asks `may` about (the game boxes, #18427): opening
40 * it is a read and asks of the account behind the request, whatever role the read needs, so an
41 * `ro` token opens what its account opens; a change asks with the credential sent, which an `ro`
42 * token is refused.
43 */
44 export const askingFor = (ctx: Ctx): Principal =>
45 ctx.request.method === "GET" || ctx.request.method === "HEAD" ? principalOfUser(ctx.auth.user) : principalOf(ctx.auth);
src/tui/screens/game_config.ts +1 −1
@@ -32,7 +32,7 @@ export function gameConfigScreen(f: Session, ctx: Ctx, box: GameBox, service: st
3232 let pending: ConfigPlan | undefined;
3333 // The text box open on a file, and the path it is for.
3434 let editing: { path: string; text: Textarea } | undefined;
35 const operator = () => mayOnGame(f.app.db, principalOfUser(f.user), "plugin config", box.id, service, "operator");
35 const operator = () => mayOnGame(f.app.db, principalOfUser(f.user), box.id, service, "operator");
3636 const say = (words: string): void => {
3737 note = words;
3838 ctx.invalidate();
src/tui/screens/game_map.ts +1 −1
@@ -33,7 +33,7 @@ export function gameMapScreen(f: Session, ctx: Ctx, box: GameBox, service: strin
3333 },
3434 key(e) {
3535 if (e.ctrl || e.alt || e.key !== "p") return false;
36 if (f.user === undefined || !mayOnGame(f.app.db, principalOfUser(f.user), "map", box.id, service, "viewer")) note = `You may not see ${service}.`;
36 if (f.user === undefined || !mayOnGame(f.app.db, principalOfUser(f.user), box.id, service, "viewer")) note = `You may not see ${service}.`;
3737 else {
3838 note = "Asking the box…";
3939 const front = mapPath("");
src/tui/screens/game_mods.ts +1 −1
@@ -32,7 +32,7 @@ export function gameModsScreen(f: Session, ctx: Ctx, box: GameBox, service: stri
3232 // The pack row, and a link just made (shown here once, then gone with the next key).
3333 let players: PackRow | undefined;
3434 let link = "";
35 const operator = () => mayOnGame(f.app.db, principalOfUser(f.user), "mods", box.id, service, "operator");
35 const operator = () => mayOnGame(f.app.db, principalOfUser(f.user), box.id, service, "operator");
3636 const say = (words: string): void => {
3737 note = words;
3838 ctx.invalidate();
src/tui/screens/game_spark.ts +2 −2
@@ -47,11 +47,11 @@ export function gameSparkScreen(f: Session, ctx: Ctx, box: GameBox): Screen {
4747 } else if (e.key === "k" || e.key === "up") {
4848 at = Math.max(0, at - 1);
4949 } else if (e.key === "enter") {
50 if (stored[at] === undefined || !mayOnGame(f.app.db, principalOfUser(f.user), "spark", box.id, "", "viewer")) return false;
50 if (stored[at] === undefined || !mayOnGame(f.app.db, principalOfUser(f.user), box.id, "", "viewer")) return false;
5151 ctx.push(gameSparkViewScreen(f, ctx, box, stored[at]!.key));
5252 return true;
5353 } else if (e.key === "u") {
54 if (!mayOnGame(f.app.db, principalOfUser(f.user), "spark_link", box.id, "", "owner")) return false;
54 if (!mayOnGame(f.app.db, principalOfUser(f.user), box.id, "", "owner")) return false;
5555 address = sparkBase(publicOrigin(f.app.config.url, `http://localhost:${String(f.app.config.port)}`), newSparkSecret(f.app.db, box.id));
5656 } else {
5757 return false;
src/tui/screens/games.ts +12 −12
@@ -220,7 +220,7 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen {
220220 return true;
221221 }
222222 if (at !== undefined && !e.ctrl && !e.alt && e.key === "!") {
223 if (!mayOnGame(f.app.db, principalOfUser(f.user), "shell", at.box.id, "", "owner")) note = `Only the owner of ${at.box.name} runs a shell on it.`;
223 if (!mayOnGame(f.app.db, principalOfUser(f.user), at.box.id, "", "owner")) note = `Only the owner of ${at.box.name} runs a shell on it.`;
224224 else ctx.push(gameShellScreen(f, ctx, at.box));
225225 ctx.invalidate();
226226 return true;
@@ -246,8 +246,8 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen {
246246 return true;
247247 }
248248 if (!e.ctrl && !e.alt && e.key === "n") {
249 const box = at?.box ?? listBoxes(f.app.db).find((b) => mayOnGame(f.app.db, principalOfUser(f.user), "create_server", b.id, "", "operator"));
250 if (box === undefined || !mayOnGame(f.app.db, principalOfUser(f.user), "create_server", box.id, "", "operator")) note = "You run no game box to add a server to.";
249 const box = at?.box ?? listBoxes(f.app.db).find((b) => mayOnGame(f.app.db, principalOfUser(f.user), b.id, "", "operator"));
250 if (box === undefined || !mayOnGame(f.app.db, principalOfUser(f.user), box.id, "", "operator")) note = "You run no game box to add a server to.";
251251 else
252252 ctx.push(
253253 promptScreen(ctx, screen, {
@@ -264,13 +264,13 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen {
264264 // The selected server out of the network: the plan shown, yes commits; its world is kept.
265265 if (at !== undefined && !e.ctrl && !e.alt && e.key === "d") {
266266 const { box, service } = at;
267 if (!mayOnGame(f.app.db, principalOfUser(f.user), "remove_server", box.id, "", "operator")) note = `You are not an operator of ${box.name}.`;
267 if (!mayOnGame(f.app.db, principalOfUser(f.user), box.id, "", "operator")) note = `You are not an operator of ${box.name}.`;
268268 else void ctx.track(planRemoveServer(f.app, box, service.name).then((planned) => showPlan(box, `Remove ${service.name} from ${box.name}`, "Remove it", planned)));
269269 ctx.invalidate();
270270 return true;
271271 }
272272 if (at !== undefined && !e.ctrl && !e.alt && e.key === "z") {
273 if (!mayOnGame(f.app.db, principalOfUser(f.user), "sleep", at.box.id, at.service.name, "operator")) note = `You are not an operator of ${at.service.name}.`;
273 if (!mayOnGame(f.app.db, principalOfUser(f.user), at.box.id, at.service.name, "operator")) note = `You are not an operator of ${at.service.name}.`;
274274 else {
275275 const { box, service } = at;
276276 note = `${box.name}/${service.name}: saving…`;
@@ -287,7 +287,7 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen {
287287 }
288288 const kind: Power | undefined = e.key === "s" ? "start" : e.key === "x" ? "stop" : e.key === "r" ? "restart" : undefined;
289289 if (kind === undefined || at === undefined || e.ctrl || e.alt) return false;
290 if (!mayOnGame(f.app.db, principalOfUser(f.user), kind, at.box.id, at.service.name, "operator")) note = `You are not an operator of ${at.service.name}.`;
290 if (!mayOnGame(f.app.db, principalOfUser(f.user), at.box.id, at.service.name, "operator")) note = `You are not an operator of ${at.service.name}.`;
291291 else {
292292 const { box, service } = at;
293293 void askPower(f.app, f.user, box, service.name, kind)
@@ -345,7 +345,7 @@ export function gameServiceScreen(f: Session, ctx: Ctx, box: GameBox, service: s
345345 let note = "";
346346 let compose: string | undefined;
347347 let loaded = false;
348 const operator = () => mayOnGame(f.app.db, principalOfUser(f.user), "server", box.id, service, "operator");
348 const operator = () => mayOnGame(f.app.db, principalOfUser(f.user), box.id, service, "operator");
349349 const load = async (): Promise<void> => {
350350 compose = (await readCompose(f.app, box))?.text;
351351 loaded = true;
@@ -527,7 +527,7 @@ export function gameSettingsScreen(f: Session, ctx: Ctx, box: GameBox, service:
527527 let values: SettingValue[] = [];
528528 let wanted: Record<string, string> = {};
529529 let plan: (SettingsPlan & { sha: string }) | undefined;
530 const operator = () => mayOnGame(f.app.db, principalOfUser(f.user), "settings", box.id, service, "operator");
530 const operator = () => mayOnGame(f.app.db, principalOfUser(f.user), box.id, service, "operator");
531531 const load = async (): Promise<void> => {
532532 compose = await readCompose(f.app, box);
533533 values = compose === undefined ? [] : settingsOf(compose.text, service, serviceEnv(compose.text, service));
@@ -677,12 +677,12 @@ export function gameBoxScreen(f: Session, ctx: Ctx, box: GameBox): Screen {
677677 else if (e.key === "p") {
678678 ctx.push(gameSparkScreen(f, ctx, box));
679679 return true;
680 } else if (e.key === "l" && mayOnGame(f.app.db, principalOfUser(f.user), "lend", box.id, "", "owner") && f.user !== undefined) {
680 } else if (e.key === "l" && mayOnGame(f.app.db, principalOfUser(f.user), box.id, "", "owner") && f.user !== undefined) {
681681 const b = listBoxes(f.app.db).find((x) => x.id === box.id) ?? box;
682682 setLend(f.app.db, b, lentRunnerOf(f.app.db, b.id) === undefined, f.user.id);
683 } else if (e.key === "w" && mayOnGame(f.app.db, principalOfUser(f.user), "door", box.id, "", "owner") && f.user !== undefined) {
683 } else if (e.key === "w" && mayOnGame(f.app.db, principalOfUser(f.user), box.id, "", "owner") && f.user !== undefined) {
684684 lockDoor(f.app.db, box.id, f.user.id);
685 } else if (e.key === "a" && mayOnGame(f.app.db, principalOfUser(f.user), "lock on leave", box.id, "", "owner")) {
685 } else if (e.key === "a" && mayOnGame(f.app.db, principalOfUser(f.user), box.id, "", "owner")) {
686686 setLocksOnLeave(f.app.db, box.id, !locksOnLeave(f.app.db, box.id));
687687 } else return false;
688688 ctx.invalidate();
@@ -695,7 +695,7 @@ export function gameBoxScreen(f: Session, ctx: Ctx, box: GameBox): Screen {
695695 export function gameGrantsScreen(f: Session, ctx: Ctx, box: GameBox): Screen {
696696 const state = new ListState();
697697 let note = "";
698 const owner = () => mayOnGame(f.app.db, principalOfUser(f.user), "grants", box.id, "", "owner");
698 const owner = () => mayOnGame(f.app.db, principalOfUser(f.user), box.id, "", "owner");
699699 const screen: Screen = {
700700 title: `${box.name} grants`,
701701 keys: [binding(["j", "k"], "move cursor"), binding(["a"], "grant")],
src/web/pages/games.ts +5 −6
@@ -2,12 +2,12 @@
22 * `/games` (forge games): the game boxes a person may see, each box's network (one row a service,
33 * with Start, Stop, Restart and Remove), a new server from a few words, and a service's console (a
44 * live log over SSE and an RCON input), environment editor and its Modrinth projects (search, a
5 * preview, then one commit; `games/mods.ts`). Every route asks `roleOn` first: viewer reads,
5 * preview, then one commit; `games/mods.ts`). Every route asks `mayOnGame` first: viewer reads,
66 * operator acts, owner (and every forge admin) also grants. Env and plugin changes are commits to
77 * the box's compose repository, so a merge redeploys and the crew can propose them too.
88 */
99
10 import { signedInPage } from "../../http/page_may.ts";
10 import { askingFor, signedInPage } from "../../http/page_may.ts";
1111 import { cancelHeld, heldOf, holdUntilQuiet, QUIET_MINUTES } from "../../games/when_quiet.ts";
1212 import { addPluginUrl, commitServiceEnv, composeServices, readCompose, serviceEnv } from "../../games/compose.ts";
1313 import { verdictsOf } from "../../games/set_verdict.ts";
@@ -176,7 +176,7 @@ export function boxOf(ctx: Ctx, need: Role, service = ""): { box: GameBox; role:
176176 if (!(boxPage ? seesGameBox(ctx.app.db, principalOf(ctx.auth), "page", box.id) : seesBox(ctx.app.db, ctx.auth.user, box.id))) return notFoundPage(ctx);
177177 const role = roleOn(ctx.app.db, ctx.auth.user, box.id, service);
178178 if (boxPage) return { box, role: role ?? "viewer" };
179 if (!mayOnGame(ctx.app.db, principalOf(ctx.auth), "page", box.id, service, need)) return notFoundPage(ctx);
179 if (!mayOnGame(ctx.app.db, askingFor(ctx), box.id, service, need)) return notFoundPage(ctx);
180180 return { box, role: role! };
181181 }
182182
@@ -542,9 +542,8 @@ function grantsBlock(ctx: Ctx, box: GameBox): Html {
542542 export function containerConsole(ctx: Ctx, boxName: string, service: string): { readonly power: Html | null; readonly logs: Html } | undefined {
543543 const box = boxNamed(ctx.app.db, boxName);
544544 if (box === undefined || !SERVICE_NAME.test(service)) return undefined;
545 const role = roleOn(ctx.app.db, ctx.auth.user, box.id, service);
546 if (!mayDo(role, "viewer")) return undefined;
547 const act = mayDo(role, "operator");
545 if (!mayOnGame(ctx.app.db, askingFor(ctx), box.id, service, "viewer")) return undefined;

Medium. containerConsole is a GET render, so askingFor() asks as the account and act is true for an owner's ro token. The page then draws Start, Stop, Restart and the RCON input. Every one of those posts is a change, which asks with the credential and refuses the ro token with a not-found page. So an ro token is shown controls that cannot work. Decide act with principalOf(ctx.auth) so the controls match what a POST allows, and add a test for an ro token on this console. The page's other mayDo(role, "operator") render checks (games.ts lines 294, 325, 569) have the same gap and should be fixed the same way.

546 const act = mayOnGame(ctx.app.db, askingFor(ctx), box.id, service, "operator");
548547 const base = `/games/${box.name}/${service}`;
549548 const blocks = serviceBlocks(ctx.app, box.id, service, act);
550549 const seen = servicesOf(ctx.app.db, box.id).find((s) => s.name === service);
test/door-inline-checks.test.ts +14
@@ -25,6 +25,11 @@ test("the scan reads the repository route modules and every MCP file", () => {
2525 expect(paths).toContain("src/http/infra_routes.ts");
2626 expect(paths).toContain("src/http/api/cloud.ts");
2727 expect(paths).toContain("src/web/pages/account.ts");
28 // The game doors (#18427): the door's API, the map, and the game pages.
29 expect(paths).toContain("src/http/api/game_door.ts");
30 expect(paths).toContain("src/http/games_map.ts");
31 expect(paths).toContain("src/web/pages/games.ts");
32 expect(paths).toContain("src/web/pages/games_door.ts");
2833 expect(paths).toContain("src/tui/screens/sentinel.ts");
2934 // A forge-wide module with no repository route is the other door's.
3035 expect(paths.every((p) => !p.startsWith("src/http/api/") || !p.endsWith("/admin.ts"))).toBe(true);
@@ -65,6 +70,15 @@ test("a fixture file with new inline checks fails, each named by file, line and
6570 for (const s of said) expect(s).toEndWith(DOOR_FIX.newLine(found[said.indexOf(s)]!.check));
6671 });
6772
73 test("an inline grant refusal in a game door is found and pointed at mayOnGame", () => {
74 const text = 'if (!mayDo(roleOn(db, user, box.id, service), "operator")) return notFoundPage(ctx);';
75 const found = doorChecks({ path: "src/web/pages/games_fixture.ts", text });
76 expect(found.map((f) => f.check)).toEqual(["grant"]);
77 expect(doorFindings(found, [])[0]).toContain("mayOnGame");
78 // Choosing what to show by the role is a read, not a door.
79 expect(doorChecks({ path: "src/web/pages/games_fixture.ts", text: 'const act = mayDo(role, "operator");' })).toEqual([]);
80 });
81
6882 test("a listed line allows itself once; a copy of it is new, and a gone line is named to delete", () => {
6983 const text = "if (!user.isAdmin) return no();";
7084 const listed = [{ file: "f.ts", text, why: "a reason" }];
test/terminal-games-may.test.ts +41 −16
@@ -1,9 +1,9 @@
11 /**
22 * Access 3, the terminal and the games. The palette offers an entry that names an action by `may`
3 * alone, as the terminal's account (#18426), and counts nothing; a game grant check still decides
4 * what a person may do on a box, `may` is asked beside it, and a parting is counted in
5 * `access_disagreement` under `game <door>`. One deliberate disagreement writes a row, and
6 * agreement writes none. `game_grant`'s viewer, operator and owner are `may`'s `game.view`,
3 * alone, as the terminal's account (#18426), and counts nothing. A game grant check is `may`'s
4 * answer alone (`mayOnGame`, #18427); seeing a box at all is still asked in shadow, a parting
5 * counted in `access_disagreement` under `game <door>`. One deliberate disagreement writes a row,
6 * and agreement writes none. `game_grant`'s viewer, operator and owner are `may`'s `game.view`,
77 * `game.operate` and `game.own`, asked over `GET /api/v1/may` too.
88 */
99 import { afterAll, beforeAll, beforeEach, expect, test } from "bun:test";
@@ -176,21 +176,46 @@ test("terminal: every entry that names an action is offered just where may holds
176176 test("game: the grant and may agree for an operator, a viewer of one server, the admin and the helper", () => {
177177 const db = app.db;
178178 const gina = principalOfUser(users.gina), nina = principalOfUser(users.nina), boss = principalOfUser(users.boss);
179 expect(mayOnGame(db, gina, "console", box, "survival", "operator")).toBe(true);
180 expect(mayOnGame(db, gina, "shell", box, "", "owner")).toBe(false);
181 expect(mayOnGame(db, nina, "page", box, "survival", "viewer")).toBe(true);
182 expect(mayOnGame(db, nina, "page", box, "creative", "viewer")).toBe(false);
183 expect(mayOnGame(db, nina, "console", box, "survival", "operator")).toBe(false);
184 expect(mayOnGame(db, boss, "shell", box, "", "owner")).toBe(true);
185 expect(mayOnGame(db, boss, "console", box, WAKER, "viewer")).toBe(false);
186 expect(mayOnGame(db, principalOfUser(undefined), "page", box, "", "viewer")).toBe(false);
187 expect(mayOnGame(db, roToken(users.gina), "page", box, "survival", "viewer")).toBe(true);
179 expect(mayOnGame(db, gina, box, "survival", "operator")).toBe(true);
180 expect(mayOnGame(db, gina, box, "", "owner")).toBe(false);
181 expect(mayOnGame(db, nina, box, "survival", "viewer")).toBe(true);
182 expect(mayOnGame(db, nina, box, "creative", "viewer")).toBe(false);
183 expect(mayOnGame(db, nina, box, "survival", "operator")).toBe(false);
184 expect(mayOnGame(db, boss, box, "", "owner")).toBe(true);
185 expect(mayOnGame(db, boss, box, WAKER, "viewer")).toBe(false);
186 expect(mayOnGame(db, principalOfUser(undefined), box, "", "viewer")).toBe(false);
187 expect(mayOnGame(db, roToken(users.gina), box, "survival", "viewer")).toBe(true);
188188 expect(rows()).toEqual([]);
189189 });
190190
191 test("game: an ro token's operator grant parts from may, counted under game <door>, and the grant still decides", () => {
192 expect(mayOnGame(app.db, roToken(users.gina), "tool console", box, "survival", "operator")).toBe(true);
193 expect(rows()).toEqual([{ door: "game tool console", action: "game.operate", principal: "ro_token", may: 0 }]);
191 test("game: may answers alone, so an ro token reads by its grant and operates or owns nothing", () => {
192 const db = app.db;
193 expect(mayOnGame(db, roToken(users.gina), box, "survival", "viewer")).toBe(true);
194 expect(mayOnGame(db, roToken(users.gina), box, "survival", "operator")).toBe(false);
195 expect(mayOnGame(db, roToken(users.boss), box, "", "owner")).toBe(false);
196 expect(mayOnGame(db, roToken(users.boss), box, "", "viewer")).toBe(true);
197 // Nothing is asked beside it any more, so nothing is counted.
198 expect(rows()).toEqual([]);
199 });
200
201 test("game door API: a read asks of the account, so an owner's ro token reads the door; a change needs rw", async () => {
202 const owned = createGameBox(app.db, { name: "ginas" }).id;
203 grant(app.db, owned, users.gina.id, "owner");
204 const ro = createToken(app.db, users.gina.id, "door ro", "ro").token;
205 const rw = createToken(app.db, users.gina.id, "door rw", "rw").token;
206 const ninas = createToken(app.db, users.nina.id, "door nina", "rw").token;
207 const call = (token: string, method = "GET") =>
208 fetch(`${servers.url}/api/v1/games/ginas/door`, {
209 method,
210 headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
211 ...(method === "POST" ? { body: JSON.stringify({ op: "lock" }) } : {}),
212 });
213 expect((await call(ro)).status).toBe(200);
214 expect((await call(ro, "POST")).status).toBe(403);
215 expect((await call(rw, "POST")).status).toBe(200);
216 // Not the owner: the box is not there for them.
217 expect((await call(ninas)).status).toBe(404);
218 expect(rows()).toEqual([]);
194219 });
195220
196221 test("game: seeing a box asks game.view over it; a grant on one server only is the parting counted", () => {

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs