Public: anyone can read this; the crew can change it.
The .git symlink refusal for crew clones lands from a branch in trevato/forge that reviewers can read #15148
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/15143-the-git-symlink-refusal-for-crew-clones → main 2 commits fork
2 files
changed +239
−11
src/crew/clone_git.ts +106 −10
| @@ -13,15 +13,16 @@ | ||
| 13 | 13 | * and `core.worktree` (so `add -A` commits a directory the forge holds). |
| 14 | 14 | * |
| 15 | 15 | * So every such call goes through `runCloneGit`: `.git` must be a directory |
| 16 | * of the clone's own (lstat, never followed) with no `commondir` and no | |
| 17 | * `alternates`, git is handed `--git-dir` and `--work-tree` by name, and | |
| 18 | * command-line config, which outranks the repository's (git-config(1), | |
| 19 | * "command" scope), turns each of those off. A driver's name is the agent's | |
| 20 | * choice, so the clone's effective config is listed first (reading config | |
| 21 | * runs nothing) and each driver key it names is blanked: an empty command | |
| 22 | * is no command. The same pins as `snapshot/hosted.ts`, widened. Where no | |
| 23 | * `-c` can undo a key (`url.<base>.insteadOf` rewriting the forge's URL, an | |
| 24 | * `include` read at run time) the clone is refused instead. | |
| 16 | * of the clone's own (lstat, never followed) with no `commondir`, no | |
| 17 | * `alternates` and no symbolic link anywhere inside it (#14916), git is | |
| 18 | * handed `--git-dir` and `--work-tree` by name, and command-line config, | |
| 19 | * which outranks the repository's (git-config(1), "command" scope), turns | |
| 20 | * each of those off. A driver's name is the agent's choice, so the clone's | |
| 21 | * effective config is listed first (reading config runs nothing) and each | |
| 22 | * driver key it names is blanked: an empty command is no command. The same | |
| 23 | * pins as `snapshot/hosted.ts`, widened. Where no `-c` can undo a key | |
| 24 | * (`url.<base>.insteadOf` rewriting the forge's URL, an `include` read at | |
| 25 | * run time) the clone is refused instead. | |
| 25 | 26 | * |
| 26 | 27 | * The checks and the git they guard are separate steps, so they hold only |
| 27 | 28 | * while nothing of the agent's can change `.git` between them, and nothing |
| @@ -31,9 +32,13 @@ | ||
| 31 | 32 | * ends (`reapGroup`), and each Bash call runs in bwrap with its own pid |
| 32 | 33 | * namespace and `--die-with-parent` (the OpenAI runner's `bashArgv`, and |
| 33 | 34 | * Claude Code's sandbox runtime), so nothing it started outlives it. |
| 35 | * The window itself stays open: were an agent's process ever left alive, | |
| 36 | * it could swap an entry in `.git` for a link after the walk and before | |
| 37 | * git reads it. Closing it would take git opening each file without | |
| 38 | * following links, which no git option does. | |
| 34 | 39 | */ |
| 35 | 40 | |
| 36 | import { existsSync, lstatSync } from "node:fs"; | |
| 41 | import { existsSync, lstatSync, opendirSync, type Dir, type Dirent } from "node:fs"; | |
| 37 | 42 | import { join } from "node:path"; |
| 38 | 43 | import { err, type Result } from "../kernel/result.ts"; |
| 39 | 44 | import { |
| @@ -124,6 +129,97 @@ export function foreignGitDir(dir: string): string | undefined { | ||
| 124 | 129 | return "the clone's .git names another repository (commondir)"; |
| 125 | 130 | if (existsSync(join(gitDir, "objects", "info", "alternates"))) |
| 126 | 131 | return "the clone's .git borrows objects from another repository (alternates)"; |
| 132 | return linkedEntry(gitDir); | |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * What each entry git reads or writes at the top of `.git` must be. Git's | |
| 137 | * lockfile writes follow a link (lockfile.c without `LOCK_NO_DEREF`), so a | |
| 138 | * linked `index`, `config` or ref has the forge's `add` or `commit` | |
| 139 | * overwrite whatever it names; a linked `objects` lends the clone another | |
| 140 | * repository's history, as `alternates` would (#14916). | |
| 141 | */ | |
| 142 | const GIT_ENTRY_KIND: Readonly<Record<string, "file" | "directory">> = { | |
| 143 | config: "file", | |
| 144 | "config.worktree": "file", | |
| 145 | HEAD: "file", | |
| 146 | index: "file", | |
| 147 | "packed-refs": "file", | |
| 148 | objects: "directory", | |
| 149 | refs: "directory", | |
| 150 | info: "directory", | |
| 151 | hooks: "directory", | |
| 152 | logs: "directory", | |
| 153 | }; | |
| 154 | ||
| 155 | /** | |
| 156 | * Entries `.git` may hold before the walk gives up and refuses. The walk | |
| 157 | * runs synchronously on the forge's process, once per git call | |
| 158 | * (`cloneGitArgv`), never cached per clone, so a link made after one call | |
| 159 | * is refused on the next one. Why 8192: a clone that git manages stays | |
| 160 | * under it, because git packs loose objects at `gc.auto` (6700), so the | |
| 161 | * cap is 6700 objects plus 256 object directories, refs, logs and hooks. | |
| 162 | * It is also low enough not to stall the forge: about 1 ms for a fresh | |
| 163 | * clone (~70 entries) and about 10 ms at the cap (#14916). Entries are | |
| 164 | * read one at a time (`opendirSync`), never a whole listing at once, so a | |
| 165 | * refused clone costs at most the cap's worth of work, however many | |
| 166 | * entries a planted directory holds. | |
| 167 | */ | |
| 168 | export const GIT_ENTRY_LIMIT = 8192; | |
| 169 | ||
| 170 | /** | |
| 171 | * The first entry under `gitDir` that is a symbolic link, neither a file | |
| 172 | * nor a directory, or not the kind `GIT_ENTRY_KIND` names; or undefined. | |
| 173 | * Kinds come from the directory listing itself (`d_type`, which is the | |
| 174 | * entry's own and never followed; Node lstats where the filesystem gives | |
| 175 | * none), so a link anywhere in `.git` (a loose ref, an object directory, a | |
| 176 | * reflog) is refused by name at one read per directory. | |
| 177 | */ | |
| 178 | function linkedEntry(gitDir: string): string | undefined { | |
| 179 | const pending = [""]; | |
| 180 | let seen = 0; | |
| 181 | while (pending.length > 0) { | |
| 182 | const rel = pending.pop()!; | |
| 183 | let dir: Dir; | |
| 184 | try { | |
| 185 | dir = opendirSync(join(gitDir, rel)); | |
| 186 | } catch { | |
| 187 | return `the clone's .git/${rel} cannot be read`; | |
| 188 | } | |
| 189 | try { | |
| 190 | const why = walkDirectory(dir, rel, pending, () => ++seen > GIT_ENTRY_LIMIT); | |
| 191 | if (why !== undefined) return why; | |
| 192 | } catch { | |
| 193 | return `the clone's .git/${rel} cannot be read`; | |
| 194 | } finally { | |
| 195 | dir.closeSync(); | |
| 196 | } | |
| 197 | } | |
| 198 | return undefined; | |
| 199 | } | |
| 200 | ||
| 201 | /** | |
| 202 | * Reads `dir` (at `rel` in `.git`) one entry at a time, queueing each | |
| 203 | * subdirectory on `pending`; the first refusal, or undefined. `overCap` is | |
| 204 | * called once per entry and stops the read when it answers true. | |
| 205 | */ | |
| 206 | function walkDirectory( | |
| 207 | dir: Dir, | |
| 208 | rel: string, | |
| 209 | pending: string[], | |
| 210 | overCap: () => boolean, | |
| 211 | ): string | undefined { | |
| 212 | for (let dirent: Dirent | null; (dirent = dir.readSync()) !== null; ) { | |
| 213 | if (overCap()) return `the clone's .git holds more than ${GIT_ENTRY_LIMIT} entries`; | |
| 214 | const entry = rel === "" ? dirent.name : `${rel}/${dirent.name}`; | |
| 215 | const shown = entry.slice(0, 120); | |
| 216 | if (dirent.isSymbolicLink()) return `the clone's .git/${shown} is a symbolic link`; | |
| 217 | const kind = dirent.isDirectory() ? "directory" : dirent.isFile() ? "file" : undefined; | |
| 218 | if (kind === undefined) return `the clone's .git/${shown} is neither a file nor a directory`; | |
| 219 | const want = rel === "" ? GIT_ENTRY_KIND[dirent.name] : undefined; | |
| 220 | if (want !== undefined && want !== kind) return `the clone's .git/${shown} is not a ${want}`; | |
| 221 | if (kind === "directory") pending.push(entry); | |
| 222 | } | |
| 127 | 223 | return undefined; |
| 128 | 224 | } |
| 129 | 225 |
test/crew-clone-git.test.ts +133 −1
| @@ -28,6 +28,7 @@ import { | ||
| 28 | 28 | existsSync, |
| 29 | 29 | mkdirSync, |
| 30 | 30 | mkdtempSync, |
| 31 | readdirSync, | |
| 31 | 32 | readFileSync, |
| 32 | 33 | symlinkSync, |
| 33 | 34 | writeFileSync, |
| @@ -35,7 +36,7 @@ import { | ||
| 35 | 36 | import { tmpdir } from "node:os"; |
| 36 | 37 | import { join } from "node:path"; |
| 37 | 38 | import { runGit } from "../src/git/env.ts"; |
| 38 | import { CLONE_PINS, foreignGitDir, runCloneGit } from "../src/crew/clone_git.ts"; | |
| 39 | import { CLONE_PINS, foreignGitDir, GIT_ENTRY_LIMIT, runCloneGit } from "../src/crew/clone_git.ts"; | |
| 39 | 40 | import { claimWorkdir } from "../src/crew/workdir.ts"; |
| 40 | 41 | import { runShell } from "../src/crew/openai_runner.ts"; |
| 41 | 42 | import { holdsUnpushedWork } from "../src/crew/orphans.ts"; |
| @@ -174,6 +175,137 @@ describe("forge git in an agent's clone (#14747)", () => { | ||
| 174 | 175 | }); |
| 175 | 176 | }); |
| 176 | 177 | |
| 178 | describe("a symbolic link inside the clone's .git is refused (#14916)", () => { | |
| 179 | test("a linked index: plain git's add overwrites the file it names; runCloneGit refuses and leaves it", async () => { | |
| 180 | const { dir } = await clone(); | |
| 181 | const outside = join(dir, "..", "forge.db"); | |
| 182 | writeFileSync(outside, "the forge's own\n"); | |
| 183 | const { renameSync } = await import("node:fs"); | |
| 184 | renameSync(join(dir, ".git", "index"), outside); | |
| 185 | const before = readFileSync(outside); | |
| 186 | symlinkSync(outside, join(dir, ".git", "index")); | |
| 187 | writeFileSync(join(dir, "b.txt"), "two\n"); | |
| 188 | await runGit(["add", "-A"], { cwd: dir }); | |
| 189 | expect(readFileSync(outside).equals(before)).toBe(false); // the attack is real | |
| 190 | const now = readFileSync(outside); | |
| 191 | writeFileSync(join(dir, "c.txt"), "three\n"); | |
| 192 | expect(foreignGitDir(dir)).toBe("the clone's .git/index is a symbolic link"); | |
| 193 | for (const args of [["add", "-A"], ["commit", "--quiet", "-m", "x"], ["config", "user.name", "x"]]) { | |
| 194 | const run = await runCloneGit(dir, args, { env: ID }); | |
| 195 | expect(run.ok).toBe(false); | |
| 196 | } | |
| 197 | expect(readFileSync(outside).equals(now)).toBe(true); | |
| 198 | }); | |
| 199 | ||
| 200 | test("a linked loose ref, config, HEAD or packed-refs is refused by name, the file it names unchanged", async () => { | |
| 201 | for (const entry of ["refs/heads/main", "config", "HEAD", "packed-refs"]) { | |
| 202 | const { dir } = await clone(); | |
| 203 | const outside = join(dir, "..", "other-ref"); | |
| 204 | writeFileSync(outside, readFileSync(join(dir, ".git", "refs", "heads", "main"))); | |
| 205 | const { rmSync } = await import("node:fs"); | |
| 206 | rmSync(join(dir, ".git", entry), { force: true }); | |
| 207 | symlinkSync(outside, join(dir, ".git", entry)); | |
| 208 | expect(foreignGitDir(dir)).toBe(`the clone's .git/${entry} is a symbolic link`); | |
| 209 | const before = readFileSync(outside, "utf8"); | |
| 210 | writeFileSync(join(dir, "b.txt"), "two\n"); | |
| 211 | expect((await runCloneGit(dir, ["add", "-A"], {})).ok).toBe(false); | |
| 212 | expect((await runCloneGit(dir, ["commit", "--quiet", "-m", "x"], { env: ID })).ok).toBe(false); | |
| 213 | expect(readFileSync(outside, "utf8")).toBe(before); | |
| 214 | } | |
| 215 | }); | |
| 216 | ||
| 217 | test("objects linked to another repository's store: nothing of it is committed on or pushed", async () => { | |
| 218 | const base = mkdtempSync(join(process.env.TMPDIR ?? tmpdir(), "clone-git-objects-")); | |
| 219 | const { dir: other } = await clone(); | |
| 220 | writeFileSync(join(other, "private.txt"), "the other tenant's\n"); | |
| 221 | await runGit(["add", "-A"], { cwd: other }); | |
| 222 | await runGit(["commit", "--quiet", "-m", "private"], { cwd: other, env: ID }); | |
| 223 | const bare = join(base, "other.git"); | |
| 224 | await runGit(["clone", "--quiet", "--bare", other, bare], { cwd: base }); | |
| 225 | const secret = await runGit(["rev-parse", "main"], { cwd: other }); | |
| 226 | expect(secret.ok).toBe(true); | |
| 227 | const fork = join(base, "fork.git"); | |
| 228 | await runGit(["init", "--quiet", "--bare", fork], { cwd: base }); | |
| 229 | const { dir } = await clone(); | |
| 230 | const { rmSync } = await import("node:fs"); | |
| 231 | rmSync(join(dir, ".git", "objects"), { recursive: true }); | |
| 232 | symlinkSync(join(bare, "objects"), join(dir, ".git", "objects")); | |
| 233 | writeFileSync(join(dir, ".git", "refs", "heads", "main"), `${secret.ok && secret.value}\n`); | |
| 234 | // Plain git reads the other repository's private history through it. | |
| 235 | const shown = await runGit(["show", "main:private.txt"], { cwd: dir }); | |
| 236 | expect(shown.ok && shown.value).toBe("the other tenant's"); | |
| 237 | expect(foreignGitDir(dir)).toBe("the clone's .git/objects is a symbolic link"); | |
| 238 | expect((await runCloneGit(dir, ["show", "main:private.txt"], {})).ok).toBe(false); | |
| 239 | expect((await runCloneGit(dir, ["commit", "--quiet", "--allow-empty", "-m", "x"], { env: ID })).ok).toBe(false); | |
| 240 | expect((await runCloneGit(dir, ["push", "--quiet", fork, "main"], {})).ok).toBe(false); | |
| 241 | const pushed = await runGit(["for-each-ref"], { cwd: fork }); | |
| 242 | expect(pushed.ok && pushed.value).toBe(""); | |
| 243 | expect(await holdsUnpushedWork(dir)).toBe(true); // refused counts as "keep it" | |
| 244 | }); | |
| 245 | ||
| 246 | // Split in four so no one test crosses the 2 s budget: each makes at | |
| 247 | // most three clones, and the entry-limit case writes 8193 files. | |
| 248 | test("a link deeper in .git is refused", async () => { | |
| 249 | const { rmSync } = await import("node:fs"); | |
| 250 | const { dir: a } = await clone(); | |
| 251 | const outside = join(a, "..", "outside"); | |
| 252 | mkdirSync(outside); | |
| 253 | const loose = readdirSync(join(a, ".git", "objects")).find((n) => /^[0-9a-f]{2}$/.test(n))!; | |
| 254 | rmSync(join(a, ".git", "objects", loose), { recursive: true }); | |
| 255 | symlinkSync(outside, join(a, ".git", "objects", loose)); | |
| 256 | expect(foreignGitDir(a)).toBe(`the clone's .git/objects/${loose} is a symbolic link`); | |
| 257 | }); | |
| 258 | ||
| 259 | test("a link where git keeps info, hooks or logs is refused", async () => { | |
| 260 | const { rmSync } = await import("node:fs"); | |
| 261 | for (const linked of ["info", "hooks", "logs"]) { | |
| 262 | const { dir } = await clone(); | |
| 263 | const outside = join(dir, "..", "outside"); | |
| 264 | mkdirSync(outside); | |
| 265 | rmSync(join(dir, ".git", linked), { recursive: true, force: true }); | |
| 266 | symlinkSync(outside, join(dir, ".git", linked)); | |
| 267 | expect(foreignGitDir(dir)).toBe(`the clone's .git/${linked} is a symbolic link`); | |
| 268 | } | |
| 269 | }); | |
| 270 | ||
| 271 | test("a non-file entry, or the wrong kind where git expects one, is refused", async () => { | |
| 272 | const { rmSync } = await import("node:fs"); | |
| 273 | const { dir: b } = await clone(); | |
| 274 | rmSync(join(b, ".git", "index")); | |
| 275 | mkdirSync(join(b, ".git", "index")); | |
| 276 | expect(foreignGitDir(b)).toBe("the clone's .git/index is not a file"); | |
| 277 | const { dir: c } = await clone(); | |
| 278 | expect(Bun.spawnSync(["mkfifo", join(c, ".git", "packed-refs")]).exitCode).toBe(0); | |
| 279 | expect(foreignGitDir(c)).toBe("the clone's .git/packed-refs is neither a file nor a directory"); | |
| 280 | }); | |
| 281 | ||
| 282 | test("a .git past the entry limit is refused; a clone git made itself passes", async () => { | |
| 283 | // More entries than a clone of its own holds: refused, not walked on. | |
| 284 | const { dir: e } = await clone(); | |
| 285 | mkdirSync(join(e, ".git", "objects", "zz")); | |
| 286 | for (let i = 0; i <= GIT_ENTRY_LIMIT; i++) writeFileSync(join(e, ".git", "objects", "zz", `${i}`), ""); | |
| 287 | expect(foreignGitDir(e)).toBe(`the clone's .git holds more than ${GIT_ENTRY_LIMIT} entries`); | |
| 288 | // A clone git made itself passes. | |
| 289 | const { dir: d } = await clone(); | |
| 290 | expect(foreignGitDir(d)).toBeUndefined(); | |
| 291 | }); | |
| 292 | }); | |
| 293 | ||
| 294 | describe("the .git walk runs on every git call (#14916)", () => { | |
| 295 | test("a link made after a call that ran is refused on the next call", async () => { | |
| 296 | const { dir } = await clone(); | |
| 297 | expect((await runCloneGit(dir, ["status", "--porcelain"], {})).ok).toBe(true); | |
| 298 | const { rmSync } = await import("node:fs"); | |
| 299 | const outside = join(dir, "..", "later"); | |
| 300 | writeFileSync(outside, "kept\n"); | |
| 301 | rmSync(join(dir, ".git", "index")); | |
| 302 | symlinkSync(outside, join(dir, ".git", "index")); | |
| 303 | const run = await runCloneGit(dir, ["add", "-A"], {}); | |
| 304 | expect(!run.ok && run.error.stderr).toBe("refused: the clone's .git/index is a symbolic link"); | |
| 305 | expect(readFileSync(outside, "utf8")).toBe("kept\n"); | |
| 306 | }); | |
| 307 | }); | |
| 308 | ||
| 177 | 309 | describe("the OpenAI runner fences the clone's .git (#14747)", () => { |
| 178 | 310 | test("Write and Edit refuse .git, by name or through a link; Read still reads it", async () => { |
| 179 | 311 | const { dir } = await clone(); |
new above
offline