Skip to content

fix(platform): emit conversation audit logs off the RLS ctx (#1972) - #2283

Merged
Israeltheminer merged 2 commits into
mainfrom
fix/conversation-status-audit-rls
Jun 30, 2026
Merged

Israeltheminer merged 2 commits into
mainfrom
fix/conversation-status-audit-rls

Conversation

@Israeltheminer

Copy link
Copy Markdown
Contributor

Closes #1972

The bug

Closing, reopening, marking-as-spam, archiving — and every bulk action — on a conversation threw insert access not allowed and 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 via AuditLogHelpers.logSuccess(ctx, …) on the RLS-wrapped ctx. createAuditLog opens every write by upserting the per-org auditLogChainGenesis sentinel — 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 createAuditLog mutation (raw ctx, bypasses RLS, atomic within the parent transaction) via a shared emitAuditSuccess helper (convex/audit_logs/emit.ts). This mirrors the existing, working pattern in prompts/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_message already run as internal mutations and are untouched.

Testing

  • New conversations/status_transitions_rls.test.ts drives the real mutationWithRLS + RLS rules + audit chain through convex-test as an editor. It fails on the pre-fix code with the exact insert access not allowed, and passes after (verified by reverting the source and re-running).
  • Full convex/conversations + convex/audit_logs suites green (80 tests); tsc --noEmit, oxlint --type-aware, and oxfmt --check all 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.

Israeltheminer and others added 2 commits June 30, 2026 03:09
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.
@Israeltheminer
Israeltheminer merged commit f71ef70 into main Jun 30, 2026
68 of 70 checks passed
@Israeltheminer
Israeltheminer deleted the fix/conversation-status-audit-rls branch June 30, 2026 15:36
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.

Bug: Conversation status transitions (close/reopen/spam + all bulk actions) fail with audit-chain RLS "insert access not allowed"

1 participant