Skip to content

ssh-agent-install no longer replaces an rc with less than it held - #137

Open
closer-claudio wants to merge 1 commit into
mainfrom
claudio/20261117-rc-rewrite
Open

closer-claudio wants to merge 1 commit into
mainfrom
claudio/20261117-rc-rewrite

Conversation

@closer-claudio

Copy link
Copy Markdown
Contributor

🎯 Why

ssh-agent-install takes its own lines out of a shell rc by copying the rc without them, then writing the copy over the rc. Two ways that lost the user's file (#135):

  • The copy was made beside the rc (<rc>.tmp) behind || true. In a home directory that takes no new file the copy was never made, and the next command's redirection had emptied the rc before cat failed on the missing copy. ssh-agent-install uninstall, or an install replacing an earlier install's export, exited 1 and left a writable rc at 0 bytes.
  • An rc with a NUL byte in it (a key binding) is binary to grep, which prints one line, Binary file <rc> matches, and exits 0. That line became the rc, with exit 0.

🛠️ What

strip_lines in agent/ssh-agent-install:

  • the copy is made with mktemp under TMPDIR, by grep -a;
  • grep's status 1 (no line left to keep) is fine, anything above stops before the rc is touched, and the partial copy is removed;
  • a failed write over the rc stops, keeps the copy and names it (writing through, which keeps a linked rc a link, cannot be atomic);
  • the copy is removed after a good run.

🧪 How

test-binary.sh 6d, run by CI against the built package:

  • a home of mode 0555 with a writable rc holding the old export beside the user's lines, one quoting the export and one with a NUL byte: byte for byte as expected after uninstall and after install;
  • an rc holding the export alone;
  • a grep that fails when making the copy: exit non-zero, rc unchanged, nothing left in TMPDIR;
  • a cat that fails on the write: exit non-zero, the named copy is there and whole;
  • TMPDIR empty after every run that succeeds.

6b and 6c now also look for the installer's mark line, which their pattern did not match.

Twelve single-clause breakages of the installer, run against sections 6b to 6d with a CI-built package: each fails a row.

📝 Notes

Draft until the checks are green: the full test-binary.sh needs the built package.

strip_lines wrote its copy of the rc to <rc>.tmp, behind `|| true`, then
wrote that copy over the rc. Two losses came of it.

`|| true` was there for grep's exit 1 (no line left to keep) and passed
over every other failure as well: in a home that takes no new file the
copy was never made, and the redirection of the second command had
emptied the rc before cat failed on the missing copy. An uninstall, or
an install replacing an earlier install's export, left a writable rc at
0 bytes there.

And an rc with a NUL byte in it (a key binding) is binary to grep, which
prints one line, "Binary file <rc> matches", and exits 0: that line
became the rc.

The copy is now made with mktemp under TMPDIR, by grep -a; a grep status
above 1 stops before the rc is touched; and a failed write through
leaves the copy and names it, since writing through (which keeps a
linked rc a link) cannot be atomic.

test-binary.sh 6d holds each clause: a home of mode 0555 with a writable
rc that carries the old export beside the user's lines, one of them
quoting the export and one with a NUL byte, compared byte for byte after
uninstall and after install; an rc holding the export alone; a grep that
fails on the copy; a cat that fails on the write; and TMPDIR empty after
every run that succeeds. 6b and 6c now also look for the mark line,
which their pattern did not match. Twelve single-clause breakages of the
installer each fail a row.

Fixes #135

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Signed-off-by: closer-claudio <claudio@farcloser.world>
@closer-claudio
closer-claudio marked this pull request as ready for review November 17, 2026 09:41
@closer-claudio

Copy link
Copy Markdown
Contributor Author

Reviewed at 9aa2dcf

1 similar comment
@closer-claudio

Copy link
Copy Markdown
Contributor Author

Reviewed at 9aa2dcf

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant