Public: anyone can read this; the crew can change it.
The app terminal refuses a sealed pull's preview exactly as the other preview doors do #14919
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14900-the-app-terminal-refuses-a-sealed-pull-s → main 4 commits fork 1 thread · 1 unresolved
5 files
changed +112
−17
src/actions/apps.ts +3 −8
| @@ -9,8 +9,7 @@ | ||
| 9 | 9 | import { runnerSettings } from "../crew/runner_env.ts"; |
| 10 | 10 | import { getVariant } from "../core/variants.ts"; |
| 11 | 11 | import { pullDir } from "../core/pulls.ts"; |
| 12 | import { getIssue } from "../core/issues.ts"; | |
| 13 | import { mayView } from "../core/sealed.ts"; | |
| 12 | import { mayView, sealedPreviewPull } from "../core/sealed.ts"; | |
| 14 | 13 | import { deriveAppName } from "../web/pages/apps-name.ts"; |
| 15 | 14 | import { REPO_INIT_FAILED, REPO_NAME_RULES } from "./repo_words.ts"; |
| 16 | 15 | import { NO_SUCH_AGENT } from "./crew_limit_words.ts"; |
| @@ -745,12 +744,8 @@ export function openEnvUrl( | ||
| 745 | 744 | return err("not_found"); |
| 746 | 745 | // A sealed pull's preview is its report's: for those who may see it, and |
| 747 | 746 | // through a grant only (`core/sealed.ts`). |
| 748 | const sealedNumber = /^pull-(\d+)$/.exec(env)?.[1]; | |
| 749 | const sealed = | |
| 750 | sealedNumber === undefined | |
| 751 | ? undefined | |
| 752 | : getIssue(app.db, repo.id, Number(sealedNumber)); | |
| 753 | if (sealed?.sealed === true && !mayView(app.db, repo, sealed, actor)) | |
| 747 | const sealed = sealedPreviewPull(app.db, repo.id, env); | |
| 748 | if (sealed !== undefined && !mayView(app.db, repo, sealed, actor)) | |
| 754 | 749 | return err("not_found"); |
| 755 | 750 | const runtime = appsRuntime(app); |
| 756 | 751 | if (runtime === undefined) return err("no_runtime"); |
src/apps/proxy.ts +4 −7
| @@ -19,10 +19,10 @@ import { landedChange } from "./landed.ts"; | ||
| 19 | 19 | import { expireCookie, setCookie } from "../auth/cookies.ts"; |
| 20 | 20 | import { compressBytes, MIN_BYTES, negotiate } from "../http/compress.ts"; |
| 21 | 21 | import type { Database } from "bun:sqlite"; |
| 22 | import { getIssue, type Issue } from "../core/issues.ts"; | |
| 22 | import { getIssue } from "../core/issues.ts"; | |
| 23 | 23 | import { getPull } from "../core/pulls.ts"; |
| 24 | 24 | import { guestWaits } from "../origins/guest.ts"; |
| 25 | import { mayView } from "../core/sealed.ts"; | |
| 25 | import { mayView, sealedPreviewPull, type SealedRow } from "../core/sealed.ts"; | |
| 26 | 26 | import { holds, weakEtag } from "../http/etag.ts"; |
| 27 | 27 | import { holdOpen } from "../http/caller.ts"; |
| 28 | 28 | import { appHostSuffix } from "./host.ts"; |
| @@ -454,11 +454,8 @@ const HOP_BY_HOP = [ | ||
| 454 | 454 | * a sealed report's fix, which only those who may see the report may open, |
| 455 | 455 | * on a public app as on a private one. |
| 456 | 456 | */ |
| 457 | function sealedPull(runtime: AppsRuntime, target: AppTarget): Issue | undefined { | |
| 458 | const number = /^pull-(\d+)$/.exec(target.env)?.[1]; | |
| 459 | if (number === undefined) return undefined; | |
| 460 | const pull = getIssue(runtime.app.db, target.app.repoId, Number(number)); | |
| 461 | return pull?.sealed === true ? pull : undefined; | |
| 457 | function sealedPull(runtime: AppsRuntime, target: AppTarget): SealedRow | undefined { | |
| 458 | return sealedPreviewPull(runtime.app.db, target.app.repoId, target.env); | |
| 462 | 459 | } |
| 463 | 460 | |
| 464 | 461 | /** |
src/apps/tty.ts +7 −1
| @@ -25,6 +25,7 @@ import { getAppBySlug, type AppRecord } from "../core/apps.ts"; | ||
| 25 | 25 | import { identityKey } from "./proxy.ts"; |
| 26 | 26 | import { canEnterApp, canRead, canWrite, getRepoById } from "../core/repos.ts"; |
| 27 | 27 | import type { User } from "../core/users.ts"; |
| 28 | import { mayView, sealedPreviewPull } from "../core/sealed.ts"; | |
| 28 | 29 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 29 | 30 | import type { SshSession } from "../ssh/server.ts"; |
| 30 | 31 | import { |
| @@ -122,7 +123,7 @@ export function ttyRefusalText( | ||
| 122 | 123 | export function appGate( |
| 123 | 124 | forge: Forge, |
| 124 | 125 | user: User | undefined, |
| 125 | target: { app: AppRecord }, | |
| 126 | target: { app: AppRecord; env: EnvName }, | |
| 126 | 127 | ): TtyRefusal | undefined { |
| 127 | 128 | const repo = getRepoById(forge.db, target.app.repoId); |
| 128 | 129 | if (!repo || !canRead(forge.db, repo, user)) return "not_found"; |
| @@ -131,6 +132,11 @@ export function appGate( | ||
| 131 | 132 | // names may enter the app, not its terminal (#752). |
| 132 | 133 | if (!canEnterApp(forge.db, repo, target.app, user, { named: false })) |
| 133 | 134 | return "not_found"; |
| 135 | // A sealed pull's preview is its report's, on a public app as on a | |
| 136 | // private one: the proxy's rule, asked through the same lookup (#14900). | |
| 137 | const sealed = sealedPreviewPull(forge.db, repo.id, target.env); | |
| 138 | if (sealed !== undefined && !mayView(forge.db, repo, sealed, user)) | |
| 139 | return "not_found"; | |
| 134 | 140 | return undefined; |
| 135 | 141 | } |
| 136 | 142 |
src/core/sealed.ts +17 −1
| @@ -224,8 +224,23 @@ export function sealedPreview( | ||
| 224 | 224 | repoId: string, |
| 225 | 225 | env: string, |
| 226 | 226 | ): boolean { |
| 227 | return sealedPreviewPull(db, repoId, env) !== undefined; | |
| 228 | } | |
| 229 | ||
| 230 | /** | |
| 231 | * The sealed pull a preview serves, when `env` is `pull-<n>` and that pull is sealed, as `mayView` | |
| 232 | * reads it: the one lookup every preview door asks — the proxy, the app actions and the app's | |
| 233 | * terminal — so they cannot drift (#14900). | |
| 234 | */ | |
| 235 | export function sealedPreviewPull( | |
| 236 | db: Database, | |
| 237 | repoId: string, | |
| 238 | env: string, | |
| 239 | ): SealedRow | undefined { | |
| 227 | 240 | const number = /^pull-(\d+)$/.exec(env)?.[1]; |
| 228 | return number !== undefined && sealedByNumber(db, repoId, Number(number)); | |
| 241 | if (number === undefined) return undefined; | |
| 242 | const row = sealedRowByNumber(db, repoId, Number(number)); | |
| 243 | return row?.sealed === true ? row : undefined; | |
| 229 | 244 | } |
| 230 | 245 | |
| 231 | 246 | /** Seal or unseal. Returns whether it changed. */ |
| @@ -243,3 +258,4 @@ export function setSealed( | ||
| 243 | 258 | .run(issueId, sealed ? 1 : 0).changes > 0 |
| 244 | 259 | ); |
| 245 | 260 | } |
| 261 | ||
test/tty-sealed-gate.test.ts added +81
| @@ -0,0 +1,81 @@ | ||
| 1 | /** | |
| 2 | * The app terminal's gate refuses a sealed pull's preview exactly as the | |
| 3 | * proxy does (#14900): a reader who may not see the report is told there | |
| 4 | * is no such app; a writer gets in; an unsealed pull's preview is open. | |
| 5 | */ | |
| 6 | ||
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { openIssue } from "../src/actions/issues.ts"; | |
| 13 | import { createRepo } from "../src/actions/repos.ts"; | |
| 14 | import { createApp as insertApp, type AppRecord } from "../src/core/apps.ts"; | |
| 15 | import { createUser, type User } from "../src/core/users.ts"; | |
| 16 | import { previewEnv, type EnvName } from "../src/apps/data.ts"; | |
| 17 | import { appGate } from "../src/apps/tty.ts"; | |
| 18 | import { sealedPreviewPull } from "../src/core/sealed.ts"; | |
| 19 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 20 | import { quietLogs } from "./helpers/globals.ts"; | |
| 21 | ||
| 22 | quietLogs(); | |
| 23 | ||
| 24 | describe("app terminal gate, sealed pull preview", () => { | |
| 25 | let dataDir = ""; | |
| 26 | let app: App; | |
| 27 | let owner: User; | |
| 28 | let stranger: User; | |
| 29 | let record: AppRecord; | |
| 30 | let sealedEnv: EnvName; | |
| 31 | let openEnv: EnvName; | |
| 32 | let repoId = ""; | |
| 33 | ||
| 34 | beforeAll(async () => { | |
| 35 | dataDir = await mkdtemp(join(tmpdir(), "forge-tty-sealed-")); | |
| 36 | app = createApp({ dataDir, port: 0 }); | |
| 37 | const seeded = await seedAdmin(app, { username: "rowan", email: "[email protected]", password: "a-long-owner-passphrase" }); | |
| 38 | if (!seeded.ok) throw new Error(String(seeded.error)); | |
| 39 | owner = seeded.value; | |
| 40 | const repo = await createRepo(app, owner, { name: "lawn", visibility: "public" }); | |
| 41 | if (!repo.ok) throw new Error(String(repo.error)); | |
| 42 | repoId = repo.value.id; | |
| 43 | const made = insertApp(app.db, { repoId, slug: "lawn", access: "public" }); | |
| 44 | if (!made.ok) throw new Error(String(made.error)); | |
| 45 | record = made.value; | |
| 46 | const hole = openIssue(app, owner, repo.value, { title: "the hole", body: "" }); | |
| 47 | const plain = openIssue(app, owner, repo.value, { title: "a weed", body: "" }); | |
| 48 | if (!hole.ok || !plain.ok) throw new Error("issues"); | |
| 49 | app.db.query("UPDATE issue SET sealed = 1 WHERE id = ?1").run(hole.value.id); | |
| 50 | sealedEnv = previewEnv(hole.value.number) as EnvName; | |
| 51 | openEnv = previewEnv(plain.value.number) as EnvName; | |
| 52 | const user = await createUser(app.db, { origin: "seed", trusted: true, username: "stranger", email: "[email protected]", password: "a-long-stranger-passphrase" }); | |
| 53 | if (!user.ok) throw new Error(String(user.error)); | |
| 54 | stranger = user.value; | |
| 55 | }, 60_000); | |
| 56 | ||
| 57 | afterAll(async () => { | |
| 58 | app.close(); | |
| 59 | await rm(dataDir, { recursive: true, force: true }); | |
| 60 | }); | |
| 61 | ||
| 62 | test("a reader who may not see the sealed pull is told there is no such app", () => { | |
| 63 | expect(appGate(app, stranger, { app: record, env: sealedEnv })).toBe("not_found"); | |
| 64 | expect(appGate(app, undefined, { app: record, env: sealedEnv })).toBe("not_found"); | |
| 65 | }); | |
| 66 | ||
| 67 | test("a writer gets in", () => { | |
| 68 | expect(appGate(app, owner, { app: record, env: sealedEnv })).toBeUndefined(); | |
| 69 | }); | |
| 70 | ||
| 71 | test("production and an unsealed pull's preview stay open to a reader", () => { | |
| 72 | expect(appGate(app, stranger, { app: record, env: "production" })).toBeUndefined(); | |
| 73 | expect(appGate(app, stranger, { app: record, env: openEnv })).toBeUndefined(); | |
| 74 | }); | |
| 75 | ||
| 76 | test("the shared lookup names only a sealed pull's preview", () => { | |
| 77 | expect(sealedPreviewPull(app.db, repoId, sealedEnv)?.sealed).toBe(true); | |
| 78 | expect(sealedPreviewPull(app.db, repoId, openEnv)).toBeUndefined(); | |
| 79 | expect(sealedPreviewPull(app.db, repoId, "production")).toBeUndefined(); | |
| 80 | }); | |
| 81 | }); |
new above
offline
Nit: stray trailing blank line at end of file; drop it.