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

A private repository answers every read route with the same 404 as a missing one, proven by one table-driven test #13553

Merged opened this

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

Merged

scout/forge:scout/13538-a-private-repository-answers-every-read → main 1 commit fork

  • Merged. scout/13538-a-private-repository-answers-every-read was merged into main as 1c0f0c3.

Review

Sign in to review this change.
1 file changed +186 −0
test/private-404-parity.test.ts added +186
@@ -0,0 +1,186 @@
1 /**
2 * #13538: a private repository answers every read route exactly as a
3 * repository that never existed does, so an answer never says one is there.
4 * Every GET route of the page and API tables whose path names a repository
5 * (`:owner/:name`), and every read-only MCP tool that takes a `repo`, is asked
6 * about `alice/HIDDEN` (private, with a commit, an issue and a branch) and
7 * `alice/ABSENT` (never made), as a stranger and as a signed-in non-member.
8 * Status, headers and body must match once the one name is swapped for the
9 * other; the two names are the same length so a Content-Length cannot tell
10 * them apart. Timing is not compared.
11 *
12 * The walk is over the tables themselves, so a new read route is covered the
13 * day it is added.
14 */
15 import { afterAll, beforeAll, expect, test } from "bun:test";
16 import { mkdtemp, rm } from "node:fs/promises";
17 import { tmpdir } from "node:os";
18 import { join } from "node:path";
19 import { createApp, type App } from "../src/app.ts";
20 import { openIssue } from "../src/actions/issues.ts";
21 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
22 import { getRepo } from "../src/core/repos.ts";
23 import { createSession } from "../src/core/sessions.ts";
24 import { createToken } from "../src/core/tokens.ts";
25 import { createUser, type User } from "../src/core/users.ts";
26 import { TOOLS } from "../src/http/mcp/tools.ts";
27 import { buildApiRouter, buildRouter, serve, type Servers } from "../src/http/server.ts";
28 import { promote } from "./helpers/members.ts";
29
30 const HIDDEN = "zqxhiddenrepo";
31 const ABSENT = "zqxabsentrepo";
32
33 /** What fills a path parameter other than the repository's own two. */
34 const FILL: Record<string, string> = {
35 number: "1", id: "1", oid: "a".repeat(40), sha: "a".repeat(40), env: "prod", label: "preview",
36 branch: "main", path: "README.md", ref: "main", file: "x", username: "alice", chapter: "1",
37 };
38 const fill = (pattern: string, name: string): string =>
39 pattern
40 .replace(":owner", "alice")
41 .replace(":name", name)
42 .replace(/:([a-z_]+)/g, (_, k: string) => FILL[k] ?? "x")
43 .replace(/\*([a-z_]+)/, (_, k: string) => FILL[k] ?? "x");
44
45 /** Headers that are fresh on every answer, whatever was asked. */
46 const FRESH = new Set(["date", "x-request-id", "server-timing", "etag", "content-length", "x-ratelimit-remaining", "x-ratelimit-reset"]);
47
48 interface Answer {
49 readonly status: number;
50 readonly headers: string;
51 readonly body: string;
52 }
53
54 let dataDir: string;
55 let app: App;
56 let servers: Servers;
57 let bobCookie: string;
58 let bobToken: string;
59 let aliceCookie: string;
60 let aliceToken: string;
61
62 /**
63 * The answer with the missing repository's name put back as the private one's, and fresh parts
64 * blanked.
65 */
66 function settle(res: Response, text: string, from: string): Answer {
67 const swap = (s: string) => (from === ABSENT ? s.replaceAll(ABSENT, HIDDEN) : s);
68 const headers = [...res.headers]
69 .filter(([k]) => !FRESH.has(k))
70 .map(([k, v]) => `${k}: ${swap(v)}`)
71 .sort()
72 .join("\n");
73 const body = swap(text)
74 .replaceAll(/<code>[0-9a-f]{8}<\/code>/g, "<code>id</code>")
75 .replaceAll(/"request_id":"[^"]*"/g, '"request_id":""');
76 return { status: res.status, headers, body };
77 }
78
79 async function get(path: string, name: string, cookie?: string): Promise<Answer> {
80 const res = await fetch(`${servers.url}${path}`, { redirect: "manual", headers: cookie ? { cookie } : {} });
81 return settle(res, await res.text(), name);
82 }
83
84 async function callTool(tool: string, args: Record<string, unknown>, name: string, token = bobToken): Promise<Answer> {
85 const res = await fetch(`${servers.url}/mcp`, {
86 method: "POST",
87 headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json, text/event-stream" },
88 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: tool, arguments: args } }),
89 });
90 return settle(res, await res.text(), name);
91 }
92
93 /** Every GET pattern that names a repository by owner and name. */
94 const repoReads = (): string[] =>
95 [
96 ...new Set(
97 [...buildRouter().routes(), ...buildApiRouter().routes()]
98 .filter((r) => r.method === "GET" && (r.pattern.startsWith("/:owner/:name") || r.pattern.startsWith("/api/v1/repos/:owner/:name")))
99 .map((r) => r.pattern),
100 ),
101 ];
102
103 /** Every read-only tool whose input takes a `repo`. */
104 const repoTools = () =>
105 TOOLS.filter((t) => !t.writes && "repo" in (((t.inputSchema as { properties?: object }).properties ?? {}) as object));
106
107 beforeAll(async () => {
108 dataDir = await mkdtemp(join(tmpdir(), "forge-private-404-"));
109 app = createApp({ dataDir, port: 0 });
110 servers = await serve(app);
111 const made: User[] = [];
112 for (const username of ["alice", "bob"]) {
113 const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase" });
114 if (!u.ok) throw new Error(u.error);
115 made.push(u.value);
116 }
117 const [alice, bob] = made as [User, User];
118 promote(app.db, alice, bob);
119 const r = await createRepo(app, alice, { name: HIDDEN, visibility: "private", description: "Not for strangers" });
120 if (!r.ok) throw new Error(String(r.error));
121 const repo = getRepo(app.db, "alice", HIDDEN)!;
122 const c = await commitToBranch(app, alice, repo, { message: "Start", files: [{ path: "README.md", content: "# hidden\n" }] });
123 if (!c.ok) throw new Error(String(c.error));
124 const i = openIssue(app, alice, repo, { title: "A private issue", body: "private" });
125 if (!i.ok) throw new Error(String(i.error));
126 bobCookie = `forge_session=${createSession(app.db, bob.id).token}`;
127 bobToken = createToken(app.db, bob.id, "parity", "rw").token;
128 aliceCookie = `forge_session=${createSession(app.db, alice.id).token}`;
129 aliceToken = createToken(app.db, alice.id, "parity", "rw").token;
130 });
131
132 afterAll(async () => {
133 servers.stop();
134 app.close();
135 await rm(dataDir, { recursive: true, force: true });
136 });
137
138 test("the walk reaches the repository's read routes and tools", () => {
139 const reads = repoReads();
140 expect(reads).toContain("/:owner/:name");
141 expect(reads).toContain("/api/v1/repos/:owner/:name");
142 expect(reads.length).toBeGreaterThan(40);
143 expect(repoTools().length).toBeGreaterThan(3);
144 });
145
146 for (const who of ["a stranger", "a signed-in non-member"] as const) {
147 test(`every read route answers ${who} the same for a private repository as for a missing one`, async () => {
148 const cookie = who === "a stranger" ? undefined : bobCookie;
149 const differs: string[] = [];
150 for (const pattern of repoReads()) {
151 const hidden = await get(fill(pattern, HIDDEN), HIDDEN, cookie);
152 const absent = await get(fill(pattern, ABSENT), ABSENT, cookie);
153 if (hidden.status !== absent.status) differs.push(`${pattern}: status ${hidden.status} vs ${absent.status}`);
154 else if (hidden.headers !== absent.headers) {
155 const a = new Set(absent.headers.split("\n"));
156 const h = new Set(hidden.headers.split("\n"));
157 differs.push(`${pattern}: headers ${[...h].filter((l) => !a.has(l)).join("; ")} vs ${[...a].filter((l) => !h.has(l)).join("; ")}`);
158 }
159 else if (hidden.body !== absent.body) differs.push(`${pattern}: body ${hidden.body.slice(0, 300)} vs ${absent.body.slice(0, 300)}`);
160 }
161 expect(differs).toEqual([]);
162 });
163 }
164
165 test("every read-only MCP tool that takes a repo answers a non-member the same for a private repository as for a missing one", async () => {
166 const differs: string[] = [];
167 for (const tool of repoTools()) {
168 const args = (name: string) => ({ repo: `alice/${name}`, number: 1, path: "README.md", ref: "main" });
169 const hidden = await callTool(tool.name, args(HIDDEN), HIDDEN);
170 const absent = await callTool(tool.name, args(ABSENT), ABSENT);
171 if (hidden.status !== absent.status || hidden.headers !== absent.headers || hidden.body !== absent.body)
172 differs.push(`${tool.name}: ${hidden.status} ${hidden.body.slice(0, 300)} vs ${absent.status} ${absent.body.slice(0, 300)}`);
173 }
174 expect(differs).toEqual([]);
175 });
176
177 test("the comparison is not blind: the owner is answered differently for her repository and a missing one", async () => {
178 const page = await get(`/alice/${HIDDEN}`, HIDDEN, aliceCookie);
179 const none = await get(`/alice/${ABSENT}`, ABSENT, aliceCookie);
180 expect([page.status, none.status]).toEqual([200, 404]);
181 const api = await get(`/api/v1/repos/alice/${HIDDEN}/issues/1`, HIDDEN, aliceCookie);
182 expect(api.status).toBe(200);
183 const told = await callTool("issues", { repo: `alice/${HIDDEN}` }, HIDDEN, aliceToken);
184 const untold = await callTool("issues", { repo: `alice/${ABSENT}` }, ABSENT, aliceToken);
185 expect(told.body).not.toBe(untold.body);
186 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs