šŸ› fix(kavach): a link that leads out of the session's places is refused

Merged
AT
19d ago
+414-7

kavach's worktree guard refuses a link that leads out of the session's places. That covers a link the command itself makes and then writes through. Releases as kavach 0.6.12 with sarathi 0.6.12.

Slice: #110

The gap (kavach 0.6.11): each target was resolved before the command ran, so a link the same command made read as inside the worktree. ln -s <main> wout && touch wout/x wrote into the main checkout. A hard link to a main checkout file escaped for good.

What changes

  • ln, and cp in link mode (-s, -l, --symbolic-link, --link), count what each link leads to as written.

    • A symbolic link's relative source is read from the folder the link lands in.
    • A hard link's, and ln -r's, is read from the working directory.
    • A -t or -S value is never read as flags.
  • Within one command, a later write at or through an entry that an earlier ln, cp or mv may have made into a link is refused as unprovable. This covers:

    • climbing out through a new link that leads inside, ln -s ../../.. a && touch a/../../../x;
    • a moved or copied symlink or folder whose relative links now lead elsewhere, mv deep/S S && touch S/x.

    Nested shells share the record.

Cost

  • A link to anything outside the session's places is refused, even one nothing would write through (ln -s /usr/bin/python3 .tmp/py).
  • A one-call make-then-write-through is refused even when it stays inside (ln -s x r && touch r, cp -R src d && touch d/x); splitting it into two calls works.

The README says both.

Not covered: links made by other programs (git checkout of a tracked symlink, tar, an interpreter).

Tests

  • Unit tests cover:
    • the refused forms;
    • the climb-through, mv and cp -R forms;
    • -t and -S option parsing;
    • forms that still pass, such as mkdir -p d && touch d/x and mv a b && touch b.
  • An existing test expected ln -s <outside file> inside-link.txt to pass. It now uses an inside source, and the outside form moved to the refused list.
  • Seven corpus rows, each checked against real bash: five escapes refused, two inside links allowed.
  • pnpm check passes at the root.

šŸ¤– Generated with Claude Code


Summary by cubic

Closes a gap where kavach's worktree guard let a command write outside the session through a link the same command created. ln -s <main> wout && touch wout/x previously wrote into the main checkout because the link didn't exist when targets were resolved.

  • ln, and cp in link mode (-s, -l, --symbolic-link, --link), now count what each link leads to as written.
  • A write at or through an entry an earlier ln, cp, or mv in the same command may have made into a link is refused; nested shells share the record.
  • A link to anything outside the session's places is refused even when nothing writes through it; splitting a make-then-write into two calls works.
  • Releases as kavach 0.6.12 with sarathi 0.6.12.