Skip to content

Commit 4063e6f

Browse files
axisrowclaude
andauthored
fix(security): eliminate info/exclude write — use in-repo .worktrees/.gitignore (closes #16) (#22)
Redesign (3rd eliminate in the worktree security series, after leave-branch and core.symlinks=false). The original createWorktree wrote .worktrees/ into <gitDir>/info/exclude to keep the worktree dir out of git status. That write was the entire #16 vulnerability: if gitDir resolved outside repoRoot (separate-git-dir, crafted .git gitfile, linked worktree), the exclude write landed at an attacker-controlled location. Detection-based fixes (boundary checks) broke after 2 iterations: git introspection cannot authenticate external common-dirs — legit linked worktrees gitdir in <main>/.git/worktrees/ is indistinguishable from attack separate-git-dir via path inspection. Two independent Codex runs converged on the same conclusion: eliminate the write, don't detect. Fix: delete the entire git-dir/info/exclude block. git worktree add works WITHOUT it (the exclude was cosmetic, from openai#137 commit 4bca062). Keep .worktrees/ out of git status via an in-repo .worktrees/.gitignore containing *, written inside the already-validated worktreesDir (#14 guard ensures inside repoRoot, not a symlink). No git-dir metadata write = no write outside repoRoot = #16 dead. Regression tests: separate-git-dir repo now works (previously attack case); linked worktree works (previously broken by boundary check); external info/exclude untouched. Verified: worktree 18/18; full npm test 161 pass / 4 pre-existing. Co-authored-by: axisrow <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 3bbf13c commit 4063e6f

2 files changed

Lines changed: 73 additions & 9 deletions

File tree

‎plugins/codex/scripts/lib/git.mjs‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -328,15 +328,31 @@ export function createWorktree(repoRoot) {
328328
);
329329
}
330330

331-
// Ensure .worktrees/ is excluded from the target repo without modifying tracked files.
332-
// Use git rev-parse to resolve the real git dir (handles linked worktrees where .git is a file).
333-
const rawGitDir = gitChecked(repoRoot, ["rev-parse", "--git-dir"]).stdout.trim();
334-
const gitDir = path.resolve(repoRoot, rawGitDir);
335-
const excludePath = path.join(gitDir, "info", "exclude");
336-
const excludeContent = fs.existsSync(excludePath) ? fs.readFileSync(excludePath, "utf8") : "";
337-
if (!excludeContent.includes(".worktrees")) {
338-
fs.mkdirSync(path.dirname(excludePath), { recursive: true });
339-
fs.appendFileSync(excludePath, `${excludeContent.endsWith("\n") || !excludeContent ? "" : "\n"}.worktrees/\n`);
331+
// SECURITY (#16, redesign — eliminate, not detect): the original code wrote
332+
// `.worktrees/` into <gitDir>/info/exclude to keep the worktree dir out of
333+
// `git status`. That write was the entire #16 vulnerability: if gitDir resolved
334+
// outside repoRoot (separate-git-dir, crafted .git gitfile, linked worktree),
335+
// the exclude write landed at an attacker-controlled location. Detection-based
336+
// fixes (boundary checks) broke legit linked worktrees and couldn't distinguish
337+
// them from attack — git introspection can't authenticate external common-dirs.
338+
//
339+
// Eliminate the write entirely: git worktree add works WITHOUT the exclude
340+
// (it was cosmetic, added in openai#137 commit 4bca062). Instead, keep
341+
// `.worktrees/` out of git status via an in-repo `.worktrees/.gitignore`
342+
// containing `*`, written inside the already-validated worktreesDir (#14 guard
343+
// ensures it's inside repoRoot and not a symlink). No git-dir metadata write.
344+
const dotGitignore = path.join(worktreesDir, ".gitignore");
345+
// SECURITY: if .gitignore already exists as a symlink (crafted repo ships it as
346+
// a dangling symlink to an external path), existsSync follows it and returns
347+
// false, then writeFileSync would create a file at the symlink target outside
348+
// repoRoot. lstat (not stat/existsSync) detects the symlink itself.
349+
if (fs.existsSync(dotGitignore) && fs.lstatSync(dotGitignore).isSymbolicLink()) {
350+
throw new Error(
351+
`Refusing to create worktree: ${dotGitignore} is a symlink (possible redirect attempt). Remove it manually if expected.`
352+
);
353+
}
354+
if (!fs.existsSync(dotGitignore)) {
355+
fs.writeFileSync(dotGitignore, "*\n", "utf8");
340356
}
341357

342358
const worktreePath = path.join(worktreesDir, `codex-${ts}`);

‎tests/worktree.test.mjs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,3 +415,51 @@ test("keep applies a retargeted existing symlink as a regular file (no host redi
415415
removeSession(session);
416416
}
417417
});
418+
419+
// SECURITY regression (#16, redesign — eliminate info/exclude write): createWorktree
420+
// no longer writes to <gitDir>/info/exclude. The #16 vulnerability (git-dir outside
421+
// repoRoot → exclude write at attacker location) is eliminated by not writing to
422+
// git-dir metadata. A separate-git-dir repo (previously the attack case) now works.
423+
test("createWorktreeSession works with separate-git-dir (no info/exclude write)", () => {
424+
const repoRoot = makeTempDir();
425+
const externalGitDir = makeTempDir("external-gitdir-");
426+
run("git", ["init", "-q", "-b", "main", `--separate-git-dir=${externalGitDir}`, repoRoot]);
427+
run("git", ["config", "user.name", "Test"], { cwd: repoRoot });
428+
run("git", ["config", "user.email", "test@test"], { cwd: repoRoot });
429+
fs.writeFileSync(path.join(repoRoot, "app.js"), "export const v = 1;\n");
430+
run("git", ["add", "app.js"], { cwd: repoRoot });
431+
run("git", ["commit", "-q", "-m", "init"], { cwd: repoRoot });
432+
433+
const session = createWorktreeSession(repoRoot);
434+
try {
435+
assert.ok(session.worktreePath);
436+
assert.match(session.branch, /^codex\/\d+$/);
437+
const excludePath = path.join(externalGitDir, "info", "exclude");
438+
if (fs.existsSync(excludePath)) {
439+
assert.doesNotMatch(
440+
fs.readFileSync(excludePath, "utf8"),
441+
/\.worktrees/,
442+
"external info/exclude must not contain .worktrees (no git-dir write)"
443+
);
444+
}
445+
} finally {
446+
removeSession(session);
447+
}
448+
});
449+
450+
// Regression: linked worktree (gitdir in main .git/) works — no git-dir metadata
451+
// write means no boundary check needed, no legit-layout breakage.
452+
test("createWorktreeSession works from a linked worktree", () => {
453+
const { repoRoot } = createRepoWithInitialCommit();
454+
const linkedWt = makeTempDir("linked-wt-");
455+
assert.equal(run("git", ["worktree", "add", linkedWt, "-b", "linked"], { cwd: repoRoot }).status, 0);
456+
457+
const session = createWorktreeSession(linkedWt);
458+
try {
459+
assert.ok(session.worktreePath);
460+
assert.match(session.branch, /^codex\/\d+$/);
461+
} finally {
462+
removeSession(session);
463+
run("git", ["worktree", "remove", "--force", linkedWt], { cwd: repoRoot });
464+
}
465+
});

0 commit comments

Comments
 (0)