Public: anyone can read this; the crew can change it.
Access 4a: one read-only view over every grant table, and a gate test that none is missing #18118
Merged 10h 55m ago, live in production
mason/forge:mason/18104-access-4a-one-read-only-view-over-every → main 0 commits fork 1 thread · 1 unresolved
4 files
changed +223
−2
scripts/unreached-allow.json +2 −1
scripts/unused-exports-allow.json +2 −1
| @@ -185,5 +185,6 @@ | ||
| 185 | 185 | "src/spawn/machines.ts forgetMachines", |
| 186 | 186 | "src/spawn/machines.ts registerMachine", |
| 187 | 187 | "src/spawn/routes.ts forgetRoutes", |
| 188 | "src/maintain/lockfiles.ts lockfileDependencies" | |
| 188 | "src/maintain/lockfiles.ts lockfileDependencies", | |
| 189 | "src/core/grants_view.ts everyGrant" | |
| 189 | 190 | ] |
src/core/grants_view.ts added +117
| @@ -0,0 +1,117 @@ | ||
| 1 | /** | |
| 2 | * Every grant in one shape (#18104, Access 4a): who holds it (principal), what it lets them do | |
| 3 | * (action or role), on what (resource), who granted it, and when. Read-only, and no table of its | |
| 4 | * own: each grant table keeps its own columns and this maps them, so a grantee typed its own way | |
| 5 | * (a device's integer id, a repository scope, a hosted forge's body) is turned into text here and | |
| 6 | * never migrated. | |
| 7 | * | |
| 8 | * A table that does not record who granted a row, or when, says so in that field | |
| 9 | * (`NOT_RECORDED`); `null` means the table records it and this row has none (the granter's account | |
| 10 | * was deleted, or the column was empty). A table that does not exist yet (`client_cloud_member` is | |
| 11 | * made on first use) lists nothing. | |
| 12 | * | |
| 13 | * `GRANT_TABLES` is the whole list; `test/grants-view.test.ts` fails when a table whose name ends | |
| 14 | * in `_grant` or `_member` is declared under `src/` and is not in it. Changes no access. Its | |
| 15 | * readers are the "Who may do what" page (#18105) and its MCP tool and terminal screen (#18106), | |
| 16 | * not yet built; until then nothing under `src/` imports this module. | |
| 17 | */ | |
| 18 | ||
| 19 | import type { Database } from "bun:sqlite"; | |
| 20 | ||
| 21 | /** The field's value when the table keeps no column for it. */ | |
| 22 | const NOT_RECORDED = { notRecorded: true } as const; | |
| 23 | type NotRecorded = typeof NOT_RECORDED; | |
| 24 | ||
| 25 | export interface Grant { | |
| 26 | table: string; | |
| 27 | principal: { kind: string; id: string }; | |
| 28 | action: string; | |
| 29 | resource: { kind: string; id: string }; | |
| 30 | grantedBy: string | null | NotRecorded; | |
| 31 | at: string | null | NotRecorded; | |
| 32 | } | |
| 33 | ||
| 34 | /** | |
| 35 | * One table's mapping. Each expression is SQL over the table's own columns; `by` and `at` are | |
| 36 | * `undefined` when the table keeps no such column; `account` is the column naming an account | |
| 37 | * (a person or an agent) as the principal, `undefined` when the principal is never an account. | |
| 38 | */ | |
| 39 | interface Mapping { | |
| 40 | table: string; | |
| 41 | principalKind: string; | |
| 42 | principalId: string; | |
| 43 | action: string; | |
| 44 | resourceKind: string; | |
| 45 | resourceId: string; | |
| 46 | by?: string; | |
| 47 | at?: string; | |
| 48 | account?: string; | |
| 49 | } | |
| 50 | ||
| 51 | const text = (word: string): string => `'${word}'`; | |
| 52 | ||
| 53 | const MAPPINGS: readonly Mapping[] = [ | |
| 54 | { table: "member", principalKind: text("user"), principalId: "user_id", action: text("member"), resourceKind: text("forge"), resourceId: "''", by: "promoted_by", at: "promoted_at", account: "user_id" }, | |
| 55 | { table: "team_member", principalKind: text("user"), principalId: "user_id", action: "role", resourceKind: text("team"), resourceId: "team_id", account: "user_id" }, | |
| 56 | { table: "app_member", principalKind: text("user"), principalId: "user_id", action: text("enter"), resourceKind: text("app"), resourceId: "app_id", by: "added_by", at: "added_at", account: "user_id" }, | |
| 57 | { table: "game_grant", principalKind: text("user"), principalId: "user_id", action: "role", resourceKind: text("game box"), resourceId: "box_id || CASE WHEN service = '' THEN '' ELSE '/' || service END", account: "user_id" }, | |
| 58 | { table: "model_grant", principalKind: text("user"), principalId: "user_id", action: "'use models, $' || printf('%g', usd_per_month) || ' a month'", resourceKind: text("forge"), resourceId: "''", by: "granted_by", at: "granted_at", account: "user_id" }, | |
| 59 | { table: "subscription_grant", principalKind: "CASE WHEN agent_id = '' THEN 'scope' ELSE 'agent' END", principalId: "CASE WHEN agent_id = '' THEN scope_id ELSE agent_id END", action: text("spend"), resourceKind: text("subscription"), resourceId: "subscription_id", account: "NULLIF(agent_id, '')" }, | |
| 60 | { table: "secret_grant", principalKind: text("repo"), principalId: "repo_id", action: text("read secret"), resourceKind: "scope || ' secret'", resourceId: "CASE WHEN scope_id = '' THEN name ELSE scope_id || '/' || name END", by: "granted_by", at: "granted_at" }, | |
| 61 | { table: "egress_grant", principalKind: text("repo"), principalId: "repo_id", action: text("reach"), resourceKind: text("host"), resourceId: "destination", by: "granted_by", at: "granted_at" }, | |
| 62 | { table: "device_grant", principalKind: text("device"), principalId: "CAST(device_id AS TEXT)", action: "'post, ' || daily_cap || ' a day'", resourceKind: text("app"), resourceId: "app_id", by: "granted_by", at: "granted_at" }, | |
| 63 | { table: "child_reach_grant", principalKind: text("hosted forge"), principalId: "CASE WHEN child = '' THEN body_id ELSE child END", action: text("reach"), resourceKind: text("host"), resourceId: "host || ':' || port", by: "granted_by", at: "granted_at" }, | |
| 64 | { table: "origin_read_grant", principalKind: "kind", principalId: "grantee_id", action: text("read"), resourceKind: text("repo"), resourceId: "repo_id", by: "granted_by", at: "created_at", account: "CASE WHEN kind = 'crew' THEN grantee_id END" }, | |
| 65 | { table: "pull_run_grant", principalKind: text("pull"), principalId: "issue_id", action: text("run"), resourceKind: text("commit"), resourceId: "commit_oid", by: "user_id", at: "granted_at" }, | |
| 66 | { table: "telemetry_grant", principalKind: text("user"), principalId: "user_id", action: text("read telemetry"), resourceKind: text("forge"), resourceId: "''", by: "granted_by", at: "granted_at", account: "user_id" }, | |
| 67 | { table: "client_cloud_member", principalKind: text("user"), principalId: "user_id", action: text("look"), resourceKind: text("client cloud"), resourceId: "cloud_id", by: "granted_by", at: "granted_at", account: "user_id" }, | |
| 68 | { table: "crew_team_member", principalKind: text("user"), principalId: "user_id", action: text("member"), resourceKind: text("crew team"), resourceId: "team_slug", account: "user_id" }, | |
| 69 | { table: "share", principalKind: text("user"), principalId: "recipient_id", action: "'receive, ' || state", resourceKind: "kind", resourceId: "item_id", by: "sender_id", at: "created_at", account: "recipient_id" }, | |
| 70 | ]; | |
| 71 | ||
| 72 | /** Every table this view reads, in the order it lists them. */ | |
| 73 | export const GRANT_TABLES: readonly string[] = MAPPINGS.map((m) => m.table); | |
| 74 | ||
| 75 | function select(m: Mapping, forAccount: boolean): string { | |
| 76 | const where = forAccount ? ` WHERE ${m.account ?? "NULL"} = ?1` : ""; | |
| 77 | return `SELECT '${m.table}' AS tbl, ${m.principalKind} AS p_kind, ${m.principalId} AS p_id, ${m.action} AS action, | |
| 78 | ${m.resourceKind} AS r_kind, ${m.resourceId} AS r_id, | |
| 79 | ${m.by ?? "NULL"} AS g_by, ${m.by === undefined ? 0 : 1} AS g_by_kept, | |
| 80 | ${m.at ?? "NULL"} AS g_at, ${m.at === undefined ? 0 : 1} AS g_at_kept | |
| 81 | FROM ${m.table}${where}`; | |
| 82 | } | |
| 83 | ||
| 84 | interface Row { | |
| 85 | tbl: string; | |
| 86 | p_kind: string; | |
| 87 | p_id: string; | |
| 88 | action: string; | |
| 89 | r_kind: string; | |
| 90 | r_id: string; | |
| 91 | g_by: string | null; | |
| 92 | g_by_kept: number; | |
| 93 | g_at: string | null; | |
| 94 | g_at_kept: number; | |
| 95 | } | |
| 96 | ||
| 97 | /** | |
| 98 | * Every grant on this forge, or, given `account`, only those whose principal is that account (a | |
| 99 | * person, or an agent named by its user id). An account with no grants lists none. | |
| 100 | */ | |
| 101 | export function everyGrant(db: Database, account?: string): Grant[] { | |
| 102 | const present = new Set( | |
| 103 | db.query<{ name: string }, []>("SELECT name FROM sqlite_master WHERE type = 'table'").all().map((r) => r.name), | |
| 104 | ); | |
| 105 | const mappings = MAPPINGS.filter((m) => present.has(m.table) && (account === undefined || m.account !== undefined)); | |
| 106 | if (mappings.length === 0) return []; | |
| 107 | const sql = mappings.map((m) => select(m, account !== undefined)).join("\nUNION ALL\n"); | |
| 108 | const rows = account === undefined ? db.query<Row, []>(sql).all() : db.query<Row, [string]>(sql).all(account); | |
| 109 | return rows.map((r) => ({ | |
| 110 | table: r.tbl, | |
| 111 | principal: { kind: r.p_kind, id: r.p_id }, | |
| 112 | action: r.action, | |
| 113 | resource: { kind: r.r_kind, id: r.r_id }, | |
| 114 | grantedBy: r.g_by_kept ? r.g_by : NOT_RECORDED, | |
| 115 | at: r.g_at_kept ? r.g_at : NOT_RECORDED, | |
| 116 | })); | |
| 117 | } |
test/grants-view.test.ts added +102
| @@ -0,0 +1,102 @@ | ||
| 1 | /** | |
| 2 | * Every grant in one shape (#18104): one row granted in each grant table is listed by the view, in | |
| 3 | * the view's shape, with "not recorded" where the table keeps no granter or time; an account with | |
| 4 | * no grants lists none; and a table named `*_grant` or `*_member` declared under src/ that the view | |
| 5 | * does not read fails here, so the view cannot fall behind the schema. | |
| 6 | */ | |
| 7 | ||
| 8 | import { describe, expect, test } from "bun:test"; | |
| 9 | import type { Database } from "bun:sqlite"; | |
| 10 | import { readFileSync } from "node:fs"; | |
| 11 | import { join } from "node:path"; | |
| 12 | import { addMember } from "../src/core/client_cloud.ts"; | |
| 13 | import { GRANT_TABLES, everyGrant } from "../src/core/grants_view.ts"; | |
| 14 | import { migrations } from "../src/core/schema.ts"; | |
| 15 | import { openDb } from "../src/kernel/db.ts"; | |
| 16 | ||
| 17 | const AT = "2026-12-04T10:00:00.000Z"; | |
| 18 | const notRecorded = { notRecorded: true }; | |
| 19 | ||
| 20 | /** | |
| 21 | * A migrated forge with one grant in each table. Foreign keys are off: this tests the read, not the | |
| 22 | * rows it points at. | |
| 23 | */ | |
| 24 | function seeded(): Database { | |
| 25 | const db = openDb(":memory:", migrations); | |
| 26 | db.run("PRAGMA foreign_keys = OFF"); | |
| 27 | const rows: [string, unknown[]][] = [ | |
| 28 | ["INSERT INTO member (user_id, promoted_by, promoted_at) VALUES ('ann', 'root', ?1)", [AT]], | |
| 29 | ["INSERT INTO team_member (team_id, user_id, role) VALUES ('t1', 'ann', 'builder')", []], | |
| 30 | ["INSERT INTO app_member (app_id, user_id, added_by, added_at) VALUES ('app1', 'ann', 'root', ?1)", [AT]], | |
| 31 | ["INSERT INTO game_grant (box_id, user_id, role, service) VALUES ('box1', 'ann', 'player', 'web')", []], | |
| 32 | ["INSERT INTO model_grant (user_id, usd_per_month, granted_by, granted_at) VALUES ('ann', 5, NULL, ?1)", [AT]], | |
| 33 | ["INSERT INTO subscription_grant (scope_id, agent_id, subscription_id) VALUES ('repo1', 'mason', 'sub1')", []], | |
| 34 | ["INSERT INTO secret_grant (repo_id, name, scope, scope_id, granted_by, granted_at) VALUES ('repo1', 'KEY', 'org', 'org1', 'root', ?1)", [AT]], | |
| 35 | ["INSERT INTO egress_grant (repo_id, destination, granted_by, granted_at) VALUES ('repo1', 'api.example.com', 'root', ?1)", [AT]], | |
| 36 | ["INSERT INTO device_grant (device_id, app_id, granted_by, granted_at, daily_cap) VALUES (7, 'app1', 'root', ?1, 3)", [AT]], | |
| 37 | ["INSERT INTO child_reach_grant (body_id, child, host, port, granted_by, granted_at) VALUES ('body1', 'kid', 'pypi.org', 443, 'root', ?1)", [AT]], | |
| 38 | ["INSERT INTO origin_read_grant (repo_id, kind, grantee_id, granted_by, created_at) VALUES ('repo1', 'crew', 'mason', 'root', ?1)", [AT]], | |
| 39 | ["INSERT INTO pull_run_grant (issue_id, commit_oid, user_id, granted_at) VALUES ('pull1', 'abc123', 'root', ?1)", [AT]], | |
| 40 | ["INSERT INTO telemetry_grant (user_id, granted_by, granted_at) VALUES ('ann', 'root', ?1)", [AT]], | |
| 41 | ["INSERT INTO crew_team_member (team_slug, user_id) VALUES ('ops', 'mason')", []], | |
| 42 | ["INSERT INTO share (id, item_id, sender_id, recipient_id, repo_id, kind, link, what, state, created_at) VALUES ('s1', 'item1', 'root', 'ann', 'repo1', 'app', '/x', 'an app', 'delivered', ?1)", [AT]], | |
| 43 | ]; | |
| 44 | for (const [sql, args] of rows) db.query(sql).run(...(args as string[])); | |
| 45 | addMember(db, "cloud1", "ann", "root", new Date(AT)); | |
| 46 | return db; | |
| 47 | } | |
| 48 | ||
| 49 | describe("the grants view", () => { | |
| 50 | test("lists one grant from each table, in one shape", () => { | |
| 51 | const grants = everyGrant(seeded()); | |
| 52 | expect(grants.map((g) => g.table).sort()).toEqual([...GRANT_TABLES].sort()); | |
| 53 | const of = (table: string) => grants.find((g) => g.table === table); | |
| 54 | expect(of("member")).toEqual({ | |
| 55 | table: "member", principal: { kind: "user", id: "ann" }, action: "member", | |
| 56 | resource: { kind: "forge", id: "" }, grantedBy: "root", at: AT, | |
| 57 | }); | |
| 58 | expect(of("team_member")).toMatchObject({ action: "builder", resource: { kind: "team", id: "t1" }, grantedBy: notRecorded, at: notRecorded }); | |
| 59 | expect(of("game_grant")).toMatchObject({ action: "player", resource: { kind: "game box", id: "box1/web" }, grantedBy: notRecorded }); | |
| 60 | expect(of("model_grant")).toMatchObject({ action: "use models, $5 a month", grantedBy: null, at: AT }); | |
| 61 | expect(of("subscription_grant")).toMatchObject({ principal: { kind: "agent", id: "mason" }, resource: { kind: "subscription", id: "sub1" } }); | |
| 62 | expect(of("secret_grant")).toMatchObject({ principal: { kind: "repo", id: "repo1" }, resource: { kind: "org secret", id: "org1/KEY" } }); | |
| 63 | expect(of("device_grant")).toMatchObject({ principal: { kind: "device", id: "7" }, action: "post, 3 a day" }); | |
| 64 | expect(of("child_reach_grant")).toMatchObject({ principal: { kind: "hosted forge", id: "kid" }, resource: { kind: "host", id: "pypi.org:443" } }); | |
| 65 | expect(of("origin_read_grant")).toMatchObject({ principal: { kind: "crew", id: "mason" }, at: AT }); | |
| 66 | expect(of("pull_run_grant")).toMatchObject({ principal: { kind: "pull", id: "pull1" }, resource: { kind: "commit", id: "abc123" }, grantedBy: "root" }); | |
| 67 | expect(of("client_cloud_member")).toMatchObject({ resource: { kind: "client cloud", id: "cloud1" }, grantedBy: "root", at: AT }); | |
| 68 | expect(of("share")).toMatchObject({ principal: { kind: "user", id: "ann" }, action: "receive, delivered", grantedBy: "root" }); | |
| 69 | }); | |
| 70 | ||
| 71 | test("by account: an account's own grants, and none for an account with none", () => { | |
| 72 | const db = seeded(); | |
| 73 | expect(everyGrant(db, "ann").map((g) => g.table).sort()).toEqual( | |
| 74 | ["app_member", "client_cloud_member", "game_grant", "member", "model_grant", "share", "team_member", "telemetry_grant"], | |
| 75 | ); | |
| 76 | expect(everyGrant(db, "mason").map((g) => g.table).sort()).toEqual(["crew_team_member", "origin_read_grant", "subscription_grant"]); | |
| 77 | expect(everyGrant(db, "nobody")).toEqual([]); | |
| 78 | }); | |
| 79 | ||
| 80 | test("a table not yet made lists nothing, and a fresh forge lists no grants", () => { | |
| 81 | const db = openDb(":memory:", migrations); | |
| 82 | expect(db.query("SELECT 1 FROM sqlite_master WHERE name = 'client_cloud_member'").get()).toBeNull(); | |
| 83 | expect(everyGrant(db)).toEqual([]); | |
| 84 | }); | |
| 85 | }); | |
| 86 | ||
| 87 | test("gate: every table named *_grant or *_member declared under src/ is in the grants view", () => { | |
| 88 | const src = new URL("../src", import.meta.url).pathname; | |
| 89 | const declared = new Set<string>(); | |
| 90 | for (const file of new Bun.Glob("**/*.ts").scanSync({ cwd: src })) { | |
| 91 | for (const m of readFileSync(join(src, file), "utf8").matchAll(/CREATE TABLE (?:IF NOT EXISTS )?"?(\w+)/gi)) { | |
| 92 | if (/_(grant|member)$/.test(m[1]!)) declared.add(m[1]!); | |
| 93 | } | |
| 94 | } | |
| 95 | const migrated = openDb(":memory:", migrations) | |
| 96 | .query<{ name: string }, []>("SELECT name FROM sqlite_master WHERE type = 'table' AND (name LIKE '%\\_grant' ESCAPE '\\' OR name LIKE '%\\_member' ESCAPE '\\')") | |
| 97 | .all(); | |
| 98 | for (const { name } of migrated) declared.add(name); | |
| 99 | expect(declared.size).toBeGreaterThan(10); | |
| 100 | const missing = [...declared].filter((t) => !GRANT_TABLES.includes(t)).sort(); | |
| 101 | expect(missing, `add these grant tables to src/core/grants_view.ts (MAPPINGS): ${missing.join(", ")}`).toEqual([]); | |
| 102 | }); |
new above
offline
Next: these one-line mapping rows are very long. Wrap them when you next touch the file; no change needed now.