Repository navigation
fix(platform): emit conversation audit logs off the RLS ctx (#1972) - #2283
Merged
Merged
Conversation
Closing, reopening, marking-as-spam, archiving and every bulk action on a conversation aborted with `insert access not allowed` and the status never changed. The status mutations are `mutationWithRLS`; they emitted their audit row via `logSuccess(ctx)` on the RLS-wrapped ctx, but `createAuditLog` upserts the per-org `auditLogChainGenesis` sentinel, which is deny-all for every role under RLS, so the audit write failed and rolled back the whole mutation. The actor is being audited, not granted audit-table access, so the write must not run on their RLS ctx. Route conversation audit writes through the internal `createAuditLog` mutation (raw ctx, bypasses RLS, atomic within the parent transaction) via a shared `emitAuditSuccess` helper, the same pattern `prompts/mutations.ts` already uses. Covers all 12 user-facing conversation mutations; the ingestion paths already run as internal mutations. Adds a convex-test regression that drives the real RLS and audit chain as an `editor` and fails on the pre-fix code.
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 #1972
The bug
Closing, reopening, marking-as-spam, archiving — and every bulk action — on a conversation threw
insert access not allowedand the status never changed (the mutation rolled back; a reload showed the conversation still in its original lane).Root cause
The status mutations are
mutationWithRLS. They emitted their audit row viaAuditLogHelpers.logSuccess(ctx, …)on the RLS-wrapped ctx.createAuditLogopens every write by upserting the per-orgauditLogChainGenesissentinel — and that table is deny-all for every role under RLS (lib/rls/helpers/rls_rules.ts). So the audit write failed the RLS insert check and aborted the whole transaction.The actor is being audited, not granted audit-table write access, so gating the audit write on their RLS capability is wrong. (Conversation ingestion works because it runs through internal mutations, which bypass RLS — which is why inbound messages arrive but user-facing status changes failed.)
The fix
Route conversation audit writes through the internal
createAuditLogmutation (raw ctx, bypasses RLS, atomic within the parent transaction) via a sharedemitAuditSuccesshelper (convex/audit_logs/emit.ts). This mirrors the existing, working pattern inprompts/mutations.ts(emitPromptAudit).Covers all 12 user-facing conversation mutations (close/reopen/spam/read/update/add-message/send-via-integration + 5 bulk). The 2 ingestion paths and the 2 internal
update_conversations/update_conversation_messagealready run as internal mutations and are untouched.Testing
conversations/status_transitions_rls.test.tsdrives the realmutationWithRLS+ RLS rules + audit chain throughconvex-testas aneditor. It fails on the pre-fix code with the exactinsert access not allowed, and passes after (verified by reverting the source and re-running).convex/conversations+convex/audit_logssuites green (80 tests);tsc --noEmit,oxlint --type-aware, andoxfmt --checkall clean.Follow-up (out of scope here)
The same
logSuccess(ctx)-on-RLS-ctx anti-pattern appears to exist in other domains (members/mutations.ts,approvals/mutations.ts,organizations/*). Those weren't in scope for this issue and need their own verification/fix — worth a tracked follow-up since they'd crash identically for non-owner roles.