diff options
| author | Claude <noreply@anthropic.com> | 2026-11-12 15:01:04 +0000 |
|---|---|---|
| committer | Claude <noreply@anthropic.com> | 2026-11-12 15:01:04 +0000 |
| commit | 052eeb00464b9db2079325ae538356c49494fb1b (patch) | |
| tree | ce074a2bbbc98df60e77957df599016d61568718 /plugins | |
| parent | e0ab6e51219eef74e417a0e1d89e5d53e5e763d4 (diff) | |
security-guidance: open the review records and markers through one helpermhegazy/sg-record-files
The commit and push reviews keep two small records (sg-reviewed-shas,
sg-reviewed-diffs) and one marker per commit under review
(sg-inflight-<sha>) in the git directory.
The plugin should only read and write files it made itself; the session
state file is already written in a similar way. All three are now opened
through one helper, which takes a regular file with no second hard link that
is no larger than a record can be. Anything else at that path is replaced by
a new file when the record is about to be written, and passed over when it
is only read. A folder is left alone. The open never waits, and reads from a
record are capped. Anything old at a marker's path that is not such a file,
other than a folder, is removed without being opened.
Three more fixes on the same lines:
- sg-reviewed-shas was locked with a blocking flock just before the findings
go out, so the findings waited for the lock, up to the hook's timeout. The
hook now waits half a second and then appends without the lock, as it does
where there is no flock.
- Bytes in that record that are not text ended the push review with a
traceback. They now cost only their own row.
- The rewritten record was still in the buffer when the lock was released, so
rows written by several sessions at once were lost. It is flushed first.
Cost: in a repository copied with hard links (cp -al) the records have two
names, so neither copy reads them. The first copy to write starts them
afresh, and commits reviewed before the copy may be reviewed once more.
Co-authored-by: mhegazy <mhegazy@anthropic.com>
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/security-guidance/hooks/diffstate.py | 93 | ||||
| -rw-r--r-- | plugins/security-guidance/tests/test_record_files.py | 626 |
2 files changed, 701 insertions, 18 deletions
diff --git a/plugins/security-guidance/hooks/diffstate.py b/plugins/security-guidance/hooks/diffstate.py index 255fdcce..6a3d9b81 100644 --- a/plugins/security-guidance/hooks/diffstate.py +++ b/plugins/security-guidance/hooks/diffstate.py @@ -6,8 +6,10 @@ there so callers keep resolving bare names through the hook module's globals — tests that ``monkeypatch.setattr(hook, "<fn>", …)`` continue to work without retargeting. """ +import errno import hashlib import os +import stat import subprocess import sys import unicodedata @@ -652,6 +654,52 @@ def capture_git_baseline(cwd): _REVIEWED_SHAS_BASENAME = "sg-reviewed-shas" _REVIEWED_SHAS_CAP = 500 +# The records and markers this module keeps in the git directory are small +# regular files of its own. Whatever else has one of their names is not one of +# them: a link leads to a file that is not the plugin's to read or change, and +# opening a pipe can hold a hook until its timeout. The largest of them is cut +# back at 1 MiB, and no more than this is ever read from one. +_OWN_FILE_MAX_BYTES = 4 * 1024 * 1024 +_NO_FOLLOW = getattr(os, "O_NOFOLLOW", 0) + + +def _is_own_file(st): + return (stat.S_ISREG(st.st_mode) and st.st_nlink <= 1 + and st.st_size <= _OWN_FILE_MAX_BYTES) + + +def _open_own_file(path, flags, mode=0o666): + """os.open for one of those files. Also works as the `opener` of open(). + Anything else at `path` is replaced by a new file when `flags` would create + one, as session_state does for the state file, and refused with OSError + otherwise. The open itself never waits. + + After the open the name is looked at again and has to be that very file: + it can come to mean something else at any moment before that.""" + try: + if not _is_own_file(os.lstat(path)): + if not flags & os.O_CREAT: + raise OSError(errno.EINVAL, "not a file of the plugin's", path) + flags |= os.O_EXCL + try: + os.unlink(path) + except FileNotFoundError: + pass + except FileNotFoundError: + pass + fd = os.open(path, flags | _NO_FOLLOW | getattr(os, "O_NONBLOCK", 0), mode) + try: + seen, st = os.lstat(path), os.fstat(fd) + own = (_is_own_file(seen) and _is_own_file(st) + and (st.st_ino, st.st_dev) == (seen.st_ino, seen.st_dev)) + except OSError: + own = False + if not own: + os.close(fd) + raise OSError(errno.EINVAL, "not a file of the plugin's", path) + return fd + + def _is_full_sha(sha): return len(sha) == 40 and all(c in "0123456789abcdef" for c in sha) @@ -669,8 +717,8 @@ def _load_reviewed_shas(repo_root, _gd=None): return set() out = set() try: - with open(p, "r") as f: - for line in f: + with open(p, "r", errors="replace", opener=_open_own_file) as f: + for line in f.read(_OWN_FILE_MAX_BYTES).splitlines(): sha = line.split("\t", 1)[0].strip() if _is_full_sha(sha): out.add(sha) @@ -684,7 +732,8 @@ def _append_reviewed_shas(repo_root, shas, vulns_found=0): Uses fcntl.flock for the read-gc-write; appends are O_APPEND-atomic but GC needs the lock so concurrent CC sessions in the same clone don't race - each other's truncation. + each other's truncation. The findings go out after this call, so it waits + for the lock only a moment, then appends without it. """ p = _reviewed_shas_path(repo_root) if not p or not shas: @@ -695,11 +744,12 @@ def _append_reviewed_shas(repo_root, shas, vulns_found=0): lines = [f"{s}\t{ts}\t{pv}\t{int(vulns_found)}\n" for s in shas] try: import fcntl - with open(p, "a+") as f: - fcntl.flock(f.fileno(), fcntl.LOCK_EX) + with open(p, "a+", errors="replace", opener=_open_own_file) as f: + if not _seen_lock(f.fileno()): + raise OSError("record is locked") try: f.seek(0) - existing = f.read().splitlines(keepends=True) + existing = f.read(_OWN_FILE_MAX_BYTES).splitlines(keepends=True) # Dedup by sha (first column) — keep newest, then cap. seen = set() merged = [] @@ -712,13 +762,14 @@ def _append_reviewed_shas(repo_root, shas, vulns_found=0): f.seek(0) f.truncate() f.writelines(merged) + f.flush() # while the lock is still held finally: fcntl.flock(f.fileno(), fcntl.LOCK_UN) except (OSError, ImportError): - # fcntl unavailable (Windows) or write failed — degrade to plain - # append; cap enforcement happens on the next locked write. + # fcntl unavailable (Windows), lock held or write failed — degrade to + # plain append; cap enforcement happens on the next locked write. try: - with open(p, "a") as f: + with open(p, "a", opener=_open_own_file) as f: f.writelines(lines) except OSError: pass @@ -895,7 +946,7 @@ def _append_diff_row(f, locked, token, done): if locked is not False and size + len(row) > _SEEN_DIFFS_TRIM_BYTES: f.seek(0) keep, room = [], _SEEN_DIFFS_TRIM_BYTES // 2 - for ln in reversed(f.read().splitlines(keepends=True)): + for ln in reversed(f.read(_OWN_FILE_MAX_BYTES).splitlines(keepends=True)): room -= len(ln) if room < 0: break @@ -929,12 +980,12 @@ def _diff_review_started(repo_root, diff_files, tip="HEAD"): best = dict.fromkeys(keys, DIFF_SEEN_NO) # Read and note under one lock, so two reviews of one diff that start # together cannot both miss each other. - with open(token[0], "a+b") as f: + with open(token[0], "a+b", opener=_open_own_file) as f: locked = _seen_lock(f.fileno()) if locked is False: raise OSError("record is locked") f.seek(0) - for ln in f.read().decode("ascii", "replace").splitlines(): + for ln in f.read(_OWN_FILE_MAX_BYTES).decode("ascii", "replace").splitlines(): row = ln.split("\t") if len(row) != 4 or not row[0].isdigit() or len(row[0]) > 12: continue @@ -961,7 +1012,7 @@ def _diff_review_done(token): if not token: return try: - with open(token[0], "a+b") as f: + with open(token[0], "a+b", opener=_open_own_file) as f: _append_diff_row(f, _seen_lock(f.fileno()), token, done=True) except Exception as e: debug_log(f"Repeated-diff count: verdict not noted ({type(e).__name__}: {e})") @@ -1111,9 +1162,13 @@ def _sweep_inflight(gd): if not name.startswith(_INFLIGHT_PREFIX) or p in _inflight_held: continue try: - if os.path.getmtime(p) > cutoff: + st = os.lstat(p) + if st.st_mtime > cutoff: + continue + if not _is_own_file(st): + os.unlink(p) # no marker, so nobody holds it continue - fd = os.open(p, os.O_RDWR) + fd = _open_own_file(p, os.O_RDWR) except OSError: continue try: @@ -1137,7 +1192,7 @@ def _mark_inflight(repo_root, shas): if not _is_full_sha(sha) or p in _inflight_held: continue try: - fd = _again_if_refused(os.open, p, os.O_RDWR | os.O_CREAT, 0o600) + fd = _again_if_refused(_open_own_file, p, os.O_RDWR | os.O_CREAT, 0o600) except OSError: # One marker that cannot be opened does not cost the others # their claim. @@ -1163,7 +1218,9 @@ def _mark_inflight(repo_root, shas): import atexit atexit.register(_release_inflight) _inflight_held[p] = fd - os.utime(p) + # The open file where that can be named; Windows keeps the name + # on this file for as long as it is open. + os.utime(fd if os.utime in os.supports_fd else p) except Exception: pass @@ -1179,7 +1236,7 @@ def _load_inflight_shas(repo_root, shas, _gd=None): if not _is_full_sha(sha) or p in _inflight_held: continue try: - fd = os.open(p, os.O_RDONLY) + fd = _open_own_file(p, os.O_RDONLY) except OSError: continue try: diff --git a/plugins/security-guidance/tests/test_record_files.py b/plugins/security-guidance/tests/test_record_files.py new file mode 100644 index 00000000..9bd59754 --- /dev/null +++ b/plugins/security-guidance/tests/test_record_files.py @@ -0,0 +1,626 @@ +"""The records and markers kept in the git directory are the plugin's own +regular files; anything else by one of their names is neither read nor written.""" +import os +import stat +import subprocess +import sys +import threading +import time + +import pytest + +from conftest import ( + GIT_ENV, STUB_VULN, bash_payload, commit_file, git, make_repo, metrics_of, run_hook, +) + +import diffstate + +DIFFS, SHAS = "sg-reviewed-diffs", "sg-reviewed-shas" +DIFF = [("handlers.py", "@@ -0,0 +1,2 @@\n+def h(x):\n+ return x")] +OTHER = "".join(f"line {i}\n" for i in range(600)) + "same\nsame\n" +SOURCE = "import os\ndef f(u):\n os.system(u)\n" + +no_fifo = pytest.mark.skipif(sys.platform == "win32", reason="no mkfifo") +BOTH = pytest.mark.parametrize("name", [DIFFS, SHAS]) + + +@pytest.fixture(autouse=True) +def _claude_code_output(monkeypatch): + # diffstate reads the output mode from the test runner's environment. + monkeypatch.setattr(diffstate, "portable_output", lambda: False) + + +@pytest.fixture(params=["flag", "no flag"]) +def every_platform(request, monkeypatch): + """Once as here, once as where os has no O_NOFOLLOW (Windows).""" + if request.param == "no flag": + monkeypatch.setattr(diffstate, "_NO_FOLLOW", 0) + + +@pytest.fixture +def repo(tmp_path): + return make_repo(tmp_path / "repo", {"app.py": "print('hi')\n"}) + + +@pytest.fixture +def other(tmp_path): + p = tmp_path / "other.txt" + p.write_text(OTHER) + os.utime(p, (time.time() - 600,) * 2) + return p + + +def _symlink(link, target): + try: + os.symlink(target, link) + except (OSError, NotImplementedError) as e: + pytest.skip(f"cannot make a link here: {e}") + + +def _hard_link(link, target): + try: + os.link(target, link) + except (OSError, NotImplementedError) as e: + pytest.skip(f"cannot make a link here: {e}") + + +LINKS = pytest.mark.parametrize("link", [_symlink, _hard_link], ids=["symlink", "hard link"]) + + +def _within(seconds, fn, *args): + """fn(*args), failing the test rather than hanging the run if it waits.""" + got = [] + t = threading.Thread(target=lambda: got.append(fn(*args)), daemon=True) + t.start() + t.join(seconds) + assert not t.is_alive(), "still waiting" + return got[0] + + +def _head(repo): + return git(repo, "rev-parse", "HEAD").strip() + + +def _write(repo, name): + """What a review does with the record `name`.""" + if name == DIFFS: + token, _ = diffstate._diff_review_started(str(repo), DIFF) + diffstate._diff_review_done(token) + else: + diffstate._append_reviewed_shas(str(repo), [_head(repo)]) + + +def _is_plain(path): + st = os.lstat(path) + return stat.S_ISREG(st.st_mode) and st.st_nlink == 1 + + +def _unchanged(other): + return other.read_text() == OTHER and time.time() - other.stat().st_mtime > 500 + + +@pytest.fixture +def released(): + yield + diffstate._release_inflight() + + +# ── the two records ────────────────────────────────────────────────────── + +@BOTH +def test_a_record_is_made_and_added_to(repo, name): + _write(repo, name) + first = (repo / ".git" / name).read_bytes() + commit_file(repo, "b.py", "x = 1\n") + _write(repo, name) + now = (repo / ".git" / name).read_bytes() + assert first and now.startswith(first) and len(now) > len(first) + assert now.endswith(b"\n") + if name == DIFFS: + assert b"\r" not in now # written as bytes, on every platform + + +@pytest.mark.skipif(os.name == "nt", reason="POSIX modes") +@BOTH +def test_a_new_record_has_the_mode_open_gives(repo, name): + old = os.umask(0o027) + try: + _write(repo, name) + finally: + os.umask(old) + assert stat.S_IMODE(os.stat(repo / ".git" / name).st_mode) == 0o640 + + +@BOTH +def test_a_repository_reached_through_a_link_keeps_its_records(repo, tmp_path, name, every_platform): + try: + os.symlink(repo, tmp_path / "alias", target_is_directory=True) + except (OSError, NotImplementedError) as e: + pytest.skip(f"cannot make a link here: {e}") + _write(tmp_path / "alias", name) + first = (repo / ".git" / name).read_bytes() + commit_file(repo, "b.py", "x = 1\n") + _write(tmp_path / "alias", name) + assert first and (repo / ".git" / name).read_bytes().startswith(first) + + +@LINKS +@BOTH +def test_a_link_is_replaced_not_followed(repo, other, name, link, every_platform): + link(repo / ".git" / name, other) + _write(repo, name) + assert _unchanged(other) + assert _is_plain(repo / ".git" / name) + assert (repo / ".git" / name).read_text().count("\n") == (2 if name == DIFFS else 1) + + +def test_a_link_put_there_while_the_review_runs_is_replaced(repo, other): + token, _ = diffstate._diff_review_started(str(repo), DIFF) + (repo / ".git" / DIFFS).unlink() + _symlink(repo / ".git" / DIFFS, other) + diffstate._diff_review_done(token) + assert _unchanged(other) and _is_plain(repo / ".git" / DIFFS) + + +@BOTH +def test_a_link_to_nothing_makes_nothing(repo, tmp_path, name, every_platform): + _symlink(repo / ".git" / name, tmp_path / "absent.txt") + _write(repo, name) + assert not os.path.lexists(tmp_path / "absent.txt") + assert _is_plain(repo / ".git" / name) + + +def test_a_link_through_a_link_is_replaced(repo, other, every_platform): + _symlink(repo / "hop", other) + _symlink(repo / ".git" / DIFFS, os.path.join("..", "hop")) + _write(repo, DIFFS) + assert _unchanged(other) and _is_plain(repo / ".git" / DIFFS) + + +@no_fifo +@BOTH +def test_a_pipe_is_replaced_without_waiting(repo, name): + os.mkfifo(repo / ".git" / name) + _within(10, _write, repo, name) + assert _is_plain(repo / ".git" / name) + + +@BOTH +def test_a_file_too_large_to_be_a_record_is_replaced(repo, name, monkeypatch): + monkeypatch.setattr(diffstate, "_OWN_FILE_MAX_BYTES", 1000) + (repo / ".git" / name).write_bytes(b"x" * 1001) + _write(repo, name) + assert b"x" not in (repo / ".git" / name).read_bytes() + + +@BOTH +def test_a_folder_is_left_and_nothing_is_raised(repo, name): + (repo / ".git" / name).mkdir() + _write(repo, name) + assert (repo / ".git" / name).is_dir() + + +def test_a_diff_that_cannot_be_noted_is_counted_as_an_error(repo): + (repo / ".git" / DIFFS).mkdir() + assert diffstate._diff_review_started(str(repo), DIFF) == ( + None, {"diff_seen": diffstate.DIFF_SEEN_ERROR}) + + +@LINKS +def test_reviewed_commits_are_not_read_through_a_link(repo, tmp_path, link, every_platform): + listed = tmp_path / "listed.txt" + listed.write_text(_head(repo) + "\t1\t1\t0\n") + link(repo / ".git" / SHAS, listed) + assert diffstate._load_reviewed_shas(str(repo)) == set() + assert os.path.lexists(repo / ".git" / SHAS), "reading removes nothing" + + +@no_fifo +def test_reviewed_commits_are_not_read_from_a_pipe(repo): + os.mkfifo(repo / ".git" / SHAS) + assert _within(10, diffstate._load_reviewed_shas, str(repo)) == set() + + +def test_no_more_than_a_record_can_hold_is_read(repo, monkeypatch): + """The size is looked at when the file is opened; it can grow after that.""" + monkeypatch.setattr(diffstate, "_OWN_FILE_MAX_BYTES", 100) + (repo / ".git" / SHAS).write_text("") + real_open = diffstate._open_own_file + + def open_then_grow(path, flags, mode=0o666): + fd = real_open(path, flags, mode) + with open(path, "a") as f: + f.write("x" * 79 + "\n" + _head(repo) + "\t1\t1\t0\n") + return fd + + monkeypatch.setattr(diffstate, "_open_own_file", open_then_grow) + assert diffstate._load_reviewed_shas(str(repo)) == set() + + +def test_bytes_that_are_no_text_cost_only_their_own_row(repo): + record = repo / ".git" / SHAS + record.write_bytes(b"\xff\xfe\x81\n" + ("0" * 40).encode() + b"\t1\t1\t0\n") + assert diffstate._load_reviewed_shas(str(repo)) == {"0" * 40} + diffstate._append_reviewed_shas(str(repo), [_head(repo)]) + assert diffstate._load_reviewed_shas(str(repo)) == {"0" * 40, _head(repo)} + + +# Locks the way a review does on this platform. +_HOLD = ( + "import os, sys, time\n" + "sys.path.insert(0, sys.argv[2])\n" + "import diffstate\n" + "fd = os.open(sys.argv[1], os.O_RDWR | os.O_CREAT, 0o600)\n" + "assert diffstate._flock_nb(fd, exclusive=True)\n" + "print('held', flush=True)\n" + "time.sleep(60)\n" +) + + +def test_a_lock_that_stays_held_does_not_hold_the_row_back(repo): + holder = subprocess.Popen( + [sys.executable, "-c", _HOLD, str(repo / ".git" / SHAS), os.path.dirname(diffstate.__file__)], + stdout=subprocess.PIPE, text=True) + try: + assert holder.stdout.readline().strip() == "held" + _within(10, diffstate._append_reviewed_shas, str(repo), [_head(repo)]) + finally: + holder.kill() + holder.wait() + holder.stdout.close() + assert _head(repo) in diffstate._load_reviewed_shas(str(repo)) + + +@pytest.mark.skipif(os.name == "nt", reason="no flock; rows are only appended there") +def test_the_row_is_in_the_file_before_the_lock_is_released(repo, monkeypatch): + import fcntl + real_flock, at_unlock = fcntl.flock, [] + + def flock(fd, op): + if op == fcntl.LOCK_UN: + at_unlock.append((repo / ".git" / SHAS).read_text()) + return real_flock(fd, op) + + monkeypatch.setattr(fcntl, "flock", flock) + diffstate._append_reviewed_shas(str(repo), [_head(repo)]) + assert at_unlock and _head(repo) in at_unlock[0] + + +# ── a name that changes between the first look and the open ────────────── + +def _swap_after_the_first_look(monkeypatch, path, swap): + real_lstat = os.lstat + done = [] + + def lstat_then_swap(p, *a, **k): + st = real_lstat(p, *a, **k) + if os.fspath(p) == str(path) and not done: + done.append(1) + path.unlink() + swap(path) + return st + + monkeypatch.setattr(diffstate.os, "lstat", lstat_then_swap) + return done + + +SWAPS = pytest.mark.parametrize("kind", ["symlink", "hard link", pytest.param("pipe", marks=no_fifo)]) + + +@SWAPS +@BOTH +def test_the_open_file_is_looked_at_too(repo, other, name, kind, monkeypatch, every_platform): + record = repo / ".git" / name + record.write_text("") + swap = {"symlink": lambda p: _symlink(p, other), + "hard link": lambda p: _hard_link(p, other), + "pipe": lambda p: os.mkfifo(p)}[kind] + swapped = _swap_after_the_first_look(monkeypatch, record, swap) + _within(10, _write, repo, name) + monkeypatch.undo() + assert swapped and _unchanged(other) + + +def _appear_after_the_first_look(monkeypatch, path, put): + real_lstat = os.lstat + done = [] + + def lstat_then_put(p, *a, **k): + try: + return real_lstat(p, *a, **k) + except FileNotFoundError: + if os.fspath(p) == str(path) and not done: + done.append(1) + put(path) + raise + + monkeypatch.setattr(diffstate.os, "lstat", lstat_then_put) + return done + + +@BOTH +def test_a_link_that_appears_where_nothing_was_is_not_written_through( + repo, other, name, monkeypatch, every_platform): + appeared = _appear_after_the_first_look( + monkeypatch, repo / ".git" / name, lambda p: _symlink(p, other)) + _write(repo, name) + monkeypatch.undo() + assert appeared and _unchanged(other) + + +@BOTH +def test_a_link_that_is_a_hard_link_by_the_second_look_is_not_written_through( + repo, other, name, monkeypatch, every_platform): + record = repo / ".git" / name + real_lstat = os.lstat + looks = [] + + def lstat_with_changes(p, *a, **k): + if os.fspath(p) == str(record): + looks.append(1) + if len(looks) == 2 and os.path.islink(record): + record.unlink() + _hard_link(record, other) + try: + return real_lstat(p, *a, **k) + except FileNotFoundError: + if os.fspath(p) == str(record) and len(looks) == 1: + _symlink(record, other) + raise + + monkeypatch.setattr(diffstate.os, "lstat", lstat_with_changes) + _write(repo, name) + monkeypatch.undo() + assert looks and _unchanged(other) + + +@BOTH +def test_a_link_to_nothing_that_appears_where_nothing_was_gets_no_row( + repo, tmp_path, name, monkeypatch, every_platform): + target = tmp_path / "absent.txt" + told_not_to_follow = diffstate._NO_FOLLOW + appeared = _appear_after_the_first_look( + monkeypatch, repo / ".git" / name, lambda p: _symlink(p, target)) + _write(repo, name) + monkeypatch.undo() + assert appeared + if told_not_to_follow: + assert not os.path.lexists(target) + else: + assert not target.exists() or target.read_bytes() == b"" + + +@no_fifo +def test_a_read_does_not_wait_for_a_name_that_turned_into_a_pipe(repo, monkeypatch): + record = repo / ".git" / SHAS + record.write_text("") + swapped = _swap_after_the_first_look(monkeypatch, record, lambda p: os.mkfifo(p)) + assert _within(10, diffstate._load_reviewed_shas, str(repo)) == set() + assert swapped + + +def test_a_name_taken_again_after_the_removal_is_left( + repo, other, tmp_path, monkeypatch, every_platform): + """The new file is made only if the name is still free.""" + record = repo / ".git" / DIFFS + _symlink(record, tmp_path / "absent.txt") + real_unlink = os.unlink + + def unlink_then_take(p, *a, **k): + real_unlink(p, *a, **k) + if os.fspath(p) == str(record): + os.symlink(other, record) + + monkeypatch.setattr(diffstate.os, "unlink", unlink_then_take) + assert diffstate._diff_review_started(str(repo), DIFF)[1] == { + "diff_seen": diffstate.DIFF_SEEN_ERROR} + monkeypatch.undo() + assert _unchanged(other) + + +def test_a_replacement_another_hook_got_to_first_still_makes_the_record(repo, tmp_path, monkeypatch): + """Two hooks can find the same stray name and both remove it.""" + record = repo / ".git" / DIFFS + _symlink(record, tmp_path / "absent.txt") + real_unlink = os.unlink + + def unlink_twice(p, *a, **k): + if os.fspath(p) == str(record): + real_unlink(p, *a, **k) + real_unlink(p, *a, **k) + + monkeypatch.setattr(diffstate.os, "unlink", unlink_twice) + assert "diff_seen" in diffstate._diff_review_started(str(repo), DIFF)[1] + monkeypatch.undo() + assert _is_plain(record) and record.read_text().count("\n") == 1 + + +@pytest.mark.skipif(os.name == "nt", reason="an open file cannot be removed there") +def test_a_marker_its_holder_removes_during_the_look_is_claimed_anew(repo, monkeypatch, released): + """A review ending as the next one starts is not a stray file.""" + marker = _marker(repo) + marker.write_text("") + holder = os.open(marker, os.O_RDONLY) # keeps its inode from being reused + real_lstat = os.lstat + done = [] + + def lstat_then_holder_ends(p, *a, **k): + st = real_lstat(p, *a, **k) + if os.fspath(p) == str(marker) and not done: + done.append(1) + marker.unlink() + return st + + monkeypatch.setattr(diffstate, "_sweep_inflight", lambda gd: None) + monkeypatch.setattr(diffstate.os, "lstat", lstat_then_holder_ends) + try: + diffstate._mark_inflight(str(repo), [_head(repo)]) + finally: + monkeypatch.undo() + os.close(holder) + assert done and str(marker) in diffstate._inflight_held + + +# ── the markers ────────────────────────────────────────────────────────── + +def _marker(repo): + return repo / ".git" / f"sg-inflight-{_head(repo)}" + + +def test_a_marker_is_made_held_and_removed(repo, released): + diffstate._mark_inflight(str(repo), [_head(repo)]) + assert _is_plain(_marker(repo)) and str(_marker(repo)) in diffstate._inflight_held + diffstate._release_inflight() + assert not os.path.lexists(_marker(repo)) + + +@LINKS +def test_a_marker_link_is_replaced_not_followed(repo, other, link, released, every_platform): + link(_marker(repo), other) + diffstate._mark_inflight(str(repo), [_head(repo)]) + assert _unchanged(other) + assert _is_plain(_marker(repo)) and str(_marker(repo)) in diffstate._inflight_held + diffstate._release_inflight() + assert other.exists() + + +def test_a_marker_link_to_nothing_makes_nothing(repo, tmp_path, released, every_platform): + _symlink(_marker(repo), tmp_path / "absent.txt") + diffstate._mark_inflight(str(repo), [_head(repo)]) + assert not os.path.lexists(tmp_path / "absent.txt") + + +@no_fifo +def test_asking_about_a_marker_that_is_a_pipe_does_not_wait(repo): + os.mkfifo(_marker(repo)) + assert _within(10, diffstate._load_inflight_shas, str(repo), [_head(repo)]) == set() + + +def test_an_old_link_among_the_markers_is_removed_unopened(repo, other, released): + link = repo / ".git" / "sg-inflight-old" + _symlink(link, other) + try: + os.utime(link, (1, 1), follow_symlinks=False) + except NotImplementedError: + pytest.skip("a link's own time cannot be set here") + diffstate._mark_inflight(str(repo), [_head(repo)]) + assert not os.path.lexists(link) and _unchanged(other) + + +@pytest.mark.skipif(os.utime not in os.supports_fd, reason="time stamps go by name here") +def test_the_time_stamp_goes_to_the_marker_that_was_opened(repo, other, released, monkeypatch): + marker = _marker(repo) + real_stat = os.stat + + def stat_then_swap(p, *a, **k): + st = real_stat(p, *a, **k) + if isinstance(p, str) and p == str(marker): + marker.unlink() + os.symlink(other, marker) + return st + + monkeypatch.setattr(diffstate.os, "stat", stat_then_swap) + diffstate._mark_inflight(str(repo), [_head(repo)]) + monkeypatch.undo() + assert _unchanged(other) + + +def test_an_old_marker_nobody_holds_is_still_swept(repo, released): + old = repo / ".git" / ("sg-inflight-" + "0" * 40) + old.write_text("") + os.utime(old, (1, 1)) + diffstate._mark_inflight(str(repo), [_head(repo)]) + assert not old.exists() + + +# ── through the hook ───────────────────────────────────────────────────── + +def _with_remote(repo, tmp_path): + remote = tmp_path / "remote.git" + git(tmp_path, "init", "-q", "--bare", str(remote)) + git(repo, "remote", "add", "origin", str(remote)) + git(repo, "push", "-q", "-u", "origin", "main") + + +def _push_event(repo): + pushed = subprocess.run(["git", "push", "origin", "main"], cwd=repo, capture_output=True, + text=True, env={**os.environ, **GIT_ENV}) + assert pushed.returncode == 0, pushed.stderr + return bash_payload(repo, "git push origin main", pushed.stdout, pushed.stderr) + + +def _others(repo, tmp_path): + """A link at each record's name, each to a file of its own.""" + others = [] + for name in (DIFFS, SHAS): + others.append(tmp_path / (name + ".txt")) + others[-1].write_text(OTHER) + _symlink(repo / ".git" / name, others[-1]) + return others + + +def test_commit_review_with_links_in_place_of_all_three(repo, tmp_path, hook_env, stub_api): + others = _others(repo, tmp_path) + sha, out = commit_file(repo, "app.py", SOURCE) + _symlink(repo / ".git" / f"sg-inflight-{sha}", tmp_path / "absent.txt") + rc, stdout, _ = run_hook(bash_payload(repo, "git commit -m change", out), hook_env) + assert rc == 0 and stub_api.calls, "the review still runs" + assert metrics_of(stdout)["diff_seen"] == diffstate.DIFF_SEEN_NO + assert all(p.read_text() == OTHER for p in others) + assert not os.path.lexists(tmp_path / "absent.txt") + assert sha in diffstate._load_reviewed_shas(str(repo)) + + +def test_commit_review_with_folders_in_place_of_all_three(repo, hook_env, stub_api): + sha, out = commit_file(repo, "app.py", SOURCE) + for name in (DIFFS, SHAS, f"sg-inflight-{sha}"): + (repo / ".git" / name).mkdir() + rc, stdout, _ = run_hook(bash_payload(repo, "git commit -m change", out), hook_env) + assert rc == 0 and stub_api.calls, "the review still runs" + assert metrics_of(stdout)["diff_seen"] == diffstate.DIFF_SEEN_ERROR + + +def test_push_review_with_links_in_place_of_the_records(repo, tmp_path, hook_env, stub_api): + _with_remote(repo, tmp_path) + others = _others(repo, tmp_path) + sha, _ = commit_file(repo, "app.py", SOURCE) + rc, _, _ = run_hook(_push_event(repo), hook_env) + assert rc == 0 and stub_api.calls, "the review still runs" + assert all(p.read_text() == OTHER for p in others) + assert sha in diffstate._load_reviewed_shas(str(repo)) + + +@no_fifo +def test_push_review_does_not_wait_on_pipes(repo, tmp_path, hook_env, stub_api): + _with_remote(repo, tmp_path) + sha, _ = commit_file(repo, "app.py", SOURCE) + os.mkfifo(repo / ".git" / SHAS) + os.mkfifo(repo / ".git" / f"sg-inflight-{sha}") + rc, _, _ = run_hook(_push_event(repo), hook_env) # run_hook gives up after 120 s + assert rc == 0 and stub_api.calls, "the review still runs" + assert sha in diffstate._load_reviewed_shas(str(repo)) + + +def test_push_review_runs_whatever_bytes_the_record_holds(repo, tmp_path, hook_env, stub_api): + _with_remote(repo, tmp_path) + sha, _ = commit_file(repo, "app.py", SOURCE) + (repo / ".git" / SHAS).write_bytes(b"\xff\xfe\x81\n") + rc, _, _ = run_hook(_push_event(repo), hook_env) + assert rc == 0 and stub_api.calls, "the review still runs" + assert sha in diffstate._load_reviewed_shas(str(repo)) + + +def test_findings_go_out_while_the_record_is_locked(repo, hook_env, stub_api): + stub_api.vulns = [STUB_VULN] + sha, out = commit_file(repo, "app.py", SOURCE) + holder = subprocess.Popen( + [sys.executable, "-c", _HOLD, str(repo / ".git" / SHAS), os.path.dirname(diffstate.__file__)], + stdout=subprocess.PIPE, text=True) + try: + assert holder.stdout.readline().strip() == "held" + rc, _, stderr = run_hook(bash_payload(repo, "git commit -m change", out), hook_env) + finally: + holder.kill() + holder.wait() + holder.stdout.close() + assert rc == 2 and "command_injection" in stderr |
