Skip to content

Commit fce0e8a

Browse files
axisrowclaude
andauthored
fix(security): warn on worktree accumulation (closes #17) (#23)
Leave-branch cleanup never removes worktrees, so each rescue call adds a new .worktrees/codex-<ts>/ + branch indefinitely. Add a stderr warning when existing worktree count reaches a threshold (default 10, configurable via CODEX_WORKTREE_WARN_THRESHOLD). Not fail-closed — leave-branch contract preserved. Regression test: create threshold+1 worktrees, assert warn. Verified: worktree 19/19; full npm test 162 pass / 4 pre-existing. Co-authored-by: axisrow <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 4063e6f commit fce0e8a

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
// `git worktree remove --force <path> && git branch -D <branch>` per rescue —
1717
// acceptable for a rare, high-stakes operation whose whole point is write-safety.
1818

19+
import fs from "node:fs";
20+
import path from "node:path";
21+
1922
import {
2023
createWorktree,
2124
getWorktreeDiff,
@@ -25,6 +28,32 @@ import {
2528

2629
export function createWorktreeSession(cwd) {
2730
const repoRoot = ensureGitRepository(cwd);
31+
// Warn (not fail) when worktrees accumulate — leave-branch never auto-removes,
32+
// so repeated rescue calls add .worktrees/codex-<ts>/ + branches indefinitely.
33+
// Threshold configurable via CODEX_WORKTREE_WARN_THRESHOLD (default 10).
34+
const worktreesDir = path.join(repoRoot, ".worktrees");
35+
const rawThreshold = process.env.CODEX_WORKTREE_WARN_THRESHOLD;
36+
const threshold = rawThreshold !== undefined && Number.isFinite(Number(rawThreshold))
37+
? Number(rawThreshold)
38+
: 10;
39+
try {
40+
// SECURITY: lstat before readdirSync — if .worktrees is a symlink (crafted
41+
// repo), readdirSync would follow it and enumerate a host/network dir before
42+
// the #14 guard in createWorktree catches it. Skip the count if symlinked.
43+
if (fs.existsSync(worktreesDir) && !fs.lstatSync(worktreesDir).isSymbolicLink()) {
44+
const existing = fs.readdirSync(worktreesDir).filter((name) => name.startsWith("codex-"));
45+
if (existing.length >= threshold) {
46+
process.stderr.write(
47+
`[codex] Warning: ${existing.length} rescue worktrees exist in ${worktreesDir}. ` +
48+
`Remove the ones you no longer need:\n` +
49+
` git worktree remove --force <path> && git branch -D <branch>\n` +
50+
`Or raise the threshold via CODEX_WORKTREE_WARN_THRESHOLD.\n`
51+
);
52+
}
53+
}
54+
} catch {
55+
// .worktrees doesn't exist yet, or unreadable — first run, no accumulation.
56+
}
2857
return createWorktree(repoRoot);
2958
}
3059

‎tests/worktree.test.mjs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,3 +463,39 @@ test("createWorktreeSession works from a linked worktree", () => {
463463
run("git", ["worktree", "remove", "--force", linkedWt], { cwd: repoRoot });
464464
}
465465
});
466+
467+
// Regression (#17): leave-branch never removes worktrees, so they accumulate.
468+
// At a threshold (default 10, configurable via CODEX_WORKTREE_WARN_THRESHOLD),
469+
// createWorktreeSession must warn on stderr with cleanup instructions. Not fail.
470+
test("createWorktreeSession warns when worktree count exceeds threshold", () => {
471+
const { repoRoot } = createRepoWithInitialCommit();
472+
const sessions = [];
473+
const threshold = 3;
474+
const originalThreshold = process.env.CODEX_WORKTREE_WARN_THRESHOLD;
475+
process.env.CODEX_WORKTREE_WARN_THRESHOLD = String(threshold);
476+
const originalWrite = process.stderr.write.bind(process.stderr);
477+
let warned = false;
478+
process.stderr.write = (chunk) => {
479+
if (typeof chunk === "string" && chunk.includes("rescue worktrees exist")) {
480+
warned = true;
481+
}
482+
return true;
483+
};
484+
485+
try {
486+
for (let i = 0; i <= threshold; i++) {
487+
sessions.push(createWorktreeSession(repoRoot));
488+
}
489+
assert.equal(warned, true, "must warn when count >= threshold");
490+
} finally {
491+
process.stderr.write = originalWrite;
492+
if (originalThreshold === undefined) {
493+
delete process.env.CODEX_WORKTREE_WARN_THRESHOLD;
494+
} else {
495+
process.env.CODEX_WORKTREE_WARN_THRESHOLD = originalThreshold;
496+
}
497+
for (const s of sessions) {
498+
removeSession(s);
499+
}
500+
}
501+
});

0 commit comments

Comments
 (0)