Public: anyone can read this; the crew can change it.

A sealed pull's diff and files answer whoever may see it, and 404 to everyone else #15015

Merged opened this

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

Merged

mason/forge:mason/15008-a-sealed-pull-s-diff-and-files-answer-wh → main 9 commits fork

  • Merged. mason/15008-a-sealed-pull-s-diff-and-files-answer-wh was merged into main as a6fbc71.

Review

Sign in to review this change.
11 files changed +314 −5
src/contract/openapi/routes/getApiV1ReposOwnerNamePullsNumberDiff.ts added +11
@@ -0,0 +1,11 @@
1 import { REPO, type Route } from "../shared.ts";
2
3 export const route: Route = {
4 method: "get",
5 path: `${REPO}/pulls/{number}/diff`,
6 summary: "A pull's diff as text",
7 detail: "Unified diff, text/plain; 413 over 2 MiB",
8 auth: false,
9 role: "anonymous",
10 refuses: [404],
11 };
src/contract/openapi/routes/getApiV1ReposOwnerNamePullsNumberFiles.ts added +11
@@ -0,0 +1,11 @@
1 import { REPO, type Route } from "../shared.ts";
2
3 export const route: Route = {
4 method: "get",
5 path: `${REPO}/pulls/{number}/files`,
6 summary: "The files a pull changes",
7 detail: "Each changed file: path, status, additions, deletions",
8 auth: false,
9 role: "anonymous",
10 refuses: [404],
11 };
src/contract/openapi/routes/index.ts +2
@@ -190,6 +190,8 @@ export { route as getApiV1ReposOwnerNamePrompts } from "./getApiV1ReposOwnerName
190190 export { route as getApiV1ReposOwnerNamePulls } from "./getApiV1ReposOwnerNamePulls.ts";
191191 export { route as getApiV1ReposOwnerNamePullsNumber } from "./getApiV1ReposOwnerNamePullsNumber.ts";
192192 export { route as getApiV1ReposOwnerNamePullsNumberChecks } from "./getApiV1ReposOwnerNamePullsNumberChecks.ts";
193 export { route as getApiV1ReposOwnerNamePullsNumberDiff } from "./getApiV1ReposOwnerNamePullsNumberDiff.ts";
194 export { route as getApiV1ReposOwnerNamePullsNumberFiles } from "./getApiV1ReposOwnerNamePullsNumberFiles.ts";
193195 export { route as getApiV1ReposOwnerNamePullsNumberLooks } from "./getApiV1ReposOwnerNamePullsNumberLooks.ts";
194196 export { route as getApiV1ReposOwnerNamePullsNumberReview } from "./getApiV1ReposOwnerNamePullsNumberReview.ts";
195197 export { route as getApiV1ReposOwnerNamePullsNumberReviewStates } from "./getApiV1ReposOwnerNamePullsNumberReviewStates.ts";
src/git/diff.ts +19
@@ -96,6 +96,25 @@ export async function diffRange(
9696 }
9797 }
9898
99 /**
100 * The unified text of `diffRange`, as git wrote it: what `.diff` serves. Past `maxBytes` of output
101 * git is killed and the failure's code is `OVER_CAP`.
102 */
103 export async function diffRangeText(
104 dir: string,
105 base: string,
106 head: string,
107 maxBytes?: number,
108 ): Promise<Result<string, GitFailure | "unsafe_rev">> {
109 if (!safeRange(base, head)) return err("unsafe_rev");
110 const raw = await runGitRaw(["diff", ...DIFF_FLAGS, base, head, "--"], {
111 cwd: dir,
112 timeoutMs: 60_000,
113 ...(maxBytes === undefined ? {} : { maxBytes }),
114 });
115 return raw.ok ? ok(new TextDecoder().decode(raw.value)) : raw;
116 }
117
99118 /** Range diffs git is producing right now, by memory key. */
100119 const running = new Map<string, Promise<Result<Diff, GitFailure>>>();
101120
src/http/api/app_pulls.ts +90 −3
@@ -3,6 +3,13 @@
33 * `GET …/app/pulls?limit=` answers the newest first, for whoever may read the
44 * repository; anyone else, or a repository with no app, gets 404. The rows are
55 * read through `pullRunLines` in `apps/pull.ts`.
6 *
7 * A pull's change as text and as a file list, for whoever may see the pull:
8 * `GET …/pulls/:number/diff`, its `…/files`, and the web `/:owner/:name/pulls/:number.diff`
9 * (`diffText`, routed in server.ts; the web route answers a session cookie, the API a bearer too).
10 * A sealed pull is read from the private fork its crew works in (`pullDir`); anyone who may not see
11 * it gets the same 404 as a pull that does not exist. One read, `readPatch` below, takes one cap
12 * and one set of refusals.
613 */
714
815 import { limitRefusal } from "../../contract/page_query.ts";
@@ -11,16 +18,94 @@ import { requireReader } from "../../actions/access.ts";
1118 import { PULL_RUNS_DEFAULT, PULL_RUNS_MAX, pullRunLines } from "../../apps/pull.ts";
1219 import { getAppByRepo } from "../../core/apps.ts";
1320 import { notePhase, takeSessionTime, timePhase } from "../../core/route_timing.ts";
14 import { loadRepo } from "../api.ts";
21 import { pullDir, pullStatus, type Pull } from "../../core/pulls.ts";
22 import type { Database } from "bun:sqlite";
23 import { diffRangeText, parseUnifiedDiff, type FileDiff } from "../../git/diff.ts";
24 import { OVER_CAP } from "../../git/env.ts";
25 import { mergeBase } from "../../git/merge.ts";
26 import { loadPull, loadRepo } from "../api.ts";
1527 import type { Ctx, Handler } from "../context.ts";
16 import { json, notFound } from "../respond.ts";
28 import { apiError, json, notFound, securityFloor, tryAgain } from "../respond.ts";
1729 import type { Router } from "../router.ts";
1830
19 /** `?limit=`: absent is the default, a whole number from 1 to 100 is itself, anything else is a 422. */
31 /** `?limit=`: absent is the default, a whole number from 1 to 100 is itself, else a 422. */
2032 function pullsLimit(ctx: Ctx): number | Response {
2133 return parseLimit(ctx.url.searchParams.get("limit"), PULL_RUNS_MAX, PULL_RUNS_DEFAULT) ?? limitRefusal(PULL_RUNS_MAX);
2234 }
2335
36 /** The most diff text one read takes: past it the read is refused, never cut short. */
37 export const PATCH_MAX_BYTES = 2 * 1024 * 1024;
38
39 type PatchRead =
40 | { readonly ok: true; readonly text: string }
41 | { readonly ok: false; readonly why: "too_large" | "unreadable" };
42
43 export async function readPatch(
44 app: { readonly db: Database; readonly reposRoot: string },
45 repo: { readonly ownerName: string; readonly name: string },
46 pull: Pull,
47 maxBytes: number = PATCH_MAX_BYTES,
48 ): Promise<PatchRead> {
49 try {
50 const dir = await pullDir(app.db, app.reposRoot, repo, pull);
51 const status = await pullStatus(dir, pull);
52 const from = (await mergeBase(dir, status.baseOid, status.headOid)) ?? status.baseOid;
53 const text = await diffRangeText(dir, from, status.headOid, maxBytes);
54 if (text.ok) return { ok: true, text: text.value };
55 return { ok: false, why: typeof text.error === "object" && text.error.code === OVER_CAP ? "too_large" : "unreadable" };
56 } catch {
57 return { ok: false, why: "unreadable" };
58 }
59 }
60
61 /** One line of the file list: what the files route answers and the terminal lists. */
62 interface PatchFile {
63 readonly path: string;
64 readonly status: FileDiff["status"];
65 readonly additions: number;
66 readonly deletions: number;
67 readonly binary: boolean;
68 }
69
70 function patchFiles(text: string): PatchFile[] {
71 return parseUnifiedDiff(text).files.map((f) => ({
72 path: f.path,
73 status: f.status,
74 additions: f.additions,
75 deletions: f.deletions,
76 binary: f.binary,
77 }));
78 }
79
80 /** The text git wrote for the pull's change, or the refusal to answer with. */
81 async function patchOf(ctx: Ctx): Promise<{ text: string } | { error: Response }> {
82 const found = loadPull(ctx);
83 if ("error" in found) return found;
84 const read = await readPatch(ctx.app, found.repo, found.pull);
85 if (read.ok) return { text: read.text };
86 if (read.why === "too_large")
87 return {
88 error: apiError("diff_too_large", 413, `the change is over ${PATCH_MAX_BYTES} bytes; read it from a clone`),
89 };
90 return { error: tryAgain("diff_unavailable", 503, "the change could not be read now; try again", 5) };
91 }
92
93 export const diffText: Handler = async (ctx) => {
94 const patch = await patchOf(ctx);
95 if ("error" in patch) return patch.error;
96 return securityFloor(
97 new Response(patch.text, {
98 headers: { "content-type": "text/plain; charset=utf-8", "cache-control": "private, no-cache", vary: "authorization, cookie" },
99 }),
100 );
101 };
102
103 const filesJson: Handler = async (ctx) => {
104 const patch = await patchOf(ctx);
105 if ("error" in patch) return patch.error;
106 return json(patchFiles(patch.text), 200, { "cache-control": "private, no-cache", vary: "authorization, cookie" });
107 };
108
24109 export function appPullsRoutes(router: Router<Handler>): void {
25110 router.add("GET", "/api/v1/repos/:owner/:name/app/pulls", (ctx) => {
26111 notePhase(ctx.request, "session", takeSessionTime(ctx.request));
@@ -35,4 +120,6 @@ export function appPullsRoutes(router: Router<Handler>): void {
35120 if (items === undefined) return notFound();
36121 return json({ items, next_cursor: null });
37122 });
123 router.add("GET", "/api/v1/repos/:owner/:name/pulls/:number/diff", diffText);
124 router.add("GET", "/api/v1/repos/:owner/:name/pulls/:number/files", filesJson);
38125 }
src/http/asset_miss.ts +1 −1
@@ -9,7 +9,7 @@
99 * the one place every response leaves through (`securityFloor`).
1010 */
1111
12 const FILE_EXTENSION = /\.(?:xml|atom|json|webmanifest|txt|md|js|css|map|png|svg|ico|wasm)$/;
12 const FILE_EXTENSION = /\.(?:diff|xml|atom|json|webmanifest|txt|md|js|css|map|png|svg|ico|wasm)$/;
1313 /** Machine surfaces that keep their own JSON miss; the one list server.ts shares. */
1414 export const MACHINE_PREFIX = /^\/(?:api|contract|mcp|oauth|\.well-known|metrics|health|healthz|readyz)(?:\/|$)/;
1515
src/http/server.ts +3
@@ -335,6 +335,7 @@ import { familyPage } from "../web/pages/family.ts";
335335 import { forgeLinkForm, forgeRevokeConfirm, forgeLinkRevoke, forgeLinkSubmit, forgeLinkUndo, forgeArchiveConfirm, forgeArchiveSubmit, forgeRestoreSubmit, forgeRemoveConfirm, forgeRemoveSubmit, forgeStopSubmit,
336336 forgeOwnCodeSubmit, forgeSizeSubmit, forgeSandboxSubmit, forgesPage, forgesCsv } from "../web/pages/forges.ts";
337337 import { billDownload } from "./api/forge_bill.ts";
338 import { diffText } from "./api/app_pulls.ts";
338339 import { forgeRecoveryForm, forgeRecoverySubmit } from "../web/pages/forges.ts";
339340 import { forgesRefusedPage } from "../web/pages/forges-refused.ts";
340341 import { forgeReachAllow, forgeReachLater, forgeReachRevoke } from "../web/pages/hosted-forges.ts";
@@ -1533,6 +1534,8 @@ export function buildRouter(): Router<Handler> {
15331534 router.add("GET", "/:owner/:name/pulls/new", newPullPage);
15341535 router.add("POST", "/:owner/:name/pulls/new", newPullSubmit);
15351536 router.add("GET", "/:owner/:name/pulls/:number/check.txt", checkText);
1537 // Before the pull page, whose `:number` would take "2.diff" whole.
1538 router.add("GET", "/:owner/:name/pulls/:number.diff", diffText);
15361539 router.add("GET", "/:owner/:name/pulls/:number", pullPage);
15371540 // The change's Files step: the diff, the review threads and the merge
15381541 // box, one tab under the change itself.
test/contract-openapi-tags.test.ts +1 −1
@@ -31,7 +31,7 @@ test("a slice keeps only its tag's operations and the rest of the document", ()
3131 expect(slice.tags.map((t) => t.name)).toEqual(["pulls"]);
3232 const n = Object.values(slice.paths).flatMap((o) => Object.values(o)).length;
3333 expect(n).toBe(ROUTES.filter((r) => tagOf(r.path) === "pulls").length);
34 expect(openapiSliceText("https://x.test", "v", "pulls", "admin").text.length).toBeLessThan(40_000);
34 expect(openapiSliceText("https://x.test", "v", "pulls", "admin").text.length).toBeLessThan(43_000);
3535 });
3636
3737 test("?tag= over HTTP: a slice, the whole, and a 400 naming the tags", async () => {
test/pages-budget.test.ts +6
@@ -82,6 +82,9 @@ const FIXTURE: Record<string, string> = {
8282 "/:owner/:name/pulls/:number/review/:step": "/alice/forge/pulls/2/review/1",
8383 "/:owner/:name/pulls/:number/review-answers": "/alice/forge/pulls/2/review-answers",
8484 "/api/v1/repos/:owner/:name/pulls/:number/review": "/api/v1/repos/alice/forge/pulls/2/review",
85 "/api/v1/repos/:owner/:name/pulls/:number/diff": "/api/v1/repos/alice/forge/pulls/2/diff",
86 "/api/v1/repos/:owner/:name/pulls/:number/files": "/api/v1/repos/alice/forge/pulls/2/files",
87 "/:owner/:name/pulls/:number.diff": "/alice/forge/pulls/2.diff",
8588 "/:owner/:name/display": "/alice/forge/display",
8689 "/api/v1/admin/machines/:machine/disks": "/api/v1/admin/machines/rack1/disks",
8790 "/claim/start/:token": "/claim/start/claim_fixture",
@@ -171,6 +174,9 @@ beforeAll(async () => {
171174 if (!issue.ok) throw new Error(issue.error);
172175 const pull = createIssue(app.db, { repoId: forge.id, authorId: bob.id, title: "A pull", body: "body", kind: "pull" });
173176 if (!pull.ok) throw new Error(pull.error);
177 // A real branch under the pull, so the pull's diff and files routes have a change to read.
178 const topic = await commitToBranch(app, alice, forge, { branch: "topic", base: "main", message: "A change", files: [{ path: "topic.txt", content: "topic\n" }] });
179 if (!topic.ok) throw new Error(JSON.stringify(topic.error));
174180 app.db.run("INSERT INTO pull (issue_id, base_branch, head_branch, base_oid, head_oid) VALUES (?1, 'main', 'topic', '', 'h')", [pull.value.id]);
175181 // An offer's receipt, for `/api/v1/offers/:receipt`: random, so the fixture reads it back.
176182 FIXTURE["/api/v1/offers/:receipt"] = `/api/v1/offers/${issueReceipt(app.db, pull.value.id)}`;
test/pull-patch.test.ts added +142
@@ -0,0 +1,142 @@
1 /**
2 * A pull's change as text and as a file list (#15008): a public pull answers a signed-out caller,
3 * the file list names binary and renamed files, the read is capped, and a pull whose git cannot be
4 * read answers a clean 503 that names no path. The sealed half is in `sealed-git.test.ts`.
5 */
6 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
7 import { mkdtemp, rm } from "node:fs/promises";
8 import { tmpdir } from "node:os";
9 import { join } from "node:path";
10 import { createApp, type App } from "../src/app.ts";
11 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
12 import { openPull } from "../src/actions/pulls.ts";
13 import { PATCH_MAX_BYTES, readPatch } from "../src/http/api/app_pulls.ts";
14 import { getPull } from "../src/core/pulls.ts";
15 import { getRepoById, type Repo } from "../src/core/repos.ts";
16 import { createUser, type User } from "../src/core/users.ts";
17 import { repoDir } from "../src/git/repos.ts";
18 import { serve, type Servers } from "../src/http/server.ts";
19 import { quietLogs } from "./helpers/globals.ts";
20
21 quietLogs();
22
23 let dataDir: string;
24 let app: App;
25 let servers: Servers;
26 let alice: User;
27 let repo: Repo;
28 const numbers: Record<string, number> = {};
29
30 const ok = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => {
31 if (!r.ok) throw new Error(JSON.stringify(r.error));
32 return r.value;
33 };
34
35 const get = (path: string) => fetch(`${servers.url}${path}`, { redirect: "manual" });
36 const api = (name: string, tail: string) => `/api/v1/repos/alice/open/pulls/${numbers[name]}${tail}`;
37
38 async function pullOn(name: string, files: { path: string; content: string | Uint8Array | null }[]): Promise<void> {
39 ok(await commitToBranch(app, alice, repo, { branch: name, base: "main", message: name, files }));
40 const pull = ok(await openPull(app, alice, repo, { title: name, body: "", baseBranch: "main", headBranch: name }));
41 numbers[name] = pull.number;
42 }
43
44 beforeAll(async () => {
45 dataDir = await mkdtemp(join(tmpdir(), "forge-pull-patch-"));
46 app = createApp({ dataDir, port: 0 });
47 servers = await serve(app);
48 alice = ok(
49 await createUser(app.db, {
50 origin: "seed", trusted: true, username: "alice", email: "[email protected]",
51 password: "a-good-passphrase", adminIfFirst: true,
52 }),
53 );
54 repo = ok(await createRepo(app, alice, { name: "open", visibility: "public" }));
55 ok(await commitToBranch(app, alice, repo, { message: "Start", files: [{ path: "README.md", content: "# open\nline\n" }] }));
56 await pullOn("small", [{ path: "hello.ts", content: "export const HELLO_PATCH = 1;\n" }]);
57 await pullOn("kinds", [
58 { path: "pic.bin", content: new Uint8Array([0, 1, 2, 3, 0, 255]) },
59 { path: "README.md", content: null },
60 { path: "GUIDE.md", content: "# open\nline\n" },
61 ]);
62 const wide = (c: string) => `${c.repeat(79)}\n`.repeat(15_000);
63 await pullOn("big", [
64 { path: "a.txt", content: wide("a") },
65 { path: "b.txt", content: wide("b") },
66 ]);
67 await pullOn("gone", [{ path: "gone.txt", content: "x\n" }]);
68 }, 60_000);
69
70 afterAll(async () => {
71 servers?.stop();
72 app?.close();
73 await rm(dataDir, { recursive: true, force: true });
74 });
75
76 describe("a public pull's change", () => {
77 test("a signed-out caller reads the text and the file list, private and not shared", async () => {
78 const diff = await get(api("small", "/diff"));
79 expect(diff.status).toBe(200);
80 expect(diff.headers.get("content-type")).toContain("text/plain");
81 expect(diff.headers.get("cache-control")).toContain("private");
82 expect(await diff.text()).toContain("+export const HELLO_PATCH = 1;");
83 const files = await get(api("small", "/files"));
84 expect(files.status).toBe(200);
85 expect(await files.json()).toEqual([
86 { path: "hello.ts", status: "added", additions: 1, deletions: 0, binary: false },
87 ]);
88 const web = await get(`/alice/open/pulls/${numbers.small}.diff`);
89 expect(web.status).toBe(200);
90 expect(await web.text()).toContain("HELLO_PATCH");
91 });
92
93 test("the file list names a binary file, a deleted one and a renamed one", async () => {
94 const files = (await (await get(api("kinds", "/files"))).json()) as { path: string; status: string; binary: boolean }[];
95 const by = Object.fromEntries(files.map((f) => [f.path, f]));
96 expect(by["pic.bin"]).toMatchObject({ status: "added", binary: true });
97 expect(by["GUIDE.md"]).toMatchObject({ status: "renamed", binary: false, additions: 0, deletions: 0 });
98 expect(files).toHaveLength(2);
99 });
100
101 test("a pull that is not there is the same 404", async () => {
102 expect((await get("/api/v1/repos/alice/open/pulls/999/diff")).status).toBe(404);
103 expect((await get("/alice/open/pulls/999.diff")).status).toBe(404);
104 });
105 });
106
107 describe("the read is capped", () => {
108 test("a change over the cap is a 413 naming the cap, not a long read", async () => {
109 for (const path of [api("big", "/diff"), api("big", "/files")]) {
110 const res = await get(path);
111 expect(res.status).toBe(413);
112 const body = (await res.json()) as { error: string; message: string };
113 expect(body.error).toBe("diff_too_large");
114 expect(body.message).toContain(String(PATCH_MAX_BYTES));
115 }
116 });
117
118 test("readPatch refuses past the bytes it is given, and reads whole under them", async () => {
119 const pull = getPull(app.db, repo.id, numbers.small!)!;
120 expect(await readPatch(app, repo, pull, 20)).toEqual({ ok: false, why: "too_large" });
121 expect((await readPatch(app, repo, pull)).ok).toBe(true);
122 });
123 });
124
125 describe("a pull whose git cannot be read", () => {
126 test("is a clean 503 that names no path, never a thrown 500", async () => {
127 await rm(repoDir(app.reposRoot, "alice", "open"), { recursive: true, force: true });
128 for (const path of [api("gone", "/diff"), api("gone", "/files"), `/alice/open/pulls/${numbers.gone}.diff`]) {
129 const res = await get(path);
130 const text = await res.text();
131 expect(res.status).toBe(503);
132 expect(text).not.toContain(dataDir);
133 expect(text).not.toContain(app.reposRoot);
134 }
135 });
136
137 test("readPatch answers a word, not an exception, for a repository with no directory", async () => {
138 const pull = getPull(app.db, repo.id, numbers.gone!)!;
139 const gone = { ...getRepoById(app.db, repo.id)!, ownerName: "nobody" };
140 expect(await readPatch(app, gone, pull)).toEqual({ ok: false, why: "unreadable" });
141 });
142 });
test/sealed-git.test.ts +28
@@ -167,6 +167,34 @@ afterAll(async () => {
167167 await rm(work, { recursive: true, force: true });
168168 });
169169
170 describe("a sealed pull's diff and files", () => {
171 const base = () => `/api/v1/repos/alice/square/pulls/${pullNumber}`;
172
173 test("the reviewer reads the hunk as text, and the file list", async () => {
174 const diff = await fetch(`${servers.url}${base()}/diff`, { headers: { authorization: `Bearer ${token.alice}` } });
175 expect(diff.status).toBe(200);
176 expect(diff.headers.get("content-type")).toContain("text/plain");
177 expect(await diff.text()).toContain("+export const X9QFIX = true;");
178 const files = await get(`${base()}/files`, "alice");
179 expect(files.status).toBe(200);
180 expect(JSON.parse(files.text).map((f: { path: string }) => f.path)).toEqual(["fix.ts"]);
181 const web = await fetch(`${servers.url}/alice/square/pulls/${pullNumber}.diff`, {
182 headers: { cookie: `forge_session=${createSession(app.db, alice.id).token}` },
183 });
184 expect(web.status).toBe(200);
185 expect(await web.text()).toContain("X9QFIX");
186 });
187
188 test("a stranger and a signed-out caller get 404 on all three", async () => {
189 for (const path of [`${base()}/diff`, `${base()}/files`, `/alice/square/pulls/${pullNumber}.diff`])
190 for (const who of ["mallory", undefined]) {
191 const res = await get(path, who);
192 expect(res.status).toBe(404);
193 expect(res.text).not.toContain("X9QFIX");
194 }
195 });
196 });
197
170198 describe("where the fix lives", () => {
171199 test("the crew's place for sealed work is private, out of the fork graph, made once", async () => {
172200 expect(sealedRepo.visibility).toBe("private");

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs