Menu ▾ ▴

#131 feat(audit): tamper-evident audit log with export, retention, and anchors

closed
nobody
None
2026-10-26
2026-10-23
Anonymous
No

Originally created by: thomijasir

Summary

Makes the audit log tamper-evident, exportable, and prunable, then closes the gaps an attacker with write access to the database could use to hide a change. Part of [#119]; the read-only auditor role (#126) is not included.

The first three commits are bosd's work from [#123], [#124] and [#125], rebased onto main with the secret rotation code from [#122] removed. That code had been pulled in through the stacked branches and kept the branch from compiling. The last five commits fix what review found in them.

Each record hashes its own contents together with the previous record's hash, and verification recomputes the chain:

record 1  hash(seq 1, contents, "")          -> h1
record 2  hash(seq 2, contents, h1)          -> h2
record 3  hash(seq 3, contents, h2)          -> h3   <- an edit here breaks h3, h4, ...

What the hardening commits change:

 startup

-  seal every record without a chain position     # a forged row was blessed on restart
+  seal nothing; older rows are reported as legacy, newer unchained rows fail
 UPDATE of a sealed record
+  rejected by a trigger (guards against bugs; the chain covers attackers)
 retention pass
-  delete, then upsert anchor; errors only logged # could commit a delete without an anchor
+  savepoint around archive + delete + anchor
+  append audit.pruned naming the range; verify requires it to match the anchor
 backup restore
-  DELETE audit_events, copy the snapshot's        # post-snapshot history vanished
+  archive the current log to audit-archive/pre-restore-<time>.jsonl first
+  carry the chain's genesis and anchor tables through restore and factory reset
 verify
+  one read snapshot for the whole walk
+  --anchor SEQ:HASH compares with a head saved outside the server
+  log an "audit checkpoint" at startup, shutdown, and after each verify

The chain on its own cannot see a full rewrite or deleted newest records. An anchor saved outside the server can, and docs/reference/audit-events.md now spells out which case needs which, plus what to do when verification fails.

Compatibility

No breaking API changes. The audit list gains seq, prevHash and recordHash; the verify report and the backup.restored metadata gain fields. Existing clients ignore them.

Migrations 0014 to 0016 add the chain columns, the pruning anchor table, the genesis table, and the update trigger. Records that already exist stay unchained and show up as legacy in verification. They are never sealed after the fact.

Merge risk: a two-way door for code, but the migrations are additive and reverting them drops the chain columns along with the evidence. Retention is off unless audit_retention_days is set, and it deletes records only after archiving them. Restore now stops if it cannot write the pre-restore archive, so an unwritable data directory will block a restore that used to succeed. Blast radius: every audit write, which now goes through the chain append under SQLite's write lock.

Verification

  • cargo fmt --check, cargo clippy --locked --all-targets --all-features -- -D warnings, and cargo test all pass on the final branch, and cargo test passes on each of the 8 commits on its own.
  • bash ./scripts/verify-backend.sh passes in a clean app/Dockerfile.test container (fmt, clippy, all tests, release build, CLI and server smoke checks).
  • An end-to-end script ran 38 checks against the release binary in the same container, editing the database with sqlite3 the way an attacker would. All 38 passed:
Scenario Before After
Edit a sealed record in SQL allowed rejected by the trigger
Same edit with the trigger dropped n/a verify exits 2, BROKEN at sequence 2
Insert an unchained record now sealed on next restart unsequenced: 1, exit 2
Delete the newest record undetectable --anchor reports missing, exit 2
Move the pruning anchor by hand read as pruning exit 2, no matching audit.pruned
Restore a backup post-snapshot records lost archived to pre-restore-*.jsonl, chain verifies
Retention past the window archive and anchor archive, anchor, and an audit.pruned record; verify reports Pruned through: sequence 3
  • bun run test:docs and bunx prettier --check pass on the changed Markdown.
  • Not yet checked by hand: upgrading a database created on main, restoring a 0.1.9 backup onto this branch, the Admin UI Audit page, and factory reset.

Checklist

  • [x] I kept this pull request focused on one problem.
  • [x] I added or updated tests for behavior changes, or explained why tests are not needed.
  • [x] I updated the documentation and CHANGELOG.md when public behavior changed, or explained why they are not needed.
  • [x] I updated relevan documentation inside /docs

Related

Tickets: #119
Tickets: #122
Tickets: #123
Tickets: #124
Tickets: #125
Tickets: #134
Tickets: #24

Discussion

  • Anonymous

    Anonymous - 2026-10-23

    Ticket changed by: dopbase

    • status: open --> closed
     

Log in to post a comment.