Repository navigation
fix(audit): protect the chain head and sign it with AUDIT_CHAIN_KEY - #615
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #595.
Problem
A role that could delete the newest rows from
auth_events(the retention setting is open to any session) could moveauth_event_chain_headback to the last row left, andGET /admin/auth-events/integritystill reportedverified: true.Changes
The head is protected by a new migration,
20261120120000-protect-auth-event-chain-head.cjs. The existing20261008130000-protect-auth-events.cjsis unchanged.seqorlast_hashthat does not come from theauth_eventsinsert trigger (pg_trigger_depth() >= 2), and it never letsseqdecrease.auth_events_chain()is redefined as SECURITY DEFINER with a pinnedsearch_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 inpubliccannot run with the owner's privileges.EXECUTEis revoked fromPUBLIC.The head is signed with a new optional
AUDIT_CHAIN_KEY.signed_seqandsignature, an HMAC-SHA256 under the key ofseqand hash. The database never sees the key.signed_seqbackwards.head_unsigned, and a signature that does not verify ashead_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.
node dist/scripts/sealAuditHead.js, and again after rotating the key. The script refuses a chain that does not verify.headSigned: false.Docs.
docs/security-posture.md, "Audit trail integrity":auth_event_chain_headandCREATEonpublic.docs/configuration.mddocumentsAUDIT_CHAIN_KEY.Contract impact
GET /admin/auth-events/integrityis additive:headSigned: boolean.firstFailure.reasoncan now behead_unsigned.The admin dashboard maps reasons in
AuditTrailPanel.tsx(FAILURE_EXPLANATIONS) and types them inuseAuditTrail.ts. It needs ahead_unsignedentry, 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 lintandnpm run format:checkpass.npm run coveragepasses: 197 files, 3465 tests, 99.08% lines, 95.85% branches.npm run generate:apiregeneratedopenapi.jsonandsrc/generated/api.ts.TEST_DB=postgressuite passes (20 files, 296 tests), including the newtests/postgres/auditChainHead.spec.ts. It covers:auth_event_hash(character, text)overload is not called by the definer trigger