Symlinked rejections file (or temp file) makes the write raise instead of being replaced as documented
The finding
_write calls self.resolve(SCOUT_FILE) and self.resolve(f".{SCOUT_FILE}.tmp") before it does anything with symlinks.
resolve() calls path.resolve(), which follows symlinks. So if either name is a symlink pointing outside the memory directory, resolve() raises MemoryPathError for the final target.
The consequences:
- For the temp file, the tmp.is_symlink() / unlink() guard is never reached.
- For the target, os.replace(tmp, path) is never reached, even though os.replace would have replaced the symlink safely.
- write_rejections only catches OSError errno values in NOT_WRITABLE. The MemoryPathError therefore escapes on every call, and writing is never turned off.
This contradicts the docstring's promise: "a symlink at the target is replaced, never written through." A stray or planted symlink in the memory directory permanently breaks rejection persistence, with an exception on each report cycle.
65| class Note:
66| name: str
67| text: str
68|
69|
70| def render_rejection(rejection: Rejection) -> str:
71| """One rejection as a line: title, severity, and the reason quoted verbatim."""
72| reason = " ".join(rejection.reason.split()) or "no reason given"
73| when = rejection.rejected_at.date().isoformat()
74| return f'"{rejection.title}" ({rejection.severity.value}), rejected {when}: reason "{reason}"'
75|
76|
77| class Memory:
78| """The memory directory. ``root=None`` means no memory: no notes, nothing written.
79|
80| ``write_rejections=False`` means :meth:`write_rejections` never writes. It also stops by itself, with one
81| warning, once the directory turns out not to be writable (:meth:`check_writable`, or a refused write)."""
82|
83| def __init__(self, root: str | Path | None, *, max_chars: int = 2000, write_rejections: bool = True) -> None:
84| self.root = Path(root).expanduser() if root else None
85| self.max_chars = max_chars
86| self.writes = write_rejections and self.root is not None
87|
88| def _stop_writing(self, error: str) -> None:
89| self.writes = FalseThe work pack
Recommendation. For scout's own fixed filenames, build the paths as self.root / SCOUT_FILE and self.root / f".{SCOUT_FILE}.tmp" without following symlinks. Containment is already guaranteed because the names are constants. O_NOFOLLOW, the unlink() of a symlinked temp file, and os.replace (which replaces the link itself rather than its target) already give the documented safe behaviour.
Acceptance criteria
- Address: Symlinked rejections file (or temp file) makes the write raise instead of being replaced as documented
- Keep the change scoped to the cited file/symbols unless impact requires otherwise
- For scout's own fixed filenames, build the paths as
self.root / SCOUT_FILEandself.root / f".{SCOUT_FILE}.tmp"without following symlinks. Containment is already guaranteed because the names are constants.O_NOFOLLOW, theunlink() - Re-check
src/argus_scout/memory.pyafter the fix
Status
- claimed
- resolved
The fix
952e91d8ab fix: a symlinked rejections file is replaced, not refused, as documented.
@@ -155,7 +155,9 @@ class Memory: def _write(self, rejections: Iterable[Rejection]) -> Path: assert self.root is not None self.root.mkdir(parents=True, exist_ok=True)- path = self.resolve(SCOUT_FILE)+ # Fixed names, joined without following links: a symlink at either is replaced (unlinked, O_NOFOLLOW,+ # os.replace swaps the link itself), never resolved, written through, or treated as an escape.+ path = self.root / SCOUT_FILE lines = [ "# Recent rejections", "",@@ -164,7 +166,7 @@ class Memory: ] rendered = [f"- {render_rejection(r)}" for r in rejections] lines += rendered or ["- none"]- tmp = self.resolve(f".{SCOUT_FILE}.tmp")+ tmp = self.root / f".{SCOUT_FILE}.tmp" flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | getattr(os, "O_NOFOLLOW", 0) if tmp.is_symlink(): tmp.unlink()@@ -76,9 +76,13 @@ def test_scout_file_symlinked_out_is_never_written_through(root, tmp_path): victim = tmp_path / "victim.md" victim.write_text("keep me") (root / SCOUT_FILE).symlink_to(victim)- with pytest.raises(MemoryPathError):- Memory(root).write_rejections([REJECTION])+ (root / f".{SCOUT_FILE}.tmp").symlink_to(victim)+ memory = Memory(root)+ for _ in range(2): # replaced, not raised, and writing carries on+ assert memory.write_rejections([REJECTION]) == root / SCOUT_FILE assert victim.read_text() == "keep me"+ assert not (root / SCOUT_FILE).is_symlink() and "Recent rejections" in (root / SCOUT_FILE).read_text()+ assert not (root / f".{SCOUT_FILE}.tmp").exists() def test_creates_root_and_no_root_means_no_memory(tmp_path):How it was judged
Proven 1 test(s) fail on dev and pass with the fix: tests/test_memory.py::test_scout_file_symlinked_out_is_never_written_through
Judged 24 Nov 2026, 04:50 UTC against dev at d0c0f66.
| Test | on dev | dev + patch |
|---|---|---|
tests/test_memory.py::test_scout_file_symlinked_out_is_never_written_through | failed | passed |
The other 20 tests in the run (20 with the same result on both trees)
tests/test_memory.py::test_an_unwritable_directory_is_one_warning_then_left_alone | passed | passed |
tests/test_memory.py::test_creates_root_and_no_root_means_no_memory | passed | passed |
tests/test_memory.py::test_other_write_errors_still_raise | passed | passed |
tests/test_memory.py::test_readme_and_underscore_files_are_not_notes[README.md] | passed | passed |
tests/test_memory.py::test_readme_and_underscore_files_are_not_notes[Readme.MD] | passed | passed |
tests/test_memory.py::test_readme_and_underscore_files_are_not_notes[_.md] | passed | passed |
tests/test_memory.py::test_readme_and_underscore_files_are_not_notes[_draft.md] | passed | passed |
tests/test_memory.py::test_readme_and_underscore_files_are_not_notes[readme.md] | passed | passed |
tests/test_memory.py::test_readme_and_underscore_files_are_not_notes[scout-recent-rejections.md] | passed | passed |
tests/test_memory.py::test_reads_operator_notes | passed | passed |
tests/test_memory.py::test_scout_writes_only_its_own_file | passed | passed |
tests/test_memory.py::test_skipped_files_do_not_use_up_the_note_cap | passed | passed |
tests/test_memory.py::test_startup_check_catches_a_read_only_directory | passed | passed |
tests/test_memory.py::test_symlink_inside_root_is_allowed | passed | passed |
tests/test_memory.py::test_symlink_out_of_root_is_refused | passed | passed |
tests/test_memory.py::test_traversal_is_refused[../escape.md] | passed | passed |
tests/test_memory.py::test_traversal_is_refused[/etc/passwd] | passed | passed |
tests/test_memory.py::test_traversal_is_refused[] | passed | passed |
tests/test_memory.py::test_traversal_is_refused[a/../../escape.md] | passed | passed |
tests/test_memory.py::test_write_rejections_off_writes_nothing | passed | passed |