Append-only is not tamper-evident
The first time a customer asks "can you prove nothing in this log has been changed?" is usually the moment a team realizes their audit log is not an audit log. It is a table of events that happens to be written to, but not read, by the application.
A normal database table is append-only in practice because nobody writes UPDATE or DELETE queries against it. That is not a security property. It is a convention. An attacker who lands on the production database with credentials, or an insider with legitimate access, can edit a row and the rest of the system will never notice.
What an auditor, a CISO, or a regulator asks for is different. They want proof. They want a cryptographic primitive that makes it impossible to rewrite history without also rewriting every entry that followed, and for that rewriting to be detectable with a verification run they can execute themselves.
This post walks through how to build that. The code is adapted from our production audit engine. The lessons are from the mistakes we made getting there.
The shape of a hash-chained log
Every entry carries a sequence_number that monotonically increases per organization, and an entry_hash computed over a canonical string that includes:
- The entry's own content fields.
- The previous entry's
entry_hash.
If you change any field in entry 42, its hash no longer matches its own content. You can rewrite the hash, but now entry 43's prev_hash is wrong, and so are 44, 45, 46, and every entry through to the latest one. To cover the tampering, an attacker has to rewrite every entry from 42 to MAX(sequence_number), and also produce a valid signature for each.
That last part is where HMAC comes in. Without a shared secret, the attacker could recompute every hash and the chain would look correct. HMAC-SHA256 means the hash itself is a keyed MAC; you need the key to forge a valid entry. Without it, any tampering shows up as a verification failure.
The canonical string: the part everyone gets wrong
The single most important decision in a hash-chained log is which fields go into the hash. Anything you leave out can be silently mutated.
Our first version covered six fields:
"|".join([
str(seq),
organization_id,
event_type,
summary,
params_hash or "",
decision or "",
prev_hash,
])
This was fine until a code reviewer pointed out that actor_id, actor_type, action_id, risk_level, cloud_provider, trace_id, and nist_controls were all mutable without invalidating the chain. An attacker with UPDATE rights could rewrite who did the action, which resource was touched, what the risk level was, and the chain would still verify as valid.
The fix was to expand the canonical string to cover every field an attacker with UPDATE rights could plausibly want to rewrite:
def _canonical_v2(entry, *, seq, prev_hash, params_hash, created_at_iso) -> str:
return "|".join([
"v2",
str(seq),
entry.organization_id,
entry.actor_type,
entry.actor_id,
entry.agent_id or "",
entry.task_id or "",
entry.action_id or "",
entry.user_id or "",
entry.event_type,
entry.summary,
params_hash or "",
entry.decision or "",
entry.execution_status or "",
_stringify_list(entry.nist_controls),
_stringify_list(entry.owasp_tags),
entry.trace_id or "",
entry.risk_level or "",
entry.cloud_provider or "",
created_at_iso,
prev_hash,
])
Two things worth calling out.
Schema versioning. We prefixed the v2 canonical with the literal string "v2" and added a hmac_schema_version column on the row. Old entries verify against _canonical_v1. New entries verify against _canonical_v2. The verifier dispatches per-row. This means you can evolve the canonical string over time without invalidating the history you already wrote.
Never mutate a canonical function. Once _canonical_v1 has been used for a single production row, it is frozen forever. If you want to change what's covered, write _canonical_v2. This is exactly the same discipline as database migration files: the past is read-only.
The concurrency trap
A hash chain is a sequence. If two requests append entries concurrently, they must not both read the same MAX(sequence_number) or you end up with two entries claiming sequence N, both pointing at the same prev_hash. The chain has forked, and verification will fail against whichever one wins the insert.
Our first implementation looked like this:
# WRONG: racy
max_seq = await db.execute(select(func.max(AuditLog.sequence_number)))
# Then SELECT ... FOR UPDATE on the row at max_seq
The intent was: lock the last row, read its hash, compute the next one, insert. The bug is that two concurrent transactions can both execute the MAX query before either one has inserted. Neither blocks the other. Both read the same value. Both insert max + 1. The chain forks.
FOR UPDATE on the last row does not help; it only serializes transactions that also try to read or update that row, and inserting a new row does not count.
The fix is a transaction-scoped advisory lock keyed on the organization:
await self.db.execute(
text("SELECT pg_advisory_xact_lock(hashtext(:org_key))"),
{"org_key": f"tracehold:audit:{req.organization_id}"},
)
max_result = await self.db.execute(
select(func.max(AuditLog.sequence_number))
.where(AuditLog.organization_id == req.organization_id)
)
pg_advisory_xact_lock takes an integer key, is held until the transaction commits or rolls back, and serializes every concurrent append for a given organization without touching the audit rows themselves. hashtext() deterministically maps the org UUID to an int4 key. No extra infrastructure. No Redis. No retry logic. Just Postgres.
Per-organization scoping matters. If you take a global lock, you serialize every append in the system behind a single mutex. With a per-org lock, Org A and Org B append in parallel; only within an org is the chain single-threaded. That is exactly what the chain property requires. No more, no less.
What the append actually does
Once the lock is held, the append is four steps:
- Read
MAX(sequence_number)for this org. Next sequence ismax + 1. - Read the previous entry's
entry_hash(or use a genesis value of"0" * 64for the first entry). - Hash the request parameters separately:
params_hash = sha256(canonical_json(parameters)). Store the hash in the canonical string; either store the raw params or redact them, depending on your PII posture. - Build the canonical string, compute
entry_hash = hmac_sha256(key, canonical_string), and insert the row.
import hashlib
import hmac
def compute_hmac(key: str, data: str) -> str:
return hmac.new(key.encode(), data.encode(), hashlib.sha256).hexdigest()
canonical = _canonical_v2(
req,
seq=next_seq,
prev_hash=prev_hash,
params_hash=params_hash,
created_at_iso=now_iso,
)
entry_hash = compute_hmac(audit_key, canonical) # HMAC-SHA256 with the audit key
row = AuditLog(
organization_id=req.organization_id,
sequence_number=next_seq,
prev_entry_hash=prev_hash,
entry_hash=entry_hash,
hmac_schema_version=2,
created_at=now,
# ... all the fields that went into canonical
)
db.add(row)
Commit releases the advisory lock. The next waiter proceeds.
Verifying the chain
The verifier walks the chain in order and re-computes each hash from scratch:
def _canonical_for(row) -> str:
if (row.hmac_schema_version or 1) >= 2:
return _canonical_v2(
row,
seq=row.sequence_number,
prev_hash=row.prev_entry_hash,
params_hash=row.parameters_hash,
created_at_iso=row.created_at.isoformat(),
)
return _canonical_v1(
row,
seq=row.sequence_number,
prev_hash=row.prev_entry_hash,
params_hash=row.parameters_hash,
)
async def verify_chain_integrity(db, org_id: str) -> ChainIntegrityReport:
result = await db.execute(
select(AuditLog)
.where(AuditLog.organization_id == org_id)
.order_by(AuditLog.sequence_number.asc())
)
rows = result.scalars().all()
expected_prev = GENESIS_HASH # "0" * 64
for row in rows:
error = None
if row.prev_entry_hash != expected_prev:
error = "prev_hash mismatch"
elif compute_hmac(audit_key, _canonical_for(row)) != row.entry_hash:
error = "hash mismatch"
if error:
return ChainIntegrityReport(
organization_id=org_id,
total_entries=len(rows),
valid=False,
first_invalid_sequence=row.sequence_number,
error=error,
)
expected_prev = row.entry_hash
return ChainIntegrityReport(organization_id=org_id, total_entries=len(rows), valid=True)
_canonical_for dispatches to _canonical_v1 or _canonical_v2 based on row.hmac_schema_version. Old rows verify under the old rules. New rows verify under the new rules. Neither changes.
The same goes for the key. When you rotate the HMAC key, rows signed before the rotation still have to verify, so keep the previous key configured for a grace period and, when the current key does not match a row, try the previous one before calling it tampered.
First-invalid reporting matters: when verification fails, you want to know which row is the first one out of sync, because that is the boundary of the tampering. Everything before it is still provably intact.
The layered threat model
HMAC hash chaining is strong against a narrow threat: an attacker with UPDATE or DELETE rights to the audit table who does not have the HMAC key. That covers a lot of realistic scenarios (a compromised replica, an insider with SQL access, a backup exfiltration), but it does not cover two important ones.
The app is compromised. If an attacker runs code in your FastAPI process, they have the HMAC key in memory and can forge any entry or rewrite the chain. HMAC alone cannot defeat this.
The database is compromised at the schema level. An attacker who can grant themselves the HMAC key via a stored secret, or who tampers with the verification code itself, can hide their own rewrites.
The defense-in-depth answers are:
-
Database-level append-only. A migration that issues
REVOKE UPDATE, DELETE ON audit_logs FROM PUBLICfor the app role, plus aBEFORE UPDATE OR DELETEtrigger that raises an exception. An application bug cannot then accidentally mutate a row, and a SQL-injection vulnerability loses the ability to rewrite history. This is not a cryptographic guarantee, but it removes one of the easier attack paths. -
External anchoring. On a timer, write the latest chain hash to an external system the attacker does not control: S3 Object Lock with compliance mode, a KMS-signed artifact, or a managed audit-log service. An attacker rewriting the chain now has to also forge a valid anchor at the external system, which is materially harder. The verifier can cross-check the anchor during compliance export, making any rewrite detectable even if the HMAC key has leaked.
-
Per-chunk KMS signatures. For high-assurance environments, sign every 1,000-row chunk with an AWS KMS or GCP KMS key. The key never leaves the HSM. Even a fully-compromised app process cannot forge a chunk signature without going through the KMS API, which is itself logged.
Tracehold does the trigger half of (1): a trigger refuses every UPDATE and DELETE on the audit table. The one exception is an organization that asks to be erased: that deletion names the organization, and only its rows may go. Tracehold does not keep an external anchor for (2). On a timer it records the chain's latest hash, but only in its own database, which proves nothing against someone who can rewrite that database. The anchor that matters lives with you: give your connector an S3 bucket and it copies the whole trail, hashes included, into your own account, which Tracehold cannot write to. (3) is not built.
What it looks like in a compliance export
The question an auditor asks is whether a specific decision was real and not retrofitted after an incident. Tracehold's compliance bundle answers with a chain_proof.json:
{
"period": {"start": "2026-07-01T00:00:00+00:00", "end": "2026-09-30T23:59:59+00:00"},
"first_sequence": 1,
"last_sequence": 4731,
"first_entry_hash": "a3f2...",
"last_entry_hash": "e109...",
"total_entries": 4731,
"hmac_scheme": "HMAC-SHA256 over canonical string",
"current_hmac_schema_version": 2,
"latest_external_anchor": {
"backend_kind": "local_only",
"last_sequence": 4728,
"last_entry_hash": "9c41...",
"anchor_hmac": "5b0d...",
"external_uri": null,
"external_version_id": null,
"anchored_at": "2026-09-30T23:45:00+00:00"
}
}
backend_kind says where that anchor lives: s3_object_lock for a write-once S3 bucket, local_only for a row in Tracehold's own database, which is what Tracehold's hosted service keeps today. latest_external_anchor is null when no anchor is kept at all.
Next to it sit a MANIFEST.json (the SHA-256 of every file in the zip) and the audit rows themselves. That gives an auditor checks they can run on their own: recompute each file's hash, walk the chain, and compare the last sequence and hash with your own copy of the trail, if your connector keeps one in your bucket. The bundle's SIGNATURE is an HMAC over the manifest, and an HMAC has no public key, so only Tracehold can check that one. A public-key signature is the natural next step.
What each row should carry
Added October 2026. Since this post first went out, the IETF's WIMSE working group has published an early draft on identity for AI agents, draft-ietf-wimse-aims, with a minimum for every audit event: the agent, the user or system it acts for, the tool or resource, the action and the decision, a timestamp and a correlation ID, the risk state behind the decision, and revocations with their cause. The v2 canonical string above already covers the first six (the agent, the user, the action and its summary, the decision, the time and the trace ID, the risk level), so none of them can be rewritten without breaking the chain. In Tracehold, stopping an agent's access is an entry of its own in the same chain, with the reason in its summary.
Keep the rows long enough, too. The EU AI Act asks deployers of high-risk AI systems to keep the logs those systems generate for at least six months (Article 26). A retention job that deletes old rows also deletes the start of the chain a verifier walks from, so if you must trim, keep the last hash before the cut. Tracehold keeps the whole chain on every plan; the only rows it deletes are those of an organization that asks to be erased.
The takeaway
A hash-chained, HMAC-signed audit log is not a heavy piece of infrastructure. It is four fields on your audit table (sequence_number, prev_entry_hash, entry_hash, hmac_schema_version), an advisory lock around the append, and a canonical string you agree never to change.
The work is not in the code. The work is in:
- Picking a canonical string that covers every mutable field, on day one, and agreeing not to alter it.
- Versioning the canonical so you can evolve it without breaking old rows.
- Using a per-org advisory lock, not a row-level one.
- Layering database-level append-only and external anchoring on top, because HMAC alone does not defend against a compromised app.
Do those four things and you have something you can hand to an auditor. Skip any of them and you have a table that looks like an audit log but does not pass a serious review.
Tracehold writes a hash-chained audit row for every call an AI agent makes and every decision on it, and on Team and Enterprise exports an evidence bundle on demand. Start free, read how the audit chain works, or book a 30-minute walkthrough and we will show you the chain end to end and how to verify it.