Public: anyone can read this; the crew can change it.
A signed-out visitor to someone's hosted forge is told whose it is and offered Sign in, not the product page #13807
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/13795-a-signed-out-visitor-to-someone-s-hosted → main 5 commits fork
6 files
changed +213
−0
docs/features/README.md +1
| @@ -525,3 +525,4 @@ Read with the guide page [When something is wrong](../guide-when-wrong.md). | ||
| 525 | 525 | - [](map-held.md) |
| 526 | 526 | - [](likers.md) |
| 527 | 527 | - [](pricing-price-line.md) |
| 528 | - [](claimed-forge-door.md) |
docs/features/claimed-forge-door.md added +10
| @@ -0,0 +1,10 @@ | ||
| 1 | # A claimed forge tells a stranger whose it is (#13795) | |
| 2 | ||
| 3 | On a hosted forge someone has claimed, `/` for a signed-out visitor says "This is <owner>'s forge" with one Sign in, the public apps if there are any, and a link to get a forge of their own. The product page and its start form stay for forges nobody owns, and for `/front`. | |
| 4 | ||
| 5 | - **Who the owner is:** read from the record, not guessed. `parent.json` names the organisation the forge was spawned as (`org`). If a person took that name (`becomeOrg`), they are the owner; otherwise the organisation's Owners team: its one person, or, with several, the forge's own name. Code: `src/spawn/forge_owner.ts`. | |
| 6 | - **When it applies:** `parent.json` exists, an admin exists, and the visitor is signed out on `/`. A signed-in viewer keeps the studio. | |
| 7 | - **Sign in:** `/signin` (with `?next=` for shared words in the query), or the platform edge's login when an edge token is set. | |
| 8 | - **The terminal:** unchanged. A guest's key that no account holds is refused at the door (`Permission denied (publickey)`, `src/ssh/keys.ts`), so a guest sees nothing of a claimed forge there. | |
| 9 | - Page: `src/web/studio/door_claimed.ts`, using only the front page's classes; test: `test/home-claimed-forge.test.ts`. | |
| 10 | - **Why now:** a stranger on a claimed forge was shown a sales page for a product they are already inside; it is a one-page change with no pipeline dependency. |
src/spawn/forge_owner.ts added +34
| @@ -0,0 +1,34 @@ | ||
| 1 | /** | |
| 2 | * Whose a claimed hosted forge is. Its `parent.json` names the organisation the forge was | |
| 3 | * spawned as; the claim hands that organisation to a person (`handOver`, `becomeOrg`), so the | |
| 4 | * record is the Owners team of that organisation, or the organisation row itself once a person | |
| 5 | * took the forge's name. Synchronous, so the signed-out home can ask it without waiting. | |
| 6 | */ | |
| 7 | ||
| 8 | import type { Database } from "bun:sqlite"; | |
| 9 | import { readFileSync } from "node:fs"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { OWNERS_TEAM, listTeamMembers, listTeams } from "../core/orgs.ts"; | |
| 12 | import { getUserByName } from "../core/users.ts"; | |
| 13 | import { hasAdmin } from "./claim.ts"; | |
| 14 | ||
| 15 | /** | |
| 16 | * The name to give as the forge's owner, or undefined when the forge is not a claimed spawned | |
| 17 | * one (no `parent.json`, or nobody administers it yet). With one person in the Owners team that | |
| 18 | * is them; with several, or none, it is the forge's own name: never a guess among people. | |
| 19 | */ | |
| 20 | export function forgeOwner(db: Database, dataDir: string): string | undefined { | |
| 21 | let org: unknown; | |
| 22 | try { | |
| 23 | org = (JSON.parse(readFileSync(join(dataDir, "parent.json"), "utf8")) as { org?: unknown }).org; | |
| 24 | } catch { | |
| 25 | return undefined; | |
| 26 | } | |
| 27 | if (typeof org !== "string" || !hasAdmin(db)) return undefined; | |
| 28 | const named = getUserByName(db, org); | |
| 29 | if (named === undefined) return undefined; | |
| 30 | if (named.kind === "user") return named.username; | |
| 31 | const team = listTeams(db, named.id).find((t) => t.name === OWNERS_TEAM); | |
| 32 | const people = team === undefined ? [] : listTeamMembers(db, team.id).filter((u) => u.kind === "user"); | |
| 33 | return people.length === 1 ? people[0]!.username : named.username; | |
| 34 | } |
src/web/studio/door_claimed.ts added +46
| @@ -0,0 +1,46 @@ | ||
| 1 | /** | |
| 2 | * The home of a claimed hosted forge for someone signed out (#13795): whose forge it is, Sign in, | |
| 3 | * and what is public. The product page and its start form are for forges nobody owns. | |
| 4 | */ | |
| 5 | ||
| 6 | import type { Ctx } from "../../http/context.ts"; | |
| 7 | import { activeEdgeToken, edgeLoginUrl } from "../../http/edge.ts"; | |
| 8 | import { GET_A_FORGE_PATH } from "../../billing/plan_words.ts"; | |
| 9 | import { html, when, type Html } from "../html.ts"; | |
| 10 | import { page } from "../layout.ts"; | |
| 11 | import { liveRegionsJs } from "../client.ts"; | |
| 12 | import { btn } from "../ui.ts"; | |
| 13 | ||
| 14 | export function claimedDoor(ctx: Ctx, owner: string, tiles: Html[]): Response { | |
| 15 | // Signing in comes back to the shared words in the query, as the product page does. | |
| 16 | const back = "/" + ctx.url.search; | |
| 17 | const nextQuery = back === "/" ? "" : `?next=${encodeURIComponent(back)}`; | |
| 18 | const signin = activeEdgeToken(ctx.app) === undefined ? `/signin${nextQuery}` : edgeLoginUrl(back); | |
| 19 | const body = html`<div class="door"> | |
| 20 | <section class="door-hero"> | |
| 21 | <h1>This is ${owner}'s forge</h1> | |
| 22 | <div class="door-under"> | |
| 23 | <div class="door-lead"> | |
| 24 | <p class="door-support">Sign in to see what ${owner} is building.</p> | |
| 25 | <div class="bar">${btn("Sign in", { href: signin, variant: "primary", size: "lg" })}</div> | |
| 26 | </div> | |
| 27 | </div> | |
| 28 | </section> | |
| 29 | ${when( | |
| 30 | tiles.length > 0, | |
| 31 | () => html`<section class="door-section"> | |
| 32 | <h2>Made here</h2> | |
| 33 | <div class="door-shelf" data-shelf>${tiles}</div> | |
| 34 | </section>`, | |
| 35 | )} | |
| 36 | <p class="door-support"><a href="${GET_A_FORGE_PATH}">Get your own forge</a></p> | |
| 37 | </div>`; | |
| 38 | return page({ | |
| 39 | ctx, | |
| 40 | title: `${owner}'s forge`, | |
| 41 | description: `${owner}'s forge. Sign in to see what they are building.`, | |
| 42 | body, | |
| 43 | scriptJs: liveRegionsJs, | |
| 44 | tools: btn("Sign in", { href: signin }), | |
| 45 | }); | |
| 46 | } |
src/web/studio/home.ts +5
| @@ -82,6 +82,8 @@ import { homeFlowPage } from "../pages/home_flow.ts"; | ||
| 82 | 82 | import { crewKeyCard } from "./crew_key.ts"; |
| 83 | 83 | import { firstAskJs, nextStepsCard, nextStepsOf } from "../pages/next-steps.ts"; |
| 84 | 84 | import { spawnedForge } from "../../spawn/claim.ts"; |
| 85 | import { forgeOwner } from "../../spawn/forge_owner.ts"; | |
| 86 | import { claimedDoor } from "./door_claimed.ts"; | |
| 85 | 87 | import { readableNotes } from "../../actions/desk.ts"; |
| 86 | 88 | import { deskStrip } from "../desk_strip.ts"; |
| 87 | 89 | import { startedFromLine } from "../started_from.ts"; |
| @@ -720,6 +722,9 @@ export function studioDoor(ctx: Ctx): Response { | ||
| 720 | 722 | const origin = (ctx.app.config.url ?? ctx.url.origin).replace(/\/$/, ""); |
| 721 | 723 | const prompt = agentPrompt(origin); |
| 722 | 724 | const asked = encodeURIComponent(prompt); |
| 725 | // A claimed hosted forge is someone's: its stranger is told whose, not sold the product (#13795). | |
| 726 | const owner = visitor && ctx.url.pathname === "/" ? forgeOwner(ctx.app.db, ctx.app.config.dataDir) : undefined; | |
| 727 | if (owner !== undefined) return claimedDoor(ctx, owner, tiles); | |
| 723 | 728 | const body = html`<div class="door"> |
| 724 | 729 | <section class="door-hero"> |
| 725 | 730 | <h1>Software for what you're building.<br /><span class="muted">Yours to keep.</span></h1> |
test/home-claimed-forge.test.ts added +117
| @@ -0,0 +1,117 @@ | ||
| 1 | /** | |
| 2 | * A signed-out visitor to a claimed hosted forge is told whose it is and offered Sign in (#13795). | |
| 3 | */ | |
| 4 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 5 | import { mkdtemp, rm, writeFile } from "node:fs/promises"; | |
| 6 | import { tmpdir } from "node:os"; | |
| 7 | import { join } from "node:path"; | |
| 8 | import { createApp, type App } from "../src/app.ts"; | |
| 9 | import { forgeHttp, serve, type Servers } from "../src/http/server.ts"; | |
| 10 | import { createUser, getUserByName } from "../src/core/users.ts"; | |
| 11 | import { OWNERS_TEAM, addTeamMember, createOrg, listTeams } from "../src/core/orgs.ts"; | |
| 12 | import { createSession } from "../src/core/sessions.ts"; | |
| 13 | import { edgeLoginUrl } from "../src/http/edge.ts"; | |
| 14 | import { forgeOwner } from "../src/spawn/forge_owner.ts"; | |
| 15 | ||
| 16 | let dataDir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | beforeAll(async () => { | |
| 20 | dataDir = await mkdtemp(join(tmpdir(), "forge-claimed-")); | |
| 21 | app = createApp({ dataDir, port: 0 }); | |
| 22 | servers = await serve(app); | |
| 23 | }); | |
| 24 | afterAll(async () => { | |
| 25 | await servers.stop?.(); | |
| 26 | app.close(); | |
| 27 | await rm(dataDir, { recursive: true, force: true }); | |
| 28 | }); | |
| 29 | ||
| 30 | const get = async (path: string, headers: Record<string, string> = {}) => | |
| 31 | (await fetch(`${servers.url}${path}`, { headers })).text(); | |
| 32 | ||
| 33 | test("an unowned forge keeps the product page", async () => { | |
| 34 | expect(await get("/")).toContain("Start my forge"); | |
| 35 | expect(forgeOwner(app.db, dataDir)).toBeUndefined(); | |
| 36 | }); | |
| 37 | ||
| 38 | test("a claimed hosted forge says whose it is, with Sign in and no start form", async () => { | |
| 39 | await writeFile(join(dataDir, "parent.json"), JSON.stringify({ org: "riley", url: "https://parent.example" })); | |
| 40 | const made = await createUser(app.db, { origin: "seed", trusted: true, username: "riley", email: "[email protected]", password: "a-good-passphrase", isAdmin: true }); | |
| 41 | expect(made.ok).toBe(true); | |
| 42 | const body = await get("/"); | |
| 43 | expect(body).toContain("This is riley's forge"); | |
| 44 | expect(body).toContain("<title>riley's forge"); | |
| 45 | expect(body).not.toContain("Start my forge"); | |
| 46 | expect(body).not.toContain('id="start-forge"'); | |
| 47 | expect(body).toContain("Get your own forge"); | |
| 48 | expect(body).toMatch(/href="\/signin"[^>]*>\s*Sign in/); | |
| 49 | // No public apps here: nothing may point at a section that is not drawn. | |
| 50 | expect(body).not.toContain('href="#made"'); | |
| 51 | expect(body).not.toContain('id="made"'); | |
| 52 | }); | |
| 53 | ||
| 54 | test("the share query is carried through Sign in", async () => { | |
| 55 | expect(await get("/?text=hi")).toContain('href="/signin?next=%2F%3Ftext%3Dhi"'); | |
| 56 | }); | |
| 57 | ||
| 58 | test("/front is the product page's own, not the claimed one", async () => { | |
| 59 | const body = await get("/front"); | |
| 60 | expect(body).not.toContain("This is riley's forge"); | |
| 61 | }); | |
| 62 | ||
| 63 | test("a signed-in viewer does not get the claimed page", async () => { | |
| 64 | const user = getUserByName(app.db, "riley")!; | |
| 65 | const { token } = createSession(app.db, user.id); | |
| 66 | const body = await get("/", { cookie: `forge_session=${token}` }); | |
| 67 | expect(body).not.toContain("This is riley's forge"); | |
| 68 | }); | |
| 69 | ||
| 70 | test("a name is escaped in the page and the title", async () => { | |
| 71 | app.db.query("UPDATE user SET username = ?1 WHERE username = 'riley'").run("<i>r</i>"); | |
| 72 | await writeFile(join(dataDir, "parent.json"), JSON.stringify({ org: "<i>r</i>" })); | |
| 73 | const body = await get("/"); | |
| 74 | expect(body).not.toContain("<i>r</i>"); | |
| 75 | expect(body).toContain("<i>r</i>"); | |
| 76 | app.db.query("UPDATE user SET username = 'riley' WHERE username = ?1").run("<i>r</i>"); | |
| 77 | await writeFile(join(dataDir, "parent.json"), JSON.stringify({ org: "riley" })); | |
| 78 | }); | |
| 79 | ||
| 80 | test("an organisation forge names its one owner, or its own name when there are several", async () => { | |
| 81 | const dir = await mkdtemp(join(tmpdir(), "forge-claimed-org-")); | |
| 82 | const orgApp = createApp({ dataDir: dir, port: 0 }); | |
| 83 | try { | |
| 84 | await writeFile(join(dir, "parent.json"), JSON.stringify({ org: "acme" })); | |
| 85 | const a = await createUser(orgApp.db, { origin: "seed", trusted: true, username: "ann", email: "[email protected]", password: "a-good-passphrase", isAdmin: true }); | |
| 86 | const b = await createUser(orgApp.db, { origin: "seed", trusted: true, username: "bo", email: "[email protected]", password: "a-good-passphrase" }); | |
| 87 | if (!a.ok || !b.ok) throw new Error("setup"); | |
| 88 | const org = createOrg(orgApp.db, "acme", a.value.id, { allowReserved: true }); | |
| 89 | if (!org.ok) throw new Error("org"); | |
| 90 | expect(forgeOwner(orgApp.db, dir)).toBe("ann"); | |
| 91 | const team = listTeams(orgApp.db, org.value.id).find((t) => t.name === OWNERS_TEAM)!; | |
| 92 | addTeamMember(orgApp.db, team, b.value.id); | |
| 93 | expect(forgeOwner(orgApp.db, dir)).toBe("acme"); | |
| 94 | } finally { | |
| 95 | orgApp.close(); | |
| 96 | await rm(dir, { recursive: true, force: true }); | |
| 97 | } | |
| 98 | }); | |
| 99 | ||
| 100 | test("behind a platform edge, Sign in goes to the edge", async () => { | |
| 101 | const dir = await mkdtemp(join(tmpdir(), "forge-claimed-edge-")); | |
| 102 | const edgeApp = createApp({ dataDir: dir, port: 0, url: "https://forge.example", edgeToken: "edge-secret-claimed" }); | |
| 103 | const http = forgeHttp(edgeApp); | |
| 104 | try { | |
| 105 | await writeFile(join(dir, "parent.json"), JSON.stringify({ org: "sam" })); | |
| 106 | await createUser(edgeApp.db, { origin: "seed", trusted: true, username: "sam", email: "[email protected]", password: "a-good-passphrase", isAdmin: true }); | |
| 107 | const res = await http.handle(new Request("https://forge.example/")); | |
| 108 | const body = await res.text(); | |
| 109 | expect(body).toContain("This is sam's forge"); | |
| 110 | expect(body).toContain(`href="${edgeLoginUrl("/")}"`); | |
| 111 | expect(body).not.toContain('href="/signin"'); | |
| 112 | } finally { | |
| 113 | http.close(); | |
| 114 | edgeApp.close(); | |
| 115 | await rm(dir, { recursive: true, force: true }); | |
| 116 | } | |
| 117 | }); |
new above
offline