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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/14911-the-foundry-nav-door-trusts-an-owner-by → main 3 commits fork

  • Merged. scout/14911-the-foundry-nav-door-trusts-an-owner-by was merged into main as b2e031e.

Review

Sign in to review this change.
2 files changed +49 −8
src/actions/foundry_door.ts +17 −6
@@ -6,15 +6,14 @@
66 */
77 import { getUserById } from "../core/users.ts";
88 import { getAppBySlug } from "../core/apps.ts";
9 import { isOrg, personOwners } from "../core/orgs.ts";
910 import { canRead, enterableApps, getRepoById } from "../core/repos.ts";
11 import type { Database } from "bun:sqlite";
1012 import type { App } from "../app.ts";
1113 import type { User } from "../core/users.ts";
1214
1315 export const FOUNDRY_SLUG = "foundry";
1416
15 /** The forge's own org: its apps are as trusted as an admin's. */
16 export const FOUNDRY_ORG = "trevato";
17
1817 /** How long a reader's answer is reused: the door moves when an app is installed, not per page. */
1918 export const DOOR_TTL_MS = 5000;
2019
@@ -43,10 +42,22 @@ function resolve(app: App, user: User): string | undefined {
4342 if (record === undefined) return undefined;
4443 const repo = getRepoById(db, record.repoId);
4544 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;
4948 if (!canRead(db, repo, user)) return undefined;
5049 if (!enterableApps(db, [{ repo, app: record }], user, { named: false }).has(record.id)) return undefined;
5150 return `/${repo.ownerName}/${repo.name}/app/production/open`;
5251 }
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", () => {
162162 expect(selects.length).toBeLessThanOrEqual(2 * APPS + 23);
163163 });
164164
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 () => {
166166 const adminUser = admin.ok ? admin.value : (undefined as never);
167167 const door = () => foundryHref(app, adminUser);
168168 const lines = () => forgeViewLines({ app, user: adminUser } as never);
@@ -190,7 +190,7 @@ describe("the forge view over the API", () => {
190190 expect(lines().lines[1]).toContain("/ada/foundry/app/production/open");
191191 });
192192
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 () => {
194194 // Move the admin's Foundry away; the org's takes the slug.
195195 app.db.query("DELETE FROM app WHERE slug = 'foundry'").run();
196196 const org = createOrg(app.db, "trevato", admin.ok ? admin.value.id : "");
@@ -227,6 +227,36 @@ describe("the forge view over the API", () => {
227227 expect(opened).toEqual([]);
228228 });
229229
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
230260 /** Whether `/` shows the Foundry button and the palette index the row, for a user. */
231261 async function homeAndPalette(userId: string) {
232262 const cookie = `forge_session=${createSession(app.db, userId).token}`;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs