Public: anyone can read this; the crew can change it.
Private apps: the crew's reviewer can't reach their previews (bare 404 at the edge), so reviews stall and pulls get handed to a person #6022
scout/forge:scout/6021-private-apps-the-crew-s-reviewer-can-t-r → main 3 commits fork
4 files
changed +180
−8
docs/APPS.md +7 −3
| @@ -346,9 +346,13 @@ started`; a deploy no push asked for — a redeploy someone pressed, a | ||
| 346 | 346 | can read it — and |
| 347 | 347 | returns them with a signed, short-lived grant for the page they wanted; |
| 348 | 348 | the proxy asks the same question again on every request, so a grant |
| 349 | stops working the moment its holder may no longer enter. Anyone else, | |
| 350 | and anything that is not a person navigating, gets the forge's own "not | |
| 351 | found". Guessing the hostname yields the same. Previews follow the app; | |
| 349 | stops working the moment its holder may no longer enter. A script | |
| 350 | or agent sends a forge token instead — `Authorization: Bearer <token>`, | |
| 351 | a run's own token included (bound to its repository's app) — and is | |
| 352 | asked the same `canEnterApp` question. Anyone else, and anything that is | |
| 353 | not a person navigating, gets `401` saying the app is private (never a | |
| 354 | bare "not found", so a privacy wall is told from a dead app); a | |
| 355 | hostname that is no app is still "not found". Previews follow the app; | |
| 352 | 356 | the terminal stays its writers' and crew's. Making the repository public |
| 353 | 357 | changes nobody's way in. A person the app names enters it and its |
| 354 | 358 | previews and gets nothing else: no code of a private repository, no |
src/apps/proxy.ts +56 −2
| @@ -33,6 +33,7 @@ import { | ||
| 33 | 33 | ownerInStanding, |
| 34 | 34 | } from "../core/repos.ts"; |
| 35 | 35 | import { getUserById, isActive, type User } from "../core/users.ts"; |
| 36 | import { verifyToken } from "../core/tokens.ts"; | |
| 36 | 37 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 37 | 38 | import { loadKey } from "../kernel/secret.ts"; |
| 38 | 39 | import { previewEnv, type EnvName } from "./data.ts"; |
| @@ -547,6 +548,42 @@ function stillEntitled( | ||
| 547 | 548 | ); |
| 548 | 549 | } |
| 549 | 550 | |
| 551 | /** How long a token's vouching for a request lasts: this request only. */ | |
| 552 | const BEARER_GRANT_MS = 60_000; | |
| 553 | ||
| 554 | /** | |
| 555 | * A forge API token presented as `Authorization: Bearer …` vouches for its | |
| 556 | * user at a private app, so a reviewer, the steward or a hunter can open | |
| 557 | * what they are reviewing without a browser. The same question a cookie | |
| 558 | * grant is asked: may this person enter the app now. A run's token is | |
| 559 | * bound to its own repository, so it reaches that repository's preview and | |
| 560 | * production and no other app. The credential never reaches the app | |
| 561 | * (`stripForgeCredentials`). | |
| 562 | */ | |
| 563 | function bearerGrant( | |
| 564 | runtime: AppsRuntime, | |
| 565 | request: Request, | |
| 566 | target: AppTarget, | |
| 567 | ): Grant | undefined { | |
| 568 | const presented = /^Bearer (\S+)$/i.exec( | |
| 569 | request.headers.get("authorization") ?? "", | |
| 570 | )?.[1]; | |
| 571 | if (presented === undefined) return undefined; | |
| 572 | const verified = verifyToken(runtime.app.db, presented); | |
| 573 | if (!verified.ok) return undefined; | |
| 574 | const { user, run } = verified.value; | |
| 575 | if (run !== undefined && run.repoId !== target.app.repoId) return undefined; | |
| 576 | const grant: Grant = { | |
| 577 | who: { | |
| 578 | id: user.id, | |
| 579 | name: user.username, | |
| 580 | role: user.isAdmin ? "admin" : "user", | |
| 581 | }, | |
| 582 | expiresMs: Date.now() + BEARER_GRANT_MS, | |
| 583 | }; | |
| 584 | return stillEntitled(runtime, target, grant) ? grant : undefined; | |
| 585 | } | |
| 586 | ||
| 550 | 587 | /** |
| 551 | 588 | * What a request addressed to an app must pass before anything is served: |
| 552 | 589 | * a private app's grant, then a live port. One gate for both the request |
| @@ -583,7 +620,9 @@ function admit( | ||
| 583 | 620 | signed !== undefined && |
| 584 | 621 | ((guarded && !stillEntitled(runtime, target, signed)) || |
| 585 | 622 | (named !== undefined && !isActive(named))); |
| 586 | const grant = lapsed ? undefined : signed; | |
| 623 | let grant = lapsed ? undefined : signed; | |
| 624 | if (guarded && grant === undefined) | |
| 625 | grant = bearerGrant(runtime, request, target); | |
| 587 | 626 | if (guarded) { |
| 588 | 627 | if (grant === undefined) { |
| 589 | 628 | // A person arriving by link is sent to the forge to sign in; it vouches |
| @@ -608,7 +647,22 @@ function admit( | ||
| 608 | 647 | }), |
| 609 | 648 | ); |
| 610 | 649 | } |
| 611 | return err(new Response("not found", { status: 404 })); | |
| 650 | // Say so: a privacy wall is not a dead app. The answer is the same | |
| 651 | // whether or not the token's holder may enter, so it tells a guesser | |
| 652 | // nothing about who is a member. | |
| 653 | return err( | |
| 654 | new Response( | |
| 655 | "private app: sign in at the forge, or send a forge token with access to this repository as `Authorization: Bearer <token>`", | |
| 656 | { | |
| 657 | status: 401, | |
| 658 | headers: { | |
| 659 | "content-type": "text/plain; charset=utf-8", | |
| 660 | "www-authenticate": 'Bearer realm="forge"', | |
| 661 | "cache-control": "no-store", | |
| 662 | }, | |
| 663 | }, | |
| 664 | ), | |
| 665 | ); | |
| 612 | 666 | } |
| 613 | 667 | } |
| 614 | 668 | // An archived repository's app is put away: stopped, its data kept, and |
test/apps-private-bearer.test.ts added +114
| @@ -0,0 +1,114 @@ | ||
| 1 | /** A private app answers a forge token that may enter it; anyone else is told it is private (#6021). */ | |
| 2 | ||
| 3 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 4 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 5 | import { tmpdir } from "node:os"; | |
| 6 | import { join } from "node:path"; | |
| 7 | import { createApp, type App } from "../src/app.ts"; | |
| 8 | import { AppsRuntime } from "../src/apps/runtime.ts"; | |
| 9 | import { newApp, type NewAppResult } from "../src/actions/apps.ts"; | |
| 10 | import { listRuns } from "../src/core/runs.ts"; | |
| 11 | import { mintRunToken } from "../src/core/run_token.ts"; | |
| 12 | import { createToken } from "../src/core/tokens.ts"; | |
| 13 | import { createUser, type User } from "../src/core/users.ts"; | |
| 14 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 15 | ||
| 16 | const DOMAIN = "apps.test"; | |
| 17 | let dataDir: string; | |
| 18 | let app: App; | |
| 19 | let servers: Servers; | |
| 20 | let runtime: AppsRuntime; | |
| 21 | let alice: User; | |
| 22 | let bob: User; | |
| 23 | let vault: NewAppResult; | |
| 24 | ||
| 25 | const get = (host: string, token?: string) => | |
| 26 | fetch(`${servers.url}/`, { | |
| 27 | headers: { | |
| 28 | host, | |
| 29 | ...(token === undefined ? {} : { authorization: `Bearer ${token}` }), | |
| 30 | }, | |
| 31 | redirect: "manual", | |
| 32 | }); | |
| 33 | ||
| 34 | async function deployed(repoId: string): Promise<void> { | |
| 35 | const deadline = Date.now() + 30_000; | |
| 36 | while (Date.now() < deadline) { | |
| 37 | const run = listRuns(app.db, repoId, { kind: "deploy", limit: 5 }).find( | |
| 38 | (r) => r.env === "production", | |
| 39 | ); | |
| 40 | if (run && (run.state === "ok" || run.state === "failed")) return; | |
| 41 | await Bun.sleep(50); | |
| 42 | } | |
| 43 | throw new Error("deploy did not settle"); | |
| 44 | } | |
| 45 | ||
| 46 | beforeAll(async () => { | |
| 47 | dataDir = await mkdtemp(join(tmpdir(), "forge-bearer-")); | |
| 48 | app = createApp({ dataDir, port: 0 }); | |
| 49 | servers = await serve(app); | |
| 50 | runtime = new AppsRuntime(app, { | |
| 51 | domain: DOMAIN, | |
| 52 | scheme: "http", | |
| 53 | publicPort: servers.port, | |
| 54 | forgeUrl: servers.url, | |
| 55 | concurrency: 2, | |
| 56 | }); | |
| 57 | await runtime.start(); | |
| 58 | const mk = async (username: string, adminIfFirst: boolean) => { | |
| 59 | const u = await createUser(app.db, { | |
| 60 | origin: "seed", | |
| 61 | trusted: true, | |
| 62 | username, | |
| 63 | email: `${username}@forge.local`, | |
| 64 | password: "a-good-passphrase", | |
| 65 | adminIfFirst, | |
| 66 | }); | |
| 67 | if (!u.ok) throw new Error(u.error); | |
| 68 | return u.value; | |
| 69 | }; | |
| 70 | alice = await mk("alice", true); | |
| 71 | bob = await mk("bob", false); | |
| 72 | const make = async (name: string) => { | |
| 73 | const made = await newApp(app, alice, { name, visibility: "private" }); | |
| 74 | if (!made.ok) throw new Error(made.error); | |
| 75 | await deployed(made.value.repo.id); | |
| 76 | return made.value; | |
| 77 | }; | |
| 78 | [vault] = await Promise.all([make("vault"), make("elsewhere")]); | |
| 79 | }, 30_000); | |
| 80 | ||
| 81 | afterAll(async () => { | |
| 82 | await runtime.stop(); | |
| 83 | servers.stop(); | |
| 84 | app.close(); | |
| 85 | await rm(dataDir, { recursive: true, force: true }); | |
| 86 | }); | |
| 87 | ||
| 88 | test("anonymous: says the app is private (401), not a bare 404", async () => { | |
| 89 | const res = await get(`vault.${DOMAIN}`); | |
| 90 | expect(res.status).toBe(401); | |
| 91 | expect(res.headers.get("www-authenticate")).toContain("Bearer"); | |
| 92 | expect(await res.text()).toContain("private app"); | |
| 93 | }); | |
| 94 | ||
| 95 | test("a token that may enter the app gets the app; a stranger's token does not", async () => { | |
| 96 | const mine = createToken(app.db, alice.id, "read", "ro").token; | |
| 97 | expect((await get(`vault.${DOMAIN}`, mine)).status).toBe(200); | |
| 98 | const theirs = createToken(app.db, bob.id, "read", "ro").token; | |
| 99 | expect((await get(`vault.${DOMAIN}`, theirs)).status).toBe(401); | |
| 100 | expect((await get(`vault.${DOMAIN}`, "forge_nonsense")).status).toBe(401); | |
| 101 | }); | |
| 102 | ||
| 103 | test("a run's token reaches its own repository's app and no other", async () => { | |
| 104 | const soon = new Date(Date.now() + 60_000).toISOString(); | |
| 105 | const run = mintRunToken( | |
| 106 | app.db, | |
| 107 | alice.id, | |
| 108 | "r1", | |
| 109 | { repoId: vault.repo.id, forkId: vault.repo.id }, | |
| 110 | soon, | |
| 111 | ).token; | |
| 112 | expect((await get(`vault.${DOMAIN}`, run)).status).toBe(200); | |
| 113 | expect((await get(`elsewhere.${DOMAIN}`, run)).status).toBe(401); | |
| 114 | }); |
test/private-app.test.ts +3 −3
| @@ -196,7 +196,7 @@ describe("a private app on a public repository stays private", () => { | ||
| 196 | 196 | }), |
| 197 | 197 | ); |
| 198 | 198 | expect(carols).not.toBe(""); |
| 199 | expect((await knock(host, carols)).status).toBe(404); | |
| 199 | expect((await knock(host, carols)).status).toBe(401); | |
| 200 | 200 | const alices = await cookieFrom( |
| 201 | 201 | grantUrl(runtime, target, "/", host, { |
| 202 | 202 | id: alice.id, |
| @@ -204,9 +204,9 @@ describe("a private app on a public repository stays private", () => { | ||
| 204 | 204 | role: "admin", |
| 205 | 205 | }), |
| 206 | 206 | ); |
| 207 | expect((await knock(host, alices)).status).not.toBe(404); | |
| 207 | expect((await knock(host, alices)).status).not.toBe(401); | |
| 208 | 208 | // Nobody named, nothing granted. |
| 209 | expect((await knock(host)).status).toBe(404); | |
| 209 | expect((await knock(host)).status).toBe(401); | |
| 210 | 210 | }); |
| 211 | 211 | |
| 212 | 212 | test("the same holds for a preview's host", async () => { |
new above
offline