Menu ▾ ▴

#123 Audit log tamper evidence: hash chain with dopbase audit verify

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

Originally created by: bosd

Implements the first slice of [#119]: tamper evidence for the audit log. Every audit record joins a hash chain, and dopbase audit verify checks it.

Stacked on [#122]: this branch builds on feat/secret-rotation, so the PR currently shows that commit too. Once [#122] merges (or on a rebase) only the audit commit remains; review 315235e for this change. Export, retention, and the read-only auditor role from [#119] follow in separate PRs.

Summary

  • Migration 0015_audit_chain adds seq, prev_hash, and record_hash to audit_events with a unique index on the sequence number. Each record hashes its stored contents together with the record before it, so editing a record, removing one, or inserting one out of order breaks the chain at that point.

  • Appends stay atomic without a new lock: every audit write already runs after its transaction's first write, so SQLite's write lock is held while the chain head is read. The ten writes that had no business transaction now take their own immediate one, which also makes those records durable with the operation they describe instead of fire-and-forget on the pool.

  • dopbase audit verify walks the records in sequence order, stops at the first break, and exits with code 0 when the chain is intact and 2 when it is not. --json prints the full report, including headSeq and headHash for external anchoring.

  • GET /api/v1/audit-events/verify serves the same check to scripts, restricted to administrators like reading the log itself. The response names the first break (sequence number, record ID, reason), the records checked before it, and any records sitting outside the chain.

  • Records written before the chain existed are sealed during the next migration pass, and a backup restored from an older release is sealed during the restore, so verification covers the whole log rather than only new records. Reverting the chain migration and reapplying re-seals the records.

  • The chain columns are additive: the audit list keeps its shape and filters, and verification reads no secret values.

Compatibility

  • Additive only: existing audit endpoints, filters, and clients are unchanged, and older clients ignore the three new columns.
  • Upgrades chain existing records during the first startup after the migration; no manual step. Reverting 0015 drops the chain columns and the evidence with them while keeping the records themselves.
  • Audit writes that previously ran on the pool now open an immediate transaction, so they serialize with other writers. Under load that is the only observable difference; record contents and endpoints are unchanged.

Verification

  • cargo fmt --check
  • cargo clippy --locked --all-targets --all-features -- -D warnings
  • cargo test: main 27, services 21 (8 new chain tests covering continuity, 24 concurrent appends, in-place tampering, deletion, unsequenced records, sealing of pre-chain records, and re-sealing after a migration rollback), constants 4, cli 208, users 15, backups 9. The 3 pre-existing commands::export::* failures need the util-linux script binary and are unrelated to this change.
  • bun run test:docs, bun run test:github (17/17), bunx prettier --check on the changed Markdown.

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: #124
Tickets: #128
Tickets: #131

Discussion

  • Anonymous

    Anonymous - 2026-10-17

    Originally posted by: bosd

    Review note — one concurrency gap in the chain.

    The append reads the chain head before it inserts, so it needs the connection to already hold SQLite's write lock. Most call sites audit inside a business transaction whose first statement is a write, but the read paths that retire an expired previous value (list, get, reveal, runtime) used a bare pooled connection, which is in autocommit. Two concurrent reads at the same grace-period expiry could read the same head and claim the same sequence number, failing on audit_events_seq_idx.

    Fixed in 0620012: retirement now runs in its own BEGIN IMMEDIATE transaction, and only when there is something to retire, so the usual read path stays read-only.

     
  • Anonymous

    Anonymous - 2026-10-23

    Originally posted by: bosd

    Superseded by [#131], which merged this work (rebased onto main, with review hardening on top). Closing.

     

    Related

    Tickets: #131

  • Anonymous

    Anonymous - 2026-10-23

    Ticket changed by: bosd

    • status: open --> closed
     

Log in to post a comment.