Repository navigation
fix(convex): harden audit integrity verifier and cron coverage (#1846) - #2218
Conversation
Close five coverage gaps and latent chain hazards in the audit-log integrity verifier and its daily cron: 1. Org cap — the cron now selects orgs round-robin by staleness and persists per-org progress, so every org is reached across runs instead of re-checking the same first MAX_ORGS_PER_RUN forever. 2. Unverified tail — the cron pages FORWARD across runs from each org's persisted cursor (new auditIntegrityProgress table), so the newest rows are covered rather than only the oldest window. 3. Broken resume — verifyAuditChain seeds the linkage from the previous page's hash and skips up to/including an exact (timestamp, _id) cursor, fixing the guaranteed false break on a resumed page and the skipped same-timestamp siblings. 4. Clock skew — createAuditLog clamps the chain timestamp to max(now, lastInsertedAt + 1) so a backward wall-clock step can no longer orphan a row and raise a false tamper alarm. 5. Intra-mutation fork — createAuditLog serializes appends sharing a MutationCtx so parallel same-org writes under Promise.all can no longer commit a duplicate previousHash (a silent fork). Adds chain_integrity_hardening.test.ts covering all five. Closes #1846
Desk Review — PR #2218 (fix for #1846)Verdict: NOT READY — CHANGES REQUIRED. One reproducible correctness defect that makes this control raise false critical "tamper detected" alerts during ordinary retention, plus a coverage hole that re-introduces issue #1846 item 1's starvation under error conditions. CI & tests (observed)
What is correctItems 4 (clock clamp), 5 runtime serialization (per-ctx BLOCKING 1 — Deleted-
|
Desk Review — PR #2218 (
|
Summary
Resolves the five coverage gaps and latent chain hazards reported in #1846 for the audit-log integrity verifier (
verify_integrity.ts) and its daily cron (integrity_check.ts).1. Orgs beyond the first 500 were never checked
listAuditedOrganizationIdsalways took the same firstMAX_ORGS_PER_RUNgenesis rows. Replaced withselectOrgsForIntegrityRun, which ranks orgs round-robin by staleness (auditIntegrityProgress.updatedAt) and verifies the stalest window each run — so every org is reached across runs, and the cron's "checked on the next run" log line is now true.2. Rows past the oldest window were never verified
The cron re-walked the oldest rows every run, never reaching the newest segment where live tampering would land. It now pages forward across runs from a persisted per-org cursor (new additive
auditIntegrityProgresstable), covering the whole chain over successive runs.3. The documented
fromTimestampresume always reported a false breakOn resume,
previousExpectedHashstarted''and the only seeding site lived inside the skippedisFirstEntryblock, so the first resumed row's realpreviousHashwas compared against''→ guaranteedfirstBrokenAt.verifyAuditChainnow acceptsafterId+previousExpectedHash, seeds the linkage from the previous page's hash, and uses an exclusive(timestamp, _id)cursor so same-timestampsiblings of the boundary row are verified rather than skipped. AfromTimestamp-only resume no longer false-breaks (boundary trusted), and a retention-deleted cursor falls back to a seeded re-walk instead of stalling.4. No write-side timestamp monotonicity clamp (clock regression orphaned a row)
createAuditLognow clamps the chain timestamp tomax(now, genesis.lastInsertedAt + 1). A backward wall-clock step (NTP correction, VM snapshot restore) can no longer sort a new row before the head it chains off — eliminating the permanent false tamper alarm. The genesis row is already read+patched for OCC, so the clamp is free.5. Same-mutation parallel appends could fork the chain
Within one mutation there was no OCC: two
createAuditLogcalls underPromise.allboth read the same head and committed the samepreviousHash.createAuditLognow serializes appends sharing aMutationCtx(per-ctx promise chain), so each observes the previous insert as itslastEntry. Cross-mutation forks remain guarded by the genesis sentinel +chainSuccessorOCC patch.Tests
chain_integrity_hardening.test.tscovers all five fixes (intra-mutation fork, clock-skew clamp, paged resume + false-break guard, forward cursor advancement, org selection).convex/audit_logs/tests pass (36), plusgovernance/legal_hold+userssuites (29) which exercise the shared audit writer.Verification
bunx tsc --noEmit— cleanbunx oxlint --type-awareon touched files — 0 warnings/errorsbun run migrations:check— green (new table is data-safe; no migration needed)No user-visible strings, env vars, or docs changed (internal backend control), so translations/docs are N/A.
Closes #1846