ADR 0004 — Tamper Evidence: Per-Tenant Hash Chain, Sectioned Digests, Signed Checkpoints
Status: Accepted (★ new: no sibling equivalent)
Context
Section titled “Context”The differentiator of an audit log over “a table with a created_at” is that a customer or an
auditor can check that nobody — including the operator — altered, removed or reordered events.
The research (landscape, “Tamper-evidence designs”) shows the spectrum:
- Per-event hashes (Retraced-style): detect edits of one event, not deletions or reordering.
- Hash chains (each entry commits to the previous one): detect edits, deletions and reordering inside the chain; tail truncation and full rewrites need an external anchor.
- Merkle-tree transparency logs (Trillian, Tessera, Rekor, C2SP checkpoints): efficient inclusion/consistency proofs for many independent verifiers; heavier machinery (tiles, witnesses).
- Signed batches / checkpoints: a signature over the current head binds the log at a point in time; publishing it to the customer makes a later rewrite detectable.
Constraints: must run on every storage driver (including plain KV), on the record hot path, inside a library, with a verifier simple enough for a customer’s auditor to run, and must coexist with retention and GDPR erasure (ADR 0011).
Decision
Section titled “Decision”- One hash chain per tenant. Each event gets a contiguous
seq(1, 2, 3, …) and anintegrityblock{ v: 1, prevHash, hash, digests }. - Sectioned digests. The privacy-bearing sections (
actor,targets,context,metadata,description) are each hashed separately:digest(section) = "sha256:" + hex(SHA-256(id + "\n" + canonical(section)))(salted with the event id so low-entropy content cannot be confirmed by brute force after erasure, ADR 0011). The chained header is{ v, id, tenant, seq, action, occurredAt, recordedAt, outcome, prevHash, digests }andhash = "sha256:" + hex(SHA-256(canonical(header))). Seq 1 links to the genesis valuesha256:+ 64 zeros.canonical= JSON with object keys sorted at every depth, no whitespace (RFC 8785 for the value shapes we accept: strings, finite numbers, booleans, null, arrays, objects). - Signed checkpoints. A checkpoint is
{ v, tenant, seq, hash, at, keyId, alg, signature }over the canonical form of everything butsignature, signed by a pluggableAuditSigner:ed25519Signer(default recommendation: verifiable with a published public key, served as a JWKS at/api/integrity/keys) orhmacSigner(symmetric, for internal-only verification). Checkpoints are created on demand (core.checkpoint(tenant)), automatically (checkpoints: { every, interval }), and on every retention prune (abasecheckpoint anchoring the new first retained event). - Checkpoints are streamed to sinks like events (ADR 0008), so a tenant can keep its own copy of signed heads — the lightweight version of transparency-log witnesses. An operator rewrite is then detectable against the tenant’s copy.
- Verification is a pure function,
verifyChain(events, { checkpoints, keys, base }), shipped on the zero-dependency@xtandard/audit/verifysubpath (Web Crypto only), behindcore.verify(tenant, range), theGET /api/tenants/:t/verifyroute, the viewer’s “Verify” button, andxtandard-audit verify --bundle export.jsonl. It reports the first break precisely (hash-mismatch,seq-gap,link-mismatch,digest-mismatch,bad-signature,checkpoint-mismatch) and which tail is not yet covered by a checkpoint. - Verifiable exports. The JSONL export bundle is
header line + events + checkpoints (+ base), so an exported log can be verified offline with nothing but the bundle and the public key. - Concurrency. Appends to one tenant are serialized in-process (per-tenant mutex) and guarded
across processes by the storage: native event-log drivers enforce
UNIQUE(tenant, seq)and the core re-chains and retries on conflict; KV drivers usecompareAndSwapon the chain head when the storage has it, and otherwise assume a single writer per tenant (documented, warned at startup — the same posture as the webhooks dispatcher on plain KV).
Merkle trees and C2SP-format checkpoints are not in v1: a chain plus signed heads already gives
customers edit/delete/reorder/rewrite detection; Merkle proofs pay off when there are many
third-party verifiers needing logarithmic proofs, which is a later, additive change (new v).
Consequences
Section titled “Consequences”- Tamper evidence is on by default and costs two SHA-256 passes per event plus one head read/write per batch.
- Tail truncation between checkpoints is detectable only against a checkpoint the tenant holds; frequent checkpoints + streaming them narrows the window.
- The format is versioned (
integrity.v, checkpointv) so it can evolve without breaking old bundles.