Sandfort / Audit verification
Exit code 3: verifying the audit log of an AI agent sandbox
Sandfort writes an audit row for the calls that change state and links each row to the one before it with a hash. This article shows how a reviewer or a cron job turns that chain into a yes or no with an exit code, how an exported file is checked without taking the server's word for it, and what the check cannot tell you.
Published: 2026-11-27Sandfort 0.1.0
How rows link
Every organization has its own chain. A row stores the actor (type and id), the action, the target, the outcome, the request id, redacted details and the time. It also stores prev_hash and hash. The hash covers the previous row's hash and a canonical form of the row: compact JSON with sorted keys, built from id, org id, actor, action, target, outcome, request id, details and time. The client IP and the user agent are stored but are not part of the hash. The first row of an org links to an empty string.
Without a key, hash is sha256(prev_hash + canonical(row)). With SANDFORT_AUDIT_CHAIN_KEY set, at least 32 bytes, it is HMAC-SHA256(key, prev_hash + canonical(row)). Change a stored field, remove a row or reorder two rows, and either the recomputed hash or the link to the previous row stops matching. The verifier reports which, as hash_mismatch, link_broken, missing_hash, head_missing or head_invalid.
Verify it and read the exit code
The admin commands run on the host with the server's settings, including the database URL and the chain key. This is a full round trip on a throwaway SQLite database that bootstrap had just created.
python -m sandfort.admin_cli audit-export --output audit.ndjson python -m sandfort.admin_cli audit-verify --head audit.ndjson echo $?
exported 2 audit rows ok: 2 audit rows verified 0
Then one stored details value was edited directly in the database, and the same check ran again.
org_01M8EFDESQCHVMN7ZAE428PWE aud_01M8EFDESTDVDRQCBTCH70YK9 hash_mismatch: recomputed hash differs audit chain broken: 1 finding(s) in 2 rows 3
audit-verify walks every org and exits 3 when any row fails. Exit 1 means the check could not run, for example a head file that holds no signed head line. That makes it safe in CI or cron: alert on any non-zero code. sandfort audit verify does the same walk for the caller's org through GET /v1/audit-events/verify, so a reviewer with an audit:read key and no host access can run it too.
The --head option matters because a chain walk alone cannot see rows deleted from the end, since the remaining rows still link. An export ends with a head that names the newest row at export time. audit-verify --head fails with head_missing when the live chain no longer reaches that row with the same hash. It needs the chain key.
Check an export without the server
With the chain key set, audit-export ends each org's rows with one head line: the last row id and hash, the row count, the first row id, a SHA-256 over the exported lines and an HMAC over them. When SANDFORT_AUDIT_SIGNING_KEY is set as well, the head also carries an Ed25519 signature, the public key and signed_at. GET /v1/status publishes audit_public_key and its fingerprint without an API key.
The CLI checks a file against that head.
sandfort audit verify-head audit.ndjson --public-key "$PINNED_KEY" --expect-org "$ORG_ID"
It verifies the signature and signed_at, then the content binding (count, first row and SHA-256 of the lines), then that every row belongs to the head's org, that the last row is the row the head names, and that each prev_hash equals the previous row's hash. Exit 0 means every check passed. Exit 3 prints a reason such as content_mismatch, count_mismatch, truncated or signature. Exit 2 means the check could not run.
Pin the public key from a second channel and pass it with --public-key. By default the key comes from the same server that produced the export, so a server that fabricated a file could publish a matching key.
The TypeScript SDK has the same checks, verifyHead for the head line and verifyExport for the file. The Python SDK exposes verify_head and verify_export.
import { readFileSync } from "node:fs"
import { verifyExport } from "@sandfort/sdk"
const text = readFileSync("audit.ndjson", "utf8")
const result = await verifyExport(text, process.env.PINNED_KEY!, { expectedOrgId: process.env.ORG_ID })
console.log(result.ok, result.reason)What gets a row
A test walks every non-GET route the app registers and fails when a route has neither an audited case nor a written reason on its exemption list. The exemptions are the internal egress policy read, the PTY WebSocket attach, a member update that changes nothing, and a repeat revoke of an API key or SCIM token. Audit rows for state changes are written in the same unit of work as the change. A command execution is audited before the driver runs it.
A call refused after a valid key leaves a request.denied row, written on a best effort basis that never changes the response. A few reads that expose sandbox content are audited too: logs, file list, stat and download, git status, and snapshot and volume export.
What is not logged
- Command text, stdin and code. A row keeps
redacted-sha256:and the byte length.envkeeps only variable names. The digests are unsalted, so a guessable value can be confirmed by hashing a guess. - Command output, and terminal keystrokes and output. A terminal session records create, resize and close.
- Outbound traffic from a sandbox. The egress proxy and the preview proxy write no rows.
- Failed authentication. A missing, malformed, unknown or wrong-secret key leaves no row. A revoked or expired key with a correct secret leaves one
request.deniedrow per key per 60 seconds per server process. - Successful reads of lists, usage and audit events.
- Host-level access. Direct access to the database or the Docker socket leaves no row, and so do most admin commands. The exceptions are
create-org,bootstrap,create-key,revoke-keyandaudit-reanchor.
Limits
The chain is tamper-evident, not immutable. It shows that a row was changed, removed or reordered. It does not stop that, and it does not say who did it, because a row names a key id or a session token id, not a person.
- Without
SANDFORT_AUDIT_CHAIN_KEYthe hash is a plain SHA-256. Anyone who can write the database can rewrite history, recompute every hash, andaudit-verifystill exits 0. In theprodenvironment the server refuses to start without the key unlessSANDFORT_UNKEYED_AUDIT_CHAIN_ACKNOWLEDGED=true. - Rows written before the key was set stay unkeyed. Verification accepts them while
SANDFORT_AUDIT_CHAIN_ACCEPT_UNKEYEDis true, which is the default, so that early part of the trail stays rewritable by a database writer. - With the key, a writer who only has the database cannot recompute the hashes. The key holder can. Keep the key out of the database and out of its backups, because losing or changing it makes keyed rows fail verification.
- A client cannot recompute keyed hashes. A server that holds both keys can sign a fabricated export, and
verify-headaccepts it. Only the server-side check proves the keyed hashes, so you trust the server for that check. - An older, genuinely signed export still verifies. Read
signed_at, and use--previousto show that a later export extends an earlier one. - Retention (
SANDFORT_AUDIT_RETENTION_DAYS, default 0, keep forever) prunes the oldest rows and never the newest. Verification then starts from the first remaining row. Export before you prune. audit-reanchoracknowledges past findings by adding oneaudit.chain_reanchoredrow. It hides nothing, and the operator name and note are free text.
Run it on a schedule
Export on a schedule and keep every file. Run verify-head --previous <earlier file> on each new one, pin the public key, and run audit-verify --head from cron with an alert on any non-zero code.
The server is source-available under FSL-1.1-Apache-2.0, and the SDKs and CLI are Apache-2.0.
- Reading and verifying the audit log, the reviewer guide
- Governance page
- audit_chain.py, the chain and verifier source