RiskboundDocs

Architecture

ADR 0007: Sign every event at append time

EnterpriseSome features on this page are in the Enterprise edition.

Date: 2026-12-01. Status: accepted.

Context#

ADR 0004 gave every tenant an append-only, hash-chained event log. A chain shows that nothing was edited, removed or reordered, as long as nobody recomputed it. Anyone with write access to the database can recompute it. A chain proves order and integrity to us. It does not prove anything to an auditor, a customer or an insurer who does not trust our database.

Phase 3 adds signatures so that an outside party can check the record with one public key and no access to our systems.

Decision#

What is signed#

Each event is signed when it is appended. The signed message is the 32 raw bytes of the event's hash (hex-decoded). That hash already covers the tenant, sequence number, type, decision id, payload, previous hash and timestamp, so one signature binds the event to its place in the chain.

What is hashed does not change. The signature, the key id and the algorithm are three new nullable columns on events (signature, signing_key_id, signature_alg), written in the same insert as the event.

The signer port#

EventSigner signs a digest and returns the signature (base64), a key id and an algorithm, and exposes the public key. Two implementations:

SignerConfigKey idFor
LocalEd25519SignerEVIDENCE_SIGNER=local, EVIDENCE_SIGNING_KEYlocal: and 16 hex characters of the SHA-256 of the public keyDevelopment and tests
KmsSignerEVIDENCE_SIGNER=kms, EVIDENCE_KMS_KEY_ID, AWS_REGIONThe KMS key ARNProduction

EVIDENCE_SIGNER=none writes unsigned events. It is the default outside production. In production the process refuses to start unless the signer is kms: a private key in an environment variable is not good enough for evidence other people rely on. At boot the KMS signer reads the public key once and checks the key, so a wrong key id or a missing permission stops the process before it serves a request.

Generate a local key with node scripts/evidence-keygen.ts. It prints the two lines for .env and the public key.

Algorithms#

KMS supports Ed25519 for signing (key spec ECC_NIST_EDWARDS25519, signing algorithm ED25519_SHA_512, which requires MessageType: RAW). We use that by default, so the local signer and KMS produce the same kind of signature and the verifier has one main path. ED25519_PH_SHA_512 (pre-hashed) is not used: it is a different scheme from plain Ed25519 and most libraries do not verify it.

KMS keys with spec ECC_NIST_P256 also work: ECDSA_SHA_256 with MessageType: RAW, so KMS hashes the 32-byte message with SHA-256 and any standard ECDSA verifier checks the DER signature. This is there in case Ed25519 is not available in the region we pick. Checked against @aws-sdk/client-kms 3.1183 and its SignRequest documentation.

The KMS key needs KeyUsage: SIGN_VERIFY. The service role needs kms:Sign and kms:GetPublicKey on that key and nothing else.

When signing fails#

Signing runs inside the transaction that makes the change and writes the event, after the tenant lock is taken. If it fails, the transaction rolls back: no event and no change. The API answers 503 evidence_unavailable. The SDK treats that as a refusal (ADR 0003), so the action does not run.

So while KMS is unreachable, nothing can be decided, approved, reported or switched. That includes the kill switch, which is acceptable because every decision is already refused for the same reason. The exception is a guard that opted in to fail-open: it would let its action through during a KMS outage, as it would during any other outage. That is the trade-off that guard already chose, and the SDK records it.

A KMS call is cut off after 2 seconds, because it holds the tenant's event lock while it runs.

Checking#

GET /v1/events/verify recomputes the chain and checks every signature against the current signer's public key. It reports:

  • ok: the chain is intact and every signature that should be there is there and valid.
  • brokenAtSeq and problem: the first event that fails, and why: chain, bad_signature, unknown_key or unsigned_after_signed.
  • signatures: how many events are signed, unsigned and invalid, and the key ids seen.
  • fullySigned: whether every event is signed.

Events written before signing was turned on stay unsigned. They are counted in signatures.unsigned and legacyUnsignedThroughSeq, and fullySigned is false. They do not make ok false: they are as trustworthy as they were under ADR 0004, no more. An unsigned event after a signed one does make ok false, because that is what stripping a signature looks like.

GET /v1/events/export returns the tenant's log as JSON lines: one header (format, tenant, event count, key ids, the public keys this server knows), then one line per event with the exact fields that are hashed, plus the signature fields.

scripts/verify-evidence.ts checks an export with no backend, no database and no npm packages. It is one file that Node 22.18 or later runs directly. It takes the public key as a separate file, never from the export, because whoever could rewrite the log could also rewrite a key inside it. It is a second implementation on purpose: an auditor reads 300 lines, not our codebase. Tests run both checkers on the same tampered data.

