An audit log is easy to claim. The useful question is what happens when somebody doubts it.
An auditor, a controller or a security lead may need to establish what an agent did, under which rule, and who approved it. If the only evidence is a table the product wrote and the product can change, they are being asked to take the product’s word. Sefra’s trail is built so that they can check it instead, and so that the limits of the check are stated.
The chain
Sefra records events in one append-only table, audit_events, in its PostgreSQL database. Each event has a sequence number, a timestamp, a type, the execution id, the step index, the actor, a body, the hash of the previous event, and its own hash.
The hash is defined exactly, so that anyone can recompute it:
hash = SHA-256( prev_hash + "\n" + canonical JSON of the event without its hash )
The canonical form is RFC 8785. The members are seq, at, type, execution_id, step_index, actor, body and prev_hash, and each value has one spelling: a timestamp always has six fractional digits in UTC, and an absent execution id is null rather than a missing member. Two correct implementations in different languages therefore produce the same bytes and the same hash. The first event uses a previous hash of all zeros.
Because each hash covers the one before it, changing an old event changes its hash, and the next event no longer points at it. To hide one edit, an attacker has to rewrite every event after it.
Written first, in the same transaction
A trail is only as good as its completeness. Two rules deal with that.
An event commits in the same database transaction as the state change it describes. Appending locks the chain head, reads its hash, inserts the event and updates the head. If the event cannot be written, the change does not happen. There is no window in which Sefra’s own state moved and the evidence has yet to follow.
A system of record is a separate system, so one case remains. If the audit write fails after a downstream call succeeded, the effect exists before its event. The step is then retried or settled, and nothing proceeds unrecorded.
Nothing calls a system of record before its audit event is written. If the audit write is unavailable, Sefra refuses. It fails closed.
That is why the trail lives in PostgreSQL. The cost is that appends are serialised, which caps throughput.
The service cannot edit its own record
Sefra runs as a database role, sefra_app, that may insert into and read audit_events and has no right to update or delete them. Migrations run separately through sefra migrate, as the owner of the tables, so the running process does not hold those rights. A deployment that uses a single role for both still works, without this protection.
The consequence is specific. A bug or an injection in Sefra cannot edit or delete an audit event. A database administrator still can, and the section on limits comes back to that.
The verifier
sefra audit verify recomputes the chain from the stored rows. It reads the database named by SEFRA_DATABASE_URL, and its output has this form:
$ sefra audit verify
OK: events 1 to 1873, head 1873 <hash>
It reads PostgreSQL directly and does not go through Sefra’s API. Through the API, a faulty or compromised service could serve a consistent but false chain. The verifier needs only a database role that may select from the audit tables, so the service does not have to be running for the check to work.
A compromised service could still append false events with valid hashes. The chain shows that recorded events were not changed afterwards, not that each one is true.
It reads one consistent snapshot, so events appended during the check are not mistaken for gaps. It stops at the first problem and names it, with its sequence number:
- a missing event
- a
prev_hashthat differs from the hash of the event before - a hash that does not match the event’s content
- a chain head that does not name the last event
For someone who should not have database access, there is an export. sefra audit export writes the events as JSON Lines, and sefra audit verify --file recomputes the chain from that file alone. The chain over the file must equal sefra audit head, and an edited line or a line missing from the middle fails at its sequence number. The file alone cannot show that it is complete: with lines cut from its start or end, it still verifies. Check that it starts at event 1 and that its last line matches a checkpoint held elsewhere.
Because the hash is specified, the auditor need not use Sefra’s verifier at all. A short program in any language with SHA-256 and an RFC 8785 library can do the same work.
What happens when a record is changed
A verifier that has only ever seen intact data proves little, so we test Sefra’s against a real edit:
sefra audit verifypasses.- Someone with database superuser rights changes a past
policy.evaluatedevent, setting its recordedeffecttoallow. sefra audit verifynow exits non-zero and names that event’s sequence number.- The service’s own self-check fails at the same number, and every console page shows it.
- Once the event is restored,
verifyand the self-check pass again.
The second step is the edit an insider would want to make: rewriting a past policy decision. A changed body is reported in this form:
$ sefra audit verify
BROKEN at seq 939: hash does not match the event's content
What a hash chain does not prove
Someone with full control of the database can rewrite the whole chain. Recomputing detects any edit, deletion or reordering that does not also rewrite every later hash. An administrator who recomputes all of them produces a chain that verifies. Deleting the newest events and moving the head back needs no recomputation, and also verifies. Within one database, that cannot be prevented.
The defence is a copy of the head held somewhere the administrator does not control. Today that step is manual: sefra audit head prints the checkpoint (sequence number, hash and time), and the operator stores it elsewhere. A rewritten chain no longer matches a checkpoint stored earlier. The comparison is by hand, because sefra audit verify does not read a stored checkpoint. Doing this automatically is planned:
Anchored audit checkpoints Planned
Until that ships, tamper evidence against a database administrator depends on the operator keeping those checkpoints.
The trail records what Sefra saw and did. It cannot observe an agent’s internal state. A rationale supplied by an agent is stored and labelled as such, and it is a claim by the agent, not a fact.
A valid chain says the record is intact, not that the decisions were right. It shows which rule ran and who approved what. Whether the rule was a good one is for the people reading it.
It does not make a deployment compliant. It is evidence that can support internal controls and oversight. What that evidence is worth for a given obligation is a question for your auditor.
How to check it
Take an export, write your own verifier from the definition above, and see whether it agrees.