fix(u6): fold the cold bug-hunt panel — a div in a span, a symlink split, and an append outside its lock

/heid-bug-hunt panel 01M35CRRK2RTVWWF1BN09AFQG3, diff-scoped against 91fd8bc.
The most severe of the three rounds, and three of its four convergent findings
were already closed by our own adversarial pass before the reply landed. Three
were not.

- The benches panel was nested inside the booth header's <span class="sub">.
  The insertion had matched the first `{% if board %}` in the template rather
  than the block-level one. A div inside a span is invalid HTML: the parser
  closes the span implicitly and hoists the div out, orphaning the rest of the
  sub-line. Nothing 500s, which is precisely why no test in this suite could
  see it. Moved to block level, pinned by an offset assertion, and verified
  with a real HTML parser.

- _booth_exists used a bare is_dir() while resolve_booth resolves and requires
  the parent to BE the data root. They disagreed on a symlink: the marker
  called a booth pointing outside the root alive while the page 404s it, so the
  row rendered healthy and the link was dead. Same containment now, and
  ValueError joins OSError in the guard -- one bad row must never cost the
  other 220.

- The board append opened its fd OUTSIDE the lock. `flock LOCK printf ... >>
  board` reads as locked and is not: the shell opens the append fd while
  parsing, before flock acquires. A concurrent unlink replaces the inode via
  os.replace, the old fd still points at the unlinked one, and the append
  succeeds, reports success, and vanishes. Pre-existing rather than this
  unit's, but it is silent data loss in the file this unit lives in. Proved by
  holding the lock and asserting nothing is written.

- The atomic write used a predictable .tmp.<pid> name; a pre-planted symlink
  there redirects the write straight through the replace. mkstemp with O_EXCL
  in the same directory, and an fsync before the replace -- os.replace orders
  the rename, not the data behind it.

Declined and recorded: on a host where booth.links cannot be imported, `booth
link` now refuses every URL rather than only booth ones. True, and kept. A
guard that fails open is not a guard, and that state is a broken install in
which most of the CLI is equally broken.

The sharpest line in the reply is one three arms found independently: this repo
had ALREADY paid for the RecursionError class in marks.py, and the new module
re-introduced the unguarded parse. Reading the new module in isolation would
never have surfaced that.

604 -> 607 tests.
This commit is contained in:
vh
2026-09-22 14:20:06 -07:00
parent e3853e2692
commit 8cb21193dc
7 changed files with 220 additions and 55 deletions
+34
View File
@@ -625,3 +625,37 @@ def test_a_credential_never_reaches_the_board(booth):
assert r.returncode != OK
assert "credentials" in r.stderr
assert not (data / "links").exists(), "a credentialed URL created the board"
def test_the_append_happens_INSIDE_the_lock(booth):
"""Cold bug-hunt panel, hulda solo. `flock LOCK printf ... >> board` reads
as locked and is not: the SHELL opens the append fd while parsing, before
flock acquires. A concurrent `unlink` rewriting the board in that window
replaces the inode, the old fd keeps pointing at the unlinked one, and the
append succeeds, reports success, and vanishes.
Proved by holding the lock: if the open were outside it, `booth link` would
write and exit while blocked. Defeating change: reverting to the bare
`flock LOCK printf ... >>` form, under which this test writes the row.
"""
import fcntl
data, _ = booth
board = data / "links"
board.mkdir(parents=True)
(board / "links.md").write_text("")
lock = board / ".links.lock"
lock.touch()
with lock.open("r+") as lf:
fcntl.flock(lf, fcntl.LOCK_EX)
try:
# subprocess.run directly: `run()` pins timeout=30 itself.
with pytest.raises(subprocess.TimeoutExpired):
subprocess.run(
[str(SCRIPT), "link", "https://ok.test/x", "blocked"],
capture_output=True, text=True, timeout=5,
env={**os.environ, "BOOTH_DATA_DIR": str(data),
"BOOTH_URL": "http://booth.invalid"})
finally:
fcntl.flock(lf, fcntl.LOCK_UN)
assert (board / "links.md").read_text() == "", \
"the row was appended while another writer held the lock"