Here's a test you can run in ten minutes. Open your admin console's codebase, find where it writes audit rows, and check which database role does the insert. If it's the same role that handles every other query in the app, your audit log is a courtesy, not a control. Anyone who can compromise that service, whether a rogue operator, a stolen session, or an injected query, can issue an UPDATE or DELETE against it and leave no trace. An append-only audit log has to be enforced by something the operator doesn't control.

We've seen this pattern in code audits more than once: a well-structured audit_events table, written by the same API process, with the same credentials, that the audit is supposed to police. The log tells a clean story right up to the moment it matters.

The threat model: who is the audit log actually for?

Be explicit about the adversary, because the design follows from it. We care about three cases:

  • A malicious or coerced operator with legitimate admin console access who wants to hide an action.
  • A compromised application server, where an attacker holds the app's DB credentials.
  • Honest mistakes, such as a migration or a cleanup script that "tidies" old rows.

We deliberately do not claim to defend against a full database superuser or cloud-account owner. More on that below. Naming the boundary is what keeps the design honest.

Schema and DB-level permissions for an append-only audit log

The schema is boring on purpose. In Postgres terms:

  • id as a bigserial, so gaps are visible.
  • occurred_at set by the database (DEFAULT now()), never accepted from the client.
  • actor_id, actor_role, action, target_type, target_id.
  • before and after as jsonb snapshots, so you record state change, not just "user edited order".
  • prev_hash and row_hash, covered in the next section.

The enforcement lives in roles, not application code. The API's runtime role gets INSERT on the audit table and nothing else: no UPDATE, no DELETE, no TRUNCATE. Reads go through a separate role used by the admin console's audit viewer. Schema changes run under a migration role that the running service never holds. Add a trigger that raises an exception on UPDATE or DELETE as a second layer; it catches the case where someone later grants the wrong privilege by accident.

This one change closes the most common failure mode. A compromised app server can still add false events, which is bad, but it can no longer erase true ones. Prefer writing audit rows in the same transaction as the action itself, so you never get an action without an event or an event without an action. The cost is that an audit insert failure now fails the user's request. We think that's the right default for privileged actions, and a poor one for high-volume reads, which usually don't belong in this log anyway.

Hash chaining: making tampering detectable

Permissions stop casual tampering. They don't help if someone with elevated DB access edits history directly. For that, we make edits detectable with a hash chain: each row stores row_hash = SHA-256(prev_hash || canonical_json(row_fields)). Change or delete any row and every later hash stops verifying.

The details are where this goes wrong:

  • Canonicalization. JSON key order and number formatting must be deterministic, or your verifier reports false positives. Hash a fixed field order serialized by one function, shared by writer and verifier.
  • Serialization of writers. A chain needs a single "previous row." Concurrent inserts race. We take a transaction-scoped advisory lock (or use a single-writer queue) around the read-previous-and-insert step. That caps audit throughput, which is fine for admin actions and wrong for a firehose.
  • Keyed vs. unkeyed. A plain SHA-256 chain can be recomputed end to end by anyone who can rewrite the table: edit row 40, then recompute rows 41 to N. An HMAC with a key the DB doesn't hold raises the bar, but the app server holds that key, so a compromised app can still forge forward.
  • External anchoring. The real fix is periodically publishing the latest hash somewhere the operator can't write: an object store with retention lock, a separate account's append-only bucket, or even an email to a monitored mailbox. Now rewriting history requires compromising two independent systems.

Then run a verifier on a schedule, from outside the app's trust boundary. A chain nobody checks is decoration.

Where this design still falls short

We'd rather list the gaps than oversell:

  • Truncation at the tail. If an attacker deletes the most recent rows and nothing was anchored yet, the chain is still internally valid. Your anchor interval is your exposure window.
  • Forged additions. A compromised app can write plausible, false events. The chain proves the history wasn't altered, not that it was true. Capture actor identity from the verified session, never a request field, and log failed and denied attempts too.
  • The superuser problem. Whoever owns the database and the anchor store can rewrite both. Separation of duties and access reviews help; they don't eliminate it.
  • Logging gaps. The best chain in the world is useless if an admin action path simply forgets to emit an event. Route privileged mutations through one choke point, and test that coverage.
  • Privacy. Immutable logs and data-deletion obligations collide. Store references and minimal snapshots, not raw personal data, or you'll have a hard conversation later.

None of this is exotic. It's a role grant, a trigger, a hash column, and a cron job, plus the discipline to treat audit as its own trust domain. Most of the value comes from the first step, and each later layer buys you a narrower and more expensive attack.

At orithLabs, we build admin consoles and backends with this kind of threat model in mind from the start, and we also review existing codebases where the audit trail turns out to be softer than anyone assumed. If you'd like a second pair of engineering eyes on yours, you'll be talking directly to the two of us who'd do the work.