Public: anyone can read this; the crew can change it.
The foundry nav door trusts an owner by id from configuration, not by the name 'trevato' #14927
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14911-the-foundry-nav-door-trusts-an-owner-by → main 3 commits fork
2 files
changed +49
−8
src/actions/foundry_door.ts +17 −6
| @@ -6,15 +6,14 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | import { getUserById } from "../core/users.ts"; |
| 8 | 8 | import { getAppBySlug } from "../core/apps.ts"; |
| 9 | import { isOrg, personOwners } from "../core/orgs.ts"; | |
| 9 | 10 | import { canRead, enterableApps, getRepoById } from "../core/repos.ts"; |
| 11 | import type { Database } from "bun:sqlite"; | |
| 10 | 12 | import type { App } from "../app.ts"; |
| 11 | 13 | import type { User } from "../core/users.ts"; |
| 12 | 14 | |
| 13 | 15 | export const FOUNDRY_SLUG = "foundry"; |
| 14 | 16 | |
| 15 | /** The forge's own org: its apps are as trusted as an admin's. */ | |
| 16 | export const FOUNDRY_ORG = "trevato"; | |
| 17 | ||
| 18 | 17 | /** How long a reader's answer is reused: the door moves when an app is installed, not per page. */ |
| 19 | 18 | export const DOOR_TTL_MS = 5000; |
| 20 | 19 | |
| @@ -43,10 +42,22 @@ function resolve(app: App, user: User): string | undefined { | ||
| 43 | 42 | if (record === undefined) return undefined; |
| 44 | 43 | const repo = getRepoById(db, record.repoId); |
| 45 | 44 | if (repo === undefined) return undefined; |
| 46 | // The slug is first come, first served: only an app an admin or the | |
| 47 | // forge's own org owns is Foundry, or the link would be anyone's to plant. | |
| 48 | if (repo.ownerName.toLowerCase() !== FOUNDRY_ORG && getUserById(db, repo.ownerId)?.isAdmin !== true) return undefined; | |
| 45 | // The slug is first come, first served: only an app an admin owns, or an | |
| 46 | // org an admin owns, is Foundry, or the link would be anyone's to plant. | |
| 47 | if (!trustedOwner(db, repo.ownerId)) return undefined; | |
| 49 | 48 | if (!canRead(db, repo, user)) return undefined; |
| 50 | 49 | if (!enterableApps(db, [{ repo, app: record }], user, { named: false }).has(record.id)) return undefined; |
| 51 | 50 | return `/${repo.ownerName}/${repo.name}/app/production/open`; |
| 52 | 51 | } |
| 52 | ||
| 53 | /** | |
| 54 | * Whether the account `ownerId` may plant the door: a site admin, or an org | |
| 55 | * with a site admin among its owners. A name is never enough (#14911): on a | |
| 56 | * forge where no admin holds a name, anyone can sign up with it. | |
| 57 | */ | |
| 58 | export function trustedOwner(db: Database, ownerId: string): boolean { | |
| 59 | const owner = getUserById(db, ownerId); | |
| 60 | if (owner === undefined) return false; | |
| 61 | if (!isOrg(owner)) return owner.isAdmin === true && owner.kind === "user"; | |
| 62 | return personOwners(db, owner.id).some((id) => getUserById(db, id)?.isAdmin === true); | |
| 63 | } |
test/api-forge-view.test.ts +32 −2
| @@ -162,7 +162,7 @@ describe("the forge view over the API", () => { | ||
| 162 | 162 | expect(selects.length).toBeLessThanOrEqual(2 * APPS + 23); |
| 163 | 163 | }); |
| 164 | 164 | |
| 165 | test("foundry has a door only for an app an admin or the forge's org owns", async () => { | |
| 165 | test("foundry has a door only for an app an admin or an admin's org owns", async () => { | |
| 166 | 166 | const adminUser = admin.ok ? admin.value : (undefined as never); |
| 167 | 167 | const door = () => foundryHref(app, adminUser); |
| 168 | 168 | const lines = () => forgeViewLines({ app, user: adminUser } as never); |
| @@ -190,7 +190,7 @@ describe("the forge view over the API", () => { | ||
| 190 | 190 | expect(lines().lines[1]).toContain("/ada/foundry/app/production/open"); |
| 191 | 191 | }); |
| 192 | 192 | |
| 193 | test("an app of the trevato org is Foundry for a reader who is no admin", async () => { | |
| 193 | test("an app of an org an admin owns is Foundry for a reader who is no admin", async () => { | |
| 194 | 194 | // Move the admin's Foundry away; the org's takes the slug. |
| 195 | 195 | app.db.query("DELETE FROM app WHERE slug = 'foundry'").run(); |
| 196 | 196 | const org = createOrg(app.db, "trevato", admin.ok ? admin.value.id : ""); |
| @@ -227,6 +227,36 @@ describe("the forge view over the API", () => { | ||
| 227 | 227 | expect(opened).toEqual([]); |
| 228 | 228 | }); |
| 229 | 229 | |
| 230 | test("the name trevato alone trusts nobody: a non-admin account or org holding it plants no door (#14911)", async () => { | |
| 231 | if (!admin.ok) throw new Error(String(admin.error)); | |
| 232 | const adminUser = admin.value; | |
| 233 | const org = app.db.query<{ id: string }, []>("SELECT id FROM user WHERE username = 'trevato'").get()!; | |
| 234 | // The org keeps its name but an admin no longer owns it: no door. | |
| 235 | const owners = app.db | |
| 236 | .query<{ id: string }, [string]>("SELECT id FROM team WHERE org_id = ?1 AND name = 'Owners'") | |
| 237 | .get(org.id)!; | |
| 238 | const nobody = await createUser(app.db, { username: "nadia", email: "[email protected]", password: "a-long-passphrase-here", origin: "seed", trusted: true }); | |
| 239 | if (!nobody.ok) throw new Error(nobody.error); | |
| 240 | app.db.query("UPDATE team_member SET user_id = ?1 WHERE team_id = ?2").run(nobody.value.id, owners.id); | |
| 241 | const made = await createRepoRow(app.db, app.reposRoot, { ownerId: org.id, name: "foundry2", visibility: "public" }); | |
| 242 | if (!made.ok) throw new Error(String(made.error)); | |
| 243 | createAppRecord(app.db, { repoId: made.value.id, slug: "foundry", access: "public" }); | |
| 244 | lapse(); | |
| 245 | expect(foundryHref(app, adminUser)).toBeUndefined(); | |
| 246 | app.db.query("DELETE FROM app WHERE slug = 'foundry'").run(); | |
| 247 | // A person who signed up as trevato, where no admin holds the name: no door. | |
| 248 | app.db.query("UPDATE user SET username = 'trevato-org' WHERE id = ?1").run(org.id); | |
| 249 | const squatter = await createUser(app.db, { username: "trevato", email: "[email protected]", password: "a-long-passphrase-here", origin: "seed", trusted: true }); | |
| 250 | if (!squatter.ok) throw new Error(squatter.error); | |
| 251 | const theirs = await createRepoRow(app.db, app.reposRoot, { ownerId: squatter.value.id, name: "foundry", visibility: "public" }); | |
| 252 | if (!theirs.ok) throw new Error(String(theirs.error)); | |
| 253 | createAppRecord(app.db, { repoId: theirs.value.id, slug: "foundry", access: "public" }); | |
| 254 | lapse(); | |
| 255 | expect(foundryHref(app, adminUser)).toBeUndefined(); | |
| 256 | app.db.query("DELETE FROM app WHERE slug = 'foundry'").run(); | |
| 257 | lapse(); | |
| 258 | }); | |
| 259 | ||
| 230 | 260 | /** Whether `/` shows the Foundry button and the palette index the row, for a user. */ |
| 231 | 261 | async function homeAndPalette(userId: string) { |
| 232 | 262 | const cookie = `forge_session=${createSession(app.db, userId).token}`; |
new above
offline