|
| 1 | +"""The audit log is called tamper-evident. These tests tamper with it. |
| 2 | +
|
| 3 | +Each one seals a log, changes it the way someone covering their tracks |
| 4 | +would, and requires verification to fail and to say where. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +import json |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | +from agentix_logos.audit_chain import ( |
| 15 | + SIDECAR_NAME, |
| 16 | + build_chain, |
| 17 | + save_chain_sidecar, |
| 18 | + verify_chain, |
| 19 | +) |
| 20 | + |
| 21 | +EVENTS = [ |
| 22 | + {"action": "snapshot", "modules": 55}, |
| 23 | + {"action": "verify", "module": "codex", "passed": True}, |
| 24 | + {"action": "verify", "module": "waku", "passed": False}, |
| 25 | + {"action": "auto_rollback", "module": "waku", "to": "a1b2c3"}, |
| 26 | + {"action": "propose", "module": "waku", "approved_by": "operator"}, |
| 27 | + {"action": "apply", "module": "waku"}, |
| 28 | +] |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def sealed(tmp_path: Path) -> Path: |
| 33 | + """A six-line audit log with its chain sealed beside it.""" |
| 34 | + audit_dir = tmp_path / ".agentix" |
| 35 | + audit_dir.mkdir() |
| 36 | + audit_path = audit_dir / "audit.jsonl" |
| 37 | + audit_path.write_text( |
| 38 | + "".join(json.dumps(e, sort_keys=True) + "\n" for e in EVENTS), encoding="utf-8" |
| 39 | + ) |
| 40 | + save_chain_sidecar(build_chain(audit_path), audit_dir / SIDECAR_NAME) |
| 41 | + return audit_path |
| 42 | + |
| 43 | + |
| 44 | +def _lines(path: Path) -> list[str]: |
| 45 | + return path.read_text(encoding="utf-8").splitlines() |
| 46 | + |
| 47 | + |
| 48 | +def _rewrite(path: Path, lines: list[str]) -> None: |
| 49 | + path.write_text("".join(line + "\n" for line in lines), encoding="utf-8") |
| 50 | + |
| 51 | + |
| 52 | +def test_an_untouched_log_verifies(sealed: Path) -> None: |
| 53 | + result = verify_chain(sealed) |
| 54 | + assert result.valid is True |
| 55 | + assert result.errors == [] |
| 56 | + assert (result.entries, result.sealed, result.unsealed) == (6, 6, 0) |
| 57 | + |
| 58 | + |
| 59 | +def test_rewriting_a_past_event_is_detected(sealed: Path) -> None: |
| 60 | + # the failed check becomes a pass; still valid JSON, same length |
| 61 | + lines = _lines(sealed) |
| 62 | + lines[2] = lines[2].replace("false", "true ") |
| 63 | + _rewrite(sealed, lines) |
| 64 | + |
| 65 | + result = verify_chain(sealed) |
| 66 | + assert result.valid is False |
| 67 | + assert any(e.startswith("Line 2:") for e in result.errors) |
| 68 | + assert not any(e.startswith(("Line 0:", "Line 1:", "Line 3:")) for e in result.errors) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize("index", range(len(EVENTS))) |
| 72 | +def test_a_single_changed_character_on_any_line_is_detected(sealed: Path, index: int) -> None: |
| 73 | + lines = _lines(sealed) |
| 74 | + lines[index] = lines[index].replace("a", "b", 1) |
| 75 | + _rewrite(sealed, lines) |
| 76 | + |
| 77 | + result = verify_chain(sealed) |
| 78 | + assert result.valid is False |
| 79 | + assert any(e.startswith(f"Line {index}:") for e in result.errors) |
| 80 | + |
| 81 | + |
| 82 | +def test_deleting_an_event_is_detected(sealed: Path) -> None: |
| 83 | + lines = _lines(sealed) |
| 84 | + del lines[3] # the rollback never happened |
| 85 | + _rewrite(sealed, lines) |
| 86 | + |
| 87 | + result = verify_chain(sealed) |
| 88 | + assert result.valid is False |
| 89 | + assert any(e.startswith("Line 3:") for e in result.errors) # first divergence |
| 90 | + assert any("shorter than its sealed chain" in e for e in result.errors) |
| 91 | + |
| 92 | + |
| 93 | +def test_inserting_an_event_is_detected(sealed: Path) -> None: |
| 94 | + lines = _lines(sealed) |
| 95 | + lines.insert(4, json.dumps({"action": "propose", "approved_by": "nobody"}, sort_keys=True)) |
| 96 | + _rewrite(sealed, lines) |
| 97 | + |
| 98 | + result = verify_chain(sealed) |
| 99 | + assert result.valid is False |
| 100 | + assert any(e.startswith("Line 4:") for e in result.errors) |
| 101 | + |
| 102 | + |
| 103 | +def test_reordering_events_is_detected(sealed: Path) -> None: |
| 104 | + lines = _lines(sealed) |
| 105 | + lines[4], lines[5] = lines[5], lines[4] # applied before it was approved |
| 106 | + _rewrite(sealed, lines) |
| 107 | + |
| 108 | + result = verify_chain(sealed) |
| 109 | + assert result.valid is False |
| 110 | + assert any(e.startswith("Line 4:") for e in result.errors) |
| 111 | + assert any(e.startswith("Line 5:") for e in result.errors) |
| 112 | + |
| 113 | + |
| 114 | +def test_cutting_the_log_short_is_detected(sealed: Path) -> None: |
| 115 | + _rewrite(sealed, _lines(sealed)[:4]) |
| 116 | + |
| 117 | + result = verify_chain(sealed) |
| 118 | + assert result.valid is False |
| 119 | + assert any("2 sealed line(s) missing" in e for e in result.errors) |
| 120 | + |
| 121 | + |
| 122 | +def test_appending_is_what_a_log_does_and_is_reported_not_refused(sealed: Path) -> None: |
| 123 | + with sealed.open("a", encoding="utf-8") as f: |
| 124 | + f.write(json.dumps({"action": "verify", "module": "codex"}, sort_keys=True) + "\n") |
| 125 | + |
| 126 | + result = verify_chain(sealed) |
| 127 | + assert result.valid is True |
| 128 | + assert (result.sealed, result.unsealed) == (6, 1) |
| 129 | + |
| 130 | + |
| 131 | +def test_editing_the_sidecar_to_match_a_changed_line_is_detected(sealed: Path) -> None: |
| 132 | + # the careful attacker: change the event, then fix up its recorded CID |
| 133 | + from agentix_logos.audit_chain import compute_cid |
| 134 | + |
| 135 | + lines = _lines(sealed) |
| 136 | + lines[2] = lines[2].replace("false", "true ") |
| 137 | + _rewrite(sealed, lines) |
| 138 | + |
| 139 | + sidecar = sealed.parent / SIDECAR_NAME |
| 140 | + records = [json.loads(r) for r in sidecar.read_text(encoding="utf-8").splitlines()] |
| 141 | + records[2]["cid"] = compute_cid(lines[2]) |
| 142 | + sidecar.write_text("".join(json.dumps(r, sort_keys=True) + "\n" for r in records), encoding="utf-8") |
| 143 | + |
| 144 | + result = verify_chain(sealed) |
| 145 | + assert result.valid is False |
| 146 | + # entry 2's root no longer matches its fields, and entry 3 no longer links to it |
| 147 | + assert any(e.startswith("Chain entry 2:") for e in result.errors) |
| 148 | + assert any(e.startswith("Chain entry 3:") for e in result.errors) |
| 149 | + |
| 150 | + |
| 151 | +def test_a_garbled_sidecar_line_fails_instead_of_being_skipped(sealed: Path) -> None: |
| 152 | + sidecar = sealed.parent / SIDECAR_NAME |
| 153 | + records = sidecar.read_text(encoding="utf-8").splitlines() |
| 154 | + records[1] = "not json" |
| 155 | + sidecar.write_text("".join(r + "\n" for r in records), encoding="utf-8") |
| 156 | + |
| 157 | + result = verify_chain(sealed) |
| 158 | + assert result.valid is False |
| 159 | + assert any(e.startswith("Chain entry 1:") for e in result.errors) |
| 160 | + |
| 161 | + |
| 162 | +def test_without_a_sidecar_nothing_is_claimed(tmp_path: Path) -> None: |
| 163 | + audit_dir = tmp_path / ".agentix" |
| 164 | + audit_dir.mkdir() |
| 165 | + audit_path = audit_dir / "audit.jsonl" |
| 166 | + audit_path.write_text(json.dumps({"action": "a"}) + "\n", encoding="utf-8") |
| 167 | + |
| 168 | + result = verify_chain(audit_path) |
| 169 | + assert result.valid is True |
| 170 | + assert (result.sealed, result.unsealed) == (0, 1) |
0 commit comments