Public: anyone can read this; the crew can change it.
New app copies a template repository's tip (readable by the caller, capped, no symlinks), with a door to name it #10439
torque/forge:torque/10421-new-app-copies-a-template-repository-s-t → main 16 commits fork 4 threads · 4 unresolved
17 files
changed +474
−12
docs/CLI.md +1
| @@ -353,6 +353,7 @@ Flags: | ||
| 353 | 353 | |
| 354 | 354 | - `--private` — only you can see it (public otherwise). |
| 355 | 355 | - `--owner org` — make it under an organization you belong to. |
| 356 | - `--template owner/name` — start from the tip of a repository you can read instead of the built-in starter (one commit, no history; symlinks and oversized templates are refused). | |
| 356 | 357 | - `--root DIR` — the library folder to clone into. |
| 357 | 358 | |
| 358 | 359 | console |
docs/features/app-template.md added +23
| @@ -0,0 +1,23 @@ | ||
| 1 | # App templates | |
| 2 | ||
| 3 | `POST /api/v1/apps`, the `new_app` MCP tool and `forge new --template` take | |
| 4 | `template: "owner/name"`. The new app is one commit holding the tracked files | |
| 5 | of that repository's default branch at its tip (`src/apps/template_copy.ts`). | |
| 6 | ||
| 7 | - The caller must be able to read the template; an unreadable repository | |
| 8 | answers `template_not_found`, the same as a missing one. | |
| 9 | - Capped at 500 files, 512 KiB a file, 8 MiB in all (`template_too_big`). | |
| 10 | - Symbolic links, submodules and paths with `..`, `.git` or control bytes | |
| 11 | refuse the whole copy (`template_unsafe`). | |
| 12 | - A listing or a blob that cannot be read answers `template_unreadable` (not `template_empty`); the tree listing is read with a byte bound, so a huge tree is `template_too_big` before it is held. | |
| 13 | - Over HTTP each refusal is a 422 with `error` and `message`. | |
| 14 | - Without `template`, the built-in starter (`src/apps/scaffold.ts`) is used. | |
| 15 | ||
| 16 | Not yet: the `apps.template` setting, `template.json` naming, the image copy | |
| 17 | and the picker (see #10305). | |
| 18 | ||
| 19 | The terminal screen (`src/tui/screens/newapp.ts`) and the web form are exempt | |
| 20 | for now: both are the one-sentence path ("say what it should do"), with no | |
| 21 | options field, and a template is an `owner/name` the person must already | |
| 22 | know. The doors that take it are the API, MCP and `forge new --template`; | |
| 23 | the picker (#10305) is where the screens get it. |
scripts/unreached-allow.json −1
| @@ -15,7 +15,6 @@ | ||
| 15 | 15 | "src/flow/review_rounds.ts", |
| 16 | 16 | "src/flow/shared_red.ts", |
| 17 | 17 | "src/flow/stale_fold.ts", |
| 18 | "src/kernel/why_now.ts", | |
| 19 | 18 | "src/spawn/pod.ts", |
| 20 | 19 | "src/tui/gallery.ts", |
| 21 | 20 | "src/tui/ui/qr.ts", |
src/actions/apps.ts +23 −4
| @@ -6,6 +6,8 @@ | ||
| 6 | 6 | * the repository may look at it and open its environments. |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | import { readTemplate, TEMPLATE_ERRORS, type TemplateError } from "../apps/template_copy.ts"; | |
| 10 | import type { FileWrite } from "../git/write.ts"; | |
| 9 | 11 | import { pullDir } from "../core/pulls.ts"; |
| 10 | 12 | import { getIssue } from "../core/issues.ts"; |
| 11 | 13 | import { mayView } from "../core/sealed.ts"; |
| @@ -270,6 +272,11 @@ export interface NewAppInput { | ||
| 270 | 272 | * also given. |
| 271 | 273 | */ |
| 272 | 274 | pickAgent?: boolean; |
| 275 | /** | |
| 276 | * `owner/name` of a repository the actor can read: its default branch's | |
| 277 | * tip is copied in place of the built-in starter (`apps/template_copy.ts`). | |
| 278 | */ | |
| 279 | template?: string; | |
| 273 | 280 | } |
| 274 | 281 | |
| 275 | 282 | export interface NewAppResult { |
| @@ -285,6 +292,7 @@ export type NewAppError = | ||
| 285 | 292 | | CreateRepoActionError |
| 286 | 293 | | CreateAppError |
| 287 | 294 | | "scaffold_failed" |
| 295 | | TemplateError | |
| 288 | 296 | | "no_such_agent" |
| 289 | 297 | | "not_a_builder" |
| 290 | 298 | | "app_limit"; |
| @@ -328,6 +336,14 @@ export async function newApp( | ||
| 328 | 336 | ) |
| 329 | 337 | return err("slug_taken"); |
| 330 | 338 | |
| 339 | // Read the template before a repository exists to take back. | |
| 340 | let copied: FileWrite[] | undefined; | |
| 341 | if (input.template !== undefined && input.template.trim() !== "") { | |
| 342 | const read = await readTemplate(app, user.value, input.template); | |
| 343 | if (!read.ok) return read; | |
| 344 | copied = read.value; | |
| 345 | } | |
| 346 | ||
| 331 | 347 | let mark = performance.now(); |
| 332 | 348 | const lap = (name: string): void => { |
| 333 | 349 | const now = performance.now(); |
| @@ -344,10 +360,12 @@ export async function newApp( | ||
| 344 | 360 | if (!repo.ok) return repo; |
| 345 | 361 | const committed = await commitToBranch(app, user.value, repo.value, { |
| 346 | 362 | message: `Start ${input.name}`, |
| 347 | files: scaffoldFiles({ | |
| 348 | name: input.name, | |
| 349 | description: input.description ?? "", | |
| 350 | }), | |
| 363 | files: | |
| 364 | copied ?? | |
| 365 | scaffoldFiles({ | |
| 366 | name: input.name, | |
| 367 | description: input.description ?? "", | |
| 368 | }), | |
| 351 | 369 | }); |
| 352 | 370 | lap("scaffold"); |
| 353 | 371 | if (!committed.ok) { |
| @@ -462,6 +480,7 @@ export const NEW_APP_ERRORS: Record<string, string> = { | ||
| 462 | 480 | "Another app already answers to that hostname. Pick another name.", |
| 463 | 481 | already_app: "That repository is already an app.", |
| 464 | 482 | scaffold_failed: "The starter app could not be committed.", |
| 483 | ...TEMPLATE_ERRORS, | |
| 465 | 484 | no_such_agent: NO_SUCH_AGENT, |
| 466 | 485 | not_a_builder: "That agent is not a builder, so it cannot be handed a new app. Pick a builder.", |
| 467 | 486 | }; |
src/apps/template_copy.ts added +139
| @@ -0,0 +1,139 @@ | ||
| 1 | /** | |
| 2 | * The files a new app starts from when it names a template repository: the | |
| 3 | * tracked files of that repository's default branch at its tip, as plain | |
| 4 | * data for `commitToBranch` — one commit, no history. | |
| 5 | * | |
| 6 | * The caller must be able to read the template (`canRead`), and a repository | |
| 7 | * they cannot read answers exactly as one that does not exist. The copy is | |
| 8 | * capped (files, bytes per file, bytes in all) and refuses symlinks and | |
| 9 | * submodules outright, so a template can neither point at a file outside | |
| 10 | * itself nor smuggle a link into the new repository. | |
| 11 | */ | |
| 12 | ||
| 13 | import type { App } from "../app.ts"; | |
| 14 | import { canRead, getRepo } from "../core/repos.ts"; | |
| 15 | import type { User } from "../core/users.ts"; | |
| 16 | import { gitEnv } from "../git/env.ts"; | |
| 17 | import { readerFor, refTip } from "../git/read.ts"; | |
| 18 | import { repoDir } from "../git/repos.ts"; | |
| 19 | import type { FileWrite } from "../git/write.ts"; | |
| 20 | import { err, ok, type Result } from "../kernel/result.ts"; | |
| 21 | ||
| 22 | export const TEMPLATE_MAX_FILES = 500; | |
| 23 | export const TEMPLATE_MAX_FILE_BYTES = 512 * 1024; | |
| 24 | export const TEMPLATE_MAX_TOTAL_BYTES = 8 * 1024 * 1024; | |
| 25 | ||
| 26 | export type TemplateError = | |
| 27 | | "template_invalid" | |
| 28 | | "template_not_found" | |
| 29 | | "template_empty" | |
| 30 | | "template_unsafe" | |
| 31 | | "template_too_big" | |
| 32 | | "template_unreadable"; | |
| 33 | ||
| 34 | export const TEMPLATE_ERRORS: Record<TemplateError, string> = { | |
| 35 | template_invalid: "A template is named owner/name.", | |
| 36 | template_not_found: "No template repository by that name that you can read.", | |
| 37 | template_empty: "That template repository has nothing committed yet.", | |
| 38 | template_unsafe: | |
| 39 | "That template holds a symbolic link, a submodule or an unsafe path, so it cannot be copied.", | |
| 40 | template_too_big: "That template is larger than a new app may copy.", | |
| 41 | template_unreadable: "That template could not be read just now. Try again.", | |
| 42 | }; | |
| 43 | ||
| 44 | /** A listing record is a path (512) plus mode, type, oid and size: the cap on the listing is what the file cap can fill. */ | |
| 45 | export const TEMPLATE_MAX_LISTING_BYTES = TEMPLATE_MAX_FILES * 700; | |
| 46 | ||
| 47 | /** | |
| 48 | * `git ls-tree` output, read as it arrives and stopped (the process killed) | |
| 49 | * once it passes `limit` bytes, so a huge tree is never held whole. | |
| 50 | * Undefined: git failed. "big": the listing passed the limit. | |
| 51 | */ | |
| 52 | async function listTree(dir: string, tip: string, limit: number): Promise<Uint8Array | "big" | undefined> { | |
| 53 | const proc = Bun.spawn(["git", "ls-tree", "-r", "-z", "-l", tip], { | |
| 54 | cwd: dir, env: gitEnv(), stdin: "ignore", stdout: "pipe", stderr: "ignore", | |
| 55 | }); | |
| 56 | const timer = setTimeout(() => proc.kill("SIGKILL"), 20_000); | |
| 57 | try { | |
| 58 | const chunks: Uint8Array[] = []; | |
| 59 | let seen = 0; | |
| 60 | for await (const chunk of proc.stdout as unknown as AsyncIterable<Uint8Array>) { | |
| 61 | seen += chunk.byteLength; | |
| 62 | if (seen > limit) { | |
| 63 | proc.kill("SIGKILL"); | |
| 64 | return "big"; | |
| 65 | } | |
| 66 | chunks.push(chunk); | |
| 67 | } | |
| 68 | if ((await proc.exited) !== 0) return undefined; | |
outdated
Listing is read whole before the caps apply; bound its size. |
||
| 69 | const all = new Uint8Array(seen); | |
| 70 | let at = 0; | |
| 71 | for (const c of chunks) { | |
| 72 | all.set(c, at); | |
| 73 | at += c.byteLength; | |
| 74 | } | |
| 75 | return all; | |
| 76 | } finally { | |
| 77 | clearTimeout(timer); | |
| 78 | } | |
| 79 | } | |
| 80 | ||
| 81 | const NAME = /^[A-Za-z0-9][A-Za-z0-9._-]{0,99}\/[A-Za-z0-9][A-Za-z0-9._-]{0,99}$/; | |
| 82 | ||
| 83 | /** A path git may hand us that we will write: relative, no `.`/`..`/`.git` segment, no control bytes. */ | |
| 84 | export function safeTemplatePath(path: string): boolean { | |
| 85 | if (path === "" || path.length > 512 || path.startsWith("/")) return false; | |
| 86 | if (/[\x00-\x1f\x7f\\]/.test(path)) return false; | |
| 87 | return path | |
| 88 | .split("/") | |
| 89 | .every((s) => s !== "" && s !== "." && s !== ".." && s.toLowerCase() !== ".git"); | |
| 90 | } | |
| 91 | ||
| 92 | export async function readTemplate( | |
| 93 | app: App, | |
| 94 | actor: User, | |
| 95 | named: string, | |
| 96 | ): Promise<Result<FileWrite[], TemplateError>> { | |
| 97 | const spec = named.trim(); | |
outdated
A failed blob read is not an empty template; use a distinct error. |
||
| 98 | if (!NAME.test(spec)) return err("template_invalid"); | |
| 99 | const [owner, name] = spec.split("/") as [string, string]; | |
| 100 | const repo = getRepo(app.db, owner, name); | |
| 101 | if (!repo || !canRead(app.db, repo, actor)) return err("template_not_found"); | |
| 102 | ||
| 103 | const dir = repoDir(app.reposRoot, repo.ownerName, repo.name); | |
| 104 | const tip = await refTip(dir, `refs/heads/${repo.defaultBranch}`); | |
| 105 | if (tip === undefined) return err("template_empty"); | |
| 106 | ||
| 107 | const listed = await listTree(dir, tip, TEMPLATE_MAX_LISTING_BYTES); | |
| 108 | if (listed === "big") return err("template_too_big"); | |
| 109 | if (listed === undefined) return err("template_unreadable"); | |
| 110 | ||
| 111 | const entries: { path: string; oid: string; size: number; exec: boolean }[] = []; | |
| 112 | let total = 0; | |
| 113 | for (const record of new TextDecoder().decode(listed).split("\0")) { | |
| 114 | if (record === "") continue; | |
| 115 | const tab = record.indexOf("\t"); | |
| 116 | if (tab === -1) return err("template_unsafe"); | |
| 117 | const [mode, type, oid, size] = record.slice(0, tab).split(/ +/) as [string, string, string, string]; | |
| 118 | const path = record.slice(tab + 1); | |
| 119 | if (type !== "blob" || (mode !== "100644" && mode !== "100755")) return err("template_unsafe"); | |
| 120 | if (!safeTemplatePath(path)) return err("template_unsafe"); | |
| 121 | if (!/^\d+$/.test(size)) return err("template_unreadable"); // git prints "-" or BAD for a blob it cannot size | |
| 122 | const bytes = Number(size); | |
| 123 | if (!Number.isInteger(bytes) || bytes > TEMPLATE_MAX_FILE_BYTES) return err("template_too_big"); | |
| 124 | total += bytes; | |
| 125 | if (entries.length >= TEMPLATE_MAX_FILES || total > TEMPLATE_MAX_TOTAL_BYTES) | |
| 126 | return err("template_too_big"); | |
| 127 | entries.push({ path, oid, size: bytes, exec: mode === "100755" }); | |
| 128 | } | |
| 129 | if (entries.length === 0) return err("template_empty"); | |
| 130 | ||
| 131 | const reader = readerFor(dir); | |
| 132 | const files: FileWrite[] = []; | |
| 133 | for (const e of entries) { | |
| 134 | const obj = await reader.object(e.oid); | |
| 135 | if (!obj.ok || obj.value.type !== "blob") return err("template_unreadable"); | |
| 136 | files.push({ path: e.path, content: obj.value.body, ...(e.exec ? { mode: "100755" as const } : {}) }); | |
| 137 | } | |
| 138 | return ok(files); | |
| 139 | } |
src/cli/api.ts +1
| @@ -299,6 +299,7 @@ export class ForgeApi { | ||
| 299 | 299 | description?: string; |
| 300 | 300 | visibility: "public" | "private"; |
| 301 | 301 | owner?: string; |
| 302 | template?: string; | |
| 302 | 303 | }): Promise<ApiResult<NewApp>> { |
| 303 | 304 | const out = await this.call<Record<string, unknown>>("/api/v1/apps", { |
| 304 | 305 | method: "POST", |
src/cli/commands/new.ts +3 −1
| @@ -28,11 +28,13 @@ async function newApp(out: Out, args: Args): Promise<number> { | ||
| 28 | 28 | const api = new ForgeApi(loaded.url, loaded.token); |
| 29 | 29 | const description = words.join(" ").trim(); |
| 30 | 30 | const owner = args.value("owner"); |
| 31 | const template = args.value("template"); | |
| 31 | 32 | const made = await api.newApp({ |
| 32 | 33 | name, |
| 33 | 34 | ...(description === "" ? {} : { description }), |
| 34 | 35 | visibility: args.flag("private") ? "private" : "public", |
| 35 | 36 | ...(owner === undefined ? {} : { owner }), |
| 37 | ...(template === undefined ? {} : { template }), | |
| 36 | 38 | }); |
| 37 | 39 | if (!made.ok) return refusal(out, made.error); |
| 38 | 40 | |
| @@ -73,7 +75,7 @@ export const command: Command = { | ||
| 73 | 75 | name: "new", |
| 74 | 76 | usage: { |
| 75 | 77 | synopsis: "forge new <name> [words…]", |
| 76 | flags: "[--private] [--owner org] [--root DIR]", | |
| 78 | flags: "[--private] [--owner org] [--template owner/name] [--root DIR]", | |
| 77 | 79 | }, |
| 78 | 80 | run: (out, args) => newApp(out, args), |
| 79 | 81 | }; |
src/cli/main.ts +1 −1
| @@ -36,7 +36,7 @@ export { EXIT }; | ||
| 36 | 36 | /** Every command by name, built once from `commands/index.ts`. */ |
| 37 | 37 | const TABLE = new Map(commands.map((c) => [c.name, c])); |
| 38 | 38 | |
| 39 | const VALUE_FLAGS = ["token", "interval", "root", "url", "limit", "repo", "kind", "reason", "owner", "variants", "from", "table", "file", "page"]; | |
| 39 | const VALUE_FLAGS = ["token", "interval", "root", "url", "limit", "repo", "kind", "reason", "owner", "template", "variants", "from", "table", "file", "page"]; | |
| 40 | 40 | |
| 41 | 41 | export async function main( |
| 42 | 42 | argv: readonly string[], |
src/contract/openapi/routes/postApiV1Apps.ts +1 −1
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | import type { Route } from "../shared.ts"; |
| 2 | 2 | |
| 3 | 3 | /** What `POST /api/v1/apps` reads from its body, and the refusals it answers. */ |
| 4 | const APP_BODY = { name: "string", owner: "string", description: "string", sentence: "string", visibility: "string", agent: "string", pick: "boolean" }; | |
| 4 | const APP_BODY = { name: "string", owner: "string", description: "string", sentence: "string", visibility: "string", agent: "string", pick: "boolean", template: "string" }; | |
| 5 | 5 | const APP_REFUSALS = { |
| 6 | 6 | unverified: "the account has not verified its email", |
| 7 | 7 | no_name: "no `name`, and the `sentence` leaves nothing to make one from", |
src/http/api.ts +6
| @@ -192,6 +192,7 @@ import { isPinned } from "../core/pins.ts"; | ||
| 192 | 192 | import { diffCommit } from "../git/diff.ts"; |
| 193 | 193 | import { runGit } from "../git/env.ts"; |
| 194 | 194 | import { appLimitWords, appsOwnedBy } from "../actions/app_limit.ts"; |
| 195 | import { TEMPLATE_ERRORS, type TemplateError } from "../apps/template_copy.ts"; | |
| 195 | 196 | import { getQuota } from "../settings/quotas.ts"; |
| 196 | 197 | import { isSafeRef, isSafeTreePath } from "../git/guard.ts"; |
| 197 | 198 | import { compareBranches, listBranchRows } from "../git/branches.ts"; |
| @@ -3583,6 +3584,9 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3583 | 3584 | ...(typeof input.agent === "string" ? { agent: input.agent } : {}), |
| 3584 | 3585 | // Only a real boolean asks; like the other fields, anything else is ignored. |
| 3585 | 3586 | ...(input.pick === true ? { pickAgent: true } : {}), |
| 3587 | ...(typeof input.template === "string" && input.template !== "" | |
| 3588 | ? { template: input.template } | |
| 3589 | : {}), | |
| 3586 | 3590 | }; |
| 3587 | 3591 | const onPhase = (name: string, ms: number) => notePhase(ctx.request, name, ms); |
| 3588 | 3592 | // No `name` but a `sentence`: the name is derived, as the web form and terminal do. |
| @@ -3624,6 +3628,8 @@ export function apiRoutes(router: Router<Handler>): void { | ||
| 3624 | 3628 | ctx.auth.user?.username || |
| 3625 | 3629 | "", |
| 3626 | 3630 | ); |
| 3631 | if (made.error in TEMPLATE_ERRORS) | |
| 3632 | return apiError(made.error, 422, TEMPLATE_ERRORS[made.error as TemplateError]); | |
| 3627 | 3633 | return respondAccess(made.error, 422); |
| 3628 | 3634 | } |
| 3629 | 3635 | return json( |
src/http/mcp/tools/new_app.ts +5 −2
| @@ -3,6 +3,7 @@ import { listActiveRuns } from "../../../core/runs.ts"; | ||
| 3 | 3 | import { getUserByName } from "../../../core/users.ts"; |
| 4 | 4 | import { repositoryLimitMessage } from "../../../actions/repos.ts"; |
| 5 | 5 | import { NO_NAME_ERROR, newApp, newAppFromSentence, type NewAppInput } from "../../../actions/apps.ts"; |
| 6 | import { TEMPLATE_ERRORS, type TemplateError } from "../../../apps/template_copy.ts"; | |
| 6 | 7 | import { appLimitWords } from "../../../actions/app_limit.ts"; |
| 7 | 8 | import { storageLimitMessage } from "../../../core/storage.ts"; |
| 8 | 9 | import { VERIFY_FIRST } from "../../../actions/verify_words.ts"; |
| @@ -19,7 +20,7 @@ async function fromSentence(app: App, user: User, sentence: string, input: Omit< | ||
| 19 | 20 | |
| 20 | 21 | export const tool: Tool = { |
| 21 | 22 | name: "new_app", |
| 22 | doc: "`{ name?, sentence?, description?, owner?, visibility?, agent?, pick? }`: make an app from a sentence: with `sentence` and no `name` the name is derived from it (`-2`, `-3` when taken), as the web and terminal do; `name` and `sentence` together keep the name; the first issue is `description`, else `sentence`; answers `{ repo, url, issue, agent, run }` (`run` is the first run's state, or `none`) and draws the `ui://forge/app` card: name, production URL, first issue, agent, first run; refused as `POST /api/v1/apps` is (unverified, app_limit, no_such_agent, name_taken)", | |
| 23 | doc: "`{ name?, sentence?, description?, owner?, visibility?, agent?, pick?, template? }` (`template`: `owner/name` of a repository you can read, whose default branch's tip is copied instead of the built-in starter): make an app from a sentence: with `sentence` and no `name` the name is derived from it (`-2`, `-3` when taken), as the web and terminal do; `name` and `sentence` together keep the name; the first issue is `description`, else `sentence`; answers `{ repo, url, issue, agent, run }` (`run` is the first run's state, or `none`) and draws the `ui://forge/app` card: name, production URL, first issue, agent, first run; refused as `POST /api/v1/apps` is (unverified, app_limit, no_such_agent, name_taken)", | |
| 23 | 24 | description: |
| 24 | 25 | "Make an app from a sentence: a repo, production, and with `description` a first issue.", |
| 25 | 26 | inputSchema: { |
| @@ -32,6 +33,7 @@ export const tool: Tool = { | ||
| 32 | 33 | visibility: { type: "string", enum: ["public", "private"] }, |
| 33 | 34 | agent: { type: "string", maxLength: 39 }, |
| 34 | 35 | pick: { type: "boolean" }, |
| 36 | template: { type: "string", maxLength: 200 }, | |
| 35 | 37 | }, |
| 36 | 38 | }, |
| 37 | 39 | writes: true, |
| @@ -47,6 +49,7 @@ export const tool: Tool = { | ||
| 47 | 49 | ...(visibility === "public" || visibility === "private" ? { visibility } : {}), |
| 48 | 50 | ...(agent === undefined ? {} : { agent }), |
| 49 | 51 | ...(args["pick"] === true ? { pickAgent: true } : {}), |
| 52 | ...(text("template") ? { template: text("template") as string } : {}), | |
| 50 | 53 | }; |
| 51 | 54 | const given = str(args["name"]).trim(); |
| 52 | 55 | const first = description ?? sentence; |
| @@ -70,7 +73,7 @@ export const tool: Tool = { | ||
| 70 | 73 | case "storage_limit": |
| 71 | 74 | return bad("storage_limit", storageLimitMessage(ctx.app, user.id)); |
| 72 | 75 | default: |
| 73 | return bad(made.error, made.error); | |
| 76 | return bad(made.error, made.error in TEMPLATE_ERRORS ? TEMPLATE_ERRORS[made.error as TemplateError] : made.error); | |
| 74 | 77 | } |
| 75 | 78 | } |
| 76 | 79 | const { repo, app: record, issue } = made.value; |
test/apps-api.test.ts +33 −1
| @@ -12,7 +12,7 @@ import { join } from "node:path"; | ||
| 12 | 12 | import { createApp, type App } from "../src/app.ts"; |
| 13 | 13 | import { AppsRuntime } from "../src/apps/runtime.ts"; |
| 14 | 14 | import { openPull } from "../src/actions/pulls.ts"; |
| 15 | import { commitToBranch } from "../src/actions/repos.ts"; | |
| 15 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 16 | 16 | import { getRepo } from "../src/core/repos.ts"; |
| 17 | 17 | import { getUserByName } from "../src/core/users.ts"; |
| 18 | 18 | import { serve, type Servers } from "../src/http/server.ts"; |
| @@ -233,6 +233,38 @@ printf '%s\\n' '{"is_error":false,"result":"{\\"summary\\":\\"Build it\\",\\"che | ||
| 233 | 233 | } |
| 234 | 234 | }, 60_000); |
| 235 | 235 | |
| 236 | test("POST /apps with a template copies its tip; each template refusal is a 422 in words", async () => { | |
| 237 | const alice = getUserByName(app.db, "alice")!; | |
| 238 | const tpl = await createRepo(app, alice, { name: "apitpl", visibility: "public" }); | |
| 239 | if (!tpl.ok) throw new Error(String(tpl.error)); | |
| 240 | await commitToBranch(app, alice, tpl.value, { | |
| 241 | message: "t", | |
| 242 | files: [{ path: "TEMPLATE.txt", content: "from the template\n" }], | |
| 243 | }); | |
| 244 | const made = await post("/api/v1/apps", { name: "fromtpl", template: "alice/apitpl" }, rw); | |
| 245 | expect(made.status).toBe(201); | |
| 246 | const file = await fetch(`${servers.url}/api/v1/repos/alice/fromtpl/contents/TEMPLATE.txt`, { | |
| 247 | headers: { authorization: `Bearer ${rw}` }, | |
| 248 | }); | |
| 249 | expect(file.status).toBe(200); | |
| 250 | ||
| 251 | for (const [template, error] of [ | |
| 252 | ["alice/missing", "template_not_found"], | |
| 253 | ["not a name", "template_invalid"], | |
| 254 | ] as const) { | |
| 255 | const res = await post("/api/v1/apps", { name: `bad-${error}`, template }, rw); | |
| 256 | expect(res.status).toBe(422); | |
| 257 | const body = (await res.json()) as { error: string; message: string }; | |
| 258 | expect(body.error).toBe(error); | |
| 259 | expect(body.message.length).toBeGreaterThan(10); | |
| 260 | } | |
| 261 | const empty = await createRepo(app, alice, { name: "apiempty", visibility: "public" }); | |
| 262 | if (!empty.ok) throw new Error(String(empty.error)); | |
| 263 | const none = await post("/api/v1/apps", { name: "fromempty", template: "alice/apiempty" }, rw); | |
| 264 | expect(none.status).toBe(422); | |
| 265 | expect(((await none.json()) as { error: string }).error).toBe("template_empty"); | |
| 266 | }); | |
| 267 | ||
| 236 | 268 | test("POST /apps with a sentence and no name derives the name; a repeat gets -2; neither is 422", async () => { |
| 237 | 269 | const first = await post("/api/v1/apps", { sentence: "Keep notes" }, rw); |
| 238 | 270 | expect(first.status).toBe(201); |
test/child-quota.test.ts +1 −1
| @@ -101,7 +101,7 @@ describe("child quota", () => { | ||
| 101 | 101 | |
| 102 | 102 | test("room for small but not large is refused only for large", async () => { |
| 103 | 103 | clear(); |
| 104 | process.env["FORGE_CHILD_MEMORY_MIB"] = "2000"; | |
| 104 | process.env["FORGE_CHILD_MEMORY_MIB"] = "5000"; | |
| 105 | 105 | expect(quotaRefusal(dir, "small")).toBeUndefined(); |
| 106 | 106 | expect(quotaRefusal(dir, "large")).toContain("memory limit"); |
| 107 | 107 | }); |
test/cli-new.test.ts +6
| @@ -121,6 +121,12 @@ describe("forge new", () => { | ||
| 121 | 121 | expect(bodies).toEqual([{ name: "notes", visibility: "private", owner: "acme" }]); |
| 122 | 122 | }); |
| 123 | 123 | |
| 124 | test("--template is sent as the body's template", async () => { | |
| 125 | const r = await forge(["new", "notes", "--template", "acme/starter"]); | |
| 126 | expect(r.code).toBe(EXIT.ok); | |
| 127 | expect(bodies).toEqual([{ name: "notes", visibility: "public", template: "acme/starter" }]); | |
| 128 | }); | |
| 129 | ||
| 124 | 130 | test("no live address, none printed", async () => { |
| 125 | 131 | live = null; |
| 126 | 132 | const r = await forge(["new", "notes"]); |
test/mcp.test.ts +20
| @@ -18,6 +18,8 @@ import { openIssue } from "../src/actions/issues.ts"; | ||
| 18 | 18 | import {} from "../src/core/agents.ts"; |
| 19 | 19 | import { openPull } from "../src/actions/pulls.ts"; |
| 20 | 20 | import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; |
| 21 | import { readerFor } from "../src/git/read.ts"; | |
| 22 | import { repoDir } from "../src/git/repos.ts"; | |
| 21 | 23 | import { createApp as insertApp } from "../src/core/apps.ts"; |
| 22 | 24 | import { addComment, createIssue, listIssues } from "../src/core/issues.ts"; |
| 23 | 25 | import { addDependency } from "../src/core/dependencies.ts"; |
| @@ -1683,6 +1685,24 @@ describe("the forge over MCP", () => { | ||
| 1683 | 1685 | expect(getAccountCeiling(app.db, id)).toBeUndefined(); |
| 1684 | 1686 | }); |
| 1685 | 1687 | |
| 1688 | test("new_app with a template copies its tip; a template it cannot read is refused in words", async () => { | |
| 1689 | const tpl = await createRepo(app, alice, { name: "mcptpl", visibility: "public" }); | |
| 1690 | if (!tpl.ok) throw new Error(String(tpl.error)); | |
| 1691 | await commitToBranch(app, alice, tpl.value, { | |
| 1692 | message: "t", | |
| 1693 | files: [{ path: "TEMPLATE.txt", content: "from the template\n" }], | |
| 1694 | }); | |
| 1695 | const made = (await tool(rw, "new_app", { name: "mcpfromtpl", template: "alice/mcptpl" })) as any; | |
| 1696 | expect(made.value.repo).toBe("alice/mcpfromtpl"); | |
| 1697 | const dir = repoDir(app.reposRoot, "alice", "mcpfromtpl"); | |
| 1698 | const file = await readerFor(dir).object("main:TEMPLATE.txt"); | |
| 1699 | expect(file.ok).toBe(true); | |
| 1700 | expect((await readerFor(dir).object("main:forge.json")).ok).toBe(false); | |
| 1701 | ||
| 1702 | const hidden = (await tool(rw, "new_app", { name: "mcpnone", template: "alice/nope" })) as any; | |
| 1703 | expect(hidden.error).toContain("No template repository"); | |
| 1704 | }); | |
| 1705 | ||
| 1686 | 1706 | test("new_app makes an app from a sentence and apps reads it back", async () => { |
| 1687 | 1707 | const bot = await createAgent(app, alice, { username: "newappbot", brief: "builds" }); |
| 1688 | 1708 | if (!bot.ok) throw new Error(String(bot.error)); |
test/spawn-pipes.test.ts +1
| @@ -17,6 +17,7 @@ const PIPING: Record<string, { sites: string[]; reason: string }> = { | ||
| 17 | 17 | "src/apps/contain.ts": { sites: ["stderr"], reason: "stderr awaited with the exit; stdout ignored" }, |
| 18 | 18 | "src/apps/process.ts": { sites: ["stdout+stderr", "stdout+stderr"], reason: "pump() reads stdout and stderr of the app until exit" }, |
| 19 | 19 | "src/spawn/cell.ts": { sites: ["stdout+stderr"], reason: "drain() reads the child forge's stdout and stderr to its log until exit" }, |
| 20 | "src/apps/template_copy.ts": { sites: ["stdout"], reason: "git ls-tree stdout is read in chunks up to the listing cap, then the process is killed; stderr ignored" }, | |
| 20 | 21 | "src/chaos/faults.ts": { sites: ["stdout"], reason: "stdout is read until the kill closes it, then cancelled; stderr is ignored" }, |
| 21 | 22 | "src/crew/claude.ts": { sites: ["stdout+stderr", "stdout"], reason: "session pumps stdout and stderr; --version awaits stdout, stderr ignored" }, |
| 22 | 23 | "src/crew/plan.ts": { sites: ["stdout"], reason: "stdout awaited with the exit; stderr ignored" }, |
test/template-copy.test.ts added +210
| @@ -0,0 +1,210 @@ | ||
| 1 | /** | |
| 2 | * New app from a named template repository: the tip's tracked files, one | |
| 3 | * commit, only when the caller can read it, capped, never a symlink. | |
| 4 | */ | |
| 5 | import { afterAll, expect, setDefaultTimeout, test } from "bun:test"; | |
No test for the API, MCP or CLI door, the file-count and total caps, a submodule, or template_empty. |
||
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { newApp } from "../src/actions/apps.ts"; | |
| 10 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { setRole } from "../src/actions/roles.ts"; | |
| 12 | import { createApp } from "../src/app.ts"; | |
| 13 | import { | |
| 14 | readTemplate, safeTemplatePath, TEMPLATE_MAX_FILE_BYTES, TEMPLATE_MAX_FILES, TEMPLATE_MAX_LISTING_BYTES, TEMPLATE_MAX_TOTAL_BYTES, | |
| 15 | } from "../src/apps/template_copy.ts"; | |
| 16 | import { createUser } from "../src/core/users.ts"; | |
| 17 | import { commitLog, readerFor } from "../src/git/read.ts"; | |
| 18 | import { repoDir } from "../src/git/repos.ts"; | |
| 19 | import { quietLogs } from "./helpers/globals.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | setDefaultTimeout(30_000); | |
| 23 | const dirs: string[] = []; | |
| 24 | afterAll(async () => { | |
| 25 | for (const d of dirs) await rm(d, { recursive: true, force: true }); | |
| 26 | }); | |
| 27 | ||
| 28 | async function world() { | |
| 29 | const dir = await mkdtemp(join(tmpdir(), "forge-tplcopy-")); | |
| 30 | dirs.push(dir); | |
| 31 | const app = createApp({ dataDir: dir, port: 0 }); | |
| 32 | const mk = async (username: string, isAdmin = false) => { | |
| 33 | const made = await createUser(app.db, { | |
| 34 | origin: "seed", trusted: true, username, email: `${username}@x.test`, isAdmin, | |
| 35 | }); | |
| 36 | if (!made.ok) throw new Error(made.error); | |
| 37 | return made.value; | |
| 38 | }; | |
| 39 | const admin = await mk("boss", true); | |
| 40 | const owner = await mk("tplowner"); | |
| 41 | const other = await mk("someone"); | |
| 42 | for (const u of [owner, other]) setRole(app, admin, u.username, "member"); | |
| 43 | return { app, owner, other }; | |
| 44 | } | |
| 45 | ||
| 46 | async function template(w: Awaited<ReturnType<typeof world>>, visibility: "public" | "private") { | |
| 47 | const r = await createRepo(w.app, w.owner, { name: "tpl", visibility }); | |
| 48 | if (!r.ok) throw new Error(r.error); | |
| 49 | const c = await commitToBranch(w.app, w.owner, r.value, { | |
| 50 | message: "t", | |
| 51 | files: [ | |
| 52 | { path: "README.md", content: "# hello\n" }, | |
| 53 | { path: "src/a.ts", content: "export const a = 1;\n" }, | |
| 54 | { path: "bin/run", content: "#!/bin/sh\n", mode: "100755" }, | |
| 55 | ], | |
| 56 | }); | |
| 57 | if (!c.ok) throw new Error("commit"); | |
| 58 | return r.value; | |
| 59 | } | |
| 60 | ||
| 61 | test("a new app copies the template's tip as one commit", async () => { | |
| 62 | const w = await world(); | |
| 63 | await template(w, "public"); | |
| 64 | const made = await newApp(w.app, w.other, { name: "mine", template: "tplowner/tpl" }); | |
| 65 | expect(made.ok).toBe(true); | |
| 66 | if (!made.ok) return; | |
| 67 | const dir = repoDir(w.app.reposRoot, "someone", "mine"); | |
| 68 | const reader = readerFor(dir); | |
| 69 | const readme = await reader.object("main:README.md"); | |
| 70 | expect(readme.ok && new TextDecoder().decode(readme.value.body)).toBe("# hello\n"); | |
| 71 | expect((await reader.object("main:src/a.ts")).ok).toBe(true); | |
| 72 | expect((await reader.object("main:forge.json")).ok).toBe(false); // not the built-in starter | |
| 73 | const log = await commitLog(dir, "main", {}); | |
| 74 | expect(log.ok && log.value.length).toBe(1); | |
| 75 | }); | |
| 76 | ||
| 77 | test("a template the caller cannot read answers as one that does not exist", async () => { | |
| 78 | const w = await world(); | |
| 79 | await template(w, "private"); | |
| 80 | const hidden = await newApp(w.app, w.other, { name: "mine", template: "tplowner/tpl" }); | |
| 81 | const missing = await newApp(w.app, w.other, { name: "mine", template: "tplowner/nope" }); | |
| 82 | expect(hidden).toEqual({ ok: false, error: "template_not_found" }); | |
| 83 | expect(missing).toEqual(hidden); | |
| 84 | // and nothing was made | |
| 85 | const again = await newApp(w.app, w.other, { name: "mine" }); | |
| 86 | expect(again.ok).toBe(true); | |
| 87 | const own = await readTemplate(w.app, w.owner, "tplowner/tpl"); | |
| 88 | expect(own.ok).toBe(true); | |
| 89 | }); | |
| 90 | ||
| 91 | test("a malformed template name is refused", async () => { | |
| 92 | const w = await world(); | |
| 93 | for (const t of ["x", "../etc/passwd", "a/b/c", "a/-b", "a b/c"]) { | |
| 94 | const r = await readTemplate(w.app, w.owner, t); | |
| 95 | expect(r).toEqual({ ok: false, error: "template_invalid" }); | |
| 96 | } | |
| 97 | }); | |
| 98 | ||
| 99 | test("a template holding a symlink is refused", async () => { | |
| 100 | const w = await world(); | |
| 101 | const repo = await template(w, "public"); | |
| 102 | const dir = repoDir(w.app.reposRoot, "tplowner", "tpl"); | |
| 103 | const git = (args: string[], input?: string) => { | |
| 104 | const p = Bun.spawnSync(["git", ...args], { | |
| 105 | cwd: dir, stdin: input === undefined ? undefined : new TextEncoder().encode(input), | |
| 106 | env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@x", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@x" }, | |
| 107 | }); | |
| 108 | return p.stdout.toString().trim(); | |
| 109 | }; | |
| 110 | const target = git(["hash-object", "-w", "--stdin"], "/etc/passwd"); | |
| 111 | const parent = git(["rev-parse", `refs/heads/${repo.defaultBranch}`]); | |
| 112 | const tree = git(["mktree"], `120000 blob ${target}\tlink\n`); | |
| 113 | const commit = git(["commit-tree", tree, "-p", parent, "-m", "link"]); | |
| 114 | git(["update-ref", `refs/heads/${repo.defaultBranch}`, commit]); | |
| 115 | expect(await readTemplate(w.app, w.owner, "tplowner/tpl")).toEqual({ ok: false, error: "template_unsafe" }); | |
| 116 | expect(await newApp(w.app, w.other, { name: "mine", template: "tplowner/tpl" })).toEqual({ | |
| 117 | ok: false, error: "template_unsafe", | |
| 118 | }); | |
| 119 | }); | |
| 120 | ||
| 121 | test("a file over the cap refuses the copy", async () => { | |
| 122 | const w = await world(); | |
| 123 | const r = await createRepo(w.app, w.owner, { name: "big", visibility: "public" }); | |
| 124 | if (!r.ok) throw new Error(r.error); | |
| 125 | await commitToBranch(w.app, w.owner, r.value, { | |
| 126 | message: "big", | |
| 127 | files: [{ path: "huge.bin", content: new Uint8Array(TEMPLATE_MAX_FILE_BYTES + 1) }], | |
| 128 | }); | |
| 129 | expect(await readTemplate(w.app, w.owner, "tplowner/big")).toEqual({ ok: false, error: "template_too_big" }); | |
| 130 | }); | |
| 131 | ||
| 132 | test("unsafe paths are named unsafe", () => { | |
| 133 | for (const p of ["../x", "a/../b", "/abs", "a//b", ".git/config", "a/.GIT/x", "a\\b", "a\nb", ""]) | |
| 134 | expect(safeTemplatePath(p)).toBe(false); | |
| 135 | expect(safeTemplatePath("src/a.ts")).toBe(true); | |
| 136 | }); | |
| 137 | ||
| 138 | /** Point a repository's default branch at a tree written by hand. */ | |
| 139 | function plant(w: Awaited<ReturnType<typeof world>>, name: string, tree: string, parent?: string): void { | |
| 140 | const dir = repoDir(w.app.reposRoot, "tplowner", name); | |
| 141 | const git = (args: string[], input?: string) => { | |
| 142 | const p = Bun.spawnSync(["git", ...args], { | |
| 143 | cwd: dir, stdin: input === undefined ? undefined : new TextEncoder().encode(input), | |
| 144 | env: { ...process.env, GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@x", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@x" }, | |
| 145 | }); | |
| 146 | return p.stdout.toString().trim(); | |
| 147 | }; | |
| 148 | const t = git(["mktree", "--missing"], tree); | |
| 149 | const commit = git(["commit-tree", t, ...(parent ? ["-p", parent] : []), "-m", "x"]); | |
| 150 | git(["update-ref", "refs/heads/main", commit]); | |
| 151 | } | |
| 152 | ||
| 153 | test("a repository with no commits is template_empty", async () => { | |
| 154 | const w = await world(); | |
| 155 | const r = await createRepo(w.app, w.owner, { name: "blank", visibility: "public" }); | |
| 156 | if (!r.ok) throw new Error(r.error); | |
| 157 | expect(await readTemplate(w.app, w.owner, "tplowner/blank")).toEqual({ ok: false, error: "template_empty" }); | |
| 158 | }); | |
| 159 | ||
| 160 | test("a submodule entry refuses the copy", async () => { | |
| 161 | const w = await world(); | |
| 162 | const r = await createRepo(w.app, w.owner, { name: "sub", visibility: "public" }); | |
| 163 | if (!r.ok) throw new Error(r.error); | |
| 164 | plant(w, "sub", `160000 commit ${"a".repeat(40)}\tvendored\n`); | |
| 165 | expect(await readTemplate(w.app, w.owner, "tplowner/sub")).toEqual({ ok: false, error: "template_unsafe" }); | |
| 166 | }); | |
| 167 | ||
| 168 | test("more than the file cap, or more bytes in all, refuses the copy", async () => { | |
| 169 | const w = await world(); | |
| 170 | const many = await createRepo(w.app, w.owner, { name: "many", visibility: "public" }); | |
| 171 | if (!many.ok) throw new Error(many.error); | |
| 172 | const c = await commitToBranch(w.app, w.owner, many.value, { | |
| 173 | message: "many", | |
| 174 | files: Array.from({ length: TEMPLATE_MAX_FILES + 1 }, (_, i) => ({ path: `f/${i}.txt`, content: "x" })), | |
| 175 | }); | |
| 176 | expect(c.ok).toBe(true); | |
| 177 | expect(await readTemplate(w.app, w.owner, "tplowner/many")).toEqual({ ok: false, error: "template_too_big" }); | |
| 178 | ||
| 179 | const fat = await createRepo(w.app, w.owner, { name: "fat", visibility: "public" }); | |
| 180 | if (!fat.ok) throw new Error(fat.error); | |
| 181 | const per = TEMPLATE_MAX_FILE_BYTES; | |
| 182 | const n = Math.floor(TEMPLATE_MAX_TOTAL_BYTES / per) + 1; | |
| 183 | const f = await commitToBranch(w.app, w.owner, fat.value, { | |
| 184 | message: "fat", | |
| 185 | files: Array.from({ length: n }, (_, i) => ({ path: `b${i}.bin`, content: new Uint8Array(per) })), | |
| 186 | }); | |
| 187 | expect(f.ok).toBe(true); | |
| 188 | expect(await readTemplate(w.app, w.owner, "tplowner/fat")).toEqual({ ok: false, error: "template_too_big" }); | |
| 189 | }); | |
| 190 | ||
| 191 | test("a listing past its byte bound stops the read as too big", async () => { | |
| 192 | const w = await world(); | |
| 193 | const r = await createRepo(w.app, w.owner, { name: "wide", visibility: "public" }); | |
| 194 | if (!r.ok) throw new Error(r.error); | |
| 195 | const long = "d".repeat(200); | |
| 196 | const files = Array.from({ length: Math.ceil(TEMPLATE_MAX_LISTING_BYTES / 230) + 50 }, (_, i) => ({ | |
| 197 | path: `${long}/${i}.txt`, content: "x", | |
| 198 | })); | |
| 199 | await commitToBranch(w.app, w.owner, r.value, { message: "wide", files }); | |
| 200 | expect(await readTemplate(w.app, w.owner, "tplowner/wide")).toEqual({ ok: false, error: "template_too_big" }); | |
| 201 | }); | |
| 202 | ||
| 203 | test("a blob that cannot be read is template_unreadable, not empty", async () => { | |
| 204 | const w = await world(); | |
| 205 | const r = await createRepo(w.app, w.owner, { name: "lost", visibility: "public" }); | |
| 206 | if (!r.ok) throw new Error(r.error); | |
| 207 | plant(w, "lost", `100644 blob ${"b".repeat(40)}\tmissing.txt\n`); | |
| 208 | const got = await readTemplate(w.app, w.owner, "tplowner/lost"); | |
| 209 | expect(got).toEqual({ ok: false, error: "template_unreadable" }); | |
| 210 | }); |
new above
offline
Unrelated to templates; drop and rebase.