Skip to content

fix(audit): protect the chain head and sign it with AUDIT_CHAIN_KEY - #615

Merged
Bccorb merged 1 commit into
mainfrom
fix/audit-chain-head
Nov 23, 2026
Merged

Bccorb merged 1 commit into
mainfrom
fix/audit-chain-head

Conversation

@Bccorb

@Bccorb Bccorb commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Closes #595.

Problem

A role that could delete the newest rows from auth_events (the retention setting is open to any session) could move auth_event_chain_head back to the last row left, and GET /admin/auth-events/integrity still reported verified: true.

Changes

The head is protected by a new migration, 20261120120000-protect-auth-event-chain-head.cjs. The existing 20261008130000-protect-auth-events.cjs is unchanged.

  • A guard trigger refuses INSERT, DELETE and TRUNCATE on the head. It also refuses any change to seq or last_hash that does not come from the auth_events insert trigger (pg_trigger_depth() >= 2), and it never lets seq decrease.
  • auth_events_chain() is redefined as SECURITY DEFINER with a pinned search_path, so the application role needs no write access to the head. Every call inside it is exactly typed (prev_hash::text), so a better-matching overload that another role creates in public cannot run with the owner's privileges. EXECUTE is revoked from PUBLIC.

The head is signed with a new optional AUDIT_CHAIN_KEY.

  • The head gains signed_seq and signature, an HMAC-SHA256 under the key of seq and hash. The database never sees the key.
  • Every audit insert moves the signature onto the new head in the same transaction, while the insert still holds the head row lock. In the common case that costs one extra statement.
  • The signature only moves forward from a signature that verifies. A head that is unsigned or forged stays that way and keeps failing the check. The guard refuses clearing the signature or moving signed_seq backwards.
  • With the key set, the integrity check reports a missing or stale signature as the new reason head_unsigned, and a signature that does not verify as head_mismatch.

Key source and upgrades. The key is a new dedicated variable, not a reuse of an existing secret, so it can be rotated without touching sessions.

  • The migration signs the head if the key is set while it runs.
  • Otherwise an operator signs it once with node dist/scripts/sealAuditHead.js, and again after rotating the key. The script refuses a chain that does not verify.
  • The key is optional so that an upgrade never blocks sign-in. Production logs a warning at boot while it is unset, and the report carries headSigned: false.

Docs. docs/security-posture.md, "Audit trail integrity":

  • New "A signed head" subsection.
  • Corrected least-privilege grants, which now cover auth_event_chain_head and CREATE on public.
  • A note that the retention setting is not an access control, and that the DELETE privilege is.

docs/configuration.md documents AUDIT_CHAIN_KEY.

Contract impact

GET /admin/auth-events/integrity is additive:

  • It gains headSigned: boolean.
  • firstFailure.reason can now be head_unsigned.

The admin dashboard maps reasons in AuditTrailPanel.tsx (FAILURE_EXPLANATIONS) and types them in useAuditTrail.ts. It needs a head_unsigned entry, or its failure text reads "undefined" for that reason. That only happens on deployments that set the key and leave the head unsigned. No other route, token or schema changes.

Verification

  • npm run typecheck, npm run lint and npm run format:check pass.
  • npm run coverage passes: 197 files, 3465 tests, 99.08% lines, 95.85% branches.
  • npm run generate:api regenerated openapi.json and src/generated/api.ts.
  • The real migration chain ran on a scratch Postgres. The full TEST_DB=postgres suite passes (20 files, 296 tests), including the new tests/postgres/auditChainHead.spec.ts. It covers:
    • the steps from the issue
    • the guard refusals
    • signing across concurrent inserts
    • catching up after an unkeyed insert
    • a head re-pointed with its triggers disabled
    • sealing a chain that does not verify, which is refused
  • The down migration was run and the up migration re-run on the scratch database. A migrate-time signature matched the expected HMAC.
  • A least-privilege role with the documented grants was tested on the scratch database:
    • it can insert events and write the signature columns
    • it is refused when it rewrites the head or deletes events
    • a planted auth_event_hash(character, text) overload is not called by the definer trigger

A role that could delete the newest audit events could move
auth_event_chain_head back to match, and the integrity check still
verified. The head now has a guard trigger, the insert trigger runs as
SECURITY DEFINER, and the head carries an HMAC under a server secret
that the database never sees.

Refs #595
@Bccorb
Bccorb merged commit e2bd56e into main Nov 23, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(audit): protect the chain head and key it so a truncated tail cannot verify

1 participant