@@ -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 , / s y m l i n k s / 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" ) , / m o d e 1 2 0 0 0 0 / ) ;
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