Why#

  • Sign the hash, not a new message. Nothing about what is hashed changes, old events still verify, and an outside verifier needs the same canonical JSON either way.
  • Sign inside the transaction. Signing afterwards leaves a window where an event exists without a signature, and a crash in that window leaves it that way forever. That is a gap we would have to explain to every auditor.
  • Fail closed. An action whose evidence cannot be written is an action nobody can later prove was checked. Refusing it is the same choice as ADR 0003.
  • Ed25519. Small keys and signatures, no choice of hash or encoding to get wrong, in KMS and in every standard library.

Consequences#

  • Every append now waits for one KMS call while holding the tenant lock. That is a few milliseconds in the same region and caps one tenant at a few hundred events a second. The way out, if needed, is the one in ADR 0004: sign batches of events instead of each one.
  • KMS availability becomes part of Riskbound availability for writes.
  • Key rotation is not built yet. The verify endpoint knows only the current key, so after a rotation, events signed with the old key show as unknown_key. Before the first rotation we need to keep retired public keys (in config or in a table) and pass them to the checker. The standalone verifier already accepts several --public-key files. Built since: see the key rotation addendum below.
  • Truncation. Each signature covers the past, not the future. Someone who deletes the newest events leaves a shorter log that still checks out. The export header's event_count catches lines dropped from a file, not rows dropped from the database. Closed since for sealed events: see ADR 0009, which seals each tenant's events to S3 Object Lock every few minutes.
  • The export loads a tenant's whole log into memory. Streaming it is the next step once a tenant has more than a few hundred thousand events.

Addendum, 2026-12-01: key rotation#

The verify endpoint knew only the current public key, so after a rotation every event signed by the old key showed as unknown_key.

Decision:

  • A new table, signing_keys, keeps every public key that has signed evidence: key id, algorithm, public key (PEM), when it was first used and when it was retired. It never holds a private key.
  • At startup, before anything is signed, the server registers its current key and marks every other key retired. Registering a key already known is harmless. A known key id with a different public key stops the process, because the log would no longer say which key signed what. Registration takes a transaction advisory lock, so servers starting together do not deadlock (the real-Postgres test caught this).
  • GET /v1/events/verify checks each event against the key its signing_key_id names, current or retired. A key id that is not in the table is still unknown_key.
  • The export header lists every key id the tenant's events use and the server's copy of each of those public keys.
  • scripts/verify-evidence.ts already took several --public-key files and tries each. Pass the old key and the new one. An auditor should still get the keys some other way: for KMS, aws kms get-public-key on each key ARN.

signing_keys is the one table without tenant_id. The signing key belongs to the server, not to a tenant, and the table holds nothing about any tenant. Reads look up only the key ids that one tenant's events name.

How to rotate: create the new KMS key, give the service role kms:Sign and kms:GetPublicKey on it, keep kms:GetPublicKey on the old one if auditors read it from KMS, set EVIDENCE_KMS_KEY_ID to the new key and redeploy. During a rolling deploy both keys sign for a short while. Both are in the table, so both verify. retired_at is informational.

Consequences:

  • A retired key still verifies events of any date. If a retired private key leaked, whoever holds it could sign events that verify. Checking each event's time against its key's window would narrow that, but rolling deploys make the window fuzzy. Built since: see the key windows addendum below.
  • Deleting a key from signing_keys makes its events unknown_key, so the table needs the same protection as the event log.

Addendum, 2026-12-01: key time windows#

A retired key verified events of any date, so a leaked retired private key could sign new events that passed.

Decision:

  • Each key is accepted only for events dated inside the period it was in use: from first_used_at to retired_at in signing_keys, widened by 15 minutes on each side. A key that is not retired has no end. An event outside its key's period is key_outside_window and counts as an invalid signature (signatures.outsideKeyWindow).
  • The 15 minutes cover clock skew between servers and a rolling deploy, in which old servers keep signing with the old key for a few minutes after a new server registered the new key and retired the old one.
  • A window alone is not enough: whoever holds the old key could date a new event inside the old window. So events must also be in time order. An event dated more than 15 minutes before an earlier event is time_out_of_order. A little disorder is normal, because a decision takes its time before it waits for the tenant lock.
  • The export header gives each key's first_used_at and retired_at, and key_window_grace_seconds. scripts/verify-evidence.ts applies them to the keys the auditor passes, matched by public key. Those dates come from our database, like the rest of the export; an auditor who wants their own should take them from the key's history in KMS (creation and the date it was replaced in config).

Consequences:

  • A leaked retired key can only sign events dated within 15 minutes of its retirement, and only if they come after the last real event in that time. It cannot rewrite events that are already sealed (ADR 0009).
  • Rolling back to an old key clears its retired_at, so its window opens again from its first use. The table keeps one period per key, not a history of periods. A key history table would fix that if rollbacks become routine.
  • A rolling deploy that lets old servers sign for more than 15 minutes after the new key is registered will fail verify for those events. Keep deploys shorter than that, or stop old tasks before the new key takes over.
  • A server whose clock is more than 15 minutes off writes events that fail time_out_of_order. That is a real defect in the record, and verify should say so.