Skip to content

The agent that grows with you

fix(update): obligation disarm never writes through a planted .restore alias (review N05)

Tekniumc0a649bedf8b
diff --git a/hermes_cli/update_cmd_commit.py b/hermes_cli/update_cmd_commit.py
index 27932fcc50c..579c39e5743 100644
--- a/hermes_cli/update_cmd_commit.py
+++ b/hermes_cli/update_cmd_commit.py
@@ -115,8 +115,11 @@ def disarm_commit_obligations() -> None:
if data is None:
path.unlink(missing_ok=True)
else:
- tmp = path.with_name(path.name + ".restore")
- tmp.write_bytes(data)
+ # A fresh file, never through whatever a leftover ``.restore`` name points at.
+ tmp = path.with_name(f".{path.name}.{os.getpid()}.restore")
+ tmp.unlink(missing_ok=True)
+ with os.fdopen(os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600), "wb") as handle:
+ handle.write(data)
os.replace(tmp, path)
except OSError:
pass # an owed tail/restart left armed is a retry, never a lost obligation
diff --git a/tests/hermes_cli/test_update_commit_obligations.py b/tests/hermes_cli/test_update_commit_obligations.py
index e4add6ae3dc..71da278fd88 100644
--- a/tests/hermes_cli/test_update_commit_obligations.py
+++ b/tests/hermes_cli/test_update_commit_obligations.py
@@ -88,3 +88,20 @@ def test_an_unreadable_record_refuses_the_arm_and_survives(root, monkeypatch):
assert host.read_text(encoding="utf-8") == "OLD"
+
[email protected]("posix") # unprivileged symlinks
+def test_disarm_never_writes_through_a_planted_restore_alias(root, tmp_path):
+ """A same-user link at a temp name must not redirect the restore (N05)."""
+ host = host_obligation_path()
+ host.parent.mkdir(parents=True, exist_ok=True)
+ host.write_text("ORIGINAL", encoding="utf-8")
+ sentinel = tmp_path / "sentinel"
+ sentinel.write_text("PRECIOUS", encoding="utf-8")
+ commit.arm_commit_obligations(root, "a" * 40)
+ for alias in (host.with_name(host.name + ".restore"), host.with_name(f".{host.name}.{os.getpid()}.restore")):
+ alias.symlink_to(sentinel)
+
+ commit.disarm_commit_obligations()
+
+ assert sentinel.read_text(encoding="utf-8") == "PRECIOUS"
+ assert not host.is_symlink() and host.read_text(encoding="utf-8") == "ORIGINAL"