Skip to content

Commit 3bbf13c

Browse files
axisrowclaude
andauthored
fix(security): neutralize symlink replay via core.symlinks=false (closes #15) (#20)
REDESIGN after four cycles of detection-based fixes each found a new TOCTOU bypass (index, refs/replace). Same pattern as worktree-cleanup whack-a-mole — remove the vulnerable path rather than guard it. Neutralize: apply with git -c core.symlinks=false apply --index. Symlink-mode entries materialize as regular text files (target path as content), NOT symlinks. Cannot point at host file, cannot exfil. Class disappears by construction. Byte-preserving patch and --no-ext-diff kept; detection logic removed. Tests (3, green): new symlink -> regular file; retargeted -> regular file; regular file mentioning mode 120000 -> applied normally. Verified: worktree 16/16; full npm test 159 pass / 4 pre-existing. Co-authored-by: axisrow <[email protected]> Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
1 parent 011ccc0 commit 3bbf13c

2 files changed

Lines changed: 74 additions & 39 deletions

File tree

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

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -370,29 +370,32 @@ export function applyWorktreePatch(repoRoot, worktreePath, baseCommit) {
370370
try {
371371
// Byte-preserving: write the patch via `git diff --cached --binary --output`
372372
// (git→file, never through a JS string). runCommand decodes stdout as UTF-8,
373-
// which would replace invalid bytes (0xff, NUL, legacy encodings) with U+FFFD
374-
// and apply a corrupted patch as "success". Detect empty via file size.
373+
// which would replace invalid bytes (0xff, NUL, legacy encodings) with U+FFFD.
374+
// --no-ext-diff/--no-textconv pin diff to built-in behavior so a poisoned
375+
// diff.external driver (writable via the linked-worktree shared commondir)
376+
// cannot affect patch generation.
375377
gitChecked(worktreePath, ["add", "-A"]);
376-
gitChecked(worktreePath, ["diff", "--cached", "--binary", baseCommit, "--output", patchPath]);
378+
gitChecked(worktreePath, [
379+
"diff", "--no-ext-diff", "--no-textconv", "--binary", "--cached", baseCommit, "--output", patchPath
380+
]);
377381
if (!fs.existsSync(patchPath) || fs.statSync(patchPath).size === 0) {
378382
return { applied: false, detail: "No tracked changes to apply." };
379383
}
380-
// SECURITY (#15): a symlink created in the worktree (ln -s ~/.ssh/authorized_keys
381-
// ./steal) is captured as a mode-120000 diff entry with the target verbatim.
382-
// `git apply --index` would recreate the symlink in repoRoot pointing at the
383-
// attacker-chosen host path → exfil/append primitive. git apply rejects literal
384-
// "../" filename paths, but symlink-mode entries bypass that guard (the repo-side
385-
// filename is normal; only the symlink target points outside). Reject the patch
386-
// if any mode-120000 line appears. Leave-branch preserved: worktree stays for
387-
// manual inspection.
388-
const patchContent = fs.readFileSync(patchPath, "utf8");
389-
if (/^(?:new file|deleted file|old|new) mode 120000$/m.test(patchContent)) {
390-
return {
391-
applied: false,
392-
detail: `Worktree contains symlinks (mode 120000); patch not applied to prevent a host-file redirect via repoRoot. Inspect and copy them manually from ${worktreePath}.`
393-
};
394-
}
395-
const applyResult = git(repoRoot, ["apply", "--index", patchPath]);
384+
// SECURITY (#15, redesign — neutralize, not detect): a symlink created or
385+
// retargeted in the worktree would be replayed by `git apply` into repoRoot
386+
// pointing at an attacker-chosen host path → exfil/append primitive. Four
387+
// cycles of DETECTION (patch-text scan, --raw, frozen-tree) each found a new
388+
// TOCTOU/mutable-indirection bypass (index, refs/replace, …). git has too many
389+
// layers of mutable object resolution to make two-phase detection airtight.
390+
//
391+
// Instead, NEUTRALIZE: apply with `core.symlinks=false`. git then materializes
392+
// any symlink-mode entry as a regular text file containing the target path,
393+
// NOT as a symlink — it cannot point at a host file, cannot exfil, cannot
394+
// append. The symlink-replay class disappears by construction, with no
395+
// detection race to win. Legitimate symlinks in the worktree also land as
396+
// text files (rare in code; the render layer already warns to inspect
397+
// special files manually).
398+
const applyResult = git(repoRoot, ["-c", "core.symlinks=false", "apply", "--index", patchPath]);
396399
if (applyResult.status !== 0) {
397400
return { applied: false, detail: applyResult.stderr.trim() || "Patch apply failed (conflicts?)." };
398401
}

‎tests/worktree.test.mjs‎

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -334,38 +334,39 @@ test("renderWorktreeTaskResult inspection command references baseCommit (staged-
334334
}
335335
});
336336

337-
// SECURITY regression (#15): a symlink created in the worktree is captured as a
338-
// mode-120000 diff entry. Without the guard, `git apply --index` would recreate
339-
// the symlink in repoRoot pointing at an attacker-chosen host path → host-file
340-
// exfil/append. keep must detect mode 120000 in the patch and refuse to apply,
341-
// leaving the worktree in place.
342-
test("keep refuses to apply a patch containing a symlink (mode 120000) and preserves the worktree", () => {
337+
// SECURITY regression (#15, neutralize redesign): a symlink created in the
338+
// worktree is applied with `core.symlinks=false`, so git materializes it as a
339+
// REGULAR TEXT FILE containing the target path — NOT as a symlink. It cannot
340+
// point at a host file, cannot exfil, cannot append. Four cycles of detection
341+
// (patch-text scan, --raw, frozen-tree) each found a new TOCTOU bypass; the
342+
// neutralize approach removes the symlink-replay class by construction.
343+
test("keep applies a worktree symlink as a regular file (core.symlinks=false neutralize), not a host redirect", () => {
343344
const { repoRoot } = createRepoWithInitialCommit();
344345
const session = createWorktreeSession(repoRoot);
345346

346347
try {
347-
// Plant a symlink in the worktree pointing at an absolute host path.
348348
fs.symlinkSync("/etc/passwd", path.join(session.worktreePath, "steal"));
349349

350350
const result = cleanupWorktreeSession(session, { keep: true });
351351

352-
assert.equal(result.applied, false);
353-
assert.match(result.detail, /symlinks/i);
354-
// The symlink must NOT have been recreated in repoRoot.
355-
assert.equal(fs.existsSync(path.join(repoRoot, "steal")), false, "symlink not replayed into repoRoot");
356-
// Worktree preserved (leave-branch).
357-
assert.equal(result.preserved, true);
352+
assert.equal(result.applied, true);
353+
// The entry MUST exist as a regular file, NOT a symlink.
354+
const target = path.join(repoRoot, "steal");
355+
const st = fs.lstatSync(target);
356+
assert.equal(st.isFile(), true, "symlink applied as a regular file, not a symlink");
357+
assert.equal(st.isSymbolicLink(), false, "no host-file symlink created in repoRoot");
358+
// Content is the defused target path as text.
359+
assert.equal(fs.readFileSync(target, "utf8"), "/etc/passwd");
358360
} finally {
359361
removeSession(session);
360362
}
361363
});
362364

363-
// SECURITY regression (#15, false-positive guard): a regular file whose CONTENT
364-
// happens to contain the literal "mode 120000" string (e.g. documentation of git
365-
// modes) must NOT trip the symlink guard. Only real diff mode headers
366-
// ("new file mode 120000" etc.) are symlinks. The regex matches line-anchored
367-
// mode headers, not arbitrary content.
368-
test("keep applies a regular file whose content literally mentions mode 120000 (no false positive)", () => {
365+
// SECURITY regression (#15, false-positive was a detection artifact — under
366+
// neutralize there is nothing to false-positive on). A regular file whose
367+
// content happens to mention "mode 120000" applies normally (it always did;
368+
// this test now just confirms the redesign didn't regress ordinary applies).
369+
test("keep applies a regular file whose content literally mentions mode 120000", () => {
369370
const { repoRoot } = createRepoWithInitialCommit();
370371
const session = createWorktreeSession(repoRoot);
371372

@@ -377,9 +378,40 @@ test("keep applies a regular file whose content literally mentions mode 120000 (
377378

378379
const result = cleanupWorktreeSession(session, { keep: true });
379380

380-
assert.equal(result.applied, true, "regular file applied despite the literal mode string in content");
381+
assert.equal(result.applied, true);
381382
assert.match(fs.readFileSync(path.join(repoRoot, "modes.md"), "utf8"), /mode 120000/);
382383
} finally {
383384
removeSession(session);
384385
}
385386
});
387+
388+
// SECURITY regression (#15 reopen): an existing tracked symlink that Codex
389+
// RETARGETS. Under neutralize (core.symlinks=false apply), the retarget is
390+
// applied but the result in repoRoot is a regular file with the new target as
391+
// text — the host-pointing symlink is NOT created. (Pre-neutralize this was the
392+
// bypass that broke #19's regex; frozen-tree closed the TOCTOU but not the
393+
// replace-ref vector. Neutralize closes the class.)
394+
test("keep applies a retargeted existing symlink as a regular file (no host redirect)", () => {
395+
const { repoRoot } = createRepoWithInitialCommit();
396+
fs.symlinkSync("/tmp/codex-benign-target", path.join(repoRoot, "existing_link"));
397+
assert.equal(run("git", ["add", "existing_link"], { cwd: repoRoot }).status, 0);
398+
assert.equal(run("git", ["commit", "-m", "add benign symlink"], { cwd: repoRoot }).status, 0);
399+
400+
const session = createWorktreeSession(repoRoot);
401+
402+
try {
403+
fs.rmSync(path.join(session.worktreePath, "existing_link"));
404+
fs.symlinkSync("/etc/passwd", path.join(session.worktreePath, "existing_link"));
405+
406+
const result = cleanupWorktreeSession(session, { keep: true });
407+
408+
assert.equal(result.applied, true);
409+
const target = path.join(repoRoot, "existing_link");
410+
const st = fs.lstatSync(target);
411+
assert.equal(st.isSymbolicLink(), false, "no host-pointing symlink in repoRoot");
412+
assert.equal(st.isFile(), true, "retarget applied as a regular file");
413+
assert.equal(fs.readFileSync(target, "utf8"), "/etc/passwd");
414+
} finally {
415+
removeSession(session);
416+
}
417+
});

0 commit comments

Comments
 (0)