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

A child's clone is refused when it carries objects/info/alternates pointing outside it #14930

Merged opened this

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

Merged

scout/forge:scout/14914-a-child-s-clone-is-refused-when-it-carri → main 1 commit fork

  • Merged. scout/14914-a-child-s-clone-is-refused-when-it-carri was merged into main as 6dfdaa2.

Review

Sign in to review this change.
2 files changed +121 −3
src/spawn/own_code.ts +71 −2
@@ -12,8 +12,8 @@
1212 * written down as a rollback, and the owner's `/forges` page says so.
1313 */
1414
15 import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
16 import { join } from "node:path";
15 import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
16 import { isAbsolute, join, resolve, sep } from "node:path";
1717 import type { App } from "../app.ts";
1818 import { runGit } from "../git/env.ts";
1919 import type { Managed } from "./manage.ts";
@@ -206,6 +206,70 @@ export function checkTree(root: string): string | undefined {
206206 return undefined;
207207 }
208208
209 /** As deep as git itself follows alternates of alternates. */
210 const ALTERNATE_DEPTH = 5;
211
212 /**
213 * Whether every object store `repo` borrows from is the child's own (#14914). A local clone reads
214 * `objects/info/alternates` and serves what those directories hold, so an entry naming one of the
215 * parent's repositories would copy the parent's objects into the child's cell. Every entry must
216 * resolve, by realpath, inside `bound` (the child's directory), hold no symbolic link, and borrow
217 * only from such stores itself. `http-alternates` names stores by URL, which no realpath can place:
218 * any entry in it is refused. A quoted entry is refused rather than unquoted, so nothing here has
219 * to read it the way git would.
220 */
221 export function alternatesInside(repo: string, bound: string): boolean {
222 let root: string;
223 try {
224 root = realpathSync(bound);
225 } catch {
226 return false;
227 }
228 const inside = (path: string): boolean => path === root || path.startsWith(root + sep);
229 const seen = new Set<string>();
230 const check = (objects: string, depth: number): boolean => {
231 if (depth > ALTERNATE_DEPTH) return false;
232 const info = join(objects, "info");
233 const lines = (file: string): string[] | undefined => {
234 const path = join(info, file);
235 try {
236 lstatSync(path);
237 } catch {
238 return [];
239 }
240 try {
241 return readFileSync(path, "utf8").split("\n").map((l) => l.trim()).filter((l) => l !== "" && !l.startsWith("#"));
242 } catch {
243 return undefined;
244 }
245 };
246 const http = lines("http-alternates");
247 if (http === undefined || http.length > 0) return false;
248 const entries = lines("alternates");
249 if (entries === undefined) return false;
250 for (const entry of entries) {
251 if (entry.startsWith('"')) return false;
252 let real: string;
253 try {
254 real = realpathSync(isAbsolute(entry) ? entry : resolve(objects, entry));
255 } catch {
256 return false;
257 }
258 if (!inside(real)) return false;
259 if (seen.has(real)) continue;
260 seen.add(real);
261 try {
262 if (walkTree(real) !== undefined) return false;
263 } catch {
264 return false;
265 }
266 if (!check(real, depth + 1)) return false;
267 }
268 return true;
269 };
270 return check(join(repo, "objects"), 0);
271 }
272
209273 const SHA = /^[0-9a-f]{40}$/;
210274
211275 /** The commit its own repository's main is at, or undefined. */
@@ -229,6 +293,10 @@ export async function checkout(app: App, name: string, repo: string, commit: str
229293 } catch {
230294 return { ok: false, why: "its repository could not be read" };
231295 }
296 // `ownRepoOf` finds it at `<childDir>/repos/<org>/forge.git`: what it borrows objects from must
297 // be in `<childDir>`.
298 if (!alternatesInside(repo, join(repo, "..", "..", "..")))
299 return { ok: false, why: "its repository borrows objects from outside its forge" };
232300 rmSync(root, { recursive: true, force: true });
233301 mkdirSync(join(dirOf(app), name), { recursive: true });
234302 const cloned = await runGit(["clone", "--quiet", "--no-hardlinks", "--no-checkout", repo, root], { timeoutMs: 120_000 });
@@ -275,6 +343,7 @@ export const ROLLBACK_WORDS: readonly string[] = [
275343 "its repository is too large to copy",
276344 "its repository contains a symbolic link",
277345 "its repository could not be read",
346 "its repository borrows objects from outside its forge",
278347 "its code could not be copied",
279348 "its code could not be checked out",
280349 "it contains a symbolic link",
test/own-code.test.ts +50 −1
@@ -19,7 +19,7 @@ import { headlessTerminal } from "../src/tui/term/terminal.ts";
1919 import { getUserByName } from "../src/core/users.ts";
2020 import { settleApp } from "./helpers/tui.ts";
2121 import { ownCode } from "../src/spawn/manage.ts";
22 import { checkTree, oneAtATime, readOwnCode, recordGood, sourcesFor, startFirstThatWorks, switchOwnCode, useTree, walkTree, type CodeSource } from "../src/spawn/own_code.ts";
22 import { alternatesInside, checkTree, oneAtATime, readOwnCode, recordGood, sourcesFor, startFirstThatWorks, switchOwnCode, useTree, walkTree, type CodeSource } from "../src/spawn/own_code.ts";
2323
2424 quietLogs();
2525
@@ -157,6 +157,55 @@ test("a symlinked main or forge.json is refused, and none of the target is repea
157157 commit(tree("v4"));
158158 });
159159
160 test("a repository borrowing objects from a sibling repository is refused before it is cloned (#14914)", async () => {
161 // One of the parent's repositories, next to the child's directory.
162 const sibling = join(dataDir, "repos", "alice", "secret.git");
163 git(["init", "--quiet", "--bare", "-b", "main", sibling], dataDir);
164 const head = commit(tree("v3b"));
165 const alternates = join(repo(), "objects", "info", "alternates");
166 mkdirSync(join(alternates, ".."), { recursive: true });
167 writeFileSync(alternates, `${join(sibling, "objects")}\n`);
168 await sources();
169 const state = readOwnCode(app, "kid");
170 expect(state.rolledBack?.why).toBe("its repository borrows objects from outside its forge");
171 expect(state.failed).toBe(head);
172 expect(existsSync(join(dataDir, "own-code", "kid", head))).toBe(false);
173 rmSync(alternates);
174 });
175
176 test("alternates pass only when every store, followed, is inside the child's directory", () => {
177 const base = mkdtempSync(join(tmpdir(), "alternates-"));
178 const kid = join(base, "kid");
179 const own = join(kid, "repos", "o", "forge.git", "objects");
180 const other = join(kid, "store", "objects");
181 const outside = join(base, "parent", "objects");
182 for (const dir of [own, other, outside]) mkdirSync(join(dir, "info"), { recursive: true });
183 const write = (objects: string, file: string, text: string) => writeFileSync(join(objects, "info", file), text);
184 const repoDir = join(own, "..");
185 expect(alternatesInside(repoDir, kid)).toBe(true);
186 write(own, "alternates", `# a comment\n${other}\n`);
187 expect(alternatesInside(repoDir, kid)).toBe(true);
188 write(own, "alternates", "../../../../store/objects\n");
189 expect(alternatesInside(repoDir, kid)).toBe(true);
190 write(own, "alternates", "../../../../../parent/objects\n");
191 expect(alternatesInside(repoDir, kid)).toBe(false);
192 // Inside, but itself borrowing from outside.
193 write(own, "alternates", `${other}\n`);
194 write(other, "alternates", `${outside}\n`);
195 expect(alternatesInside(repoDir, kid)).toBe(false);
196 rmSync(join(other, "info", "alternates"));
197 // A link inside the child's directory that names the outside store.
198 symlinkSync(outside, join(kid, "link"));
199 write(own, "alternates", `${join(kid, "link")}\n`);
200 expect(alternatesInside(repoDir, kid)).toBe(false);
201 write(own, "alternates", `"${other}"\n`);
202 expect(alternatesInside(repoDir, kid)).toBe(false);
203 write(own, "alternates", "");
204 write(own, "http-alternates", "http://example.test/objects\n");
205 expect(alternatesInside(repoDir, kid)).toBe(false);
206 rmSync(base, { recursive: true, force: true });
207 });
208
160209 test("a main that parses but never answers falls back, then to the parent's code", async () => {
161210 commit(tree("v3"));
162211 const found = await sources();

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs