The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(update): the checkout-lock record never writes through a planted link (review N04)
Teknium976873d961f8
diff --git a/hermes_cli/update_lock.py b/hermes_cli/update_lock.pyindex e985613d2c2..eb0fe8ffe62 100644--- a/hermes_cli/update_lock.py+++ b/hermes_cli/update_lock.py@@ -34,6 +34,7 @@ import timefrom contextlib import contextmanager, suppressfrom dataclasses import dataclassfrom pathlib import Path+from stat import S_ISREGlogger = logging.getLogger(__name__)@@ -1029,7 +1030,8 @@ def _open_lock_file(path: Path) -> tuple[int | None, object]:"""``(fd, True)`` read-write; ``(fd, False)`` read-only for an existing lock file we may notwrite (left root-owned by a ``sudo hermes update``: the kernel lock works on a read-only fd,contract A5); ``(None, reason)`` when neither opens."""- binary = getattr(os, "O_BINARY", 0)+ # Never follow a link planted at the name: the holder record is written into this file.+ binary = getattr(os, "O_BINARY", 0) | getattr(os, "O_NOFOLLOW", 0)try:return os.open(path, os.O_RDWR | os.O_CREAT | binary, 0o644), Trueexcept PermissionError as exc:@@ -1056,6 +1058,12 @@ def _acquire_checkout(install_root: Path) -> UpdateHolder | None:fd, writable = _open_lock_file(path)if fd is None:return UpdateHolder(pid=0, age_seconds=0.0, reason=f"{path} is not writable ({writable})")+ found = os.fstat(fd)+ if not S_ISREG(found.st_mode) or found.st_nlink != 1:+ # A hard link (or special file) at the name: truncating it would rewrite data outside+ # the install. Refuse; never unlink/recreate it (waiters need one stable inode).+ os.close(fd)+ return UpdateHolder(pid=0, age_seconds=0.0, reason=f"{path} is not a regular single-link file; delete it")try:if not _lock_with_contention_wait(fd, path):if _held_by_our_windows_ancestor(path):diff --git a/tests/hermes_cli/test_update_lock.py b/tests/hermes_cli/test_update_lock.pyindex 2b37532eef3..5beaa3c4b4b 100644--- a/tests/hermes_cli/test_update_lock.py+++ b/tests/hermes_cli/test_update_lock.py@@ -865,3 +865,19 @@ def test_unwritable_lock_file_still_locks_the_checkout(tmp_path):)first.release()assert second.returncode == 3, "a second update ran while the read-only-locked checkout was held"++[email protected]("link", [pytest.param("symlink", marks=pytest.mark.platforms("posix")), "link"])+def test_checkout_lock_never_writes_through_a_link_to_a_file_outside_the_install(tmp_path, link):+ """The holder record is written into the lock file itself: a symlink or hard link planted at+ its name is refused, never followed into truncating the file it points at."""+ root = tmp_path / "install"+ root.mkdir()+ outside = tmp_path / "outside.txt"+ outside.write_bytes(b"PRECIOUS USER DATA")+ getattr(os, link)(outside, checkout_lock_path(root))++ lock = UpdateLock(path=tmp_path / "home" / ".hermes-update-in-progress", install_root=root)+ assert lock.acquire() is False+ assert lock.holder is not None and lock.holder.reason+ assert outside.read_bytes() == b"PRECIOUS USER DATA"