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
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
15 files
changed +152
−93
- docs/features/may.md
- src/design/door_checks.ts
- src/games/game_may.ts
- src/http/api/game_door.ts
- src/http/games_map.ts
- src/http/mcp/tools/games.ts
- src/http/page_may.ts
- src/tui/screens/game_config.ts
- src/tui/screens/game_map.ts
- src/tui/screens/game_mods.ts
- src/tui/screens/game_spark.ts
- src/tui/screens/games.ts
- src/web/pages/games.ts
- test/door-inline-checks.test.ts
- test/terminal-games-may.test.ts
docs/features/may.md +15 −7
| @@ -79,13 +79,21 @@ Nothing anyone is offered changed: the palette offers just what the old gates di | ||
| 79 | 79 | has no action for (a telemetry role, a subscription, an organization, a game grant) or on nothing names no action and |
| 80 | 80 | keeps its own `offered`, with a `// Names no \`asks\`: …` comment beside it saying why. |
| 81 | 81 | |
| 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`). | |
| 89 | 97 | |
| 90 | 98 | Not asked yet: the checks that only filter what a list shows (which servers, snapshots and status lines a box's page, |
| 91 | 99 | 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 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * The inline access check does not come back into a moved door (#18406, Access 3's last step). The |
| 3 | 3 | * 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). | |
| 9 | 11 | * The terminal's palette entries under `src/tui/screens/` offer by `may` too (#18426): there the |
| 10 | 12 | * gate is an entry's own `offered`, and one is found unless the comment right above it says why |
| 11 | 13 | * the entry names no action (`// Names no \`asks\`: …`). |
| @@ -20,7 +22,7 @@ | ||
| 20 | 22 | */ |
| 21 | 23 | import { tsSources, type Source } from "./ratchets.ts"; |
| 22 | 24 | |
| 23 | export type DoorCheck = "isAdmin" | "scopeAllows" | "signedOut" | "entryGate"; | |
| 25 | export type DoorCheck = "isAdmin" | "scopeAllows" | "signedOut" | "grant" | "entryGate"; | |
| 24 | 26 | |
| 25 | 27 | /** One line found: its file, line number, which check, and the line's trimmed text. */ |
| 26 | 28 | export interface DoorFound { |
| @@ -35,6 +37,7 @@ const HELPER: Record<DoorCheck, string> = { | ||
| 35 | 37 | isAdmin: `adminPage(ctx) or personAdminPage(ctx) on a page, holds(db, principalOf(ctx.auth), "forge.administer") on a route`, |
| 36 | 38 | scopeAllows: `holds(db, principalOf(ctx.auth), "forge.write") (mayToolWrite in the MCP)`, |
| 37 | 39 | signedOut: "signedInPage(ctx)", |
| 40 | grant: `mayOnGame(db, askingFor(ctx), box, service, need) (src/games/game_may.ts)`, | |
| 38 | 41 | entryGate: `asks: "forge.administer" or "forge.read" on the entry, with for: "person" for a person's screen`, |
| 39 | 42 | }; |
| 40 | 43 | |
| @@ -50,6 +53,7 @@ export const DOOR_FIX = { | ||
| 50 | 53 | const SHAPES: readonly (readonly [DoorCheck, RegExp])[] = [ |
| 51 | 54 | ["isAdmin", /!\s*[\w?.]*\bisAdmin\b|\bisAdmin\s*!==?\s*true\b|\bisAdmin\s*===?\s*false\b|\bif\s*\(.*\bisAdmin\b/], |
| 52 | 55 | ["scopeAllows", /\bscopeAllows\([^)]*"rw"\s*\)/], |
| 56 | ["grant", /!\s*mayDo\(/], | |
| 53 | 57 | ["signedOut", /!\s*ctx\.auth\.user\b(?!\s*\??\.)|(?:\bif\s*\(|\|\||&&)\s*ctx\.auth\.user\s*===?\s*(?:undefined|null)\b/], |
| 54 | 58 | ]; |
| 55 | 59 | |
| @@ -85,15 +89,20 @@ export function doorChecks(src: Source): DoorFound[] { | ||
| 85 | 89 | }); |
| 86 | 90 | } |
| 87 | 91 | |
| 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"]); | |
| 90 | 98 | |
| 91 | 99 | /** |
| 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. | |
| 94 | 103 | */ |
| 95 | 104 | 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)); | |
| 97 | 106 | const mcp = tsSources(root, "src/http").filter((f) => MOVED.has(f.path)); |
| 98 | 107 | return [...api, ...mcp, ...tsSources(root, "src/http/mcp"), ...tsSources(root, "src/web/pages"), ...tsSources(root, SCREENS.slice(0, -1))]; |
| 99 | 108 | } |
src/games/game_may.ts +22 −27
| @@ -1,21 +1,25 @@ | ||
| 1 | 1 | /** |
| 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. | |
| 6 | 5 | * |
| 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`). | |
| 11 | 15 | */ |
| 12 | 16 | |
| 13 | 17 | import type { Database } from "bun:sqlite"; |
| 14 | 18 | 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"; | |
| 16 | 20 | import { userOf, type Principal } from "../core/principal.ts"; |
| 17 | 21 | 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"; | |
| 19 | 23 | |
| 20 | 24 | const ACTION_OF: Readonly<Record<Role, Action>> = { viewer: "game.view", operator: "game.operate", owner: "game.own" }; |
| 21 | 25 | |
| @@ -24,21 +28,9 @@ export const gameResource = (db: Database, user: User | undefined, boxId: string | ||
| 24 | 28 | game: { granted: grantOn(db, user, boxId, service), helper: isWaker(service) }, |
| 25 | 29 | }); |
| 26 | 30 | |
| 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)); | |
| 42 | 34 | |
| 43 | 35 | /** |
| 44 | 36 | * 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: | ||
| 46 | 38 | * are on one server only sees the box yet holds no `game.view` over all of it: that parting is |
| 47 | 39 | * expected, and counted under `game <door>`. |
| 48 | 40 | */ |
| 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 | ||
| 14 | 14 | import type { Ctx, Handler } from "../context.ts"; |
| 15 | 15 | import { apiError, body, json, notFound, requireScope } from "../respond.ts"; |
| 16 | 16 | import type { Router } from "../router.ts"; |
| 17 | import { principalOf } from "../../core/principal.ts"; | |
| 17 | import { askingFor } from "../page_may.ts"; | |
| 18 | 18 | import { mayOnGame } from "../../games/game_may.ts"; |
| 19 | 19 | |
| 20 | 20 | /** The box when the caller owns it. */ |
| 21 | 21 | function ownedBox(ctx: Ctx) { |
| 22 | 22 | const user = ctx.auth.user; |
| 23 | 23 | 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; | |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** 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> { | ||
| 55 | 55 | const userId = box === undefined ? undefined : passUser(ctx.app.config.dataDir, box.id, service, ctx.params.pass ?? ""); |
| 56 | 56 | const user = userId === undefined ? undefined : getUserById(ctx.app.db, userId); |
| 57 | 57 | // 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"); | |
| 59 | 59 | if (!servicesOf(ctx.app.db, box.id).some((s) => s.name === service)) return refuse(404, "no such server"); |
| 60 | 60 | const ask = mapPath(ctx.params.path ?? ""); |
| 61 | 61 | if (!ask.ok) return refuse(404, ask.error); |
src/http/mcp/tools/games.ts +5 −5
| @@ -122,7 +122,7 @@ export const tool: Tool = { | ||
| 122 | 122 | // A server added or taken out is the box's: its operators', planned first. |
| 123 | 123 | if (action === "create_server" || action === "remove_server") { |
| 124 | 124 | 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}`); | |
| 126 | 126 | let planned; |
| 127 | 127 | if (action === "create_server") { |
| 128 | 128 | const parsed = await wishOn(ctx.app, box, str(args["words"])); |
| @@ -151,15 +151,15 @@ export const tool: Tool = { | ||
| 151 | 151 | const asking = principalOf(ctx.auth); |
| 152 | 152 | // The whole box's numbers ask may over the box; one server's, over that server alone. |
| 153 | 153 | 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"); | |
| 156 | 156 | const window = (str(args["window"]) || "1h") as Window; |
| 157 | 157 | if (!(window in WINDOWS)) return bad("invalid", `window is one of ${Object.keys(WINDOWS).join(", ")}`); |
| 158 | 158 | return good(compactSeries(db, box, service, window)); |
| 159 | 159 | } |
| 160 | 160 | if (box === undefined || !seesBox(db, user, box.id) || !SERVICE_NAME.test(service)) return bad("not_found", "no such box or service here"); |
| 161 | 161 | 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}`); | |
| 163 | 163 | const got = await serviceSettings(ctx.app, box, service); |
| 164 | 164 | if (!got.ok) return bad("refused", got.error); |
| 165 | 165 | return good({ |
| @@ -176,7 +176,7 @@ export const tool: Tool = { | ||
| 176 | 176 | })), |
| 177 | 177 | }); |
| 178 | 178 | } |
| 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}`); | |
| 180 | 180 | if (action === "console") { |
| 181 | 181 | const text = str(args["text"]).trim(); |
| 182 | 182 | if (text === "") return bad("invalid", "say the command as text"); |
src/http/page_may.ts +10 −1
| @@ -17,7 +17,7 @@ | ||
| 17 | 17 | */ |
| 18 | 18 | |
| 19 | 19 | 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"; | |
| 21 | 21 | import type { User } from "../core/users.ts"; |
| 22 | 22 | import type { Ctx } from "./context.ts"; |
| 23 | 23 | |
| @@ -34,3 +34,12 @@ export const adminPage = (ctx: Ctx): ctx is SignedIn => asks(ctx, "forge.adminis | ||
| 34 | 34 | |
| 35 | 35 | /** The forge's admin, and a person rather than an agent: the admin pages that are a person's. */ |
| 36 | 36 | 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 | ||
| 32 | 32 | let pending: ConfigPlan | undefined; |
| 33 | 33 | // The text box open on a file, and the path it is for. |
| 34 | 34 | 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"); | |
| 36 | 36 | const say = (words: string): void => { |
| 37 | 37 | note = words; |
| 38 | 38 | 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 | ||
| 33 | 33 | }, |
| 34 | 34 | key(e) { |
| 35 | 35 | 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}.`; | |
| 37 | 37 | else { |
| 38 | 38 | note = "Asking the box…"; |
| 39 | 39 | 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 | ||
| 32 | 32 | // The pack row, and a link just made (shown here once, then gone with the next key). |
| 33 | 33 | let players: PackRow | undefined; |
| 34 | 34 | 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"); | |
| 36 | 36 | const say = (words: string): void => { |
| 37 | 37 | note = words; |
| 38 | 38 | ctx.invalidate(); |
src/tui/screens/game_spark.ts +2 −2
| @@ -47,11 +47,11 @@ export function gameSparkScreen(f: Session, ctx: Ctx, box: GameBox): Screen { | ||
| 47 | 47 | } else if (e.key === "k" || e.key === "up") { |
| 48 | 48 | at = Math.max(0, at - 1); |
| 49 | 49 | } 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; | |
| 51 | 51 | ctx.push(gameSparkViewScreen(f, ctx, box, stored[at]!.key)); |
| 52 | 52 | return true; |
| 53 | 53 | } 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; | |
| 55 | 55 | address = sparkBase(publicOrigin(f.app.config.url, `http://localhost:${String(f.app.config.port)}`), newSparkSecret(f.app.db, box.id)); |
| 56 | 56 | } else { |
| 57 | 57 | return false; |
src/tui/screens/games.ts +12 −12
| @@ -220,7 +220,7 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen { | ||
| 220 | 220 | return true; |
| 221 | 221 | } |
| 222 | 222 | 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.`; | |
| 224 | 224 | else ctx.push(gameShellScreen(f, ctx, at.box)); |
| 225 | 225 | ctx.invalidate(); |
| 226 | 226 | return true; |
| @@ -246,8 +246,8 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen { | ||
| 246 | 246 | return true; |
| 247 | 247 | } |
| 248 | 248 | 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."; | |
| 251 | 251 | else |
| 252 | 252 | ctx.push( |
| 253 | 253 | promptScreen(ctx, screen, { |
| @@ -264,13 +264,13 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen { | ||
| 264 | 264 | // The selected server out of the network: the plan shown, yes commits; its world is kept. |
| 265 | 265 | if (at !== undefined && !e.ctrl && !e.alt && e.key === "d") { |
| 266 | 266 | 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}.`; | |
| 268 | 268 | else void ctx.track(planRemoveServer(f.app, box, service.name).then((planned) => showPlan(box, `Remove ${service.name} from ${box.name}`, "Remove it", planned))); |
| 269 | 269 | ctx.invalidate(); |
| 270 | 270 | return true; |
| 271 | 271 | } |
| 272 | 272 | 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}.`; | |
| 274 | 274 | else { |
| 275 | 275 | const { box, service } = at; |
| 276 | 276 | note = `${box.name}/${service.name}: saving…`; |
| @@ -287,7 +287,7 @@ export function gamesScreen(f: Session, ctx: Ctx): Screen { | ||
| 287 | 287 | } |
| 288 | 288 | const kind: Power | undefined = e.key === "s" ? "start" : e.key === "x" ? "stop" : e.key === "r" ? "restart" : undefined; |
| 289 | 289 | 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}.`; | |
| 291 | 291 | else { |
| 292 | 292 | const { box, service } = at; |
| 293 | 293 | 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 | ||
| 345 | 345 | let note = ""; |
| 346 | 346 | let compose: string | undefined; |
| 347 | 347 | 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"); | |
| 349 | 349 | const load = async (): Promise<void> => { |
| 350 | 350 | compose = (await readCompose(f.app, box))?.text; |
| 351 | 351 | loaded = true; |
| @@ -527,7 +527,7 @@ export function gameSettingsScreen(f: Session, ctx: Ctx, box: GameBox, service: | ||
| 527 | 527 | let values: SettingValue[] = []; |
| 528 | 528 | let wanted: Record<string, string> = {}; |
| 529 | 529 | 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"); | |
| 531 | 531 | const load = async (): Promise<void> => { |
| 532 | 532 | compose = await readCompose(f.app, box); |
| 533 | 533 | 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 { | ||
| 677 | 677 | else if (e.key === "p") { |
| 678 | 678 | ctx.push(gameSparkScreen(f, ctx, box)); |
| 679 | 679 | 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) { | |
| 681 | 681 | const b = listBoxes(f.app.db).find((x) => x.id === box.id) ?? box; |
| 682 | 682 | 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) { | |
| 684 | 684 | 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")) { | |
| 686 | 686 | setLocksOnLeave(f.app.db, box.id, !locksOnLeave(f.app.db, box.id)); |
| 687 | 687 | } else return false; |
| 688 | 688 | ctx.invalidate(); |
| @@ -695,7 +695,7 @@ export function gameBoxScreen(f: Session, ctx: Ctx, box: GameBox): Screen { | ||
| 695 | 695 | export function gameGrantsScreen(f: Session, ctx: Ctx, box: GameBox): Screen { |
| 696 | 696 | const state = new ListState(); |
| 697 | 697 | 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"); | |
| 699 | 699 | const screen: Screen = { |
| 700 | 700 | title: `${box.name} grants`, |
| 701 | 701 | keys: [binding(["j", "k"], "move cursor"), binding(["a"], "grant")], |
src/web/pages/games.ts +5 −6
| @@ -2,12 +2,12 @@ | ||
| 2 | 2 | * `/games` (forge games): the game boxes a person may see, each box's network (one row a service, |
| 3 | 3 | * with Start, Stop, Restart and Remove), a new server from a few words, and a service's console (a |
| 4 | 4 | * 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, | |
| 6 | 6 | * operator acts, owner (and every forge admin) also grants. Env and plugin changes are commits to |
| 7 | 7 | * the box's compose repository, so a merge redeploys and the crew can propose them too. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { signedInPage } from "../../http/page_may.ts"; | |
| 10 | import { askingFor, signedInPage } from "../../http/page_may.ts"; | |
| 11 | 11 | import { cancelHeld, heldOf, holdUntilQuiet, QUIET_MINUTES } from "../../games/when_quiet.ts"; |
| 12 | 12 | import { addPluginUrl, commitServiceEnv, composeServices, readCompose, serviceEnv } from "../../games/compose.ts"; |
| 13 | 13 | import { verdictsOf } from "../../games/set_verdict.ts"; |
| @@ -176,7 +176,7 @@ export function boxOf(ctx: Ctx, need: Role, service = ""): { box: GameBox; role: | ||
| 176 | 176 | if (!(boxPage ? seesGameBox(ctx.app.db, principalOf(ctx.auth), "page", box.id) : seesBox(ctx.app.db, ctx.auth.user, box.id))) return notFoundPage(ctx); |
| 177 | 177 | const role = roleOn(ctx.app.db, ctx.auth.user, box.id, service); |
| 178 | 178 | 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); | |
| 180 | 180 | return { box, role: role! }; |
| 181 | 181 | } |
| 182 | 182 | |
| @@ -542,9 +542,8 @@ function grantsBlock(ctx: Ctx, box: GameBox): Html { | ||
| 542 | 542 | export function containerConsole(ctx: Ctx, boxName: string, service: string): { readonly power: Html | null; readonly logs: Html } | undefined { |
| 543 | 543 | const box = boxNamed(ctx.app.db, boxName); |
| 544 | 544 | 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; | |
| 546 | const act = mayOnGame(ctx.app.db, askingFor(ctx), box.id, service, "operator"); | |
| 548 | 547 | const base = `/games/${box.name}/${service}`; |
| 549 | 548 | const blocks = serviceBlocks(ctx.app, box.id, service, act); |
| 550 | 549 | 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", () => { | ||
| 25 | 25 | expect(paths).toContain("src/http/infra_routes.ts"); |
| 26 | 26 | expect(paths).toContain("src/http/api/cloud.ts"); |
| 27 | 27 | 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"); | |
| 28 | 33 | expect(paths).toContain("src/tui/screens/sentinel.ts"); |
| 29 | 34 | // A forge-wide module with no repository route is the other door's. |
| 30 | 35 | 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 | ||
| 65 | 70 | for (const s of said) expect(s).toEndWith(DOOR_FIX.newLine(found[said.indexOf(s)]!.check)); |
| 66 | 71 | }); |
| 67 | 72 | |
| 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 | ||
| 68 | 82 | test("a listed line allows itself once; a copy of it is new, and a gone line is named to delete", () => { |
| 69 | 83 | const text = "if (!user.isAdmin) return no();"; |
| 70 | 84 | const listed = [{ file: "f.ts", text, why: "a reason" }]; |
test/terminal-games-may.test.ts +41 −16
| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * 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`, | |
| 7 | 7 | * `game.operate` and `game.own`, asked over `GET /api/v1/may` too. |
| 8 | 8 | */ |
| 9 | 9 | 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 | ||
| 176 | 176 | test("game: the grant and may agree for an operator, a viewer of one server, the admin and the helper", () => { |
| 177 | 177 | const db = app.db; |
| 178 | 178 | 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); | |
| 188 | 188 | expect(rows()).toEqual([]); |
| 189 | 189 | }); |
| 190 | 190 | |
| 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([]); | |
| 194 | 219 | }); |
| 195 | 220 | |
| 196 | 221 | test("game: seeing a box asks game.view over it; a grant on one server only is the parting counted", () => { |
new above
offline
Medium. containerConsole is a GET render, so askingFor() asks as the account and
actis true for an owner'srotoken. 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 therotoken with a not-found page. So anrotoken is shown controls that cannot work. Decideactwith 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 othermayDo(role, "operator")render checks (games.ts lines 294, 325, 569) have the same gap and should be fixed the same way.