fix(kvb-check-gate): escape control characters in the recorded lock holder
A busy message printed the holder line of a lock file raw, so a recorded command carrying terminal escapes or newlines could forge stderr lines or move the cursor in another agent's terminal. holder() now shows every non-printable character as its escape sequence and names no holder when the recorded pid is not a number. 🎯 Fix: 95 % 💥 Break: 2 % 💸 Tokens: 100 % ⏱️ Velocity: – 🛡️ Security: 100 % 🏆 Score: 93 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
51c04da936
commit
3632086ea0
2 files changed
+16
-1
No files matched your search
@@ -415,7 +415,10 @@ def holder(fd: int) -> str:
|
||||
pid, _, command = os.pread(fd, HOLDER_BYTES, 0).decode("utf-8", "replace").strip().partition("\t")
|
||||
except OSError:
|
||||
pid, command = "", ""
|
||||
return f"pid {pid}: {command}" if pid and command else "holder not recorded"
|
||||
if not (pid.isdecimal() and command):
|
||||
return "holder not recorded"
|
||||
shown = "".join(char if char.isprintable() else repr(char)[1:-1] for char in command)
|
||||
return f"pid {pid}: {shown}"
|
||||
|
||||
|
||||
def shared(sync: TextIO, repo: Path, caught: list[int]) -> None:
|
||||
|
||||
@@ -555,6 +555,18 @@ class TestWorktreeLock(GateCase):
|
||||
self.assertIn("(last recorded pid 4242: gate.py run --repo OLD-FINISHED-RUN, possibly stale)", err.getvalue())
|
||||
self.assertEqual(git_lock.read_text(encoding="utf-8"), "4242\tgate.py run --repo OLD-FINISHED-RUN\n")
|
||||
|
||||
def test_a_recorded_holder_reaches_the_message_with_control_characters_escaped(self):
|
||||
gate, lock = load_gate(), self.root / "holder.lock"
|
||||
for recorded, named in (
|
||||
("4242\tgate.py run \x1b[2Jforged\nERROR: done\x07\n", "pid 4242: gate.py run \\x1b[2Jforged\\nERROR: done\\x07"),
|
||||
("4242 \x1b[2J\tgate.py run\n", "holder not recorded"),
|
||||
("4242\tgate.py hold -- make test-ä\n", "pid 4242: gate.py hold -- make test-ä"),
|
||||
):
|
||||
with self.subTest(recorded=recorded):
|
||||
lock.write_text(recorded, encoding="utf-8")
|
||||
with lock.open("rb") as handle:
|
||||
self.assertEqual(gate.holder(handle.fileno()), named)
|
||||
|
||||
def test_a_fifo_link_or_second_name_as_git_dir_lock_ends_the_hold_with_2_at_once(self):
|
||||
git_lock = self.repo / ".git" / RUN_LOCK
|
||||
victim, marker = self.root / "victim", self.root / "ran"
|
||||
|
||||
Reference in new issue
Block a user